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
-- 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);
193
# BUG#18239: Possible to overload internal functions with stored functions
197
drop function if exists pi;
200
--error ER_NATIVE_FCT_NAME_COLLISION
201
CREATE FUNCTION pi RETURNS STRING SONAME "should_not_parse.so";
203
# Verify that Stored Functions and UDF are mutually exclusive
204
DROP FUNCTION IF EXISTS metaphon;
206
CREATE FUNCTION metaphon(a int) RETURNS int
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";
213
DROP FUNCTION metaphon;
214
DROP FUNCTION metaphon;
216
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
217
eval CREATE FUNCTION metaphon RETURNS STRING SONAME "$UDF_EXAMPLE_LIB";
219
--error ER_UDF_EXISTS
220
CREATE FUNCTION metaphon(a int) RETURNS int
223
--error ER_UDF_EXISTS
224
CREATE FUNCTION test.metaphon(a int) RETURNS int
230
# Drop the example functions from udf_example
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;
244
# Bug #15439: UDF name case handling forces DELETE FROM mysql.func to remove
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";
253
drop function IS_const;
255
select * from mysql.func;
261
# Bug#18761: constant expression as UDF parameters not passed in as constant
263
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
264
eval CREATE FUNCTION is_const RETURNS STRING SONAME "$UDF_EXAMPLE_LIB";
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;
275
create table bug18761 (n int);
276
insert into bug18761 values (null),(2);
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
295
select is_const((1,2,3));
297
drop function if exists is_const;
300
# Bug #25382: Passing NULL to an UDF called from stored procedures
303
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
304
eval CREATE FUNCTION metaphon RETURNS STRING SONAME "$UDF_EXAMPLE_LIB";
306
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
307
eval CREATE FUNCTION myfunc_double RETURNS REAL SONAME "$UDF_EXAMPLE_LIB";
309
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
310
eval CREATE FUNCTION myfunc_int RETURNS INTEGER SONAME "$UDF_EXAMPLE_LIB";
313
create function f1(p1 varchar(255))
319
create function f2(p1 varchar(255))
322
return myfunc_double(p1);
325
create function f3(p1 varchar(255))
328
return myfunc_int(p1);
340
drop function metaphon;
341
drop function myfunc_double;
342
drop function myfunc_int;
345
# Bug #28921: Queries containing UDF functions are cached
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);
352
set GLOBAL query_cache_size=1355776;
355
select metaphon('MySQL') from t1;
356
show status like "Qcache_hits";
357
show status like "Qcache_queries_in_cache";
359
select metaphon('MySQL') from t1;
360
show status like "Qcache_hits";
361
show status like "Qcache_queries_in_cache";
364
drop function metaphon;
365
set GLOBAL query_cache_size=default;
368
# Bug#28318 CREATE FUNCTION (UDF) requires a schema
372
DROP DATABASE IF EXISTS mysqltest;
374
CREATE DATABASE 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;
383
# Bug #29804 UDF parameters don't contain correct string length
386
CREATE TABLE const_len_bug (
387
str_const varchar(4000),
388
result1 varchar(4000),
389
result2 varchar(4000)
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);
398
CREATE PROCEDURE check_const_len_sp (IN str_const VARCHAR(4000))
400
DECLARE result VARCHAR(4000);
401
SET result = check_const_len(str_const);
402
insert into const_len_bug values(str_const, result, "");
406
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
407
eval CREATE FUNCTION check_const_len RETURNS string SONAME "$UDF_EXAMPLE_LIB";
409
CALL check_const_len_sp("foo");
411
SELECT * from const_len_bug;
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;
420
# Bug #30355: Incorrect ordering of UDF results
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;
430
SELECT sequence() AS seq, a FROM t1 ORDER BY seq ASC;
431
SELECT sequence() AS seq, a FROM t1 ORDER BY seq DESC;
433
SELECT * FROM t1 WHERE a = sequence();
434
SELECT * FROM t2 WHERE a = sequence();
436
DROP FUNCTION sequence;
439
--echo End of 5.0 tests.