Bug #11248 Calling a function in a stored procedure crashes database
Submitted: 10 Jun 2005 16:22 Modified: 10 Jun 2005 19:42
Reporter: Robin Bailes Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: User-defined functions ( UDF ) Severity:S1 (Critical)
Version:5.0.6-beta-nt OS:Windows (Win XP)
Assigned to: CPU Architecture:Any

[10 Jun 2005 16:22] Robin Bailes
Description:
Calling a user defined function that performs database access from a stored procedure crashes the service.

How to repeat:
-- Create a test table with a single column
DROP TABLE IF EXISTS `test`.`testtable`;
CREATE TABLE `testtable` (
  `Col1` varchar(20) NOT NULL,
  PRIMARY KEY  (`Col1`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
----------------------------------------------------

-- Populate the table
insert into `test`.`testtable` values ('A'), ('B'), ('C'), ('D')
----------------------------------------------------

-- Create a function that selects from the table
DELIMITER $$

DROP FUNCTION IF EXISTS `test`.`test_function`$$
CREATE FUNCTION `test`.`test_function`(parm_value char) RETURNS int(11)
BEGIN
      return (select count(*) from testtable where Col1 = parm_value);
END$$

DELIMITER ;
----------------------------------------------------

-- Create a proc that uses the function in a select statement
DELIMITER $$

DROP PROCEDURE IF EXISTS `test`.`prcTest`$$
CREATE PROCEDURE `test`.`prcTest`()
begin
SELECT Col1,  test_function('B')
FROM testtable;

end$$

DELIMITER ;
----------------------------------------------------

Run the test procedure and the system crashes.  

Coding the the function in-line in the procedure does not result in a crash.  This code works correctly:

DELIMITER $$

DROP PROCEDURE IF EXISTS `test`.`prcTest`$$
CREATE PROCEDURE `test`.`prcTest`()
begin
SELECT Col1,  (select count(*) from testtable where Col1 = 'B')
FROM testtable;

end$$

DELIMITER ;
[10 Jun 2005 19:42] MySQL Verification Team
Thank you for your bug report. This issue has been committed to our
source repository of that product and will be incorporated into the
next release.

If necessary, you can access the source repository and build the latest
available version, including the bugfix, yourself. More information 
about accessing the source trees is available at
    http://www.mysql.com/doc/en/Installing_source_tree.html

Additional info:

With latest Windows server I was unable to repeat the crash:

C:\mysql\bin>mysql -uroot test
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1 to server version: 5.0.8-beta-debug

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> call prcTest();
+------+--------------------+
| Col1 | test_function('B') |
+------+--------------------+
| A    |                  1 |
| B    |                  1 |
| C    |                  1 |
| D    |                  1 |
+------+--------------------+
4 rows in set (0.30 sec)

Query OK, 0 rows affected (0.31 sec)

mysql>