Skip to content

Commit

Permalink
MDEV-10500 CASE/IF Statement returns multiple values and shifts furth…
Browse files Browse the repository at this point in the history
…er result values to the next column

We assume all around the code that null_value==true is in sync
with NULL value returned by val_str()/val_decimal().
Item_sum_sum::val_decimal() erroneously returned a non-NULL value together
with null_value set to true. Fixing to return NULL instead.
  • Loading branch information
Alexander Barkov committed Aug 8, 2016
1 parent 5e23b63 commit 1b3430a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
27 changes: 27 additions & 0 deletions mysql-test/r/func_group.result
Original file line number Diff line number Diff line change
Expand Up @@ -2270,3 +2270,30 @@ t2_id GROUP_CONCAT(IF (t6.b, t6.f, t5.f) ORDER BY 1)
EXECUTE stmt;
t2_id GROUP_CONCAT(IF (t6.b, t6.f, t5.f) ORDER BY 1)
DROP TABLE t1,t2,t3,t4,t5,t6;
#
# MDEV-10500 CASE/IF Statement returns multiple values and shifts further result values to the next column
#
CREATE TABLE t1 (
id int not null AUTO_INCREMENT,
active bool not null,
data1 bigint,
data2 bigint,
data3 bigint,
primary key (id)
);
INSERT INTO t1 (active,data1,data2,data3) VALUES (1,null,100,200);
SELECT
CASE WHEN active THEN SUM(data1) END AS C_1,
SUM(data2) AS C_2,
SUM(data3) AS C_3
FROM t1;
C_1 C_2 C_3
NULL 100 200
SELECT
IF(active, SUM(data1), 5) AS C_1,
SUM(data2) AS C_2,
SUM(data3) AS C_3
FROM t1;
C_1 C_2 C_3
NULL 100 200
DROP TABLE t1;
25 changes: 25 additions & 0 deletions mysql-test/t/func_group.test
Original file line number Diff line number Diff line change
Expand Up @@ -1565,3 +1565,28 @@ EXECUTE stmt;
EXECUTE stmt;

DROP TABLE t1,t2,t3,t4,t5,t6;

--echo #
--echo # MDEV-10500 CASE/IF Statement returns multiple values and shifts further result values to the next column
--echo #

CREATE TABLE t1 (
id int not null AUTO_INCREMENT,
active bool not null,
data1 bigint,
data2 bigint,
data3 bigint,
primary key (id)
);
INSERT INTO t1 (active,data1,data2,data3) VALUES (1,null,100,200);
SELECT
CASE WHEN active THEN SUM(data1) END AS C_1,
SUM(data2) AS C_2,
SUM(data3) AS C_3
FROM t1;
SELECT
IF(active, SUM(data1), 5) AS C_1,
SUM(data2) AS C_2,
SUM(data3) AS C_3
FROM t1;
DROP TABLE t1;
2 changes: 1 addition & 1 deletion sql/item_sum.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1462,7 +1462,7 @@ my_decimal *Item_sum_sum::val_decimal(my_decimal *val)
if (aggr)
aggr->endup();
if (hybrid_type == DECIMAL_RESULT)
return (dec_buffs + curr_dec_buff);
return null_value ? NULL : (dec_buffs + curr_dec_buff);
return val_decimal_from_real(val);
}

Expand Down

0 comments on commit 1b3430a

Please sign in to comment.