Skip to content

Commit a183b02

Browse files
author
Alexander Nozdrin
committed
Fix for Bug#12652873 - 61392: CONTINUE HANDLER FOR NOT FOUND BEING
TRIGGERED FROM INTERNAL STORED FUNCTION. The problem was that RETURN statement in stored programs did not clear the Diagnostics Area, as it should have done according to the SQL Standard. The user-visible problem was that in some cases an SQL-condition, raised in the nested function call, would be visible in the outer scope, which in turn would lead to incorrect handler activation. The fix is to clear the Diagnostics Area before executing RETURN statement. Note, that after the patch, there is no way to throw an SQL-warning out of a stored function. From the user's view point, he/she will not get any warning, even if there were warnings, raised during the function execution. This change is backward incompatible, but it makes the behaviour of stored programs more in line with The Standard.
1 parent b68f8ab commit a183b02

File tree

9 files changed

+138
-125
lines changed

9 files changed

+138
-125
lines changed

mysql-test/r/func_rollback.result

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,6 @@ END;
190190
SELECT f1_insert_select(2);
191191
f1_insert_select(2)
192192
1
193-
Warnings:
194-
Warning 1048 Column 'f2' cannot be null
195193
SELECT * FROM t1_not_null ORDER BY f1,f2;
196194
f1 f2
197195
2 0
@@ -267,8 +265,6 @@ END;
267265
SELECT f1_insert_with_two_rows();
268266
f1_insert_with_two_rows()
269267
1
270-
Warnings:
271-
Warning 1048 Column 'f2' cannot be null
272268
SELECT * FROM t1_not_null ORDER BY f1,f2;
273269
f1 f2
274270
10 0

mysql-test/r/signal.result

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,19 +1313,25 @@ drop procedure test_signal $$
13131313
#
13141314
# Test where SIGNAL can be used
13151315
#
1316+
1317+
# RETURN statement clears Diagnostics Area, thus
1318+
# the warnings raised in a stored function are not
1319+
# visible outsidef the stored function. So, we're using
1320+
# @@warning_count variable to check that SIGNAL succeeded.
1321+
13161322
create function test_signal_func() returns integer
13171323
begin
1324+
DECLARE v INT;
13181325
DECLARE warn CONDITION FOR SQLSTATE "01XXX";
13191326
SIGNAL warn SET
13201327
MESSAGE_TEXT = "This function SIGNAL a warning",
13211328
MYSQL_ERRNO = 1012;
1322-
return 5;
1329+
SELECT @@warning_count INTO v;
1330+
return v;
13231331
end $$
13241332
select test_signal_func() $$
13251333
test_signal_func()
1326-
5
1327-
Warnings:
1328-
Warning 1012 This function SIGNAL a warning
1334+
1
13291335
drop function test_signal_func $$
13301336
create function test_signal_func() returns integer
13311337
begin

mysql-test/r/sp-error.result

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2846,3 +2846,50 @@ CALL p2();
28462846
SET SESSION max_error_count= @old_max_error_count;
28472847
DROP PROCEDURE p1;
28482848
DROP PROCEDURE p2;
2849+
2850+
# Bug#12652873: 61392: Continue handler for NOT FOUND being triggered
2851+
# from internal stored function.
2852+
2853+
DROP FUNCTION IF EXISTS f1;
2854+
DROP FUNCTION IF EXISTS f2;
2855+
DROP TABLE IF EXISTS t1;
2856+
2857+
CREATE TABLE t1 (a INT, b INT);
2858+
INSERT INTO t1 VALUES (1, 2);
2859+
2860+
# f1() raises NOT_FOUND condition.
2861+
# Raising NOT_FOUND can not be simulated by SIGNAL,
2862+
# because SIGNAL would raise SQL-error in that case.
2863+
2864+
CREATE FUNCTION f1() RETURNS INTEGER
2865+
BEGIN
2866+
DECLARE v VARCHAR(5) DEFAULT -1;
2867+
SELECT b FROM t1 WHERE a = 2 INTO v;
2868+
RETURN v;
2869+
END|
2870+
2871+
# Here we check that the NOT_FOUND condition raised in f1()
2872+
# is not visible in the outer function (f2), i.e. the continue
2873+
# handler in f2() will not be called.
2874+
2875+
CREATE FUNCTION f2() RETURNS INTEGER
2876+
BEGIN
2877+
DECLARE v INTEGER;
2878+
DECLARE CONTINUE HANDLER FOR NOT FOUND
2879+
SET @msg = 'Handler activated.';
2880+
SELECT f1() INTO v;
2881+
RETURN v;
2882+
END|
2883+
SET @msg = '';
2884+
2885+
SELECT f2();
2886+
f2()
2887+
-1
2888+
2889+
SELECT @msg;
2890+
@msg
2891+
2892+
2893+
DROP FUNCTION f1;
2894+
DROP FUNCTION f2;
2895+
DROP TABLE t1;

mysql-test/r/sp.result

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5425,13 +5425,9 @@ end|
54255425
select func_20028_a()|
54265426
func_20028_a()
54275427
0
5428-
Warnings:
5429-
Warning 1329 No data - zero rows fetched, selected, or processed
54305428
select func_20028_b()|
54315429
func_20028_b()
54325430
0
5433-
Warnings:
5434-
Warning 1329 No data - zero rows fetched, selected, or processed
54355431
select func_20028_c()|
54365432
ERROR 22012: Division by 0
54375433
call proc_20028_a()|
@@ -5484,13 +5480,9 @@ end|
54845480
select func_20028_a()|
54855481
func_20028_a()
54865482
0
5487-
Warnings:
5488-
Warning 1329 No data - zero rows fetched, selected, or processed
54895483
select func_20028_b()|
54905484
func_20028_b()
54915485
0
5492-
Warnings:
5493-
Warning 1329 No data - zero rows fetched, selected, or processed
54945486
select func_20028_c()|
54955487
func_20028_c()
54965488
NULL
@@ -6110,8 +6102,6 @@ END|
61106102
SELECT bug5274_f2()|
61116103
bug5274_f2()
61126104
x
6113-
Warnings:
6114-
Warning 1265 Data truncated for column 'bug5274_f1' at row 1
61156105
DROP FUNCTION bug5274_f1|
61166106
DROP FUNCTION bug5274_f2|
61176107
drop procedure if exists proc_21513|
@@ -6204,19 +6194,13 @@ c1
62046194
SELECT f1(2);
62056195
f1(2)
62066196
0
6207-
Warnings:
6208-
Warning 1329 No data - zero rows fetched, selected, or processed
62096197
PREPARE s1 FROM 'SELECT f1(2)';
62106198
EXECUTE s1;
62116199
f1(2)
62126200
0
6213-
Warnings:
6214-
Warning 1329 No data - zero rows fetched, selected, or processed
62156201
EXECUTE s1;
62166202
f1(2)
62176203
0
6218-
Warnings:
6219-
Warning 1329 No data - zero rows fetched, selected, or processed
62206204
DROP PROCEDURE p1;
62216205
DROP PROCEDURE p2;
62226206
DROP FUNCTION f1;

mysql-test/r/view_grant.result

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -341,13 +341,9 @@ use mysqltest;
341341
select * from v1;
342342
f2()
343343
NULL
344-
Warnings:
345-
Warning 1329 No data - zero rows fetched, selected, or processed
346344
select * from v2;
347345
f2()
348346
NULL
349-
Warnings:
350-
Warning 1329 No data - zero rows fetched, selected, or processed
351347
select * from v3;
352348
ERROR HY000: View 'mysqltest.v3' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
353349
select * from v4;
@@ -387,13 +383,9 @@ ERROR HY000: View 'mysqltest.v2' references invalid table(s) or column(s) or fun
387383
select * from v3;
388384
f2()
389385
NULL
390-
Warnings:
391-
Warning 1329 No data - zero rows fetched, selected, or processed
392386
select * from v4;
393387
f2()
394388
NULL
395-
Warnings:
396-
Warning 1329 No data - zero rows fetched, selected, or processed
397389
select * from v5;
398390
ERROR HY000: View 'mysqltest.v5' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
399391
drop view v1, v2, v3, v4, v5;

0 commit comments

Comments
 (0)