summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorunknown <igor@rurik.mysql.com>2006-05-06 23:48:13 -0700
committerunknown <igor@rurik.mysql.com>2006-05-06 23:48:13 -0700
commit375749b8afd8b7f47a0b717d0546ad1b2a8f05eb (patch)
treec0395e36606a03a1a826b85cd00bc0c1484e9314
parent427bcfb440d76124539222f70b4787e8737e2100 (diff)
downloadmariadb-git-375749b8afd8b7f47a0b717d0546ad1b2a8f05eb.tar.gz
Fixed bug #14927.
A query with a group by and having clauses could return a wrong result set if the having condition contained a constant conjunct evaluated to FALSE. It happened because the pushdown condition for table with grouping columns lost its constant conjuncts. Pushdown conditions are always built by the function make_cond_for_table that ignores constant conjuncts. This is apparently not correct when constant false conjuncts are present. mysql-test/r/having.result: Added a test case for bug #14927. mysql-test/t/having.test: Added a test case for bug #14927. sql/sql_lex.cc: Fixed bug #14927. Initialized fields for having conditions in st_select_lex::init_query(). sql/sql_lex.h: Fixed bug #14927. Added a field to restore having condititions for execution in SP and PS. sql/sql_prepare.cc: Fixed bug #14927. Added code to restore havinf conditions for execution in SP and PS. sql/sql_select.cc: Fixed bug #14927. Performed evaluation of constant expressions in having clauses. If the having condition contains a constant conjunct that is always false an empty result set is returned after the optimization phase. In this case the corresponding EXPLAIN command now returns "Impossible HAVING" in the last column.
-rw-r--r--mysql-test/r/having.result17
-rw-r--r--mysql-test/t/having.test16
-rw-r--r--sql/sql_lex.cc2
-rw-r--r--sql/sql_lex.h1
-rw-r--r--sql/sql_prepare.cc10
-rw-r--r--sql/sql_select.cc28
6 files changed, 63 insertions, 11 deletions
diff --git a/mysql-test/r/having.result b/mysql-test/r/having.result
index 9730f9f81bf..ccd1f0e61e7 100644
--- a/mysql-test/r/having.result
+++ b/mysql-test/r/having.result
@@ -141,3 +141,20 @@ SUM(a)
6
4
DROP TABLE t1;
+CREATE TABLE t1 (a int);
+INSERT INTO t1 VALUES (1), (2), (1), (3), (2), (1);
+SELECT a FROM t1 GROUP BY a HAVING a > 1;
+a
+2
+3
+SELECT a FROM t1 GROUP BY a HAVING 1 != 1 AND a > 1;
+a
+SELECT 0 AS x, a FROM t1 GROUP BY x,a HAVING x=1 AND a > 1;
+x a
+EXPLAIN SELECT a FROM t1 GROUP BY a HAVING 1 != 1 AND a > 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Impossible HAVING
+EXPLAIN SELECT 0 AS x, a FROM t1 GROUP BY x,a HAVING x=1 AND a > 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Impossible HAVING
+DROP table t1;
diff --git a/mysql-test/t/having.test b/mysql-test/t/having.test
index 3dd9ace6a1b..8b39e3bd454 100644
--- a/mysql-test/t/having.test
+++ b/mysql-test/t/having.test
@@ -135,4 +135,20 @@ SELECT SUM(a) FROM t1 GROUP BY a HAVING SUM(a);
DROP TABLE t1;
+#
+# Bug #14927: HAVING clause containing constant false conjunct
+#
+
+CREATE TABLE t1 (a int);
+INSERT INTO t1 VALUES (1), (2), (1), (3), (2), (1);
+
+SELECT a FROM t1 GROUP BY a HAVING a > 1;
+SELECT a FROM t1 GROUP BY a HAVING 1 != 1 AND a > 1;
+SELECT 0 AS x, a FROM t1 GROUP BY x,a HAVING x=1 AND a > 1;
+
+EXPLAIN SELECT a FROM t1 GROUP BY a HAVING 1 != 1 AND a > 1;
+EXPLAIN SELECT 0 AS x, a FROM t1 GROUP BY x,a HAVING x=1 AND a > 1;
+
+DROP table t1;
+
# End of 4.1 tests
diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc
index 16641ad6dd5..7348816ea27 100644
--- a/sql/sql_lex.cc
+++ b/sql/sql_lex.cc
@@ -1074,6 +1074,7 @@ void st_select_lex::init_query()
item_list.empty();
join= 0;
where= 0;
+ having= 0;
olap= UNSPECIFIED_OLAP_TYPE;
having_fix_field= 0;
resolve_mode= NOMATTER_MODE;
@@ -1081,6 +1082,7 @@ void st_select_lex::init_query()
ref_pointer_array= 0;
select_n_having_items= 0;
prep_where= 0;
+ prep_having= 0;
subquery_in_having= explicit_limit= 0;
parsing_place= NO_MATTER;
is_item_list_lookup= 0;
diff --git a/sql/sql_lex.h b/sql/sql_lex.h
index bd79a194122..35f02db6cf9 100644
--- a/sql/sql_lex.h
+++ b/sql/sql_lex.h
@@ -422,6 +422,7 @@ public:
char *db, *db1, *table1, *db2, *table2; /* For outer join using .. */
Item *where, *having; /* WHERE & HAVING clauses */
Item *prep_where; /* saved WHERE clause for prepared statement processing */
+ Item *prep_having;/* saved HAVING clause for prepared statement processing */
enum olap_type olap;
SQL_LIST table_list, group_list; /* FROM & GROUP BY clauses */
List<Item> item_list; /* list of fields & expressions */
diff --git a/sql/sql_prepare.cc b/sql/sql_prepare.cc
index 741d84eab44..2d9e80df63c 100644
--- a/sql/sql_prepare.cc
+++ b/sql/sql_prepare.cc
@@ -1667,10 +1667,11 @@ int mysql_stmt_prepare(THD *thd, char *packet, uint packet_length,
for (; sl; sl= sl->next_select_in_list())
{
/*
- Save WHERE clause pointers, because they may be changed
+ Save WHERE, HAVING clause pointers, because they may be changed
during query optimisation.
*/
sl->prep_where= sl->where;
+ sl->prep_having= sl->having;
/*
Switch off a temporary flag that prevents evaluation of
subqueries in statement prepare.
@@ -1696,13 +1697,18 @@ static void reset_stmt_for_execute(Prepared_statement *stmt)
/* remove option which was put by mysql_explain_union() */
sl->options&= ~SELECT_DESCRIBE;
/*
- Copy WHERE clause pointers to avoid damaging they by optimisation
+ Copy WHERE, HAVING clause pointers to avoid damaging they by optimisation
*/
if (sl->prep_where)
{
sl->where= sl->prep_where->copy_andor_structure(thd);
sl->where->cleanup();
}
+ if (sl->prep_having)
+ {
+ sl->having= sl->prep_having->copy_andor_structure(thd);
+ sl->having->cleanup();
+ }
DBUG_ASSERT(sl->join == 0);
ORDER *order;
/* Fix GROUP list */
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index 4995a164226..57fb9738612 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -501,12 +501,24 @@ JOIN::optimize()
DBUG_RETURN(1);
}
- if (cond_value == Item::COND_FALSE ||
- (!unit->select_limit_cnt && !(select_options & OPTION_FOUND_ROWS)))
- { /* Impossible cond */
- zero_result_cause= "Impossible WHERE";
- error= 0;
- DBUG_RETURN(0);
+ {
+ Item::cond_result having_value;
+ having= optimize_cond(thd, having, &having_value);
+ if (thd->net.report_error)
+ {
+ error= 1;
+ DBUG_PRINT("error",("Error from optimize_cond"));
+ DBUG_RETURN(1);
+ }
+
+ if (cond_value == Item::COND_FALSE || having_value == Item::COND_FALSE ||
+ (!unit->select_limit_cnt && !(select_options & OPTION_FOUND_ROWS)))
+ { /* Impossible cond */
+ zero_result_cause= having_value == Item::COND_FALSE ?
+ "Impossible HAVING" : "Impossible WHERE";
+ error= 0;
+ DBUG_RETURN(0);
+ }
}
/* Optimize count(*), min() and max() */
@@ -4612,10 +4624,8 @@ optimize_cond(THD *thd, COND *conds, Item::cond_result *cond_value)
DBUG_EXECUTE("info", print_where(conds, "after remove"););
}
else
- {
*cond_value= Item::COND_TRUE;
- select->prep_where= 0;
- }
+
DBUG_RETURN(conds);
}