From 1688ada0e2ce3bf5e62f1539811f0cfb0d8bc4b6 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 21 Jun 2003 16:59:40 +0500 Subject: [PATCH 1/2] Proposed fix for #674 This crash happens in rather exotic case when we try to run SELECT DISTINCT some_func(SUM(some_field)) GROUP BY another_field; on a table with single row. Optimizer marks this table as const, sets group=NULL (with remove_const) thus, create_tmp_table makes mistake collecting columns for temporary table and then crashes because the field_count gets less than hidden_columns_count. sql/sql_select.cc: There's several ways to fix this bug. This one looks easy and correct to me --- sql/sql_select.cc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 0e8b191e4ef..cb056100e2a 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -453,6 +453,9 @@ mysql_select(THD *thd,TABLE_LIST *tables,List &fields,COND *conds, goto err; thd->proc_info="preparing"; + + select_distinct= select_distinct && (join.const_tables != join.tables); + if (result->initialize_tables(&join)) goto err; if (join.const_table_map != join.found_const_table_map && From 8abad215650386dedf0001812f44859009de74ef Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 25 Jun 2003 17:15:33 +0500 Subject: [PATCH 2/2] Testcase for #674 added mysql-test/r/sel000100.result: Result of the testcase added mysql-test/t/sel000100.test: checking for bug #674 added --- mysql-test/r/sel000100.result | 10 ++++++++++ mysql-test/t/sel000100.test | 15 +++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/mysql-test/r/sel000100.result b/mysql-test/r/sel000100.result index f9234815a2b..3ffa4004b84 100644 --- a/mysql-test/r/sel000100.result +++ b/mysql-test/r/sel000100.result @@ -26,3 +26,13 @@ ORDER BY link; key_link_id link NULL NULL drop table t1,t2; +CREATE TABLE t1 ( +html varchar(5) default NULL, +rin int(11) default '0', +out int(11) default '0' +) TYPE=MyISAM; +INSERT INTO t1 VALUES ('1',1,0); +SELECT DISTINCT html,SUM(out)/(SUM(rin)+1) as 'prod' FROM t1 GROUP BY rin; +html prod +1 0.00 +drop table t1; diff --git a/mysql-test/t/sel000100.test b/mysql-test/t/sel000100.test index cedb78b17e7..d587fa4ebd0 100644 --- a/mysql-test/t/sel000100.test +++ b/mysql-test/t/sel000100.test @@ -29,3 +29,18 @@ GROUP BY t1.id ORDER BY link; drop table t1,t2; + +# +# test case for #674 +# +CREATE TABLE t1 ( + html varchar(5) default NULL, + rin int(11) default '0', + out int(11) default '0' +) TYPE=MyISAM; + +INSERT INTO t1 VALUES ('1',1,0); + +SELECT DISTINCT html,SUM(out)/(SUM(rin)+1) as 'prod' FROM t1 GROUP BY rin; + +drop table t1;