From 312c2f4759514c6594e630e6d1237b8875022328 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 6 Jan 2006 22:28:26 -0800 Subject: [PATCH 1/3] Fixed bug #16016: MIN/MAX optimization was not applied to views. mysql-test/r/view.result: Added a test case for bug #16016. mysql-test/t/view.test: Added a test case for bug #16016. sql/opt_sum.cc: Fixed bug #16016: MIN/MAX optimization was not applied to views. The fix employs the standard way of handling direct references to view fields. --- mysql-test/r/view.result | 29 +++++++++++++++++++++++++++++ mysql-test/t/view.test | 24 ++++++++++++++++++++++++ sql/opt_sum.cc | 10 +++++----- 3 files changed, 58 insertions(+), 5 deletions(-) diff --git a/mysql-test/r/view.result b/mysql-test/r/view.result index b9380dcf033..8b6d0787ada 100644 --- a/mysql-test/r/view.result +++ b/mysql-test/r/view.result @@ -2475,3 +2475,32 @@ alias1 alias2 5 5 drop view v2, v1; drop table t1; +CREATE TABLE t1 (a int PRIMARY KEY, b int); +INSERT INTO t1 VALUES (2,20), (3,10), (1,10), (0,30), (5,10); +CREATE VIEW v1 AS SELECT * FROM t1; +SELECT MAX(a) FROM t1; +MAX(a) +5 +SELECT MAX(a) FROM v1; +MAX(a) +5 +EXPLAIN SELECT MAX(a) FROM t1; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Select tables optimized away +EXPLAIN SELECT MAX(a) FROM v1; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away +SELECT MIN(a) FROM t1; +MIN(a) +0 +SELECT MIN(a) FROM v1; +MIN(a) +0 +EXPLAIN SELECT MIN(a) FROM t1; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Select tables optimized away +EXPLAIN SELECT MIN(a) FROM v1; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away +DROP VIEW v1; +DROP TABLE t1; diff --git a/mysql-test/t/view.test b/mysql-test/t/view.test index db6c12fdacb..5f3678215f2 100644 --- a/mysql-test/t/view.test +++ b/mysql-test/t/view.test @@ -2339,3 +2339,27 @@ order by v2.receipt_id; drop view v2, v1; drop table t1; + +# +# Bug#16016: MIN/MAX optimization for views +# + +CREATE TABLE t1 (a int PRIMARY KEY, b int); +INSERT INTO t1 VALUES (2,20), (3,10), (1,10), (0,30), (5,10); + +CREATE VIEW v1 AS SELECT * FROM t1; + +SELECT MAX(a) FROM t1; +SELECT MAX(a) FROM v1; + +EXPLAIN SELECT MAX(a) FROM t1; +EXPLAIN SELECT MAX(a) FROM v1; + +SELECT MIN(a) FROM t1; +SELECT MIN(a) FROM v1; + +EXPLAIN SELECT MIN(a) FROM t1; +EXPLAIN SELECT MIN(a) FROM v1; + +DROP VIEW v1; +DROP TABLE t1; diff --git a/sql/opt_sum.cc b/sql/opt_sum.cc index 37acce2934b..ed8e694dcb7 100644 --- a/sql/opt_sum.cc +++ b/sql/opt_sum.cc @@ -180,14 +180,14 @@ int opt_sum_query(TABLE_LIST *tables, List &all_fields,COND *conds) indexes to find the key. */ Item *expr=item_sum->args[0]; - if (expr->type() == Item::FIELD_ITEM) + if (expr->real_item()->type() == Item::FIELD_ITEM) { byte key_buff[MAX_KEY_LENGTH]; TABLE_REF ref; uint range_fl, prefix_len; ref.key_buff= key_buff; - Item_field *item_field= ((Item_field*) expr); + Item_field *item_field= (Item_field*) (expr->real_item()); TABLE *table= item_field->field->table; /* @@ -267,14 +267,14 @@ int opt_sum_query(TABLE_LIST *tables, List &all_fields,COND *conds) indexes to find the key. */ Item *expr=item_sum->args[0]; - if (expr->type() == Item::FIELD_ITEM) + if (expr->real_item()->type() == Item::FIELD_ITEM) { byte key_buff[MAX_KEY_LENGTH]; TABLE_REF ref; - uint range_fl, prefix_len; + uint range_fl, prefix_len; ref.key_buff= key_buff; - Item_field *item_field= ((Item_field*) expr); + Item_field *item_field= (Item_field*) (expr->real_item()); TABLE *table= item_field->field->table; /* From 770e0e8118065a27c1e9583b1707864ab4c3120c Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 7 Jan 2006 23:00:06 -0800 Subject: [PATCH 2/3] Fixed bug #14274: a query with a having clause containing only set function returned a wrong result set. mysql-test/r/having.result: Added a test case for bug #14274. mysql-test/t/having.test: Added a test case for bug #14274. sql/sql_select.cc: Fixed bug #14274: a query with a having clause containing only set function returned a wrong result set. It happened because processing of the set functions in having started with a call of the split_sum_func method, instead of the split_sum_func2 method. --- mysql-test/r/having.result | 13 +++++++++++++ mysql-test/t/having.test | 12 ++++++++++++ sql/sql_select.cc | 2 +- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/mysql-test/r/having.result b/mysql-test/r/having.result index 218276406b1..9730f9f81bf 100644 --- a/mysql-test/r/having.result +++ b/mysql-test/r/having.result @@ -128,3 +128,16 @@ id description c 1 test 0 2 test2 0 drop table t1,t2,t3; +CREATE TABLE t1 (a int); +INSERT INTO t1 VALUES (3), (4), (1), (3), (1); +SELECT SUM(a) FROM t1 GROUP BY a HAVING SUM(a)>0; +SUM(a) +2 +6 +4 +SELECT SUM(a) FROM t1 GROUP BY a HAVING SUM(a); +SUM(a) +2 +6 +4 +DROP TABLE t1; diff --git a/mysql-test/t/having.test b/mysql-test/t/having.test index 920a3b752ab..3dd9ace6a1b 100644 --- a/mysql-test/t/having.test +++ b/mysql-test/t/having.test @@ -123,4 +123,16 @@ group by a.id, a.description having (a.description is not null) and (c=0); drop table t1,t2,t3; +# +# Bug #14274: HAVING clause containing only set function +# + +CREATE TABLE t1 (a int); +INSERT INTO t1 VALUES (3), (4), (1), (3), (1); + +SELECT SUM(a) FROM t1 GROUP BY a HAVING SUM(a)>0; +SELECT SUM(a) FROM t1 GROUP BY a HAVING SUM(a); + +DROP TABLE t1; + # End of 4.1 tests diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 5e29c98e2c8..09900c89307 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -287,7 +287,7 @@ JOIN::prepare(Item ***rref_pointer_array, if (having_fix_rc || thd->net.report_error) DBUG_RETURN(-1); /* purecov: inspected */ if (having->with_sum_func) - having->split_sum_func(thd, ref_pointer_array, all_fields); + having->split_sum_func2(thd, ref_pointer_array, all_fields, &having); } // Is it subselect From d92b8db1d6424ae5beb77f63d36701365d2974b2 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 8 Jan 2006 17:17:22 -0800 Subject: [PATCH 3/3] Adjustment after a merge. --- sql/sql_select.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 585368fbedd..1a217df2878 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -365,7 +365,8 @@ JOIN::prepare(Item ***rref_pointer_array, if (having_fix_rc || thd->net.report_error) DBUG_RETURN(-1); /* purecov: inspected */ if (having->with_sum_func) - having->split_sum_func2(thd, ref_pointer_array, all_fields, &having); + having->split_sum_func2(thd, ref_pointer_array, all_fields, + &having, TRUE); thd->lex->allow_sum_func= save_allow_sum_func; } if (select_lex->inner_sum_func_list)