diff --git a/mysql-test/main/func_math.result b/mysql-test/main/func_math.result index 475485cbcae59..6364662b19b75 100644 --- a/mysql-test/main/func_math.result +++ b/mysql-test/main/func_math.result @@ -45,9 +45,9 @@ select log(exp(10)),exp(log(sqrt(10))*2),log(-1),log(NULL),log(1,1),log(3,9),log log(exp(10)) exp(log(sqrt(10))*2) log(-1) log(NULL) log(1,1) log(3,9) log(-1,2) log(NULL,2) 10 10.000000000000002 NULL NULL NULL 2 NULL NULL Warnings: -Warning 1365 Division by 0 -Warning 1365 Division by 0 -Warning 1365 Division by 0 +Warning 4256 Passing non-positive argument to log() makes the function's result undefined +Warning 4257 Passing non-positive or equal-to-1 base to log() makes the function's result undefined +Warning 4257 Passing non-positive or equal-to-1 base to log() makes the function's result undefined explain extended select log(exp(10)),exp(log(sqrt(10))*2),log(-1),log(NULL),log(1,1),log(3,9),log(-1,2),log(NULL,2); id select_type table type possible_keys key key_len ref rows filtered Extra 1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL No tables used @@ -57,8 +57,8 @@ select ln(exp(10)),exp(ln(sqrt(10))*2),ln(-1),ln(0),ln(NULL); ln(exp(10)) exp(ln(sqrt(10))*2) ln(-1) ln(0) ln(NULL) 10 10.000000000000002 NULL NULL NULL Warnings: -Warning 1365 Division by 0 -Warning 1365 Division by 0 +Warning 4255 Passing non-positive argument to ln() makes the function's result undefined +Warning 4255 Passing non-positive argument to ln() makes the function's result undefined explain extended select ln(exp(10)),exp(ln(sqrt(10))*2),ln(-1),ln(0),ln(NULL); id select_type table type possible_keys key key_len ref rows filtered Extra 1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL No tables used @@ -68,8 +68,8 @@ select log2(8),log2(15),log2(-2),log2(0),log2(NULL); log2(8) log2(15) log2(-2) log2(0) log2(NULL) 3 3.9068905956085187 NULL NULL NULL Warnings: -Warning 1365 Division by 0 -Warning 1365 Division by 0 +Warning 4258 Passing non-positive argument to log2() makes the function's result undefined +Warning 4258 Passing non-positive argument to log2() makes the function's result undefined explain extended select log2(8),log2(15),log2(-2),log2(0),log2(NULL); id select_type table type possible_keys key key_len ref rows filtered Extra 1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL No tables used @@ -79,8 +79,8 @@ select log10(100),log10(18),log10(-4),log10(0),log10(NULL); log10(100) log10(18) log10(-4) log10(0) log10(NULL) 2 1.255272505103306 NULL NULL NULL Warnings: -Warning 1365 Division by 0 -Warning 1365 Division by 0 +Warning 4259 Passing non-positive argument to log10() makes the function's result undefined +Warning 4259 Passing non-positive argument to log10() makes the function's result undefined explain extended select log10(100),log10(18),log10(-4),log10(0),log10(NULL); id select_type table type possible_keys key key_len ref rows filtered Extra 1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL No tables used @@ -239,27 +239,27 @@ select ln(-1); ln(-1) NULL Warnings: -Warning 1365 Division by 0 +Warning 4255 Passing non-positive argument to ln() makes the function's result undefined select log10(-1); log10(-1) NULL Warnings: -Warning 1365 Division by 0 +Warning 4259 Passing non-positive argument to log10() makes the function's result undefined select log2(-1); log2(-1) NULL Warnings: -Warning 1365 Division by 0 +Warning 4258 Passing non-positive argument to log2() makes the function's result undefined select log(2,-1); log(2,-1) NULL Warnings: -Warning 1365 Division by 0 +Warning 4256 Passing non-positive argument to log() makes the function's result undefined select log(-2,1); log(-2,1) NULL Warnings: -Warning 1365 Division by 0 +Warning 4257 Passing non-positive or equal-to-1 base to log() makes the function's result undefined set sql_mode=''; select round(111,-10); round(111,-10) diff --git a/mysql-test/main/func_math_div_zero.result b/mysql-test/main/func_math_div_zero.result new file mode 100644 index 0000000000000..12588dea85218 --- /dev/null +++ b/mysql-test/main/func_math_div_zero.result @@ -0,0 +1,238 @@ +# +# MDEV-37939: Use correct error for invalid logarithm arguments +# +# LOG/LN/LOG2/LOG10 now emit function-specific domain warnings +# instead of ER_DIVISION_BY_ZERO, since these functions do not +# perform division. +# +# +# Test valid inputs first (before any warning-producing statements) +# This ensures SHOW WARNINGS is truly empty +# +SET SESSION sql_mode='ERROR_FOR_DIVISION_BY_ZERO'; +SELECT LOG(10); +LOG(10) +2.302585092994046 +SELECT LN(2.718); +LN(2.718) +0.999896315728952 +SELECT LOG2(8); +LOG2(8) +3 +SELECT LOG10(100); +LOG10(100) +2 +SELECT LOG(2, 8); +LOG(2, 8) +3 +SHOW WARNINGS; +Level Code Message +# Should be empty - valid inputs produce no warnings +# +# Test sql_mode='' behavior (before any warnings are produced) +# +SET SESSION sql_mode=''; +SELECT LOG(-1); +LOG(-1) +NULL +SHOW WARNINGS; +Level Code Message +# Should be empty - no warning when sql_mode doesn't include ERROR_FOR_DIVISION_BY_ZERO +SELECT LN(-1); +LN(-1) +NULL +SHOW WARNINGS; +Level Code Message +# +# Now test warning-producing cases +# +SET SESSION sql_mode='ERROR_FOR_DIVISION_BY_ZERO'; +# +# LOG family should report function-specific domain warnings +# +SELECT LOG(-1); +LOG(-1) +NULL +Warnings: +Warning 4256 Passing non-positive argument to log() makes the function's result undefined +SHOW WARNINGS; +Level Code Message +Warning 4256 Passing non-positive argument to log() makes the function's result undefined +SELECT LOG(0); +LOG(0) +NULL +Warnings: +Warning 4256 Passing non-positive argument to log() makes the function's result undefined +SHOW WARNINGS; +Level Code Message +Warning 4256 Passing non-positive argument to log() makes the function's result undefined +SELECT LN(0); +LN(0) +NULL +Warnings: +Warning 4255 Passing non-positive argument to ln() makes the function's result undefined +SHOW WARNINGS; +Level Code Message +Warning 4255 Passing non-positive argument to ln() makes the function's result undefined +SELECT LN(-5); +LN(-5) +NULL +Warnings: +Warning 4255 Passing non-positive argument to ln() makes the function's result undefined +SHOW WARNINGS; +Level Code Message +Warning 4255 Passing non-positive argument to ln() makes the function's result undefined +SELECT LOG2(-1); +LOG2(-1) +NULL +Warnings: +Warning 4258 Passing non-positive argument to log2() makes the function's result undefined +SHOW WARNINGS; +Level Code Message +Warning 4258 Passing non-positive argument to log2() makes the function's result undefined +SELECT LOG10(-1); +LOG10(-1) +NULL +Warnings: +Warning 4259 Passing non-positive argument to log10() makes the function's result undefined +SHOW WARNINGS; +Level Code Message +Warning 4259 Passing non-positive argument to log10() makes the function's result undefined +# Two-argument LOG: invalid second argument +SELECT LOG(2, -1); +LOG(2, -1) +NULL +Warnings: +Warning 4256 Passing non-positive argument to log() makes the function's result undefined +SHOW WARNINGS; +Level Code Message +Warning 4256 Passing non-positive argument to log() makes the function's result undefined +# Two-argument LOG: invalid base (negative) +SELECT LOG(-2, 10); +LOG(-2, 10) +NULL +Warnings: +Warning 4257 Passing non-positive or equal-to-1 base to log() makes the function's result undefined +SHOW WARNINGS; +Level Code Message +Warning 4257 Passing non-positive or equal-to-1 base to log() makes the function's result undefined +# Two-argument LOG: base = 1 (invalid - would cause log(1)=0 in denominator) +SELECT LOG(1, 10); +LOG(1, 10) +NULL +Warnings: +Warning 4257 Passing non-positive or equal-to-1 base to log() makes the function's result undefined +SHOW WARNINGS; +Level Code Message +Warning 4257 Passing non-positive or equal-to-1 base to log() makes the function's result undefined +# +# Division operations should report ER_DIVISION_BY_ZERO (1365) +# +# Division - integer +SELECT 1/0; +1/0 +NULL +Warnings: +Warning 1365 Division by 0 +SHOW WARNINGS; +Level Code Message +Warning 1365 Division by 0 +# Division - real/float +SELECT 1.0/0.0; +1.0/0.0 +NULL +Warnings: +Warning 1365 Division by 0 +SHOW WARNINGS; +Level Code Message +Warning 1365 Division by 0 +# Division - decimal +SELECT CAST(1 AS DECIMAL(10,2)) / CAST(0 AS DECIMAL(10,2)); +CAST(1 AS DECIMAL(10,2)) / CAST(0 AS DECIMAL(10,2)) +NULL +Warnings: +Warning 1365 Division by 0 +SHOW WARNINGS; +Level Code Message +Warning 1365 Division by 0 +# Division - with string arguments +SELECT '1'/'0'; +'1'/'0' +NULL +Warnings: +Warning 1365 Division by 0 +SHOW WARNINGS; +Level Code Message +Warning 1365 Division by 0 +# Integer DIV +SELECT 5 DIV 0; +5 DIV 0 +NULL +Warnings: +Warning 1365 Division by 0 +SHOW WARNINGS; +Level Code Message +Warning 1365 Division by 0 +# Integer DIV - with string arguments +SELECT '5' DIV '0'; +'5' DIV '0' +NULL +Warnings: +Warning 1365 Division by 0 +SHOW WARNINGS; +Level Code Message +Warning 1365 Division by 0 +# MOD - integer +SELECT 5 MOD 0; +5 MOD 0 +NULL +Warnings: +Warning 1365 Division by 0 +SHOW WARNINGS; +Level Code Message +Warning 1365 Division by 0 +# MOD - real/float +SELECT 5.5e0 MOD 0.0e0; +5.5e0 MOD 0.0e0 +NULL +Warnings: +Warning 1365 Division by 0 +SHOW WARNINGS; +Level Code Message +Warning 1365 Division by 0 +# MOD - decimal +SELECT CAST(5 AS DECIMAL(10,2)) MOD CAST(0 AS DECIMAL(10,2)); +CAST(5 AS DECIMAL(10,2)) MOD CAST(0 AS DECIMAL(10,2)) +NULL +Warnings: +Warning 1365 Division by 0 +SHOW WARNINGS; +Level Code Message +Warning 1365 Division by 0 +# MOD - with string arguments +SELECT '5' MOD '0'; +'5' MOD '0' +NULL +Warnings: +Warning 1365 Division by 0 +SHOW WARNINGS; +Level Code Message +Warning 1365 Division by 0 +# MOD function syntax +SELECT MOD(5, 0); +MOD(5, 0) +NULL +Warnings: +Warning 1365 Division by 0 +SHOW WARNINGS; +Level Code Message +Warning 1365 Division by 0 +# MOD function - decimal +SELECT MOD(5.5, 0.0); +MOD(5.5, 0.0) +NULL +Warnings: +Warning 1365 Division by 0 +SHOW WARNINGS; +Level Code Message +Warning 1365 Division by 0 diff --git a/mysql-test/main/func_math_div_zero.test b/mysql-test/main/func_math_div_zero.test new file mode 100644 index 0000000000000..15fd264779a84 --- /dev/null +++ b/mysql-test/main/func_math_div_zero.test @@ -0,0 +1,123 @@ +--echo # +--echo # MDEV-37939: Use correct error for invalid logarithm arguments +--echo # +--echo # LOG/LN/LOG2/LOG10 now emit function-specific domain warnings +--echo # instead of ER_DIVISION_BY_ZERO, since these functions do not +--echo # perform division. +--echo # + +--echo # +--echo # Test valid inputs first (before any warning-producing statements) +--echo # This ensures SHOW WARNINGS is truly empty +--echo # +SET SESSION sql_mode='ERROR_FOR_DIVISION_BY_ZERO'; + +SELECT LOG(10); +SELECT LN(2.718); +SELECT LOG2(8); +SELECT LOG10(100); +SELECT LOG(2, 8); +SHOW WARNINGS; +--echo # Should be empty - valid inputs produce no warnings + +--echo # +--echo # Test sql_mode='' behavior (before any warnings are produced) +--echo # +SET SESSION sql_mode=''; + +SELECT LOG(-1); +SHOW WARNINGS; +--echo # Should be empty - no warning when sql_mode doesn't include ERROR_FOR_DIVISION_BY_ZERO + +SELECT LN(-1); +SHOW WARNINGS; + +--echo # +--echo # Now test warning-producing cases +--echo # +SET SESSION sql_mode='ERROR_FOR_DIVISION_BY_ZERO'; + +--echo # +--echo # LOG family should report function-specific domain warnings +--echo # +SELECT LOG(-1); +SHOW WARNINGS; + +SELECT LOG(0); +SHOW WARNINGS; + +SELECT LN(0); +SHOW WARNINGS; + +SELECT LN(-5); +SHOW WARNINGS; + +SELECT LOG2(-1); +SHOW WARNINGS; + +SELECT LOG10(-1); +SHOW WARNINGS; + +--echo # Two-argument LOG: invalid second argument +SELECT LOG(2, -1); +SHOW WARNINGS; + +--echo # Two-argument LOG: invalid base (negative) +SELECT LOG(-2, 10); +SHOW WARNINGS; + +--echo # Two-argument LOG: base = 1 (invalid - would cause log(1)=0 in denominator) +SELECT LOG(1, 10); +SHOW WARNINGS; + +--echo # +--echo # Division operations should report ER_DIVISION_BY_ZERO (1365) +--echo # + +--echo # Division - integer +SELECT 1/0; +SHOW WARNINGS; + +--echo # Division - real/float +SELECT 1.0/0.0; +SHOW WARNINGS; + +--echo # Division - decimal +SELECT CAST(1 AS DECIMAL(10,2)) / CAST(0 AS DECIMAL(10,2)); +SHOW WARNINGS; + +--echo # Division - with string arguments +SELECT '1'/'0'; +SHOW WARNINGS; + +--echo # Integer DIV +SELECT 5 DIV 0; +SHOW WARNINGS; + +--echo # Integer DIV - with string arguments +SELECT '5' DIV '0'; +SHOW WARNINGS; + +--echo # MOD - integer +SELECT 5 MOD 0; +SHOW WARNINGS; + +--echo # MOD - real/float +SELECT 5.5e0 MOD 0.0e0; +SHOW WARNINGS; + +--echo # MOD - decimal +SELECT CAST(5 AS DECIMAL(10,2)) MOD CAST(0 AS DECIMAL(10,2)); +SHOW WARNINGS; + +--echo # MOD - with string arguments +SELECT '5' MOD '0'; +SHOW WARNINGS; + +--echo # MOD function syntax +SELECT MOD(5, 0); +SHOW WARNINGS; + +--echo # MOD function - decimal +SELECT MOD(5.5, 0.0); +SHOW WARNINGS; diff --git a/mysql-test/suite/gcol/r/gcol_supported_sql_funcs_innodb.result b/mysql-test/suite/gcol/r/gcol_supported_sql_funcs_innodb.result index 1b901e6b87e8e..5dbd34a828e21 100644 --- a/mysql-test/suite/gcol/r/gcol_supported_sql_funcs_innodb.result +++ b/mysql-test/suite/gcol/r/gcol_supported_sql_funcs_innodb.result @@ -274,13 +274,13 @@ t1 CREATE TABLE `t1` ( insert into t1 values (2,default); insert into t1 values (-2,default); Warnings: -Warning 1365 Division by 0 +Warning 4255 Passing non-positive argument to ln() makes the function's result undefined select * from t1; a b -2 NULL 2 0.693147 Warnings: -Warning 1365 Division by 0 +Warning 4255 Passing non-positive argument to ln() makes the function's result undefined drop table t1; set sql_warnings = 0; # LOG() @@ -297,14 +297,14 @@ insert into t1 values (2,65536,default); insert into t1 values (10,100,default); insert into t1 values (1,100,default); Warnings: -Warning 1365 Division by 0 +Warning 4257 Passing non-positive or equal-to-1 base to log() makes the function's result undefined select * from t1; a b c 1 100 NULL 10 100 2 2 65536 16 Warnings: -Warning 1365 Division by 0 +Warning 4257 Passing non-positive or equal-to-1 base to log() makes the function's result undefined drop table t1; set sql_warnings = 0; set sql_warnings = 1; @@ -318,13 +318,13 @@ t1 CREATE TABLE `t1` ( insert into t1 values (2,default); insert into t1 values (-2,default); Warnings: -Warning 1365 Division by 0 +Warning 4256 Passing non-positive argument to log() makes the function's result undefined select * from t1; a b -2 NULL 2 0.693147 Warnings: -Warning 1365 Division by 0 +Warning 4256 Passing non-positive argument to log() makes the function's result undefined drop table t1; set sql_warnings = 0; # LOG2() @@ -339,13 +339,13 @@ t1 CREATE TABLE `t1` ( insert into t1 values (65536,default); insert into t1 values (-100,default); Warnings: -Warning 1365 Division by 0 +Warning 4258 Passing non-positive argument to log2() makes the function's result undefined select * from t1; a b -100 NULL 65536 16 Warnings: -Warning 1365 Division by 0 +Warning 4258 Passing non-positive argument to log2() makes the function's result undefined drop table t1; set sql_warnings = 0; # LOG10() @@ -361,14 +361,14 @@ insert into t1 values (2,default); insert into t1 values (100,default); insert into t1 values (-100,default); Warnings: -Warning 1365 Division by 0 +Warning 4259 Passing non-positive argument to log10() makes the function's result undefined select * from t1; a b -100 NULL 100 2 2 0.30103 Warnings: -Warning 1365 Division by 0 +Warning 4259 Passing non-positive argument to log10() makes the function's result undefined drop table t1; set sql_warnings = 0; # - diff --git a/mysql-test/suite/gcol/r/gcol_supported_sql_funcs_myisam.result b/mysql-test/suite/gcol/r/gcol_supported_sql_funcs_myisam.result index dab3d50b00eb3..ec5894543241d 100644 --- a/mysql-test/suite/gcol/r/gcol_supported_sql_funcs_myisam.result +++ b/mysql-test/suite/gcol/r/gcol_supported_sql_funcs_myisam.result @@ -274,13 +274,13 @@ t1 CREATE TABLE `t1` ( insert into t1 values (2,default); insert into t1 values (-2,default); Warnings: -Warning 1365 Division by 0 +Warning 4255 Passing non-positive argument to ln() makes the function's result undefined select * from t1; a b -2 NULL 2 0.693147 Warnings: -Warning 1365 Division by 0 +Warning 4255 Passing non-positive argument to ln() makes the function's result undefined drop table t1; set sql_warnings = 0; # LOG() @@ -297,14 +297,14 @@ insert into t1 values (2,65536,default); insert into t1 values (10,100,default); insert into t1 values (1,100,default); Warnings: -Warning 1365 Division by 0 +Warning 4257 Passing non-positive or equal-to-1 base to log() makes the function's result undefined select * from t1; a b c 1 100 NULL 10 100 2 2 65536 16 Warnings: -Warning 1365 Division by 0 +Warning 4257 Passing non-positive or equal-to-1 base to log() makes the function's result undefined drop table t1; set sql_warnings = 0; set sql_warnings = 1; @@ -318,13 +318,13 @@ t1 CREATE TABLE `t1` ( insert into t1 values (2,default); insert into t1 values (-2,default); Warnings: -Warning 1365 Division by 0 +Warning 4256 Passing non-positive argument to log() makes the function's result undefined select * from t1; a b -2 NULL 2 0.693147 Warnings: -Warning 1365 Division by 0 +Warning 4256 Passing non-positive argument to log() makes the function's result undefined drop table t1; set sql_warnings = 0; # LOG2() @@ -339,13 +339,13 @@ t1 CREATE TABLE `t1` ( insert into t1 values (65536,default); insert into t1 values (-100,default); Warnings: -Warning 1365 Division by 0 +Warning 4258 Passing non-positive argument to log2() makes the function's result undefined select * from t1; a b -100 NULL 65536 16 Warnings: -Warning 1365 Division by 0 +Warning 4258 Passing non-positive argument to log2() makes the function's result undefined drop table t1; set sql_warnings = 0; # LOG10() @@ -361,14 +361,14 @@ insert into t1 values (2,default); insert into t1 values (100,default); insert into t1 values (-100,default); Warnings: -Warning 1365 Division by 0 +Warning 4259 Passing non-positive argument to log10() makes the function's result undefined select * from t1; a b -100 NULL 100 2 2 0.30103 Warnings: -Warning 1365 Division by 0 +Warning 4259 Passing non-positive argument to log10() makes the function's result undefined drop table t1; set sql_warnings = 0; # - diff --git a/mysql-test/suite/vcol/r/vcol_supported_sql_funcs.result b/mysql-test/suite/vcol/r/vcol_supported_sql_funcs.result index 3c062b5f27af2..3b9adc3182b2f 100644 --- a/mysql-test/suite/vcol/r/vcol_supported_sql_funcs.result +++ b/mysql-test/suite/vcol/r/vcol_supported_sql_funcs.result @@ -274,13 +274,13 @@ t1 CREATE TABLE `t1` ( insert into t1 values (2,default); insert ignore into t1 values (-2,default); Warnings: -Warning 1365 Division by 0 +Warning 4255 Passing non-positive argument to ln() makes the function's result undefined select * from t1; a b 2 0.693147 -2 NULL Warnings: -Warning 1365 Division by 0 +Warning 4255 Passing non-positive argument to ln() makes the function's result undefined drop table t1; set sql_warnings = 0; # LOG() @@ -297,14 +297,14 @@ insert into t1 values (2,65536,default); insert ignore into t1 values (10,100,default); insert into t1 values (1,100,default); Warnings: -Warning 1365 Division by 0 +Warning 4257 Passing non-positive or equal-to-1 base to log() makes the function's result undefined select * from t1; a b c 2 65536 16 10 100 2 1 100 NULL Warnings: -Warning 1365 Division by 0 +Warning 4257 Passing non-positive or equal-to-1 base to log() makes the function's result undefined drop table t1; set sql_warnings = 0; set sql_warnings = 1; @@ -318,13 +318,13 @@ t1 CREATE TABLE `t1` ( insert into t1 values (2,default); insert ignore into t1 values (-2,default); Warnings: -Warning 1365 Division by 0 +Warning 4256 Passing non-positive argument to log() makes the function's result undefined select * from t1; a b 2 0.693147 -2 NULL Warnings: -Warning 1365 Division by 0 +Warning 4256 Passing non-positive argument to log() makes the function's result undefined drop table t1; set sql_warnings = 0; # LOG2() @@ -339,13 +339,13 @@ t1 CREATE TABLE `t1` ( insert into t1 values (65536,default); insert ignore into t1 values (-100,default); Warnings: -Warning 1365 Division by 0 +Warning 4258 Passing non-positive argument to log2() makes the function's result undefined select * from t1; a b 65536 16 -100 NULL Warnings: -Warning 1365 Division by 0 +Warning 4258 Passing non-positive argument to log2() makes the function's result undefined drop table t1; set sql_warnings = 0; # LOG10() @@ -361,14 +361,14 @@ insert into t1 values (2,default); insert ignore into t1 values (100,default); insert into t1 values (-100,default); Warnings: -Warning 1365 Division by 0 +Warning 4259 Passing non-positive argument to log10() makes the function's result undefined select * from t1; a b 2 0.30103 100 2 -100 NULL Warnings: -Warning 1365 Division by 0 +Warning 4259 Passing non-positive argument to log10() makes the function's result undefined drop table t1; set sql_warnings = 0; # - diff --git a/sql/item_func.cc b/sql/item_func.cc index 89ee8cfc1863f..d443f48f88373 100644 --- a/sql/item_func.cc +++ b/sql/item_func.cc @@ -783,6 +783,15 @@ void Item_udf_func::fix_num_length_and_dec() #endif +static void signal_log_domain_error(uint sql_errno) +{ + THD *thd= current_thd; + if (thd->variables.sql_mode & MODE_ERROR_FOR_DIVISION_BY_ZERO) + push_warning(thd, Sql_condition::WARN_LEVEL_WARN, sql_errno, + ER_THD(thd, sql_errno)); +} + + void Item_func::signal_divide_by_null() { THD *thd= current_thd; @@ -2021,7 +2030,8 @@ double Item_func_ln::val_real() return 0.0; if (value <= 0.0) { - signal_divide_by_null(); + signal_log_domain_error(ER_INVALID_ARGUMENT_FOR_LN); + null_value= 1; return 0.0; } return log(value); @@ -2036,27 +2046,35 @@ double Item_func_ln::val_real() double Item_func_log::val_real() { DBUG_ASSERT(fixed()); - double value= args[0]->val_real(); + double base= args[0]->val_real(); if ((null_value= args[0]->null_value)) return 0.0; - if (value <= 0.0) - { - signal_divide_by_null(); - return 0.0; - } if (arg_count == 2) { - double value2= args[1]->val_real(); + double value= args[1]->val_real(); if ((null_value= args[1]->null_value)) return 0.0; - if (value2 <= 0.0 || value == 1.0) + if (base <= 0.0 || base == 1.0) { - signal_divide_by_null(); + signal_log_domain_error(ER_INVALID_BASE_FOR_LOG); + null_value= 1; return 0.0; } - return log(value2) / log(value); + if (value <= 0.0) + { + signal_log_domain_error(ER_INVALID_ARGUMENT_FOR_LOG); + null_value= 1; + return 0.0; + } + return log(value) / log(base); } - return log(value); + if (base <= 0.0) + { + signal_log_domain_error(ER_INVALID_ARGUMENT_FOR_LOG); + null_value= 1; + return 0.0; + } + return log(base); } double Item_func_log2::val_real() @@ -2068,7 +2086,8 @@ double Item_func_log2::val_real() return 0.0; if (value <= 0.0) { - signal_divide_by_null(); + signal_log_domain_error(ER_INVALID_ARGUMENT_FOR_LOG2); + null_value= 1; return 0.0; } return log(value) / M_LN2; @@ -2082,7 +2101,8 @@ double Item_func_log10::val_real() return 0.0; if (value <= 0.0) { - signal_divide_by_null(); + signal_log_domain_error(ER_INVALID_ARGUMENT_FOR_LOG10); + null_value= 1; return 0.0; } return log10(value); diff --git a/sql/item_func.h b/sql/item_func.h index 0be8cf61591b1..285da27a0062f 100644 --- a/sql/item_func.h +++ b/sql/item_func.h @@ -249,8 +249,9 @@ class Item_func :public Item_func_or_sum return null_value; } String *val_str_from_val_str_ascii(String *str, String *str2); - +protected: void signal_divide_by_null(); +public: friend class udf_handler; Field *create_field_for_create_select(MEM_ROOT *root, TABLE *table) override { return tmp_table_field_from_field_type(root, table); } diff --git a/sql/share/errmsg-utf8.txt b/sql/share/errmsg-utf8.txt index 56b1c7e07088b..7a52f75e27611 100644 --- a/sql/share/errmsg-utf8.txt +++ b/sql/share/errmsg-utf8.txt @@ -12386,3 +12386,13 @@ ER_PARTIAL_ROWS_LOG_EVENT_BAD_STREAM eng "Broken Partial_rows_log_event stream. Found %s, expected Partial_rows_log_event %u / %u" ER_SLAVE_INCOMPATIBLE_TABLE_DEF eng "Table structure for binlog event is not compatible with the table definition on this slave: %s" +ER_INVALID_ARGUMENT_FOR_LN 2201E + eng "Passing non-positive argument to ln() makes the function's result undefined" +ER_INVALID_ARGUMENT_FOR_LOG 2201E + eng "Passing non-positive argument to log() makes the function's result undefined" +ER_INVALID_BASE_FOR_LOG 2201E + eng "Passing non-positive or equal-to-1 base to log() makes the function's result undefined" +ER_INVALID_ARGUMENT_FOR_LOG2 2201E + eng "Passing non-positive argument to log2() makes the function's result undefined" +ER_INVALID_ARGUMENT_FOR_LOG10 2201E + eng "Passing non-positive argument to log10() makes the function's result undefined"