From d513a4ce74cb1932ca945a8708a6746175ddf745 Mon Sep 17 00:00:00 2001 From: Rex Date: Tue, 30 Apr 2024 10:39:20 +1100 Subject: [PATCH] MDEV-19520 Extend condition normalization to include 'NOT a' Having Item_func_not items in item trees breaks assumptions during the optimization phase about transformation possibilities in fix_fields(). Remove Item_func_not by extending normalization during parsing. Reviewed by Oleksandr Byelkin (sanja@mariadb.com) --- mysql-test/main/having_cond_pushdown.result | 12 ++++++++++++ mysql-test/main/having_cond_pushdown.test | 12 ++++++++++++ sql/sql_parse.cc | 16 ++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/mysql-test/main/having_cond_pushdown.result b/mysql-test/main/having_cond_pushdown.result index fea8f83f9a1..1afed563fcb 100644 --- a/mysql-test/main/having_cond_pushdown.result +++ b/mysql-test/main/having_cond_pushdown.result @@ -4969,4 +4969,16 @@ SELECT a,b FROM t1 GROUP BY a,b HAVING a = (b IS NULL); a b 0 11 DROP TABLE t1; +# +# MDEV-19520 Extend condition normalization to include 'NOT a' +# having Item_func_not in item tree breaks assumptions during the +# optimization phase about transformation possibilities in fix_fields(). +# Remove Item_func_not by extending normalization during parsing. +# +CREATE TABLE t1 (a INT); +INSERT INTO t1 VALUES (0),(1); +SELECT a FROM t1 GROUP BY a HAVING NOT a; +a +0 +DROP TABLE t1; End of 10.4 tests diff --git a/mysql-test/main/having_cond_pushdown.test b/mysql-test/main/having_cond_pushdown.test index 6d42b4ffefe..57f70152375 100644 --- a/mysql-test/main/having_cond_pushdown.test +++ b/mysql-test/main/having_cond_pushdown.test @@ -1485,4 +1485,16 @@ SELECT a,b FROM t1 GROUP BY a,b HAVING a = (b IS NULL); DROP TABLE t1; +--echo # +--echo # MDEV-19520 Extend condition normalization to include 'NOT a' +--echo # having Item_func_not in item tree breaks assumptions during the +--echo # optimization phase about transformation possibilities in fix_fields(). +--echo # Remove Item_func_not by extending normalization during parsing. +--echo # + +CREATE TABLE t1 (a INT); +INSERT INTO t1 VALUES (0),(1); +SELECT a FROM t1 GROUP BY a HAVING NOT a; +DROP TABLE t1; + --echo End of 10.4 tests diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index f3b7cb75c81..561aca11e20 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -9234,6 +9234,7 @@ push_new_name_resolution_context(THD *thd, /** Fix condition which contains only field (f turns to f <> 0 ) + or only contains the function NOT field (not f turns to f == 0) @param cond The condition to fix @@ -9249,6 +9250,21 @@ Item *normalize_cond(THD *thd, Item *cond) { cond= new (thd->mem_root) Item_func_ne(thd, cond, new (thd->mem_root) Item_int(thd, 0)); } + else + { + if (type == Item::FUNC_ITEM) + { + Item_func *func_item= (Item_func *)cond; + if (func_item->functype() == Item_func::NOT_FUNC) + { + Item *arg= func_item->arguments()[0]; + if (arg->type() == Item::FIELD_ITEM || + arg->type() == Item::REF_ITEM) + cond= new (thd->mem_root) Item_func_eq(thd, arg, + new (thd->mem_root) Item_int(thd, 0)); + } + } + } } return cond; }