mirror of
https://github.com/MariaDB/server.git
synced 2025-01-17 20:42:30 +01:00
9a63adc8fd
When there is no index defined filesort is used to sort the result of a query. If there is a function in the select list and the result set should be ordered by it's value then this function will be evaluated twice. First time to get the value of the sort key and second time to send its value to a user. This happens because filesort when sorts a table remembers only values of its fields but not values of functions. All functions are affected. But taking into account that SP and UDF functions can be both expensive and non-deterministic a temporary table should be used to store their results and then sort it to avoid twice SP evaluation and to get a correct result. If an expression referenced in an ORDER clause contains a SP or UDF function, force the use of a temporary table. A new Item_processor function called func_type_checker_processor is added to check whether the expression contains a function of a particular type. mysql-test/t/udf.test: Added test case for bug#19862: Sort with filesort by function evaluates function twice mysql-test/t/sp.test: Added test case for bug#19862: Sort with filesort by function evaluates function twice mysql-test/r/sp.result: Added test case for bug#19862: Sort with filesort by function evaluates function twice mysql-test/r/udf.result: Added test case for bug#19862: Sort with filesort by function evaluates function twice sql/sql_select.cc: Fixed bug#19862: Sort with filesort by function evaluates function twice If an expression referenced in an ORDER clause contains a SP or UDF function, force the use of a temporary table. sql/item_func.h: Fixed bug#19862: Sort with filesort by function evaluates function twice A new Item_processor function called func_type_checker_processor is added to check whether the expression contains a function of a particular type. sql/item.h: Fixed bug#19862: Sort with filesort by function evaluates function twice A new Item_processor function called func_type_checker_processor is added to check whether the expression contains a function of a particular type. sql/item_func.cc: Fixed bug#19862: Sort with filesort by function evaluates function twice A new Item_processor function called func_type_checker_processor is added to check whether the expression contains a function of a particular type.
135 lines
3.6 KiB
Text
135 lines
3.6 KiB
Text
--source include/have_udf.inc
|
|
#
|
|
# To run this tests the "sql/udf_example.cc" need to be compiled into
|
|
# udf_example.so and LD_LIBRARY_PATH should be setup to point out where
|
|
# the library are.
|
|
#
|
|
|
|
|
|
--disable_warnings
|
|
drop table if exists t1;
|
|
--enable_warnings
|
|
|
|
#
|
|
# Create the example functions from udf_example
|
|
#
|
|
|
|
--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";
|
|
|
|
--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB
|
|
--error ER_CANT_FIND_DL_ENTRY
|
|
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";
|
|
|
|
--error 0
|
|
select myfunc_double();
|
|
select myfunc_double(1);
|
|
select myfunc_double(78654);
|
|
--error 1305
|
|
select myfunc_nonexist();
|
|
select myfunc_int();
|
|
--error 0
|
|
select lookup();
|
|
select lookup("127.0.0.1");
|
|
--error 0
|
|
select lookup(127,0,0,1);
|
|
select lookup("localhost");
|
|
--error 0
|
|
select reverse_lookup();
|
|
|
|
# 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
|
|
select reverse_lookup("127.0.0.1");
|
|
select reverse_lookup(127,0,0,1);
|
|
--enable_result_log
|
|
|
|
select reverse_lookup("localhost");
|
|
--error 0
|
|
select avgcost();
|
|
--error 0
|
|
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
|
|
#------------------------------------------------------------------------
|
|
|
|
select metaphon('hello');
|
|
|
|
delimiter //;
|
|
CREATE PROCEDURE `XXX1`(in testval varchar(10))
|
|
begin
|
|
select metaphon(testval);
|
|
end//
|
|
delimiter ;//
|
|
|
|
call XXX1('hello');
|
|
drop procedure xxx1;
|
|
|
|
delimiter //;
|
|
CREATE PROCEDURE `XXX2`()
|
|
begin
|
|
declare testval varchar(10);
|
|
set testval = 'hello';
|
|
select metaphon(testval);
|
|
end//
|
|
delimiter ;//
|
|
|
|
call XXX2();
|
|
drop procedure xxx2;
|
|
|
|
#
|
|
# 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;
|
|
|
|
#
|
|
# 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;
|
|
--echo End of 5.0 tests.
|
|
|
|
#
|
|
# Drop the example functions from udf_example
|
|
#
|
|
|
|
DROP FUNCTION metaphon;
|
|
DROP FUNCTION myfunc_double;
|
|
--error ER_SP_DOES_NOT_EXIST
|
|
DROP FUNCTION myfunc_nonexist;
|
|
DROP FUNCTION myfunc_int;
|
|
DROP FUNCTION sequence;
|
|
DROP FUNCTION lookup;
|
|
DROP FUNCTION reverse_lookup;
|
|
DROP FUNCTION avgcost;
|
|
|
|
|