Commit 8f904e9c authored by evgen@moonbone.local's avatar evgen@moonbone.local

Fixed bug#16272: IF function with decimal args can produce wrong result

The Item_func_if::fix_length_and_dec() function when calculating length of 
result doesn't take into account unsigned_flag. But it is taken when 
calculating length of temporary field. This result in creating field that 
shorter than needed. Due to this, in the reported query 40.0 converted to 9.99.

The function Item_func_if::fix_length_and_dec() now adds 1 to the max_length if 
the unsigned_flag isn't set.
parent f9a23667
...@@ -121,3 +121,10 @@ a NULLIF(a,'') ...@@ -121,3 +121,10 @@ a NULLIF(a,'')
NULL NULL NULL NULL
NULL NULL
DROP TABLE t1; DROP TABLE t1;
create table t1 (f1 int, f2 int);
insert into t1 values(1,1),(0,0);
select f1, f2, if(f1, 40.0, 5.00) from t1 group by f1 order by f2;
f1 f2 if(f1, 40.0, 5.00)
0 0 5.00
1 1 40.00
drop table t1;
...@@ -89,3 +89,11 @@ SELECT a, NULLIF(a,'') FROM t1 WHERE NULLIF(a,'') IS NULL; ...@@ -89,3 +89,11 @@ SELECT a, NULLIF(a,'') FROM t1 WHERE NULLIF(a,'') IS NULL;
DROP TABLE t1; DROP TABLE t1;
# End of 4.1 tests # End of 4.1 tests
#
# Bug #16272 IF function with decimal args can produce wrong result
#
create table t1 (f1 int, f2 int);
insert into t1 values(1,1),(0,0);
select f1, f2, if(f1, 40.0, 5.00) from t1 group by f1 order by f2;
drop table t1;
...@@ -1383,7 +1383,8 @@ Item_func_if::fix_length_and_dec() ...@@ -1383,7 +1383,8 @@ Item_func_if::fix_length_and_dec()
max_length= max_length=
(cached_result_type == DECIMAL_RESULT || cached_result_type == INT_RESULT) ? (cached_result_type == DECIMAL_RESULT || cached_result_type == INT_RESULT) ?
(max(args[1]->max_length - args[1]->decimals, (max(args[1]->max_length - args[1]->decimals,
args[2]->max_length - args[2]->decimals) + decimals) : args[2]->max_length - args[2]->decimals) + decimals +
(unsigned_flag ? 0 : 1) ) :
max(args[1]->max_length, args[2]->max_length); max(args[1]->max_length, args[2]->max_length);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment