1
--source include/have_udf.inc
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
10
drop table if exists t1;
14
# Create the example functions from udf_example
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";
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";
38
--error ER_CANT_INITIALIZE_UDF
39
select myfunc_double();
40
select myfunc_double(1);
41
select myfunc_double(78654);
43
select myfunc_nonexist();
45
--error ER_CANT_INITIALIZE_UDF
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();
54
# These two functions should return "localhost", but it's
55
# depending on configuration, so just call them and don't log the result
57
select reverse_lookup("127.0.0.1");
58
select reverse_lookup(127,0,0,1);
61
select reverse_lookup("localhost");
62
--error ER_CANT_INITIALIZE_UDF
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;
70
insert into t1 values(100, 54.33), (200, 199.99);
71
select avgcost(sum, price) from t1;
74
#------------------------------------------------------------------------
75
# BUG#17261 Passing a variable from a stored procedure to UDF crashes mysqld
76
#------------------------------------------------------------------------
78
select metaphon('hello');
81
CREATE PROCEDURE `XXX1`(in testval varchar(10))
83
select metaphon(testval);
91
CREATE PROCEDURE `XXX2`()
93
declare testval varchar(10);
94
set testval = 'hello';
95
select metaphon(testval);
103
# Bug#19904: UDF: not initialized *is_null per row
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;
113
# Bug#21269: DEFINER-clause is allowed for UDF-functions
116
--error ER_PARSE_ERROR
117
CREATE DEFINER=CURRENT_USER() FUNCTION should_not_parse
118
RETURNS STRING SONAME "should_not_parse.so";
120
--error ER_PARSE_ERROR
121
CREATE DEFINER=someone@somewhere FUNCTION should_not_parse
122
RETURNS STRING SONAME "should_not_parse.so";
124
# Bug#19862: Sort with filesort by function evaluates function twice
126
create table t1(f1 int);
127
insert into t1 values(1),(2);
128
explain select myfunc_int(f1) from t1 order by 1;
132
# Bug #21809: Error 1356 while selecting from view with grouping though
133
# underlying select OK.
135
CREATE TABLE t1(a INT, b INT); INSERT INTO t1 values (1,1),(2,2);
138
CREATE FUNCTION fn(a int) RETURNS int DETERMINISTIC
145
CREATE VIEW v1 AS SELECT a, fn(MIN(b)) as c FROM t1 GROUP BY a;
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;
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;
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;
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;
174
--echo End of 5.0 tests.
177
# Bug#24736: UDF functions parsed as Stored Functions
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));
187
select myfunc_double(abs(3 AS wrong));
188
select abs(myfunc_double(3) AS wrong);
191
# BUG#18239: Possible to overload internal functions with stored functions
195
drop function if exists pi;
198
--error ER_NATIVE_FCT_NAME_COLLISION
199
CREATE FUNCTION pi RETURNS STRING SONAME "should_not_parse.so";
201
# Verify that Stored Functions and UDF are mutually exclusive
202
DROP FUNCTION IF EXISTS metaphon;
204
CREATE FUNCTION metaphon(a int) RETURNS int
207
# this currently passes, and eclipse the stored function
208
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
209
eval CREATE FUNCTION metaphon RETURNS STRING SONAME "$UDF_EXAMPLE_LIB";
211
DROP FUNCTION metaphon;
212
DROP FUNCTION metaphon;
214
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
215
eval CREATE FUNCTION metaphon RETURNS STRING SONAME "$UDF_EXAMPLE_LIB";
217
--error ER_UDF_EXISTS
218
CREATE FUNCTION metaphon(a int) RETURNS int
221
--error ER_UDF_EXISTS
222
CREATE FUNCTION test.metaphon(a int) RETURNS int
228
# Drop the example functions from udf_example
231
DROP FUNCTION metaphon;
232
DROP FUNCTION myfunc_double;
233
--error ER_SP_DOES_NOT_EXIST
234
DROP FUNCTION myfunc_nonexist;
235
DROP FUNCTION myfunc_int;
236
DROP FUNCTION sequence;
237
DROP FUNCTION lookup;
238
DROP FUNCTION reverse_lookup;
239
DROP FUNCTION avgcost;
242
# Bug #15439: UDF name case handling forces DELETE FROM mysql.func to remove
245
select * from mysql.func;
246
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
247
eval CREATE FUNCTION is_const RETURNS STRING SONAME "$UDF_EXAMPLE_LIB";
251
drop function IS_const;
253
select * from mysql.func;
259
# Bug#18761: constant expression as UDF parameters not passed in as constant
261
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
262
eval CREATE FUNCTION is_const RETURNS STRING SONAME "$UDF_EXAMPLE_LIB";
265
is_const(3) as const,
266
is_const(3.14) as const,
267
is_const('fnord') as const,
268
is_const(2+3) as const,
269
is_const(rand()) as 'nc rand()',
270
is_const(sin(3.14)) as const,
271
is_const(upper('test')) as const;
273
create table bug18761 (n int);
274
insert into bug18761 values (null),(2);
276
is_const(3) as const,
277
is_const(3.14) as const,
278
is_const('fnord') as const,
279
is_const(2+3) as const,
280
is_const(2+n) as 'nc 2+n ',
281
is_const(sin(n)) as 'nc sin(n)',
282
is_const(sin(3.14)) as const,
283
is_const(upper('test')) as const,
284
is_const(rand()) as 'nc rand()',
285
is_const(n) as 'nc n ',
286
is_const(is_const(n)) as 'nc ic?(n)',
287
is_const(is_const('c')) as const
293
select is_const((1,2,3));
295
drop function if exists is_const;
298
# Bug #25382: Passing NULL to an UDF called from stored procedures
301
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
302
eval CREATE FUNCTION metaphon RETURNS STRING SONAME "$UDF_EXAMPLE_LIB";
304
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
305
eval CREATE FUNCTION myfunc_double RETURNS REAL SONAME "$UDF_EXAMPLE_LIB";
307
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
308
eval CREATE FUNCTION myfunc_int RETURNS INTEGER SONAME "$UDF_EXAMPLE_LIB";
311
create function f1(p1 varchar(255))
317
create function f2(p1 varchar(255))
320
return myfunc_double(p1);
323
create function f3(p1 varchar(255))
326
return myfunc_int(p1);
338
drop function metaphon;
339
drop function myfunc_double;
340
drop function myfunc_int;
343
# Bug #28921: Queries containing UDF functions are cached
346
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
347
eval CREATE FUNCTION metaphon RETURNS STRING SONAME "$UDF_EXAMPLE_LIB";
348
create table t1 (a char);
350
set GLOBAL query_cache_size=1355776;
353
select metaphon('MySQL') from t1;
354
show status like "Qcache_hits";
355
show status like "Qcache_queries_in_cache";
357
select metaphon('MySQL') from t1;
358
show status like "Qcache_hits";
359
show status like "Qcache_queries_in_cache";
362
drop function metaphon;
363
set GLOBAL query_cache_size=default;
366
# Bug#28318 CREATE FUNCTION (UDF) requires a schema
370
DROP DATABASE IF EXISTS mysqltest;
372
CREATE DATABASE mysqltest;
374
DROP DATABASE mysqltest;
375
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
376
eval CREATE FUNCTION metaphon RETURNS STRING SONAME "$UDF_EXAMPLE_LIB";
377
DROP FUNCTION metaphon;
381
# Bug #29804 UDF parameters don't contain correct string length
384
CREATE TABLE const_len_bug (
385
str_const varchar(4000),
386
result1 varchar(4000),
387
result2 varchar(4000)
391
CREATE TRIGGER check_const_len_trigger BEFORE INSERT ON const_len_bug FOR EACH ROW BEGIN
392
set NEW.str_const = 'bar';
393
set NEW.result2 = check_const_len(NEW.str_const);
396
CREATE PROCEDURE check_const_len_sp (IN str_const VARCHAR(4000))
398
DECLARE result VARCHAR(4000);
399
SET result = check_const_len(str_const);
400
insert into const_len_bug values(str_const, result, "");
404
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
405
eval CREATE FUNCTION check_const_len RETURNS string SONAME "$UDF_EXAMPLE_LIB";
407
CALL check_const_len_sp("foo");
409
SELECT * from const_len_bug;
411
DROP FUNCTION check_const_len;
412
DROP PROCEDURE check_const_len_sp;
413
DROP TRIGGER check_const_len_trigger;
414
DROP TABLE const_len_bug;
418
# Bug #30355: Incorrect ordering of UDF results
421
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
422
eval CREATE FUNCTION sequence RETURNS INTEGER SONAME "$UDF_EXAMPLE_LIB";
423
CREATE TABLE t1 (a INT);
424
CREATE TABLE t2 (a INT PRIMARY KEY);
425
INSERT INTO t1 VALUES (4),(3),(2),(1);
426
INSERT INTO t2 SELECT * FROM t1;
428
SELECT sequence() AS seq, a FROM t1 ORDER BY seq ASC;
429
SELECT sequence() AS seq, a FROM t1 ORDER BY seq DESC;
431
SELECT * FROM t1 WHERE a = sequence();
432
SELECT * FROM t2 WHERE a = sequence();
434
DROP FUNCTION sequence;
437
--echo End of 5.0 tests.