udf.test 4.84 KB
Newer Older
1 2
--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
5 6 7 8 9 10 11 12
# the library are.
#


--disable_warnings
drop table if exists t1;
--enable_warnings

13
#
14 15 16
# Create the example functions from udf_example
#

17 18 19 20
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
eval CREATE FUNCTION metaphon RETURNS STRING SONAME "$UDF_EXAMPLE_LIB";
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
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 25 26 27 28 29 30 31 32 33 34 35 36
eval CREATE FUNCTION myfunc_nonexist RETURNS INTEGER SONAME "$UDF_EXAMPLE_LIB";
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
eval CREATE FUNCTION myfunc_int RETURNS INTEGER SONAME "$UDF_EXAMPLE_LIB";
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
eval CREATE FUNCTION sequence RETURNS INTEGER SONAME "$UDF_EXAMPLE_LIB";
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
eval CREATE FUNCTION lookup RETURNS STRING SONAME "$UDF_EXAMPLE_LIB";
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
eval CREATE FUNCTION reverse_lookup
        RETURNS STRING SONAME "$UDF_EXAMPLE_LIB";
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
eval CREATE AGGREGATE FUNCTION avgcost
        RETURNS REAL SONAME "$UDF_EXAMPLE_LIB";
37

38
--error 0
39 40 41
select myfunc_double();
select myfunc_double(1);
select myfunc_double(78654);
42
--error 1305
unknown's avatar
unknown committed
43
select myfunc_nonexist();
44
select myfunc_int();
45
--error 0
46 47
select lookup();
select lookup("127.0.0.1");
48
--error 0
49 50
select lookup(127,0,0,1);
select lookup("localhost");
51
--error 0
52
select reverse_lookup();
unknown's avatar
unknown committed
53 54 55 56

# These two functions should return "localhost", but it's
# depending on configuration, so just call them and don't log the result
--disable_result_log
57 58
select reverse_lookup("127.0.0.1");
select reverse_lookup(127,0,0,1);
unknown's avatar
unknown committed
59 60
--enable_result_log

61
select reverse_lookup("localhost");
62
--error 0
63
select avgcost();
64
--error 0
65 66 67 68 69 70 71 72 73 74 75 76 77
select avgcost(100,23.76);
create table t1(sum int, price float(24));
insert into t1 values(100, 50.00), (100, 100.00);
select avgcost(sum, price) from t1;
delete from t1;
insert into t1 values(100, 54.33), (200, 199.99);
select avgcost(sum, price) from t1;
drop table t1;

#------------------------------------------------------------------------
# BUG#17261 Passing a variable from a stored procedure to UDF crashes mysqld
#------------------------------------------------------------------------

78
select metaphon('hello');
79 80 81

delimiter //;
CREATE PROCEDURE `XXX1`(in testval varchar(10))
82 83
begin
select metaphon(testval);
84 85 86 87 88 89 90 91
end//
delimiter ;//

call XXX1('hello');
drop procedure xxx1;

delimiter //;
CREATE PROCEDURE `XXX2`()
92
begin
93 94
declare testval varchar(10);
set testval = 'hello';
95
select metaphon(testval);
96 97 98 99 100 101
end//
delimiter ;//

call XXX2();
drop procedure xxx2;

102 103 104 105 106 107 108 109 110 111
#
# Bug#19904: UDF: not initialized *is_null per row
#

CREATE TABLE bug19904(n INT, v varchar(10));
INSERT INTO bug19904 VALUES (1,'one'),(2,'two'),(NULL,NULL),(3,'three'),(4,'four');
SELECT myfunc_double(n) AS f FROM bug19904;
SELECT metaphon(v) AS f FROM bug19904;
DROP TABLE bug19904;

112 113 114 115 116 117 118 119 120 121 122
#
# Bug#21269: DEFINER-clause is allowed for UDF-functions
#

--error ER_WRONG_USAGE
CREATE DEFINER=CURRENT_USER() FUNCTION should_not_parse
RETURNS STRING SONAME "should_not_parse.so";

--error ER_WRONG_USAGE
CREATE DEFINER=someone@somewhere FUNCTION should_not_parse
RETURNS STRING SONAME "should_not_parse.so";
123 124 125 126 127 128 129
#
# Bug#19862: Sort with filesort by function evaluates function twice
#
create table t1(f1 int);
insert into t1 values(1),(2);
explain select myfunc_int(f1) from t1 order by 1;
drop table t1;
130
--echo End of 5.0 tests.
131

132
#
133 134 135 136 137
# Drop the example functions from udf_example
#

DROP FUNCTION metaphon;
DROP FUNCTION myfunc_double;
138
--error ER_SP_DOES_NOT_EXIST
unknown's avatar
unknown committed
139
DROP FUNCTION myfunc_nonexist;
140 141 142 143 144 145
DROP FUNCTION myfunc_int;
DROP FUNCTION sequence;
DROP FUNCTION lookup;
DROP FUNCTION reverse_lookup;
DROP FUNCTION avgcost;

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
#
# Bug#18761: constant expression as UDF parameters not passed in as constant
#
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
eval CREATE FUNCTION is_const RETURNS STRING SONAME "$UDF_EXAMPLE_LIB";

select
  is_const(3) as const,
  is_const(3.14) as const,
  is_const('fnord') as const,
  is_const(2+3) as const,
  is_const(rand()) as 'nc rand()',
  is_const(sin(3.14)) as const,
  is_const(upper('test')) as const;

create table bug18761 (n int);
insert into bug18761 values (null),(2);
select
  is_const(3) as const,
  is_const(3.14) as const,
  is_const('fnord') as const,
  is_const(2+3) as const,
  is_const(2+n) as 'nc  2+n  ',
  is_const(sin(n)) as 'nc sin(n)',
  is_const(sin(3.14)) as const,
  is_const(upper('test')) as const,
  is_const(rand()) as 'nc rand()',
  is_const(n) as 'nc   n   ',
  is_const(is_const(n)) as 'nc ic?(n)',
  is_const(is_const('c')) as const
from
  bug18761;
drop table bug18761;

--error 1241
select is_const((1,2,3));

drop function if exists is_const;