← Back to team overview

maria-developers team mailing list archive

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

 

Hi Serg,

Please review a patch for MDEV-10500.
Thanks.
diff --git a/mysql-test/r/func_group.result b/mysql-test/r/func_group.result
index ac076ec..0253548 100644
--- a/mysql-test/r/func_group.result
+++ b/mysql-test/r/func_group.result
@@ -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;
diff --git a/mysql-test/t/func_group.test b/mysql-test/t/func_group.test
index bd3ed4a..5824d99 100644
--- a/mysql-test/t/func_group.test
+++ b/mysql-test/t/func_group.test
@@ -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;
diff --git a/sql/item_sum.cc b/sql/item_sum.cc
index adf48f6..4458951 100644
--- a/sql/item_sum.cc
+++ b/sql/item_sum.cc
@@ -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);
 }
 

Follow ups