diff options
author | Alexey Kopytov <Alexey.Kopytov@Sun.com> | 2009-10-13 12:31:42 +0400 |
---|---|---|
committer | Alexey Kopytov <Alexey.Kopytov@Sun.com> | 2009-10-13 12:31:42 +0400 |
commit | 7e3b4d21c0010bf6bc4fd8cdb8eee3caf8207f29 (patch) | |
tree | fea2364401dec8d958b129af9bc211c8ad3c9f96 /sql/item_func.cc | |
parent | 4337c3808f32c0851b7b9e6efb2516302d691bf8 (diff) | |
download | mariadb-git-7e3b4d21c0010bf6bc4fd8cdb8eee3caf8207f29.tar.gz |
Backport of the patch for bug #8457 "Precision math: DIV
returns incorrect result with large decimal value"
For the DIV operator, neither operands nor result were checked
for integer overflows.
This patch changes the DIV behavior for non-integer operands as
follows: if either of the operands has a non-integer type,
convert both operands to the DECIMAL type, then calculate the
division using DECIMAL arithmetics. Convert the resulting
DECIMAL value into BIGINT [UNSIGNED] if it fits into the
corresponding range, or throw an 'out of range' error
otherwise.
mysql-test/r/func_math.result:
Added a test case for bug #8457.
Fixed results for a test case depending on the wrong behavior.
mysql-test/r/type_varchar.result:
Fixed results for a test case depending on the wrong behavior.
mysql-test/t/func_math.test:
Added a test case for bug #8457.
sql/item_func.cc:
If either of the operands has a non-integer type, convert both
operands to the DECIMAL type, then calculate the division using
DECIMAL arithmetics. Convert the resulting DECIMAL value into
BIGINT [UNSIGNED] if it fits into the corresponding range, or
throw an 'out of range' error otherwise.
Diffstat (limited to 'sql/item_func.cc')
-rw-r--r-- | sql/item_func.cc | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/sql/item_func.cc b/sql/item_func.cc index cf499aaf93c..c0a84e3be28 100644 --- a/sql/item_func.cc +++ b/sql/item_func.cc @@ -1348,6 +1348,38 @@ void Item_func_div::fix_length_and_dec() longlong Item_func_int_div::val_int() { DBUG_ASSERT(fixed == 1); + + /* + Perform division using DECIMAL math if either of the operands has a + non-integer type + */ + if (args[0]->result_type() != INT_RESULT || + args[1]->result_type() != INT_RESULT) + { + my_decimal value0, value1, tmp; + my_decimal *val0, *val1; + longlong res; + int err; + + val0= args[0]->val_decimal(&value0); + val1= args[1]->val_decimal(&value1); + if ((null_value= (args[0]->null_value || args[1]->null_value))) + return 0; + + if ((err= my_decimal_div(E_DEC_FATAL_ERROR & ~E_DEC_DIV_ZERO, &tmp, + val0, val1, 0)) > 3) + { + if (err == E_DEC_DIV_ZERO) + signal_divide_by_null(); + return 0; + } + + if (my_decimal2int(E_DEC_FATAL_ERROR, &tmp, unsigned_flag, &res) & + E_DEC_OVERFLOW) + my_error(ER_WARN_DATA_OUT_OF_RANGE, MYF(0), name, 1); + return res; + } + longlong value=args[0]->val_int(); longlong val2=args[1]->val_int(); if ((null_value= (args[0]->null_value || args[1]->null_value))) |