1
by brian
clean slate |
1 |
--source include/have_udf.inc
|
2 |
#
|
|
3 |
# To run this tests the "sql/udf_example.c" need to be compiled into |
|
4 |
# udf_example.so and LD_LIBRARY_PATH should be setup to point out where |
|
5 |
# the library are. |
|
6 |
#
|
|
7 |
||
8 |
||
9 |
--disable_warnings
|
|
10 |
drop table if exists t1; |
|
11 |
--enable_warnings
|
|
12 |
||
13 |
#
|
|
14 |
# Create the example functions from udf_example |
|
15 |
#
|
|
16 |
||
17 |
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
|
|
18 |
eval CREATE FUNCTION metaphon RETURNS STRING SONAME "$UDF_EXAMPLE_LIB"; |
|
19 |
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
|
|
20 |
eval CREATE FUNCTION myfunc_double RETURNS REAL SONAME "$UDF_EXAMPLE_LIB"; |
|
21 |
||
22 |
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
|
|
23 |
--error ER_CANT_FIND_DL_ENTRY
|
|
24 |
eval CREATE FUNCTION myfunc_nonexist RETURNS INTEGER SONAME "$UDF_EXAMPLE_LIB"; |
|
25 |
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
|
|
26 |
eval CREATE FUNCTION myfunc_int RETURNS INTEGER SONAME "$UDF_EXAMPLE_LIB"; |
|
27 |
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
|
|
28 |
eval CREATE FUNCTION sequence RETURNS INTEGER SONAME "$UDF_EXAMPLE_LIB"; |
|
29 |
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
|
|
30 |
eval CREATE FUNCTION lookup RETURNS STRING SONAME "$UDF_EXAMPLE_LIB"; |
|
31 |
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
|
|
32 |
eval CREATE FUNCTION reverse_lookup |
|
33 |
RETURNS STRING SONAME "$UDF_EXAMPLE_LIB"; |
|
34 |
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
|
|
35 |
eval CREATE AGGREGATE FUNCTION avgcost |
|
36 |
RETURNS REAL SONAME "$UDF_EXAMPLE_LIB"; |
|
37 |
||
38 |
--error ER_CANT_INITIALIZE_UDF
|
|
39 |
select myfunc_double(); |
|
40 |
select myfunc_double(1); |
|
41 |
select myfunc_double(78654); |
|
42 |
--error 1305
|
|
43 |
select myfunc_nonexist(); |
|
44 |
select myfunc_int(); |
|
45 |
--error ER_CANT_INITIALIZE_UDF
|
|
46 |
select lookup(); |
|
47 |
select lookup("127.0.0.1"); |
|
48 |
--error ER_CANT_INITIALIZE_UDF
|
|
49 |
select lookup(127,0,0,1); |
|
50 |
select lookup("localhost"); |
|
51 |
--error ER_CANT_INITIALIZE_UDF
|
|
52 |
select reverse_lookup(); |
|
53 |
||
54 |
# These two functions should return "localhost", but it's |
|
55 |
# depending on configuration, so just call them and don't log the result |
|
56 |
--disable_result_log
|
|
57 |
select reverse_lookup("127.0.0.1"); |
|
58 |
select reverse_lookup(127,0,0,1); |
|
59 |
--enable_result_log
|
|
60 |
||
61 |
select reverse_lookup("localhost"); |
|
62 |
--error ER_CANT_INITIALIZE_UDF
|
|
63 |
select avgcost(); |
|
64 |
--error ER_CANT_INITIALIZE_UDF
|
|
65 |
select avgcost(100,23.76); |
|
66 |
create table t1(sum int, price float(24)); |
|
67 |
insert into t1 values(100, 50.00), (100, 100.00); |
|
68 |
select avgcost(sum, price) from t1; |
|
69 |
delete from t1; |
|
70 |
insert into t1 values(100, 54.33), (200, 199.99); |
|
71 |
select avgcost(sum, price) from t1; |
|
72 |
drop table t1; |
|
73 |
||
74 |
#------------------------------------------------------------------------ |
|
75 |
# BUG#17261 Passing a variable from a stored procedure to UDF crashes mysqld |
|
76 |
#------------------------------------------------------------------------ |
|
77 |
||
78 |
select metaphon('hello'); |
|
79 |
||
80 |
delimiter //; |
|
81 |
CREATE PROCEDURE `XXX1`(in testval varchar(10)) |
|
82 |
begin
|
|
83 |
select metaphon(testval); |
|
84 |
end// |
|
85 |
delimiter ;// |
|
86 |
||
87 |
call XXX1('hello'); |
|
88 |
drop procedure xxx1; |
|
89 |
||
90 |
delimiter //; |
|
91 |
CREATE PROCEDURE `XXX2`() |
|
92 |
begin
|
|
93 |
declare testval varchar(10); |
|
94 |
set testval = 'hello'; |
|
95 |
select metaphon(testval); |
|
96 |
end// |
|
97 |
delimiter ;// |
|
98 |
||
99 |
call XXX2(); |
|
100 |
drop procedure xxx2; |
|
101 |
||
102 |
#
|
|
103 |
# Bug#19904: UDF: not initialized *is_null per row |
|
104 |
#
|
|
105 |
||
106 |
CREATE TABLE bug19904(n INT, v varchar(10)); |
|
107 |
INSERT INTO bug19904 VALUES (1,'one'),(2,'two'),(NULL,NULL),(3,'three'),(4,'four'); |
|
108 |
SELECT myfunc_double(n) AS f FROM bug19904; |
|
109 |
SELECT metaphon(v) AS f FROM bug19904; |
|
110 |
DROP TABLE bug19904; |
|
111 |
||
112 |
#
|
|
113 |
# Bug#21269: DEFINER-clause is allowed for UDF-functions |
|
114 |
#
|
|
115 |
||
116 |
--error ER_PARSE_ERROR
|
|
117 |
CREATE DEFINER=CURRENT_USER() FUNCTION should_not_parse |
|
118 |
RETURNS STRING SONAME "should_not_parse.so"; |
|
119 |
||
120 |
--error ER_PARSE_ERROR
|
|
121 |
CREATE DEFINER=someone@somewhere FUNCTION should_not_parse |
|
122 |
RETURNS STRING SONAME "should_not_parse.so"; |
|
123 |
#
|
|
124 |
# Bug#19862: Sort with filesort by function evaluates function twice |
|
125 |
#
|
|
126 |
create table t1(f1 int); |
|
127 |
insert into t1 values(1),(2); |
|
128 |
explain select myfunc_int(f1) from t1 order by 1; |
|
129 |
drop table t1; |
|
130 |
||
131 |
#
|
|
132 |
# Bug #21809: Error 1356 while selecting from view with grouping though |
|
133 |
# underlying select OK. |
|
134 |
#
|
|
135 |
CREATE TABLE t1(a INT, b INT); INSERT INTO t1 values (1,1),(2,2); |
|
136 |
||
137 |
DELIMITER ||; |
|
138 |
CREATE FUNCTION fn(a int) RETURNS int DETERMINISTIC |
|
139 |
BEGIN
|
|
140 |
RETURN a; |
|
141 |
END
|
|
142 |
||
|
|
143 |
DELIMITER ;|| |
|
144 |
||
145 |
CREATE VIEW v1 AS SELECT a, fn(MIN(b)) as c FROM t1 GROUP BY a; |
|
146 |
||
147 |
SELECT myfunc_int(a AS attr_name) FROM t1; |
|
148 |
EXPLAIN EXTENDED SELECT myfunc_int(a AS attr_name) FROM t1; |
|
149 |
EXPLAIN EXTENDED SELECT myfunc_int(a) FROM t1; |
|
150 |
SELECT a,c FROM v1; |
|
151 |
||
152 |
--error ER_WRONG_PARAMETERS_TO_STORED_FCT
|
|
153 |
SELECT a, fn(MIN(b) xx) as c FROM t1 GROUP BY a; |
|
154 |
--error ER_WRONG_PARAMETERS_TO_STORED_FCT
|
|
155 |
SELECT myfunc_int(fn(MIN(b) xx)) as c FROM t1 GROUP BY a; |
|
156 |
--error ER_PARSE_ERROR
|
|
157 |
SELECT myfunc_int(test.fn(MIN(b) xx)) as c FROM t1 GROUP BY a; |
|
158 |
||
159 |
SELECT myfunc_int(fn(MIN(b)) xx) as c FROM t1 GROUP BY a; |
|
160 |
SELECT myfunc_int(test.fn(MIN(b)) xx) as c FROM t1 GROUP BY a; |
|
161 |
||
162 |
EXPLAIN EXTENDED SELECT myfunc_int(MIN(b) xx) as c FROM t1 GROUP BY a; |
|
163 |
EXPLAIN EXTENDED SELECT test.fn(MIN(b)) as c FROM t1 GROUP BY a; |
|
164 |
EXPLAIN EXTENDED SELECT myfunc_int(fn(MIN(b))) as c FROM t1 GROUP BY a; |
|
165 |
EXPLAIN EXTENDED SELECT myfunc_int(test.fn(MIN(b))) as c FROM t1 GROUP BY a; |
|
166 |
SELECT myfunc_int(MIN(b) xx) as c FROM t1 GROUP BY a; |
|
167 |
SELECT test.fn(MIN(b)) as c FROM t1 GROUP BY a; |
|
168 |
SELECT myfunc_int(fn(MIN(b))) as c FROM t1 GROUP BY a; |
|
169 |
SELECT myfunc_int(test.fn(MIN(b))) as c FROM t1 GROUP BY a; |
|
170 |
DROP VIEW v1; |
|
171 |
DROP TABLE t1; |
|
172 |
DROP FUNCTION fn; |
|
173 |
||
174 |
--echo End of 5.0 tests.
|
|
175 |
||
176 |
#
|
|
177 |
# Bug#24736: UDF functions parsed as Stored Functions |
|
178 |
#
|
|
179 |
||
180 |
select myfunc_double(3); |
|
181 |
select myfunc_double(3 AS three); |
|
182 |
select myfunc_double(abs(3)); |
|
183 |
select myfunc_double(abs(3) AS named_param); |
|
184 |
select abs(myfunc_double(3)); |
|
185 |
select abs(myfunc_double(3 AS three)); |
|
186 |
||
187 |
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
|
|
188 |
select myfunc_double(abs(3 AS wrong)); |
|
189 |
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
|
|
190 |
select abs(myfunc_double(3) AS wrong); |
|
191 |
||
192 |
#
|
|
193 |
# BUG#18239: Possible to overload internal functions with stored functions |
|
194 |
#
|
|
195 |
||
196 |
--disable_warnings
|
|
197 |
drop function if exists pi; |
|
198 |
--enable_warnings
|
|
199 |
||
200 |
--error ER_NATIVE_FCT_NAME_COLLISION
|
|
201 |
CREATE FUNCTION pi RETURNS STRING SONAME "should_not_parse.so"; |
|
202 |
||
203 |
# Verify that Stored Functions and UDF are mutually exclusive |
|
204 |
DROP FUNCTION IF EXISTS metaphon; |
|
205 |
||
206 |
CREATE FUNCTION metaphon(a int) RETURNS int |
|
207 |
return 0; |
|
208 |
||
209 |
# this currently passes, and eclipse the stored function |
|
210 |
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
|
|
211 |
eval CREATE FUNCTION metaphon RETURNS STRING SONAME "$UDF_EXAMPLE_LIB"; |
|
212 |
||
213 |
DROP FUNCTION metaphon; |
|
214 |
DROP FUNCTION metaphon; |
|
215 |
||
216 |
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
|
|
217 |
eval CREATE FUNCTION metaphon RETURNS STRING SONAME "$UDF_EXAMPLE_LIB"; |
|
218 |
||
219 |
--error ER_UDF_EXISTS
|
|
220 |
CREATE FUNCTION metaphon(a int) RETURNS int |
|
221 |
return 0; |
|
222 |
||
223 |
--error ER_UDF_EXISTS
|
|
224 |
CREATE FUNCTION test.metaphon(a int) RETURNS int |
|
225 |
return 0; |
|
226 |
||
227 |
# End of Bug#18239 |
|
228 |
||
229 |
#
|
|
230 |
# Drop the example functions from udf_example |
|
231 |
#
|
|
232 |
||
233 |
DROP FUNCTION metaphon; |
|
234 |
DROP FUNCTION myfunc_double; |
|
235 |
--error ER_SP_DOES_NOT_EXIST
|
|
236 |
DROP FUNCTION myfunc_nonexist; |
|
237 |
DROP FUNCTION myfunc_int; |
|
238 |
DROP FUNCTION sequence; |
|
239 |
DROP FUNCTION lookup; |
|
240 |
DROP FUNCTION reverse_lookup; |
|
241 |
DROP FUNCTION avgcost; |
|
242 |
||
243 |
#
|
|
244 |
# Bug #15439: UDF name case handling forces DELETE FROM mysql.func to remove |
|
245 |
# the UDF |
|
246 |
#
|
|
247 |
select * from mysql.func; |
|
248 |
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
|
|
249 |
eval CREATE FUNCTION is_const RETURNS STRING SONAME "$UDF_EXAMPLE_LIB"; |
|
250 |
||
251 |
select IS_const(3); |
|
252 |
||
253 |
drop function IS_const; |
|
254 |
||
255 |
select * from mysql.func; |
|
256 |
||
257 |
--error 1305
|
|
258 |
select is_const(3); |
|
259 |
||
260 |
#
|
|
261 |
# Bug#18761: constant expression as UDF parameters not passed in as constant |
|
262 |
#
|
|
263 |
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
|
|
264 |
eval CREATE FUNCTION is_const RETURNS STRING SONAME "$UDF_EXAMPLE_LIB"; |
|
265 |
||
266 |
select
|
|
267 |
is_const(3) as const, |
|
268 |
is_const(3.14) as const, |
|
269 |
is_const('fnord') as const, |
|
270 |
is_const(2+3) as const, |
|
271 |
is_const(rand()) as 'nc rand()', |
|
272 |
is_const(sin(3.14)) as const, |
|
273 |
is_const(upper('test')) as const; |
|
274 |
||
275 |
create table bug18761 (n int); |
|
276 |
insert into bug18761 values (null),(2); |
|
277 |
select
|
|
278 |
is_const(3) as const, |
|
279 |
is_const(3.14) as const, |
|
280 |
is_const('fnord') as const, |
|
281 |
is_const(2+3) as const, |
|
282 |
is_const(2+n) as 'nc 2+n ', |
|
283 |
is_const(sin(n)) as 'nc sin(n)', |
|
284 |
is_const(sin(3.14)) as const, |
|
285 |
is_const(upper('test')) as const, |
|
286 |
is_const(rand()) as 'nc rand()', |
|
287 |
is_const(n) as 'nc n ', |
|
288 |
is_const(is_const(n)) as 'nc ic?(n)', |
|
289 |
is_const(is_const('c')) as const |
|
290 |
from
|
|
291 |
bug18761; |
|
292 |
drop table bug18761; |
|
293 |
||
294 |
--error 1241
|
|
295 |
select is_const((1,2,3)); |
|
296 |
||
297 |
drop function if exists is_const; |
|
298 |
||
299 |
#
|
|
300 |
# Bug #25382: Passing NULL to an UDF called from stored procedures |
|
301 |
# crashes server |
|
302 |
#
|
|
303 |
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
|
|
304 |
eval CREATE FUNCTION metaphon RETURNS STRING SONAME "$UDF_EXAMPLE_LIB"; |
|
305 |
||
306 |
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
|
|
307 |
eval CREATE FUNCTION myfunc_double RETURNS REAL SONAME "$UDF_EXAMPLE_LIB"; |
|
308 |
||
309 |
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
|
|
310 |
eval CREATE FUNCTION myfunc_int RETURNS INTEGER SONAME "$UDF_EXAMPLE_LIB"; |
|
311 |
||
312 |
delimiter //; |
|
313 |
create function f1(p1 varchar(255)) |
|
314 |
returns varchar(255) |
|
315 |
begin
|
|
316 |
return metaphon(p1); |
|
317 |
end// |
|
318 |
||
319 |
create function f2(p1 varchar(255)) |
|
320 |
returns double |
|
321 |
begin
|
|
322 |
return myfunc_double(p1); |
|
323 |
end// |
|
324 |
||
325 |
create function f3(p1 varchar(255)) |
|
326 |
returns double |
|
327 |
begin
|
|
328 |
return myfunc_int(p1); |
|
329 |
end// |
|
330 |
||
331 |
delimiter ;// |
|
332 |
||
333 |
select f3(NULL); |
|
334 |
select f2(NULL); |
|
335 |
select f1(NULL); |
|
336 |
||
337 |
drop function f1; |
|
338 |
drop function f2; |
|
339 |
drop function f3; |
|
340 |
drop function metaphon; |
|
341 |
drop function myfunc_double; |
|
342 |
drop function myfunc_int; |
|
343 |
||
344 |
#
|
|
345 |
# Bug #28921: Queries containing UDF functions are cached |
|
346 |
#
|
|
347 |
||
348 |
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
|
|
349 |
eval CREATE FUNCTION metaphon RETURNS STRING SONAME "$UDF_EXAMPLE_LIB"; |
|
350 |
create table t1 (a char); |
|
351 |
||
352 |
set GLOBAL query_cache_size=1355776; |
|
353 |
reset query cache; |
|
354 |
||
355 |
select metaphon('MySQL') from t1; |
|
356 |
show status like "Qcache_hits"; |
|
357 |
show status like "Qcache_queries_in_cache"; |
|
358 |
||
359 |
select metaphon('MySQL') from t1; |
|
360 |
show status like "Qcache_hits"; |
|
361 |
show status like "Qcache_queries_in_cache"; |
|
362 |
||
363 |
drop table t1; |
|
364 |
drop function metaphon; |
|
365 |
set GLOBAL query_cache_size=default; |
|
366 |
||
367 |
#
|
|
368 |
# Bug#28318 CREATE FUNCTION (UDF) requires a schema |
|
369 |
#
|
|
370 |
||
371 |
--disable_warnings
|
|
372 |
DROP DATABASE IF EXISTS mysqltest; |
|
373 |
--enable_warnings
|
|
374 |
CREATE DATABASE mysqltest; |
|
375 |
USE mysqltest; |
|
376 |
DROP DATABASE mysqltest; |
|
377 |
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
|
|
378 |
eval CREATE FUNCTION metaphon RETURNS STRING SONAME "$UDF_EXAMPLE_LIB"; |
|
379 |
DROP FUNCTION metaphon; |
|
380 |
USE test; |
|
381 |
||
382 |
#
|
|
383 |
# Bug #29804 UDF parameters don't contain correct string length |
|
384 |
#
|
|
385 |
||
386 |
CREATE TABLE const_len_bug (
|
|
387 |
str_const varchar(4000),
|
|
388 |
result1 varchar(4000),
|
|
389 |
result2 varchar(4000)
|
|
390 |
);
|
|
391 |
||
392 |
DELIMITER |;
|
|
393 |
CREATE TRIGGER check_const_len_trigger BEFORE INSERT ON const_len_bug FOR EACH ROW BEGIN
|
|
394 |
set NEW.str_const = 'bar'; |
|
395 |
set NEW.result2 = check_const_len(NEW.str_const); |
|
396 |
END | |
|
397 |
||
398 |
CREATE PROCEDURE check_const_len_sp (IN str_const VARCHAR(4000)) |
|
399 |
BEGIN
|
|
400 |
DECLARE result VARCHAR(4000); |
|
401 |
SET result = check_const_len(str_const); |
|
402 |
insert into const_len_bug values(str_const, result, ""); |
|
403 |
END | |
|
404 |
DELIMITER ;| |
|
405 |
||
406 |
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
|
|
407 |
eval CREATE FUNCTION check_const_len RETURNS string SONAME "$UDF_EXAMPLE_LIB"; |
|
408 |
||
409 |
CALL check_const_len_sp("foo"); |
|
410 |
||
411 |
SELECT * from const_len_bug; |
|
412 |
||
413 |
DROP FUNCTION check_const_len; |
|
414 |
DROP PROCEDURE check_const_len_sp; |
|
415 |
DROP TRIGGER check_const_len_trigger; |
|
416 |
DROP TABLE const_len_bug; |
|
417 |
||
418 |
||
419 |
#
|
|
420 |
# Bug #30355: Incorrect ordering of UDF results |
|
421 |
#
|
|
422 |
||
423 |
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
|
|
424 |
eval CREATE FUNCTION sequence RETURNS INTEGER SONAME "$UDF_EXAMPLE_LIB"; |
|
425 |
CREATE TABLE t1 (a INT); |
|
426 |
CREATE TABLE t2 (a INT PRIMARY KEY); |
|
427 |
INSERT INTO t1 VALUES (4),(3),(2),(1); |
|
428 |
INSERT INTO t2 SELECT * FROM t1; |
|
429 |
||
430 |
SELECT sequence() AS seq, a FROM t1 ORDER BY seq ASC; |
|
431 |
SELECT sequence() AS seq, a FROM t1 ORDER BY seq DESC; |
|
432 |
||
433 |
SELECT * FROM t1 WHERE a = sequence(); |
|
434 |
SELECT * FROM t2 WHERE a = sequence(); |
|
435 |
||
436 |
DROP FUNCTION sequence; |
|
437 |
DROP TABLE t1,t2; |
|
438 |
||
439 |
--echo End of 5.0 tests.
|