From 78adb4bc15d8700e8517e7bacc30c4470ab1d767 Mon Sep 17 00:00:00 2001 From: "ramil@mysql.com" <> Date: Mon, 6 Mar 2006 16:38:35 +0400 Subject: [PATCH 1/4] Fix for bug #17896: MIN of CASE WHEN returns non-minimum value! --- mysql-test/r/case.result | 8 ++++++++ mysql-test/t/case.test | 11 +++++++++++ sql/item_cmpfunc.cc | 19 +++++++++++++++---- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/mysql-test/r/case.result b/mysql-test/r/case.result index fb0bc19a67e..a5495d0fc3e 100644 --- a/mysql-test/r/case.result +++ b/mysql-test/r/case.result @@ -169,3 +169,11 @@ SELECT CASE '1' WHEN '2' THEN 'BUG' ELSE 'nobug' END; case+union+test case+union+test nobug +create table t1(a float, b int default 3); +insert into t1 (a) values (2), (11), (8); +select min(a), min(case when 1=1 then a else NULL end), +min(case when 1!=1 then NULL else a end) +from t1 where b=3 group by b; +min(a) min(case when 1=1 then a else NULL end) min(case when 1!=1 then NULL else a end) +2 2 2 +drop table t1; diff --git a/mysql-test/t/case.test b/mysql-test/t/case.test index fbbbce15576..fd1b6e5247f 100644 --- a/mysql-test/t/case.test +++ b/mysql-test/t/case.test @@ -119,4 +119,15 @@ SELECT 'case+union+test' UNION SELECT CASE '1' WHEN '2' THEN 'BUG' ELSE 'nobug' END; +# +# Bug #17896: problem with MIN(CASE...) +# + +create table t1(a float, b int default 3); +insert into t1 (a) values (2), (11), (8); +select min(a), min(case when 1=1 then a else NULL end), + min(case when 1!=1 then NULL else a end) +from t1 where b=3 group by b; +drop table t1; + # End of 4.1 tests diff --git a/sql/item_cmpfunc.cc b/sql/item_cmpfunc.cc index f9aa220c181..57a3c9e5623 100644 --- a/sql/item_cmpfunc.cc +++ b/sql/item_cmpfunc.cc @@ -37,10 +37,21 @@ static Item_result item_store_type(Item_result a,Item_result b) static void agg_result_type(Item_result *type, Item **items, uint nitems) { - uint i; - type[0]= items[0]->result_type(); - for (i=1 ; i < nitems ; i++) - type[0]= item_store_type(type[0], items[i]->result_type()); + Item **item, **item_end; + + /* Note: NULL items don't affect the result type */ + *type= STRING_RESULT; + /* Skip beginning NULL items */ + for (item= items, item_end= item + nitems; item < item_end; item++) + if ((*item)->type() != Item::NULL_ITEM) + { + *type= (*item)->result_type(); + item++; + break; + } + for (; item < item_end; item++) + if ((*item)->type() != Item::NULL_ITEM) + *type= item_store_type(type[0], (*item)->result_type()); } static void agg_cmp_type(Item_result *type, Item **items, uint nitems) From 408599af843730e73b95de55ade0bce3e211fd71 Mon Sep 17 00:00:00 2001 From: "knielsen@mysql.com" <> Date: Wed, 12 Apr 2006 14:32:34 +0200 Subject: [PATCH 2/4] Fix broken --valgrind-options option. --- mysql-test/mysql-test-run.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mysql-test/mysql-test-run.pl b/mysql-test/mysql-test-run.pl index 20bb6e0117a..aa9d22ffded 100755 --- a/mysql-test/mysql-test-run.pl +++ b/mysql-test/mysql-test-run.pl @@ -2436,7 +2436,7 @@ sub valgrind_arguments { if ( $opt_valgrind_options ) { # FIXME split earlier and put into @glob_valgrind_* - mtr_add_arg($args, split(' ', $opt_valgrind_options)); + mtr_add_arg($args, '%s', $_) for (split(' ', $opt_valgrind_options))); } mtr_add_arg($args, $$exe); From b9fc8e1f697f5bae808d94f7c5b3d5be10cff741 Mon Sep 17 00:00:00 2001 From: "ramil@mysql.com" <> Date: Fri, 21 Apr 2006 11:48:00 +0500 Subject: [PATCH 3/4] Bug #17896: MIN of CASE WHEN returns non-minimum value! - after review fixes --- sql/item_cmpfunc.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sql/item_cmpfunc.cc b/sql/item_cmpfunc.cc index 57a3c9e5623..3c41fb56d89 100644 --- a/sql/item_cmpfunc.cc +++ b/sql/item_cmpfunc.cc @@ -39,19 +39,23 @@ static void agg_result_type(Item_result *type, Item **items, uint nitems) { Item **item, **item_end; - /* Note: NULL items don't affect the result type */ *type= STRING_RESULT; /* Skip beginning NULL items */ for (item= items, item_end= item + nitems; item < item_end; item++) + { if ((*item)->type() != Item::NULL_ITEM) { *type= (*item)->result_type(); item++; break; } + } + /* Combine result types. Note: NULL items don't affect the result */ for (; item < item_end; item++) + { if ((*item)->type() != Item::NULL_ITEM) *type= item_store_type(type[0], (*item)->result_type()); + } } static void agg_cmp_type(Item_result *type, Item **items, uint nitems) From 17a80c9da4b70af717c5620445f0275c824be47f Mon Sep 17 00:00:00 2001 From: "knielsen@mysql.com" <> Date: Mon, 24 Apr 2006 15:34:01 +0200 Subject: [PATCH 4/4] Fix typo in mysql-test-run.pl (not caught by aotupush since it uses mysql-test-run.sh). --- mysql-test/mysql-test-run.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mysql-test/mysql-test-run.pl b/mysql-test/mysql-test-run.pl index aa9d22ffded..121102ec262 100755 --- a/mysql-test/mysql-test-run.pl +++ b/mysql-test/mysql-test-run.pl @@ -2436,7 +2436,7 @@ sub valgrind_arguments { if ( $opt_valgrind_options ) { # FIXME split earlier and put into @glob_valgrind_* - mtr_add_arg($args, '%s', $_) for (split(' ', $opt_valgrind_options))); + mtr_add_arg($args, '%s', $_) for (split(' ', $opt_valgrind_options)); } mtr_add_arg($args, $$exe);