Skip to content

Commit 2dbd269

Browse files
author
Alexander Barkov
committed
MDEV-7283 UDF: CREATE OR REPLACE, CREATE IF NOT EXISTS, DROP IF EXISTS
MDEV-7282 SP: CREATE OR REPLACE, CREATE IF NOT EXISTS
1 parent 48ed8ab commit 2dbd269

23 files changed

+1011
-147
lines changed

mysql-test/r/create_drop_binlog.result

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,111 @@ Log_name Pos Event_type Server_id End_log_pos Info
2727
# # Query 1 # DROP DATABASE IF EXISTS d1
2828
RESET MASTER;
2929
USE test;
30+
CREATE OR REPLACE FUNCTION f1() RETURNS INT RETURN 1;
31+
CREATE OR REPLACE FUNCTION f1() RETURNS INT RETURN 1;
32+
DROP FUNCTION f1;
33+
CREATE FUNCTION IF NOT EXISTS f1() RETURNS INT RETURN 1;
34+
CREATE FUNCTION IF NOT EXISTS f1() RETURNS INT RETURN 1;
35+
Warnings:
36+
Note 1304 FUNCTION f1 already exists
37+
DROP FUNCTION IF EXISTS f1;
38+
DROP FUNCTION IF EXISTS f1;
39+
Warnings:
40+
Note 1305 FUNCTION test.f1 does not exist
41+
SHOW BINLOG EVENTS;
42+
Log_name Pos Event_type Server_id End_log_pos Info
43+
# # Format_desc 1 # VER
44+
# # Gtid_list 1 # []
45+
# # Binlog_checkpoint 1 # master-bin.000001
46+
# # Gtid 1 # GTID #-#-#
47+
# # Query 1 # use `test`; CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `f1`() RETURNS int(11)
48+
RETURN 1
49+
# # Gtid 1 # GTID #-#-#
50+
# # Query 1 # use `test`; CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `f1`() RETURNS int(11)
51+
RETURN 1
52+
# # Gtid 1 # GTID #-#-#
53+
# # Query 1 # use `test`; DROP FUNCTION f1
54+
# # Gtid 1 # GTID #-#-#
55+
# # Query 1 # use `test`; CREATE DEFINER=`root`@`localhost` FUNCTION IF NOT EXISTS `f1`() RETURNS int(11)
56+
RETURN 1
57+
# # Gtid 1 # GTID #-#-#
58+
# # Query 1 # use `test`; CREATE DEFINER=`root`@`localhost` FUNCTION IF NOT EXISTS `f1`() RETURNS int(11)
59+
RETURN 1
60+
# # Gtid 1 # GTID #-#-#
61+
# # Query 1 # use `test`; DROP FUNCTION IF EXISTS f1
62+
# # Gtid 1 # GTID #-#-#
63+
# # Query 1 # use `test`; DROP FUNCTION IF EXISTS f1
64+
RESET MASTER;
65+
CREATE TABLE t1 (a DATETIME);
66+
CREATE OR REPLACE PROCEDURE p1() DELETE FROM t1;
67+
CREATE OR REPLACE PROCEDURE p1() DELETE FROM t1;
68+
DROP PROCEDURE p1;
69+
CREATE PROCEDURE IF NOT EXISTS p1() DELETE FROM t1;
70+
CREATE PROCEDURE IF NOT EXISTS p1() DELETE FROM t1;
71+
Warnings:
72+
Note 1304 PROCEDURE p1 already exists
73+
DROP PROCEDURE IF EXISTS p1;
74+
DROP PROCEDURE IF EXISTS p1;
75+
Warnings:
76+
Note 1305 PROCEDURE test.p1 does not exist
77+
SHOW BINLOG EVENTS;
78+
Log_name Pos Event_type Server_id End_log_pos Info
79+
# # Format_desc 1 # VER
80+
# # Gtid_list 1 # []
81+
# # Binlog_checkpoint 1 # master-bin.000001
82+
# # Gtid 1 # GTID #-#-#
83+
# # Query 1 # use `test`; CREATE TABLE t1 (a DATETIME)
84+
# # Gtid 1 # GTID #-#-#
85+
# # Query 1 # use `test`; CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `p1`()
86+
DELETE FROM t1
87+
# # Gtid 1 # GTID #-#-#
88+
# # Query 1 # use `test`; CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `p1`()
89+
DELETE FROM t1
90+
# # Gtid 1 # GTID #-#-#
91+
# # Query 1 # use `test`; DROP PROCEDURE p1
92+
# # Gtid 1 # GTID #-#-#
93+
# # Query 1 # use `test`; CREATE DEFINER=`root`@`localhost` PROCEDURE IF NOT EXISTS `p1`()
94+
DELETE FROM t1
95+
# # Gtid 1 # GTID #-#-#
96+
# # Query 1 # use `test`; CREATE DEFINER=`root`@`localhost` PROCEDURE IF NOT EXISTS `p1`()
97+
DELETE FROM t1
98+
# # Gtid 1 # GTID #-#-#
99+
# # Query 1 # use `test`; DROP PROCEDURE IF EXISTS p1
100+
# # Gtid 1 # GTID #-#-#
101+
# # Query 1 # use `test`; DROP PROCEDURE IF EXISTS p1
102+
DROP TABLE t1;
103+
RESET MASTER;
104+
CREATE OR REPLACE FUNCTION metaphon RETURNS STRING SONAME 'UDF_EXAMPLE_LIB';
105+
CREATE OR REPLACE FUNCTION metaphon RETURNS STRING SONAME 'UDF_EXAMPLE_LIB';
106+
DROP FUNCTION metaphon;
107+
CREATE FUNCTION IF NOT EXISTS metaphon RETURNS STRING SONAME 'UDF_EXAMPLE_LIB';
108+
CREATE FUNCTION IF NOT EXISTS metaphon RETURNS STRING SONAME 'UDF_EXAMPLE_LIB';
109+
Warnings:
110+
Note 1125 Function 'metaphon' already exists
111+
DROP FUNCTION IF EXISTS metaphon;
112+
DROP FUNCTION IF EXISTS metaphon;
113+
Warnings:
114+
Note 1305 FUNCTION test.metaphon does not exist
115+
SHOW BINLOG EVENTS;
116+
Log_name Pos Event_type Server_id End_log_pos Info
117+
# # Format_desc 1 # VER
118+
# # Gtid_list 1 # []
119+
# # Binlog_checkpoint 1 # master-bin.000001
120+
# # Gtid 1 # GTID #-#-#
121+
# # Query 1 # use `test`; CREATE OR REPLACE FUNCTION metaphon RETURNS STRING SONAME 'UDM_EXAMPLE_LIB'
122+
# # Gtid 1 # GTID #-#-#
123+
# # Query 1 # use `test`; CREATE OR REPLACE FUNCTION metaphon RETURNS STRING SONAME 'UDM_EXAMPLE_LIB'
124+
# # Gtid 1 # GTID #-#-#
125+
# # Query 1 # use `test`; DROP FUNCTION metaphon
126+
# # Gtid 1 # GTID #-#-#
127+
# # Query 1 # use `test`; CREATE FUNCTION IF NOT EXISTS metaphon RETURNS STRING SONAME 'UDM_EXAMPLE_LIB'
128+
# # Gtid 1 # GTID #-#-#
129+
# # Query 1 # use `test`; CREATE FUNCTION IF NOT EXISTS metaphon RETURNS STRING SONAME 'UDM_EXAMPLE_LIB'
130+
# # Gtid 1 # GTID #-#-#
131+
# # Query 1 # use `test`; DROP FUNCTION IF EXISTS metaphon
132+
# # Gtid 1 # GTID #-#-#
133+
# # Query 1 # use `test`; DROP FUNCTION IF EXISTS metaphon
134+
RESET MASTER;
30135
#
31136
# CREATE SERVER is not logged
32137
#
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
SET timestamp=UNIX_TIMESTAMP('2014-09-30 08:00:00');
2+
CREATE FUNCTION f1(str char(20))
3+
RETURNS CHAR(100)
4+
RETURN CONCAT('Hello, ', str, '!');
5+
SELECT * FROM mysql.proc WHERE name like 'f1';
6+
db name type specific_name language sql_data_access is_deterministic security_type param_list returns body definer created modified sql_mode comment character_set_client collation_connection db_collation body_utf8
7+
test f1 FUNCTION f1 SQL CONTAINS_SQL NO DEFINER str char(20) char(100) CHARSET latin1 RETURN CONCAT('Hello, ', str, '!') root@localhost 2014-09-30 08:00:00 2014-09-30 08:00:00 latin1 latin1_swedish_ci latin1_swedish_ci RETURN CONCAT('Hello, ', str, '!')
8+
SELECT f1('world');
9+
f1('world')
10+
Hello, world!
11+
CREATE FUNCTION f1(str char(20))
12+
RETURNS TEXT
13+
RETURN CONCAT('Hello2, ', str, '!');
14+
ERROR 42000: FUNCTION f1 already exists
15+
SELECT body FROM mysql.proc WHERE name like 'f1';
16+
body
17+
RETURN CONCAT('Hello, ', str, '!')
18+
CREATE FUNCTION IF NOT EXISTS f1(str char(20))
19+
RETURNS CHAR(100)
20+
RETURN CONCAT('Hello3, ', str, '!');
21+
Warnings:
22+
Note 1304 FUNCTION f1 already exists
23+
SELECT body FROM mysql.proc WHERE name like 'f1';
24+
body
25+
RETURN CONCAT('Hello, ', str, '!')
26+
CREATE OR REPLACE FUNCTION IF NOT EXISTS f1(str char(20))
27+
RETURNS CHAR(100)
28+
RETURN CONCAT('Hello4, ', str, '!');
29+
ERROR HY000: Incorrect usage of OR REPLACE and IF NOT EXISTS
30+
SELECT body FROM mysql.proc WHERE name like 'f1';
31+
body
32+
RETURN CONCAT('Hello, ', str, '!')
33+
CREATE OR REPLACE FUNCTION f1(str char(20))
34+
RETURNS CHAR(100)
35+
RETURN CONCAT('Hello5, ', str, '!');
36+
SELECT body FROM mysql.proc WHERE name like 'f1';
37+
body
38+
RETURN CONCAT('Hello5, ', str, '!')
39+
DROP FUNCTION f1;
40+
CREATE FUNCTION IF NOT EXISTS f1(str char(20))
41+
RETURNS CHAR(100)
42+
RETURN CONCAT('Hello6, ', str, '!');
43+
SELECT body FROM mysql.proc WHERE name like 'f1';
44+
body
45+
RETURN CONCAT('Hello6, ', str, '!')
46+
SELECT f1('world');
47+
f1('world')
48+
Hello6, world!
49+
DROP FUNCTION IF EXISTS f1;
50+
SELECT body FROM mysql.proc WHERE name like 'f1';
51+
body
52+
DROP FUNCTION IF EXISTS f1;
53+
Warnings:
54+
Note 1305 FUNCTION test.f1 does not exist
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
CREATE TABLE t1 (id INT);
2+
CREATE PROCEDURE proc1 (OUT cnt INT) COMMENT 'comment1' BEGIN SELECT COUNT(*) INTO cnt FROM t1; END$$
3+
CALL proc1(@cnt);
4+
SELECT @cnt;
5+
@cnt
6+
0
7+
INSERT INTO t1 VALUES (1), (2), (3);
8+
CALL proc1(@cnt);
9+
SELECT @cnt;
10+
@cnt
11+
3
12+
CREATE PROCEDURE proc1 (OUT cnt INT) COMMENT 'comment2'
13+
BEGIN SELECT COUNT(*) INTO cnt FROM t1; END$$
14+
ERROR 42000: PROCEDURE proc1 already exists
15+
SELECT comment FROM mysql.proc WHERE name='proc1'$$
16+
comment
17+
comment1
18+
CREATE PROCEDURE IF NOT EXISTS proc1 (OUT cnt INT) COMMENT 'comment3'
19+
BEGIN SELECT COUNT(*) INTO cnt FROM t1; END$$
20+
Warnings:
21+
Note 1304 PROCEDURE proc1 already exists
22+
SELECT comment FROM mysql.proc WHERE name='proc1'$$
23+
comment
24+
comment1
25+
CREATE OR REPLACE PROCEDURE IF NOT EXISTS proc1 (OUT cnt INT) COMMENT 'comment4'
26+
BEGIN SELECT COUNT(*) INTO cnt FROM t1; END$$
27+
ERROR HY000: Incorrect usage of OR REPLACE and IF NOT EXISTS
28+
SELECT comment FROM mysql.proc WHERE name='proc1'$$
29+
comment
30+
comment1
31+
CREATE OR REPLACE PROCEDURE proc1 (OUT cnt INT) COMMENT 'comment5'
32+
BEGIN SELECT COUNT(*) INTO cnt FROM t1; END$$
33+
SELECT comment FROM mysql.proc WHERE name='proc1'$$
34+
comment
35+
comment5
36+
DROP PROCEDURE proc1;
37+
CREATE PROCEDURE IF NOT EXISTS proc1 (OUT cnt INT) BEGIN SELECT COUNT(*) INTO cnt FROM t1; END$$
38+
INSERT INTO t1 VALUES (1), (2), (3);
39+
CALL proc1(@cnt);
40+
SELECT @cnt;
41+
@cnt
42+
6
43+
DROP TABLE IF EXISTS t1;
44+
DROP PROCEDURE IF EXISTS proc1;
45+
DROP PROCEDURE IF EXISTS proc1;
46+
Warnings:
47+
Note 1305 PROCEDURE test.proc1 does not exist

mysql-test/r/create_drop_udf.result

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
CREATE FUNCTION IF NOT EXISTS metaphon RETURNS STRING SONAME "UDF_EXAMPLE_LIB";
2+
SELECT ret FROM mysql.func WHERE name like 'metaphon';
3+
ret
4+
0
5+
CREATE FUNCTION metaphon RETURNS INT SONAME "$UDF_EXAMPLE_SO";
6+
ERROR HY000: Function 'metaphon' already exists
7+
SELECT ret FROM mysql.func WHERE name like 'metaphon';
8+
ret
9+
0
10+
CREATE FUNCTION IF NOT EXISTS metaphon RETURNS REAL SONAME "UDF_EXAMPLE_LIB";
11+
Warnings:
12+
Note 1125 Function 'metaphon' already exists
13+
SELECT ret FROM mysql.func WHERE name like 'metaphon';
14+
ret
15+
0
16+
DROP FUNCTION IF EXISTS metaphon;
17+
CREATE FUNCTION IF NOT EXISTS metaphon RETURNS REAL SONAME "UDF_EXAMPLE_LIB";
18+
SELECT ret FROM mysql.func WHERE name like 'metaphon';
19+
ret
20+
1
21+
CREATE OR REPLACE FUNCTION IF NOT EXISTS metaphon RETURNS STRING SONAME "udf_example.so";
22+
ERROR HY000: Incorrect usage of OR REPLACE and IF NOT EXISTS
23+
SELECT ret FROM mysql.func WHERE name like 'metaphon';
24+
ret
25+
1
26+
CREATE OR REPLACE FUNCTION metaphon RETURNS STRING SONAME "UDF_EXAMPLE_LIB";
27+
SELECT ret FROM mysql.func WHERE name like 'metaphon';
28+
ret
29+
0
30+
CREATE FUNCTION metaphon RETURNS STRING SONAME "udf_example.so";
31+
ERROR HY000: Function 'metaphon' already exists
32+
SELECT metaphon('mariadb');
33+
metaphon('mariadb')
34+
MRTB
35+
DROP FUNCTION metaphon;
36+
DROP FUNCTION IF EXISTS metaphon;
37+
Warnings:
38+
Note 1305 FUNCTION test.metaphon does not exist

mysql-test/r/create_or_replace_permission.result

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ ERROR 42000: Access denied for user 'mysqltest_1'@'localhost' to database 'db2'
2929
USE db1;
3030
CREATE OR REPLACE TABLE t1(id INT);
3131
ERROR 42000: DROP command denied to user 'mysqltest_1'@'localhost' for table 't1'
32+
CREATE OR REPLACE PROCEDURE proc1 (OUT cnt INT) BEGIN END;
33+
ERROR 42000: alter routine command denied to user 'mysqltest_1'@'localhost' for routine 'db1.proc1'
34+
CREATE OR REPLACE FUNCTION lookup RETURNS STRING SONAME "udf_example.so";
35+
ERROR 42000: Access denied for user 'mysqltest_1'@'localhost' to database 'mysql'
36+
CREATE OR REPLACE FUNCTION hello(str char(20)) RETURNS TEXT RETURN CONCAT('Hello, ', str, '!');
37+
ERROR 42000: alter routine command denied to user 'mysqltest_1'@'localhost' for routine 'db1.hello'
3238
SELECT CURRENT_USER;
3339
CURRENT_USER
3440
root@localhost

mysql-test/r/sp-error.result

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,9 +1223,7 @@ END' at line 1
12231223
CREATE PROCEDURE IF NOT EXISTS bug14702()
12241224
BEGIN
12251225
END;
1226-
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'IF NOT EXISTS bug14702()
1227-
BEGIN
1228-
END' at line 1
1226+
DROP PROCEDURE IF EXISTS bug14702;
12291227
DROP TABLE IF EXISTS t1;
12301228
CREATE TABLE t1 (i INT);
12311229
CREATE PROCEDURE bug20953() CREATE VIEW v AS SELECT 1 INTO @a;

mysql-test/suite/funcs_1/r/storedproc.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2262,7 +2262,7 @@ ERROR 42000: You have an error in your SQL syntax; check the manual that corresp
22622262
SELECT * from t1 where f2=f1' at line 1
22632263
CREATE PROCEDURE if()
22642264
SELECT * from t1 where f2=f1;
2265-
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'if()
2265+
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '()
22662266
SELECT * from t1 where f2=f1' at line 1
22672267
CREATE PROCEDURE ignore()
22682268
SELECT * from t1 where f2=f1;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
include/master-slave.inc
2+
[connection master]
3+
CREATE FUNCTION hello (str CHAR(20)) RETURNS CHAR(50) DETERMINISTIC RETURN
4+
CONCAT('Hello, ', str, '!');
5+
CREATE FUNCTION hello (str CHAR(20)) RETURNS CHAR(50) DETERMINISTIC RETURN
6+
CONCAT('Hello, ', str, '!');
7+
ERROR 42000: FUNCTION hello already exists
8+
SHOW CREATE FUNCTION hello;
9+
Function sql_mode Create Function character_set_client collation_connection Database Collation
10+
hello CREATE DEFINER=`root`@`localhost` FUNCTION `hello`(str CHAR(20)) RETURNS char(50) CHARSET latin1
11+
DETERMINISTIC
12+
RETURN
13+
CONCAT('Hello, ', str, '!') latin1 latin1_swedish_ci latin1_swedish_ci
14+
CREATE OR REPLACE FUNCTION IF NOT EXISTS hello (str CHAR(20)) RETURNS CHAR(50) DETERMINISTIC RETURN
15+
CONCAT('Hello, ', str, '!');
16+
ERROR HY000: Incorrect usage of OR REPLACE and IF NOT EXISTS
17+
CREATE OR REPLACE FUNCTION hello (str CHAR(20)) RETURNS CHAR(50) DETERMINISTIC RETURN
18+
CONCAT('Hello, ', str, '!');
19+
SELECT hello('master');
20+
hello('master')
21+
Hello, master!
22+
SELECT hello('slave');
23+
hello('slave')
24+
Hello, slave!
25+
CREATE FUNCTION IF NOT EXISTS hello (str CHAR(20)) RETURNS CHAR(50) DETERMINISTIC RETURN
26+
CONCAT('Hello, ', str, '!');
27+
Warnings:
28+
Note 1304 FUNCTION hello already exists
29+
CREATE OR REPLACE FUNCTION bye (str CHAR(20)) RETURNS CHAR(50) DETERMINISTIC RETURN
30+
CONCAT('Bye, ', str, '!');
31+
SELECT hello('master');
32+
hello('master')
33+
Hello, master!
34+
SELECT bye('master');
35+
bye('master')
36+
Bye, master!
37+
SELECT hello('slave');
38+
hello('slave')
39+
Hello, slave!
40+
SELECT bye('slave');
41+
bye('slave')
42+
Bye, slave!
43+
DROP FUNCTION hello;
44+
DROP FUNCTION IF EXISTS bye;
45+
DROP FUNCTION hello;
46+
ERROR 42000: FUNCTION test.hello does not exist
47+
DROP FUNCTION IF EXISTS bye;
48+
Warnings:
49+
Note 1305 FUNCTION test.bye does not exist
50+
include/rpl_end.inc

0 commit comments

Comments
 (0)