1
2#
3# WL#4179: Stored programs: validation of stored program statements.
4#
5
6# The test case below demonstrates that meta-data changes are detected
7# by triggers.
8SET sql_mode = 'NO_ENGINE_SUBSTITUTION';
9Warnings:
10Warning	3090	Changing sql mode 'NO_AUTO_CREATE_USER' is deprecated. It will be removed in a future release.
11CREATE TABLE t1 (a INT, b INT);
12CREATE TABLE t2 (a INT, b INT);
13CREATE TABLE t3 (a INT);
14INSERT INTO t2 VALUES (11, 12), (21, 22);
15CREATE TRIGGER t3_ai AFTER INSERT ON t3 FOR EACH ROW
16INSERT INTO t1 SELECT * FROM t2;
17INSERT INTO t3 (a) VALUES (1);
18SELECT * FROM t1;
19a	b
2011	12
2121	22
22SELECT * FROM t2;
23a	b
2411	12
2521	22
26ALTER TABLE t1 ADD COLUMN c INT;
27ALTER TABLE t2 ADD COLUMN c INT;
28INSERT INTO t2 VALUES (31, 32, 33);
29INSERT INTO t3 (a) VALUES (2);
30SELECT * FROM t1;
31a	b	c
3211	12	NULL
3321	22	NULL
3411	12	NULL
3521	22	NULL
3631	32	33
37SELECT * FROM t2;
38a	b	c
3911	12	NULL
4021	22	NULL
4131	32	33
42DROP TABLE t1;
43DROP TABLE t2;
44DROP TABLE t3;
45
46# Check that NEW/OLD rows work within triggers.
47
48CREATE TABLE t1 (a INT);
49INSERT INTO t1(a) VALUES (1);
50CREATE TRIGGER t1_bu BEFORE UPDATE ON t1 FOR EACH ROW
51BEGIN
52SET @a = OLD.a;
53SET @b = NEW.a;
54SELECT OLD.a INTO @c;
55SELECT NEW.a INTO @d;
56SET NEW.a = NEW.a * 2;
57END|
58UPDATE t1 SET a = a * 10;
59SELECT @a, @c, @b, @d;
60@a	@c	@b	@d
611	1	10	10
62SELECT a FROM t1;
63a
6420
65DROP TABLE t1;
66
67CREATE TABLE t1 (a INT);
68INSERT INTO t1 VALUES (1), (2);
69CREATE PROCEDURE p1()
70SELECT * FROM t1;
71CALL p1();
72a
731
742
75
76# 1.1 Check if added column into table is recognized correctly
77# in a stored procedure.
78ALTER TABLE t1 ADD COLUMN b INT DEFAULT 0;
79CALL p1();
80a	b
811	0
822	0
83
84# 1.2 Check if dropped column is not appeared in SELECT query
85# executed inside a stored procedure.
86ALTER TABLE t1 DROP COLUMN a;
87CALL p1();
88b
890
900
91
92# 1.3 Check if changed column is picked up properly.
93ALTER TABLE t1 CHANGE COLUMN b b VARCHAR(255) DEFAULT 'n/a';
94DELETE FROM t1;
95INSERT INTO t1 VALUES (b), ('hello');
96CALL p1();
97b
98n/a
99hello
100
101# 1.4 Check if table's recreation is handled correctly
102# inside a call of stored procedure.
103DROP TABLE t1;
104DROP PROCEDURE p1;
105CREATE TABLE t1 (a INT);
106INSERT INTO t1 VALUES (1), (2);
107CREATE PROCEDURE p1()
108SELECT * FROM t1;
109CALL p1();
110a
1111
1122
113DROP TABLE t1;
114CALL p1();
115ERROR 42S02: Table 'test.t1' doesn't exist
116CREATE TABLE t1 (a INT);
117INSERT INTO t1 VALUES (1), (2);
118CALL p1();
119a
1201
1212
122
123# 1.5 Recreate table t1 with another set of columns and
124# re-call a stored procedure.
125DROP TABLE t1;
126DROP PROCEDURE p1;
127CREATE TABLE t1 (a INT);
128INSERT INTO t1 VALUES (1), (2);
129CREATE PROCEDURE p1()
130SELECT * FROM t1;
131CALL p1();
132a
1331
1342
135DROP TABLE t1;
136CALL p1();
137ERROR 42S02: Table 'test.t1' doesn't exist
138CREATE TABLE t1 (b VARCHAR(10), c VARCHAR(10));
139INSERT INTO t1 VALUES ('a', 'b'), ('c', 'd');
140CALL p1();
141b	c
142a	b
143c	d
144DROP TABLE t1;
145DROP PROCEDURE p1;
146
147# 2.1 Stored program that uses query like 'SELECT * FROM v' must be
148# re-executed successfully if some columns were added into the view
149# definition by ALTER VIEW;
150CREATE VIEW v1 AS SELECT 1, 2, 3;
151CREATE PROCEDURE p1()
152SELECT * FROM v1;
153CALL p1();
1541	2	3
1551	2	3
156ALTER VIEW v1 AS SELECT 1, 2, 3, 4, 5;
157CALL p1();
1581	2	3	4	5
1591	2	3	4	5
160
161# 2.2 Stored program that uses query like 'SELECT * FROM v' must be
162# re-executed successfully if some columns were removed from the view
163# definition by ALTER VIEW;
164ALTER VIEW v1 AS SELECT 1, 5;
165CALL p1();
1661	5
1671	5
168
169# 2.3 Stored program that uses query like 'SELECT * FROM v' must be
170# re-executed successfully if a base table for the view being used was
171# extended by new columns (by ALTER TABLE);
172CREATE TABLE t1(a INT, b INT);
173INSERT INTO t1 VALUES (1, 2);
174DROP VIEW v1;
175CREATE VIEW v1 AS SELECT * FROM t1;
176DROP PROCEDURE p1;
177CREATE PROCEDURE p1()
178SELECT * FROM v1;
179CALL p1();
180a	b
1811	2
182ALTER TABLE t1 ADD COLUMN c INT DEFAULT 3;
183CALL p1();
184a	b
1851	2
186
187# 2.4 Stored program that uses query like 'SELECT * FROM v' must be
188# re-executed successfully if not used columns were removed from the
189# base table of this view (by ALTER TABLE);
190DROP TABLE t1;
191CREATE TABLE t1(a INT, b INT, c INT);
192INSERT INTO t1 VALUES (1, 2, 3);
193DROP VIEW v1;
194CREATE VIEW v1 AS SELECT b, c FROM t1;
195DROP PROCEDURE p1;
196CREATE PROCEDURE p1()
197SELECT * FROM v1;
198CALL p1();
199b	c
2002	3
201ALTER TABLE t1 DROP COLUMN a;
202CALL p1();
203b	c
2042	3
205ALTER TABLE t1 DROP COLUMN b;
206CALL p1();
207ERROR HY000: View 'test.v1' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
208
209# 2.5 Stored program that uses query like 'SELECT * FROM v' must be
210# re-executed successfully if a type of some base table's columns were
211# changed (by ALTER TABLE);
212DROP TABLE t1;
213CREATE TABLE t1(a INT, b INT, c INT);
214INSERT INTO t1 VALUES (1, 2, 3);
215DROP VIEW v1;
216CREATE VIEW v1 AS SELECT b, c FROM t1;
217DROP PROCEDURE p1;
218CREATE PROCEDURE p1()
219SELECT * FROM v1;
220CALL p1();
221b	c
2222	3
223ALTER TABLE t1 CHANGE COLUMN b b VARCHAR(255) DEFAULT 'n/a';
224DELETE FROM t1;
225INSERT INTO t1(a, c) VALUES (10, 30);
226CALL p1();
227b	c
228n/a	30
229
230# 2.6 Stored program that uses query like 'SELECT * FROM v' must be
231# re-executed successfully if the view 'v' was dropped and created again
232# with the same definition;
233#
234# 2.7 Stored program that uses query like 'SELECT * FROM v' must be
235# re-executed successfully if the view 'v' was dropped and created again
236# with different, but compatible definition.
237DROP VIEW v1;
238DROP TABLE t1;
239DROP PROCEDURE p1;
240CREATE VIEW v1 AS SELECT 1, 2, 3;
241CREATE PROCEDURE p1()
242SELECT * FROM v1;
243CALL p1();
2441	2	3
2451	2	3
246DROP VIEW v1;
247CALL p1();
248ERROR 42S02: Table 'test.v1' doesn't exist
249CREATE VIEW v1 AS SELECT 4, 5, 6;
250CALL p1();
2514	5	6
2524	5	6
253
254# 2.8 Stored program that uses query like 'SELECT * FROM v' must be
255# re-executed successfully if the view base tables have been re-created
256# using the same or compatible definition.
257DROP VIEW v1;
258DROP PROCEDURE p1;
259CREATE TABLE t1(a INT, b INT);
260INSERT INTO t1 VALUES (1, 2);
261CREATE VIEW v1 AS SELECT * FROM t1;
262CREATE PROCEDURE p1()
263SELECT * FROM v1;
264CALL p1();
265a	b
2661	2
267DROP TABLE t1;
268CALL p1();
269ERROR HY000: View 'test.v1' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
270CREATE TABLE t1(a VARCHAR(255), b VARCHAR(255));
271INSERT INTO t1 VALUES ('a', 'b'), ('c', 'd');
272CALL p1();
273a	b
274a	b
275c	d
276DROP VIEW v1;
277DROP TABLE t1;
278DROP PROCEDURE p1;
279
280# 3.1 Stored program that uses query like 'SELECT * FROM t' must be
281# re-executed successfully if some columns were added into temporary table
282# table 't' (by ALTER TABLE);
283CREATE TEMPORARY TABLE t1(a INT, b INT);
284INSERT INTO t1 VALUES (1, 2);
285CREATE PROCEDURE p1() SELECT * FROM t1;
286CALL p1();
287a	b
2881	2
289ALTER TABLE t1 ADD COLUMN c INT DEFAULT 3;
290CALL p1();
291a	b	c
2921	2	3
293
294# 3.2 Stored program that uses query like 'SELECT * FROM t' must be
295# re-executed successfully if some columns were removed from temporary
296# table 't' (by ALTER TABLE);
297ALTER TABLE t1 DROP COLUMN a;
298CALL p1();
299b	c
3002	3
301
302# 3.3 Stored program that uses query like 'SELECT * FROM t' must be
303# re-executed successfully if a type of some temporary table's columns were
304# changed (by ALTER TABLE);
305ALTER TABLE t1 CHANGE COLUMN b b VARCHAR(255) DEFAULT 'n/a';
306INSERT INTO t1(c) VALUES (4);
307CALL p1();
308b	c
3092	3
310n/a	4
311
312# 3.4 Stored program that uses query like 'SELECT * FROM t' must be
313# re-executed successfully if the temporary table 't' was dropped and
314# created again with the same definition;
315#
316# 3.5 Stored program that uses query like 'SELECT * FROM t' must be
317# re-executed successfully if the temporary table 't' was dropped and
318# created again with different, but compatible definition.
319DROP TEMPORARY TABLE t1;
320CREATE TEMPORARY TABLE t1(a INT, b INT);
321INSERT INTO t1 VALUES (1, 2);
322CALL p1();
323a	b
3241	2
325DROP TEMPORARY TABLE t1;
326CREATE TEMPORARY TABLE t1(a VARCHAR(255), b VARCHAR(255), c VARCHAR(255));
327INSERT INTO t1 VALUES ('aa', 'bb', 'cc');
328CALL p1();
329a	b	c
330aa	bb	cc
331DROP TEMPORARY TABLE t1;
332DROP PROCEDURE p1;
333
334# 4.1 Stored program must fail when it is re-executed after a table's column
335# that this program is referenced to has been removed;
336CREATE TABLE t1(a INT, b INT);
337INSERT INTO t1 VALUES (1, 2);
338CREATE PROCEDURE p1() SELECT a, b FROM t1;
339CALL p1();
340a	b
3411	2
342ALTER TABLE t1 DROP COLUMN b;
343CALL p1();
344ERROR 42S22: Unknown column 'b' in 'field list'
345DROP PROCEDURE p1;
346DROP TABLE t1;
347
348# 4.2 Stored program must fail when it is re-executed after a temporary
349# table's column that this program is referenced to has been removed;
350CREATE TEMPORARY TABLE t1(a INT, b INT);
351INSERT INTO t1 VALUES (1, 2);
352CREATE PROCEDURE p1() SELECT a, b FROM t1;
353CALL p1();
354a	b
3551	2
356ALTER TABLE t1 DROP COLUMN b;
357CALL p1();
358ERROR 42S22: Unknown column 'b' in 'field list'
359DROP PROCEDURE p1;
360DROP TEMPORARY TABLE t1;
361
362# 4.3 Stored program must fail when it is re-executed after a view's
363# column that this program is referenced to has been removed;
364CREATE VIEW v1 AS SELECT 1 AS a, 2 AS b;
365CREATE PROCEDURE p1() SELECT a, b FROM v1;
366CALL p1();
367a	b
3681	2
369ALTER VIEW v1 AS SELECT 1 AS a;
370CALL p1();
371ERROR 42S22: Unknown column 'b' in 'field list'
372DROP PROCEDURE p1;
373DROP VIEW v1;
374
375# 4.4 Stored program must fail when it is re-executed after a regular table
376# that this program referenced to was removed;
377CREATE TABLE t1(a INT, b INT);
378INSERT INTO t1 VALUES (1, 2);
379CREATE PROCEDURE p1() SELECT a, b FROM t1;
380CALL p1();
381a	b
3821	2
383DROP TABLE t1;
384CALL p1();
385ERROR 42S02: Table 'test.t1' doesn't exist
386DROP PROCEDURE p1;
387
388# 4.5 Stored program must fail when it is re-executed after a view that
389# this program referenced to was removed;
390CREATE VIEW v1 AS SELECT 1 AS a, 2 AS b;
391CREATE PROCEDURE p1() SELECT a, b FROM v1;
392CALL p1();
393a	b
3941	2
395DROP VIEW v1;
396CALL p1();
397ERROR 42S02: Table 'test.v1' doesn't exist
398DROP PROCEDURE p1;
399
400# 4.6 Stored program must fail when it is re-executed after a temporary
401# table that this program referenced to was removed;
402CREATE TEMPORARY TABLE t1(a INT, b INT);
403INSERT INTO t1 VALUES (1, 2);
404CREATE PROCEDURE p1() SELECT a, b FROM t1;
405CALL p1();
406a	b
4071	2
408DROP TABLE t1;
409CALL p1();
410ERROR 42S02: Table 'test.t1' doesn't exist
411DROP PROCEDURE p1;
412
413# 4.7 Stored program must fail if the program executes some
414# SQL-statement and afterwards re-executes it again when some table 't'
415# referenced by the statement was dropped in the period between statement
416# execution;
417CREATE TABLE t1(a INT);
418CREATE TABLE t2(a INT);
419CREATE PROCEDURE p1()
420BEGIN
421DECLARE CONTINUE HANDLER FOR 1146
422SELECT 'Table t1 does not exist anymore' as msg;
423SELECT * FROM t1;
424INSERT INTO t2 VALUES (1);
425SELECT GET_LOCK('m1', 10000);
426SELECT * FROM t1;
427END|
428
429# -- connection: con1
430SELECT GET_LOCK('m1', 0);
431GET_LOCK('m1', 0)
4321
433
434# -- connection: default
435CALL p1();
436
437# -- connection: con1
438DROP TABLE t1;
439SELECT RELEASE_LOCK('m1');
440RELEASE_LOCK('m1')
4411
442
443# -- connection: default
444a
445GET_LOCK('m1', 10000)
4461
447msg
448Table t1 does not exist anymore
449DROP TABLE t2;
450DROP PROCEDURE p1;
451
452# 5.1 Regular table -> View
453CREATE TABLE t1(a INT, b INT);
454INSERT INTO t1 VALUES (1, 2);
455CREATE PROCEDURE p1() SELECT * FROM t1;
456CALL p1();
457a	b
4581	2
459DROP TABLE t1;
460CREATE VIEW t1 AS SELECT 1 AS a, 2 AS b;
461CALL p1();
462a	b
4631	2
464DROP PROCEDURE p1;
465DROP VIEW t1;
466
467# 5.2 Regular table -> Temporary table
468CREATE TABLE t1(a INT, b INT);
469INSERT INTO t1 VALUES (1, 2);
470CREATE PROCEDURE p1() SELECT * FROM t1;
471CALL p1();
472a	b
4731	2
474DROP TABLE t1;
475CREATE TEMPORARY TABLE t1(a INT, b INT);
476INSERT INTO t1 VALUES (1, 2);
477CALL p1();
478a	b
4791	2
480DROP PROCEDURE p1;
481DROP TEMPORARY TABLE t1;
482
483# 5.3 View -> Regular table
484CREATE VIEW t1 AS SELECT 1 AS a, 2 AS b;
485CREATE PROCEDURE p1() SELECT * FROM t1;
486CALL p1();
487a	b
4881	2
489DROP VIEW t1;
490CREATE TABLE t1(a INT, b INT);
491INSERT INTO t1 VALUES (1, 2);
492CALL p1();
493a	b
4941	2
495DROP PROCEDURE p1;
496DROP TABLE t1;
497
498# 5.4 View -> Temporary table
499CREATE VIEW t1 AS SELECT 1 AS a, 2 AS b;
500CREATE PROCEDURE p1() SELECT * FROM t1;
501CALL p1();
502a	b
5031	2
504DROP VIEW t1;
505CREATE TEMPORARY TABLE t1(a INT, b INT);
506INSERT INTO t1 VALUES (1, 2);
507CALL p1();
508a	b
5091	2
510DROP PROCEDURE p1;
511DROP TEMPORARY TABLE t1;
512
513# 5.5 Temporary table -> View
514CREATE TEMPORARY TABLE t1(a INT, b INT);
515INSERT INTO t1 VALUES (1, 2);
516CREATE PROCEDURE p1() SELECT * FROM t1;
517CALL p1();
518a	b
5191	2
520DROP TEMPORARY TABLE t1;
521CREATE VIEW t1 AS SELECT 1 AS a, 2 AS b;
522CALL p1();
523a	b
5241	2
525DROP PROCEDURE p1;
526DROP VIEW t1;
527
528# 5.6 Temporary table -> Regular table
529CREATE TEMPORARY TABLE t1(a INT, b INT);
530INSERT INTO t1 VALUES (1, 2);
531CREATE PROCEDURE p1() SELECT * FROM t1;
532CALL p1();
533a	b
5341	2
535DROP TEMPORARY TABLE t1;
536CREATE TABLE t1(a INT, b INT);
537INSERT INTO t1 VALUES (1, 2);
538CALL p1();
539a	b
5401	2
541DROP PROCEDURE p1;
542DROP TABLE t1;
543
544# 6.1 Trigger that uses column 'a' of table 't' via pseudo-variable NEW
545# must be re-executed successfully if the table definition has been changed
546# in a compatible way. "Compatible way" in this case is that if the table
547# 't' still has a column named 'a' and the column type is compatible with
548# the operation that NEW.a takes part of.
549#
550# 6.2 Trigger that uses column 'a' of table 't' via pseudo-variable OLD
551# must be re-executed successfully if the table definition has been changed
552# in a compatible way. "Compatible way" in this case is that if the table
553# 't' still has a column named 'a' and the column type is compatible with
554# the operation that OLD.a takes part of.
555CREATE TABLE t1(a INT, b INT);
556INSERT INTO t1 VALUES (1, 2);
557CREATE TRIGGER t1_bu BEFORE UPDATE ON t1 FOR EACH ROW
558BEGIN
559SET @x = OLD.a;
560SET @y = NEW.a;
561END|
562
563SET @x = 0, @y = 0;
564UPDATE t1 SET a = 3, b = 4;
565SELECT @x, @y;
566@x	@y
5671	3
568
569ALTER TABLE t1 ADD COLUMN c INT DEFAULT -1;
570
571SET @x = 0, @y = 0;
572UPDATE t1 SET a = 5, b = 6;
573SELECT @x, @y;
574@x	@y
5753	5
576
577ALTER TABLE t1 CHANGE COLUMN a a VARCHAR(255);
578
579SET @x = 0, @y = 0;
580UPDATE t1 SET a = CONCAT('xxx_', a), b = 7;
581SELECT @x, @y;
582@x	@y
5835	xxx_5
584
585DROP TABLE t1;
586
587# 6.3 Re-execution of a trigger that uses column 'a' of table 't' via
588# pseudo-variable NEW must fail if the table definition has been changed in
589# the way that the column 'a' does not exist anymore.
590#
591# 6.4 Re-execution of a trigger that uses column 'a' of table 't' via
592# pseudo-variable OLD must fail if the table definition has been changed in
593# the way that the column 'a' does not exist anymore.
594CREATE TABLE t1(a INT, b INT);
595INSERT INTO t1 VALUES (1, 2);
596CREATE TRIGGER t1_bu BEFORE UPDATE ON t1 FOR EACH ROW
597BEGIN
598SET @x = OLD.a;
599SET @y = NEW.b;
600END|
601
602UPDATE t1 SET a = 3, b = 4;
603
604ALTER TABLE t1 CHANGE COLUMN a a2 INT;
605
606UPDATE t1 SET a2 = 5, b = 6;
607ERROR 42S22: Unknown column 'a' in 'OLD'
608
609ALTER TABLE t1 CHANGE COLUMN a2 a INT;
610ALTER TABLE t1 CHANGE COLUMN b b2 INT;
611
612UPDATE t1 SET a = 5, b2 = 6;
613ERROR 42S22: Unknown column 'b' in 'NEW'
614
615DROP TABLE t1;
616
617# 7.1 Setup:
618#   - stored program 'a', which alters regular table 't' in a compatible
619#     way;
620#   - stored program 'b', which calls 'a' and uses 't' before and after the
621#     call;
622# Stored program 'b' must be executed successfully.
623CREATE TABLE t1(a INT, b INT);
624INSERT INTO t1 VALUES (1, 2);
625CREATE PROCEDURE p1()
626ALTER TABLE t1 ADD COLUMN c INT DEFAULT 3|
627CREATE PROCEDURE p2()
628BEGIN
629SELECT a, b FROM t1;
630CALL p1();
631SELECT a, b FROM t1;
632END|
633
634CALL p2();
635a	b
6361	2
637a	b
6381	2
639
640DROP PROCEDURE p1;
641DROP PROCEDURE p2;
642DROP TABLE t1;
643
644# 7.2 Setup:
645#   - stored program 'a', which alters temporary table 't' in a compatible
646#     way;
647#   - stored program 'b', which calls 'a' and uses 't' before and after the
648#     call;
649# Stored program 'b' must be executed successfully.
650CREATE TEMPORARY TABLE t1(a INT, b INT);
651INSERT INTO t1 VALUES (1, 2);
652CREATE PROCEDURE p1()
653ALTER TABLE t1 ADD COLUMN c INT DEFAULT 3|
654CREATE PROCEDURE p2()
655BEGIN
656SELECT a, b FROM t1;
657CALL p1();
658SELECT a, b FROM t1;
659END|
660
661CALL p2();
662a	b
6631	2
664a	b
6651	2
666
667DROP PROCEDURE p1;
668DROP PROCEDURE p2;
669DROP TEMPORARY TABLE t1;
670
671# 7.3 Setup:
672#   - stored program 'a', which re-creates regular table 't' in a
673#     compatible way;
674#   - stored program 'b', which calls 'a' and uses 't' before and after the
675#     call;
676# Stored program 'b' must be executed successfully.
677CREATE TABLE t1(a INT, b INT);
678INSERT INTO t1 VALUES (1, 2);
679CREATE PROCEDURE p1()
680BEGIN
681DROP TABLE t1;
682CREATE TABLE t1(a INT, b INT, c INT);
683INSERT INTO t1 VALUES (1, 2, 3);
684END|
685CREATE PROCEDURE p2()
686BEGIN
687SELECT a, b FROM t1;
688CALL p1();
689SELECT a, b FROM t1;
690END|
691
692CALL p2();
693a	b
6941	2
695a	b
6961	2
697
698DROP PROCEDURE p1;
699DROP PROCEDURE p2;
700DROP TABLE t1;
701
702# 7.4 Setup:
703#   - stored program 'a', which re-creates temporary table 't' in a
704#     compatible way;
705#   - stored program 'b', which calls 'a' and uses 't' before and after the
706#     call;
707# Stored program 'b' must be executed successfully.
708CREATE TEMPORARY TABLE t1(a INT, b INT);
709INSERT INTO t1 VALUES (1, 2);
710CREATE PROCEDURE p1()
711BEGIN
712DROP TEMPORARY TABLE t1;
713CREATE TEMPORARY TABLE t1(a INT, b INT, c INT);
714INSERT INTO t1 VALUES (1, 2, 3);
715END|
716CREATE PROCEDURE p2()
717BEGIN
718SELECT a, b FROM t1;
719CALL p1();
720SELECT a, b FROM t1;
721END|
722
723CALL p2();
724a	b
7251	2
726a	b
7271	2
728
729DROP PROCEDURE p1;
730DROP PROCEDURE p2;
731DROP TEMPORARY TABLE t1;
732
733# 7.5 Setup:
734#   - stored program 'a', which re-creates view 'v' in a compatible way;
735#   - stored program 'b', which calls 'a' and uses 'v' before and after the
736#     call;
737# Stored program 'b' must be executed successfully.
738CREATE VIEW v1 AS SELECT 1 AS a, 2 AS b;
739CREATE PROCEDURE p1()
740BEGIN
741DROP VIEW v1;
742CREATE VIEW v1 AS SELECT 1 AS a, 2 AS b, 3 AS c;
743END|
744CREATE PROCEDURE p2()
745BEGIN
746SELECT a, b FROM v1;
747CALL p1();
748SELECT a, b FROM v1;
749END|
750
751CALL p2();
752a	b
7531	2
754a	b
7551	2
756
757DROP PROCEDURE p1;
758DROP PROCEDURE p2;
759DROP VIEW v1;
760
761# 7.6 Setup:
762#   - stored program 'a', which alters regular table 't' in an incompatible
763#     way;
764#   - stored program 'b', which calls 'a' and uses 't' before and after the
765#     call;
766# Stored program 'b' must fail on access to the table after its
767# modification.
768CREATE TABLE t1(a INT, b INT);
769INSERT INTO t1 VALUES (1, 2);
770CREATE PROCEDURE p1()
771ALTER TABLE t1 DROP COLUMN a|
772CREATE PROCEDURE p2()
773BEGIN
774SELECT a, b FROM t1;
775CALL p1();
776SELECT a, b FROM t1;
777END|
778
779CALL p2();
780a	b
7811	2
782ERROR 42S22: Unknown column 'a' in 'field list'
783
784DROP PROCEDURE p1;
785DROP PROCEDURE p2;
786DROP TABLE t1;
787
788# 7.7 Setup:
789#   - stored program 'a', which alters temporary table 't' in an
790#     incompatible way;
791#   - stored program 'b', which calls 'a' and uses 't' before and after the
792#     call;
793# Stored program 'b' must fail on access to the table after its
794# modification.
795CREATE TEMPORARY TABLE t1(a INT, b INT);
796INSERT INTO t1 VALUES (1, 2);
797CREATE PROCEDURE p1()
798ALTER TABLE t1 DROP COLUMN a|
799CREATE PROCEDURE p2()
800BEGIN
801SELECT a, b FROM t1;
802CALL p1();
803SELECT a, b FROM t1;
804END|
805
806CALL p2();
807a	b
8081	2
809ERROR 42S22: Unknown column 'a' in 'field list'
810
811DROP PROCEDURE p1;
812DROP PROCEDURE p2;
813DROP TEMPORARY TABLE t1;
814
815# 7.8 Setup:
816#   - stored program 'a', which re-creates regular table 't' in an
817#     incompatible way;
818#   - stored program 'b', which calls 'a' and uses 't' before and after the
819#     call;
820# Stored program 'b' must fail on access to the table after its
821# modification.
822CREATE TABLE t1(a INT, b INT);
823INSERT INTO t1 VALUES (1, 2);
824CREATE PROCEDURE p1()
825BEGIN
826DROP TABLE t1;
827CREATE TABLE t1(b INT, c INT);
828INSERT INTO t1 VALUES (2, 3);
829END|
830CREATE PROCEDURE p2()
831BEGIN
832SELECT a, b FROM t1;
833CALL p1();
834SELECT a, b FROM t1;
835END|
836
837CALL p2();
838a	b
8391	2
840ERROR 42S22: Unknown column 'a' in 'field list'
841
842DROP PROCEDURE p1;
843DROP PROCEDURE p2;
844DROP TABLE t1;
845
846# 7.9 Setup:
847#   - stored program 'a', which re-creates temporary table 't' in an
848#     incompatible way;
849#   - stored program 'b', which calls 'a' and uses 't' before and after the
850#     call;
851# Stored program 'b' must fail on access to the table after its
852# modification.
853CREATE TEMPORARY TABLE t1(a INT, b INT);
854INSERT INTO t1 VALUES (1, 2);
855CREATE PROCEDURE p1()
856BEGIN
857DROP TEMPORARY TABLE t1;
858CREATE TEMPORARY TABLE t1(b INT, c INT);
859INSERT INTO t1 VALUES (2, 3);
860END|
861CREATE PROCEDURE p2()
862BEGIN
863SELECT a, b FROM t1;
864CALL p1();
865SELECT a, b FROM t1;
866END|
867
868CALL p2();
869a	b
8701	2
871ERROR 42S22: Unknown column 'a' in 'field list'
872
873DROP PROCEDURE p1;
874DROP PROCEDURE p2;
875DROP TEMPORARY TABLE t1;
876
877# 7.10 Setup:
878#   - stored program 'a', which re-creates view 'v' in an incompatible way;
879#   - stored program 'b', which calls 'a' and uses 'v' before and after the
880#     call;
881# Stored program 'b' must fail on access to the view after its
882# modification.
883CREATE VIEW v1 AS SELECT 1 AS a, 2 AS b;
884CREATE PROCEDURE p1()
885BEGIN
886DROP VIEW v1;
887CREATE VIEW v1 AS SELECT 2 AS b, 3 AS c;
888END|
889CREATE PROCEDURE p2()
890BEGIN
891SELECT a, b FROM v1;
892CALL p1();
893SELECT a, b FROM v1;
894END|
895
896CALL p2();
897a	b
8981	2
899ERROR 42S22: Unknown column 'a' in 'field list'
900
901DROP PROCEDURE p1;
902DROP PROCEDURE p2;
903DROP VIEW v1;
904# 8. Stored program must be executed successfully when:
905#  a. the program uses a table/view/temporary table that doesn't exist
906#     at the time of start program execution
907#  b. failed reference to the missed table/view/temporary table handled
908#     by stored program
909#  c. this table/view/temporary table is created as part of the
910#     program execution
911#  d. stored program gets access to newly created table/view/temporary
912#     table from some SQL-statement during subsequent stored program execution.
913CREATE PROCEDURE p1()
914BEGIN
915DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
916BEGIN
917SELECT 'SQLEXCEPTION caught' AS msg;
918CREATE TABLE t1(a INT, b INT);
919INSERT INTO t1 VALUES (1, 2);
920END;
921SELECT * FROM t1;
922SELECT * FROM t1;
923DROP TABLE t1;
924END|
925CREATE PROCEDURE p2()
926BEGIN
927DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
928BEGIN
929SELECT 'SQLEXCEPTION caught' AS msg;
930CREATE TEMPORARY TABLE t1(a INT, b INT);
931INSERT INTO t1 VALUES (1, 2);
932END;
933SELECT * FROM t1;
934SELECT * FROM t1;
935DROP TEMPORARY TABLE t1;
936END|
937CREATE PROCEDURE p3()
938BEGIN
939DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
940BEGIN
941SELECT 'SQLEXCEPTION caught' AS msg;
942CREATE VIEW v1 AS SELECT 1 AS a, 2 AS b;
943END;
944SELECT * FROM v1;
945SELECT * FROM v1;
946DROP VIEW v1;
947END|
948CALL p1();
949msg
950SQLEXCEPTION caught
951a	b
9521	2
953CALL p2();
954msg
955SQLEXCEPTION caught
956a	b
9571	2
958CALL p3();
959msg
960SQLEXCEPTION caught
961a	b
9621	2
963DROP PROCEDURE p1;
964DROP PROCEDURE p2;
965DROP PROCEDURE p3;
966
967# 9. Stored program must be executed successfully when
968#    - the stored program has an expression in one of the following
969#      statements
970#      - RETURN
971#      - IF
972#      - CASE
973#      - WHILE
974#      - UNTIL
975#      - SET
976#    - the expression depends on the meta-data of some table/view/temporary table;
977#    - the meta-data of dependent object has changed in a compatible way.
978#
979#    Note, that CASE-expression must be evaluated once even if (some)
980#    CASE-expressions need to be re-parsed.
981#
982# 10. Subsequent executions of a stored program must fail when
983#    - the stored program has an expression in one of the following
984#      statements
985#      - RETURN
986#      - IF
987#      - CASE
988#      - WHILE
989#      - UNTIL
990#      - SET
991#    - the expression depends on the meta-data of some table/view/temporary table;
992#    - the meta-data of dependent object has changed in a non-compatible way.
993#
994#    Note, that CASE-expression must be evaluated once even if (some)
995#    CASE-expressions need to be re-parsed.
996
997# Check IF-statement.
998
999CREATE PROCEDURE p1()
1000BEGIN
1001IF(SELECT * FROM t1)THEN
1002SELECT 1;
1003ELSE
1004SELECT 2;
1005END IF;
1006END|
1007CREATE PROCEDURE p2()
1008BEGIN
1009DECLARE v INT DEFAULT 1;
1010IF v * (SELECT * FROM t1) THEN
1011SELECT 1;
1012ELSE
1013SELECT 2;
1014END IF;
1015END|
1016CREATE FUNCTION f1() RETURNS INT
1017BEGIN
1018IF (SELECT * FROM t1) THEN
1019RETURN 1;
1020ELSE
1021RETURN 2;
1022END IF;
1023RETURN 3;
1024END|
1025CREATE FUNCTION f2() RETURNS INT
1026BEGIN
1027DECLARE v INT DEFAULT 1;
1028IF v * (SELECT * FROM t1) THEN
1029RETURN 1;
1030ELSE
1031RETURN 2;
1032END IF;
1033RETURN 3;
1034END|
1035CREATE TABLE t1(a INT);
1036INSERT INTO t1 VALUES (1);
1037
1038CALL p1();
10391
10401
1041CALL p2();
10421
10431
1044SELECT f1();
1045f1()
10461
1047SELECT f2();
1048f2()
10491
1050
1051UPDATE t1 SET a = 0;
1052
1053CALL p1();
10542
10552
1056CALL p2();
10572
10582
1059SELECT f1();
1060f1()
10612
1062SELECT f2();
1063f2()
10642
1065
1066ALTER TABLE t1 ADD COLUMN b INT DEFAULT 1;
1067
1068CALL p1();
1069ERROR 21000: Operand should contain 1 column(s)
1070CALL p2();
1071ERROR 21000: Operand should contain 1 column(s)
1072SELECT f1();
1073ERROR 21000: Operand should contain 1 column(s)
1074SELECT f2();
1075ERROR 21000: Operand should contain 1 column(s)
1076
1077ALTER TABLE t1 DROP COLUMN a;
1078
1079CALL p1();
10801
10811
1082CALL p2();
10831
10841
1085SELECT f1();
1086f1()
10871
1088SELECT f2();
1089f2()
10901
1091
1092DROP PROCEDURE p1;
1093DROP PROCEDURE p2;
1094DROP FUNCTION f1;
1095DROP FUNCTION f2;
1096DROP TABLE t1;
1097
1098# Check WHILE-statement.
1099
1100CREATE PROCEDURE p1(x INT)
1101BEGIN
1102WHILE(SELECT * FROM t1)DO
1103SELECT x;
1104UPDATE t1 SET a = x;
1105SET x = x - 1;
1106END WHILE;
1107END|
1108CREATE TABLE t1(a INT);
1109INSERT INTO t1 VALUES (0);
1110CALL p1(3);
1111UPDATE t1 SET a = 1;
1112CALL p1(3);
1113x
11143
1115x
11162
1117x
11181
1119x
11200
1121UPDATE t1 SET a = 1;
1122ALTER TABLE t1 ADD COLUMN b INT DEFAULT 1;
1123CALL p1(3);
1124ERROR 21000: Operand should contain 1 column(s)
1125ALTER TABLE t1 DROP COLUMN a;
1126CALL p1(3);
1127x
11283
1129ERROR 42S22: Unknown column 'a' in 'field list'
1130DROP PROCEDURE p1;
1131DROP TABLE t1;
1132
1133# Check REPEAT-statement.
1134
1135CREATE PROCEDURE p1(x INT)
1136BEGIN
1137REPEAT
1138SELECT x;
1139UPDATE t1 SET a = x;
1140SET x = x - 1;
1141UNTIL(NOT (SELECT * FROM t1))END REPEAT;
1142END|
1143CREATE TABLE t1(a INT);
1144INSERT INTO t1 VALUES (0);
1145CALL p1(3);
1146x
11473
1148x
11492
1150x
11511
1152x
11530
1154UPDATE t1 SET a = 1;
1155CALL p1(3);
1156x
11573
1158x
11592
1160x
11611
1162x
11630
1164UPDATE t1 SET a = 1;
1165ALTER TABLE t1 ADD COLUMN b INT DEFAULT 1;
1166CALL p1(3);
1167x
11683
1169ERROR 21000: Operand should contain 1 column(s)
1170ALTER TABLE t1 DROP COLUMN a;
1171CALL p1(3);
1172x
11733
1174ERROR 42S22: Unknown column 'a' in 'field list'
1175DROP PROCEDURE p1;
1176DROP TABLE t1;
1177
1178# Check CASE-statement (round #1).
1179
1180CREATE PROCEDURE p1()
1181BEGIN
1182CASE
1183WHEN (SELECT * FROM t1) = 1 THEN SELECT 'a1';
1184WHEN (SELECT * FROM t1) = 2 THEN SELECT 'a2';
1185WHEN (SELECT * FROM t1) = 3 THEN SELECT 'a3';
1186ELSE SELECT 'a4';
1187END CASE;
1188END|
1189CREATE PROCEDURE p2()
1190BEGIN
1191CASE (SELECT * FROM t1)
1192WHEN 1 THEN SELECT 'a1';
1193WHEN 2 THEN SELECT 'a2';
1194WHEN 3 THEN SELECT 'a3';
1195ELSE SELECT 'a4';
1196END CASE;
1197END|
1198CREATE TABLE t1(a INT);
1199INSERT INTO t1 VALUES (0);
1200
1201CALL p1();
1202a4
1203a4
1204CALL p2();
1205a4
1206a4
1207
1208UPDATE t1 SET a = 3;
1209
1210CALL p1();
1211a3
1212a3
1213CALL p2();
1214a3
1215a3
1216
1217ALTER TABLE t1 ADD COLUMN b INT DEFAULT 2;
1218
1219CALL p1();
1220ERROR 21000: Operand should contain 2 column(s)
1221CALL p2();
1222ERROR 21000: Operand should contain 1 column(s)
1223
1224ALTER TABLE t1 DROP COLUMN a;
1225
1226CALL p1();
1227a2
1228a2
1229CALL p2();
1230a2
1231a2
1232
1233DROP PROCEDURE p1;
1234DROP PROCEDURE p2;
1235DROP TABLE t1;
1236
1237# Check CASE-statement (round #2).
1238#
1239# Check that CASE-expression is executed once even if the metadata, used
1240# in a WHEN-expression, have changed.
1241
1242CREATE TABLE t1(a INT);
1243CREATE TABLE t2(a INT);
1244INSERT INTO t1 VALUES (1);
1245INSERT INTO t2 VALUES (1);
1246CREATE FUNCTION f1() RETURNS INT
1247BEGIN
1248SET @x = @x + 1;
1249RETURN (SELECT a FROM t1);
1250END|
1251CREATE PROCEDURE p1()
1252BEGIN
1253CASE f1()
1254WHEN 1 THEN SELECT 'a1';
1255WHEN 2 THEN SELECT 'a2';
1256WHEN (SELECT * FROM t2) THEN SELECT 'subselect';
1257ELSE SELECT 'else';
1258END CASE;
1259END|
1260
1261SET @x = 0;
1262CALL p1();
1263a1
1264a1
1265SELECT @x;
1266@x
12671
1268
1269UPDATE t1 SET a = 3;
1270ALTER TABLE t2 ADD COLUMN b INT DEFAULT 3;
1271
1272SET @x = 0;
1273CALL p1();
1274ERROR 21000: Operand should contain 1 column(s)
1275SELECT @x;
1276@x
12771
1278
1279ALTER TABLE t2 DROP COLUMN a;
1280
1281SET @x = 0;
1282CALL p1();
1283subselect
1284subselect
1285SELECT @x;
1286@x
12871
1288
1289DROP PROCEDURE p1;
1290DROP FUNCTION f1;
1291DROP TABLE t1;
1292DROP TABLE t2;
1293
1294# Check DEFAULT clause.
1295#
1296
1297CREATE TABLE t1(a INT);
1298INSERT INTO t1 VALUES (1);
1299CREATE PROCEDURE p1()
1300BEGIN
1301DECLARE v INT DEFAULT (SELECT * FROM t1);
1302SELECT v;
1303END|
1304
1305CALL p1();
1306v
13071
1308
1309ALTER TABLE t1 ADD COLUMN b INT DEFAULT 2;
1310
1311CALL p1();
1312ERROR 21000: Operand should contain 1 column(s)
1313
1314ALTER TABLE t1 DROP COLUMN a;
1315
1316CALL p1();
1317v
13182
1319
1320DROP PROCEDURE p1;
1321DROP TABLE t1;
1322
1323# Check SET.
1324#
1325
1326CREATE TABLE t1(a INT);
1327INSERT INTO t1 VALUES (1);
1328CREATE TABLE t2(a INT);
1329INSERT INTO t2 VALUES (1);
1330CREATE PROCEDURE p1()
1331BEGIN
1332DECLARE x INT;
1333SET x = (SELECT * FROM t1);
1334SELECT x;
1335END|
1336CREATE PROCEDURE p2()
1337BEGIN
1338SET @x = NULL;
1339SET @x = (SELECT * FROM t1);
1340SELECT @x;
1341END|
1342CREATE TRIGGER t2_bu BEFORE UPDATE ON t2 FOR EACH ROW
1343BEGIN
1344SET NEW.a = (SELECT * FROM t1) * 2;
1345END|
1346
1347CALL p1();
1348x
13491
1350
1351CALL p2();
1352@x
13531
1354
1355UPDATE t2 SET a = 10;
1356
1357ALTER TABLE t1 ADD COLUMN b INT DEFAULT 2;
1358
1359CALL p1();
1360ERROR 21000: Operand should contain 1 column(s)
1361
1362CALL p2();
1363ERROR 21000: Operand should contain 1 column(s)
1364
1365UPDATE t2 SET a = 20;
1366ERROR 21000: Operand should contain 1 column(s)
1367
1368ALTER TABLE t1 DROP COLUMN a;
1369
1370CALL p1();
1371x
13722
1373
1374CALL p2();
1375@x
13762
1377
1378UPDATE t2 SET a = 30;
1379
1380DROP PROCEDURE p1;
1381DROP PROCEDURE p2;
1382DROP TABLE t1;
1383DROP TABLE t2;
1384
1385# 11.1 If metadata of the objects (regular tables, temporary tables,
1386# views), used in SELECT-statement changed between DECLARE CURSOR and
1387# OPEN statements, the SELECT-statement should be re-parsed to use
1388# up-to-date metadata.
1389
1390
1391# - Regular table.
1392
1393CREATE TABLE t1(a INT);
1394INSERT INTO t1 VALUES (1);
1395CREATE PROCEDURE p1()
1396BEGIN
1397DECLARE v INT;
1398DECLARE c CURSOR FOR SELECT * FROM t1;
1399ALTER TABLE t1 ADD COLUMN b INT DEFAULT 2;
1400ALTER TABLE t1 DROP COLUMN a;
1401OPEN c;
1402FETCH c INTO v;
1403CLOSE c;
1404SELECT v;
1405END|
1406
1407CALL p1();
1408v
14092
1410
1411DROP TABLE t1;
1412DROP PROCEDURE p1;
1413
1414# - Temporary table.
1415
1416CREATE TEMPORARY TABLE t1(a INT);
1417INSERT INTO t1 VALUES (1);
1418CREATE PROCEDURE p1()
1419BEGIN
1420DECLARE v INT;
1421DECLARE c CURSOR FOR SELECT * FROM t1;
1422ALTER TABLE t1 ADD COLUMN b INT DEFAULT 2;
1423ALTER TABLE t1 DROP COLUMN a;
1424OPEN c;
1425FETCH c INTO v;
1426CLOSE c;
1427SELECT v;
1428END|
1429
1430CALL p1();
1431v
14322
1433
1434DROP TEMPORARY TABLE t1;
1435DROP PROCEDURE p1;
1436
1437# 11.2 If the metadata changed between OPEN and FETCH or CLOSE
1438# statements, those changes should not be noticed.
1439
1440CREATE TABLE t1(a INT);
1441INSERT INTO t1 VALUES (1);
1442CREATE PROCEDURE p1()
1443BEGIN
1444DECLARE v INT;
1445DECLARE c CURSOR FOR SELECT * FROM t1;
1446OPEN c;
1447ALTER TABLE t1 ADD COLUMN b INT DEFAULT 2;
1448ALTER TABLE t1 DROP COLUMN a;
1449FETCH c INTO v;
1450CLOSE c;
1451SELECT v;
1452END|
1453
1454CALL p1();
1455v
14561
1457
1458DROP TABLE t1;
1459DROP PROCEDURE p1;
1460
1461# 11.3 Re-parsing of the SELECT-statement should be made correctly
1462# (in the correct parsing context) if the metadata changed between
1463# DECLARE CURSOR and OPEN statements, and those statements reside in different
1464# parsing contexts.
1465
1466CREATE TABLE t1(a INT);
1467INSERT INTO t1 VALUES (1);
1468CREATE PROCEDURE p1()
1469BEGIN
1470DECLARE f1 INT;
1471DECLARE f2 INT;
1472DECLARE f3 INT;
1473DECLARE x INT DEFAULT 1;
1474DECLARE y INT DEFAULT 2;
1475DECLARE c CURSOR FOR SELECT x, y, t1.a FROM t1;
1476ALTER TABLE t1 ADD COLUMN b INT;
1477BEGIN
1478DECLARE x INT DEFAULT 10;
1479DECLARE y INT DEFAULT 20;
1480OPEN c;
1481FETCH c INTO f1, f2, f3;
1482SELECT f1, f2, f3;
1483CLOSE c;
1484END;
1485END|
1486
1487CALL p1();
1488f1	f2	f3
14891	2	1
1490
1491DROP TABLE t1;
1492DROP PROCEDURE p1;
1493
1494# Test procedure behaviour after view recreation.
1495CREATE TABLE t1 (a INT);
1496INSERT INTO t1 VALUES (1), (2);
1497CREATE VIEW v1 AS SELECT * FROM t1;
1498CREATE PROCEDURE p1()
1499SELECT * FROM v1;
1500CALL p1();
1501a
15021
15032
1504# Alter underlying table and recreate the view.
1505ALTER TABLE t1 ADD COLUMN (b INT);
1506ALTER VIEW v1 AS SELECT * FROM t1;
1507# And check whether the call of stored procedure handles it correctly.
1508CALL p1();
1509a	b
15101	NULL
15112	NULL
1512DROP VIEW v1;
1513DROP TABLE t1;
1514DROP PROCEDURE p1;
1515# Test if metadata changes for temporary table is handled
1516# correctly inside a stored procedure.
1517CREATE TEMPORARY TABLE t1 (a INT);
1518INSERT INTO t1 VALUES (1), (2);
1519CREATE PROCEDURE p1()
1520SELECT * FROM t1;
1521CALL p1();
1522a
15231
15242
1525# Test if added temporary table's column is recognized during
1526# procedure invocation.
1527ALTER TABLE t1 ADD COLUMN (b INT);
1528CALL p1();
1529a	b
15301	NULL
15312	NULL
1532# Test if dropped temporary table's column is not appeared
1533# in procedure's result.
1534ALTER TABLE t1 DROP COLUMN a;
1535CALL p1();
1536b
1537NULL
1538NULL
1539DROP PROCEDURE p1;
1540DROP TABLE t1;
1541# Test handle of metadata changes with stored function.
1542CREATE TABLE t1 (a INT);
1543INSERT INTO t1 VALUES (1), (2);
1544CREATE FUNCTION f1() RETURNS INT
1545BEGIN
1546CREATE TEMPORARY TABLE t1_result_set AS SELECT * FROM t1;
1547RETURN 0;
1548END|
1549SELECT f1();
1550f1()
15510
1552SELECT * FROM t1_result_set;
1553a
15541
15552
1556DROP TABLE t1_result_set;
1557# Check if added column is noticed by invocation of stored function.
1558ALTER TABLE t1 ADD COLUMN (b INT);
1559SELECT f1();
1560f1()
15610
1562SELECT * FROM t1_result_set;
1563a	b
15641	NULL
15652	NULL
1566DROP TABLE t1_result_set;
1567# Check if dropped column is noticed by invocation of stored function.
1568ALTER TABLE t1 DROP COLUMN a;
1569SELECT f1();
1570f1()
15710
1572SELECT * FROM t1_result_set;
1573b
1574NULL
1575NULL
1576DROP TABLE t1_result_set;
1577DROP TABLE t1;
1578DROP FUNCTION f1;
1579# Test if table's recreation is handled correctly
1580# inside a stored function.
1581CREATE TABLE t1 (a INT);
1582INSERT INTO t1 VALUES (1), (2);
1583CREATE FUNCTION f1() RETURNS INT
1584BEGIN
1585CREATE TEMPORARY TABLE t1_result_set AS SELECT * FROM t1;
1586RETURN 0;
1587END|
1588SELECT f1();
1589f1()
15900
1591SELECT * FROM t1_result_set;
1592a
15931
15942
1595DROP TABLE t1_result_set;
1596# Recreate table and check if it is handled correctly
1597# by function invocation.
1598DROP TABLE t1;
1599CREATE TABLE t1 (a INT);
1600INSERT INTO t1 VALUES (1), (2);
1601SELECT f1();
1602f1()
16030
1604SELECT * FROM t1_result_set;
1605a
16061
16072
1608DROP TABLE t1_result_set;
1609DROP FUNCTION f1;
1610DROP TABLE t1;
1611# Test if changes in the view's metadata is handled
1612# correctly by function call.
1613CREATE TABLE t1 (a INT);
1614INSERT INTO t1 VALUES (1), (2);
1615CREATE VIEW v1 AS SELECT * FROM t1;
1616CREATE FUNCTION f1() RETURNS INT
1617BEGIN
1618CREATE TEMPORARY TABLE t1_result_set AS SELECT * FROM v1;
1619RETURN 0;
1620END|
1621SELECT f1();
1622f1()
16230
1624SELECT * FROM t1_result_set;
1625a
16261
16272
1628DROP TABLE t1_result_set;
1629ALTER TABLE t1 ADD COLUMN (b INT);
1630ALTER VIEW v1 AS SELECT * FROM t1;
1631SELECT f1();
1632f1()
16330
1634SELECT * FROM t1_result_set;
1635a	b
16361	NULL
16372	NULL
1638DROP TABLE t1_result_set;
1639DROP TABLE t1;
1640DROP VIEW v1;
1641DROP FUNCTION f1;
1642# Check if queried object's type substitution (table->view, view->table,
1643# table->temp table, etc.) is handled correctly during invocation of
1644# stored function/procedure.
1645CREATE TABLE t1 (a INT);
1646INSERT INTO t1 VALUES (1), (2);
1647CREATE FUNCTION f1() RETURNS INT
1648BEGIN
1649CREATE TEMPORARY TABLE t1_result_set AS SELECT * FROM t1;
1650RETURN 0;
1651END|
1652CREATE PROCEDURE p1()
1653SELECT * FROM t1|
1654CALL p1();
1655a
16561
16572
1658SELECT f1();
1659f1()
16600
1661SELECT * FROM t1_result_set;
1662a
16631
16642
1665DROP TABLE t1_result_set;
1666DROP TABLE t1;
1667CREATE TEMPORARY TABLE t1 (a INT);
1668INSERT INTO t1 VALUES (1), (2);
1669CALL p1;
1670a
16711
16722
1673SELECT f1();
1674f1()
16750
1676SELECT * FROM t1_result_set;
1677a
16781
16792
1680DROP TABLE t1_result_set;
1681DROP TABLE t1;
1682CREATE TABLE t2 (a INT);
1683INSERT INTO t2 VALUES (1), (2);
1684CREATE VIEW t1 AS SELECT * FROM t2;
1685CALL p1;
1686a
16871
16882
1689SELECT f1();
1690f1()
16910
1692SELECT * FROM t1_result_set;
1693a
16941
16952
1696DROP TABLE t1_result_set;
1697DROP TABLE t2;
1698DROP VIEW t1;
1699DROP FUNCTION f1;
1700DROP PROCEDURE p1;
1701# Test handle of metadata changes with triggers.
1702CREATE TABLE t1 (a INT);
1703CREATE TRIGGER trg1 BEFORE INSERT ON t1 FOR EACH ROW
1704SET new.a = new.a + 100;
1705INSERT INTO t1 VALUES (1), (2);
1706SELECT * FROM t1;
1707a
1708101
1709102
1710# Check if added table's column is handled correctly inside trigger.
1711ALTER TABLE t1 ADD COLUMN (b INT);
1712INSERT INTO t1 VALUES (3, 4);
1713SELECT * FROM t1;
1714a	b
1715101	NULL
1716102	NULL
1717103	4
1718DROP TRIGGER trg1;
1719DROP TABLE t1;
1720# Test if deleted column is handled correctly by trigger invocation.
1721CREATE TABLE t1 (a INT, b INT);
1722CREATE TRIGGER trg1 BEFORE INSERT ON t1 FOR EACH ROW
1723SET new.a = new.a + 100;
1724INSERT INTO t1 VALUES (1, 2), (3, 4);
1725SELECT * FROM t1;
1726a	b
1727101	2
1728103	4
1729ALTER TABLE t1 DROP COLUMN b;
1730INSERT INTO t1 VALUES (5);
1731SELECT * FROM t1;
1732a
1733101
1734103
1735105
1736DROP TRIGGER trg1;
1737DROP TABLE t1;
1738# Check if server returns and error when was dropped a column
1739# that is used inside a trigger body.
1740CREATE TABLE t1 (a INT, b INT);
1741CREATE TRIGGER trg1 BEFORE INSERT ON t1 FOR EACH ROW
1742SET new.a = new.a + 100;
1743INSERT INTO t1 VALUES (1, 2), (3, 4);
1744SELECT * FROM t1;
1745a	b
1746101	2
1747103	4
1748ALTER TABLE t1 DROP COLUMN a;
1749INSERT INTO t1 VALUES (5);
1750ERROR 42S22: Unknown column 'a' in 'NEW'
1751DROP TRIGGER trg1;
1752DROP TABLE t1;
1753
1754# Check updateable views inside triggers.
1755CREATE TABLE t1(a INT);
1756INSERT INTO t1 VALUES (1);
1757CREATE TABLE t2(a INT);
1758INSERT INTO t2 VALUES (1);
1759CREATE VIEW v1 AS SELECT a FROM t1;
1760CREATE TRIGGER t2_bu BEFORE UPDATE ON t2 FOR EACH ROW
1761BEGIN
1762INSERT INTO v1 VALUES (NEW.a);
1763SET @x = (SELECT CHARSET(a) FROM v1 LIMIT 1);
1764END|
1765
1766SET @x = NULL;
1767UPDATE t2 SET a = 10;
1768SELECT * FROM v1;
1769a
17701
177110
1772SELECT @x;
1773@x
1774binary
1775
1776ALTER TABLE t1 CHANGE COLUMN a a CHAR(2);
1777
1778SET @x = NULL;
1779UPDATE t2 SET a = 20;
1780SELECT * FROM v1;
1781a
17821
178310
178420
1785SELECT @x;
1786@x
1787latin1
1788
1789DROP TABLE t1;
1790DROP TABLE t2;
1791DROP VIEW v1;
1792SET sql_mode = default;
1793