From 8b5627f9ef5e780d51a86984a65db6b4344b77be Mon Sep 17 00:00:00 2001 From: abdelrahim Date: Sun, 8 Feb 2026 01:28:58 +0200 Subject: [PATCH] MDEV-38670 Unary minus on empty string returns -0 Normalize -0.0 to +0.0 in Item_func_neg::real_op() to match binary subtraction behavior. Add test case in type_newdecimal. add result for tests fix tests Revert whitespace changes in type_newdecimal.result Revert whitespace changes in type_newdecimal.test --- mysql-test/main/func_math.result | 9 +++++++++ mysql-test/main/func_math.test | 7 +++++++ sql/item_func.cc | 9 ++++++++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/mysql-test/main/func_math.result b/mysql-test/main/func_math.result index 475485cbcae59..7cbff4266544f 100644 --- a/mysql-test/main/func_math.result +++ b/mysql-test/main/func_math.result @@ -3739,3 +3739,12 @@ cast(1 as unsigned) - cast(2 as unsigned) -1 set sql_mode=default; # End of 10.5 tests +# MDEV-38670 Unary minus on empty string should return 0 +@x := -'' +0 +# Ensure binary subtraction matches +@y := 0 - '' +0 +# Additional edge cases +-0.0 -'0' -' ' -@x --@x +0.0 0 0 0 0 \ No newline at end of file diff --git a/mysql-test/main/func_math.test b/mysql-test/main/func_math.test index 363d84eab3e07..7fbce63011957 100644 --- a/mysql-test/main/func_math.test +++ b/mysql-test/main/func_math.test @@ -1998,3 +1998,10 @@ select cast(1 as unsigned) - cast(2 as unsigned); set sql_mode=default; --echo # End of 10.5 tests + +--echo # +--echo # MDEV-38670 Unary minus on empty string should return 0 +--echo # +SELECT @x := -''; +SELECT @y := 0 - ''; +SELECT -0.0, -'0', -' ', -@x, --@x; \ No newline at end of file diff --git a/sql/item_func.cc b/sql/item_func.cc index 0d2b43d100844..31d2d8881adff 100644 --- a/sql/item_func.cc +++ b/sql/item_func.cc @@ -1825,7 +1825,11 @@ double Item_func_neg::real_op() { double value= args[0]->val_real(); null_value= args[0]->null_value; - return -value; + double res= -value; + // Normalize -0.0 to +0.0 to match binary subtraction behavior + if (res == 0.0) + res= 0.0; + return res; } @@ -1858,6 +1862,9 @@ my_decimal *Item_func_neg::decimal_op(my_decimal *decimal_value) { my_decimal2decimal(value.ptr(), decimal_value); my_decimal_neg(decimal_value); + // Normalize -0 to +0 in decimal representation + if (my_decimal_is_zero(decimal_value)) + decimal_value->sign(false); return decimal_value; } return 0;