revision-id: c776cad5f8ecf2675510deeb55724d7255a52503 (mariadb-10.4.11-267-gc776cad5f8e) parent(s): cc0dca366357651ddb549e31a12b1ecd39c7380e author: Varun Gupta committer: Varun Gupta timestamp: 2020-07-08 14:58:17 +0530 message: MDEV-22702: Assertion `!field->is_null()' failed in my_decimal::my_decimal With implicit grouping with window functions, we need to make sure that all the fields inside the window functions are nullable as any non-aggregated field can produce a NULL value. --- mysql-test/main/win.result | 14 ++++++++++++++ mysql-test/main/win.test | 11 +++++++++++ sql/sql_lex.h | 2 ++ sql/sql_select.cc | 12 ++++++++++++ 4 files changed, 39 insertions(+) diff --git a/mysql-test/main/win.result b/mysql-test/main/win.result index 6ef3a10966a..e70e982807c 100644 --- a/mysql-test/main/win.result +++ b/mysql-test/main/win.result @@ -3856,3 +3856,17 @@ row_number() OVER (order by a) 2 3 drop table t1; +# +# MDEV-22702: Assertion `!field->is_null()' failed in my_decimal::my_decimal +# +CREATE TABLE t1(a INT, b DECIMAL(10, 0) NOT NULL); +SELECT a, bit_or(min(a)) OVER (ORDER BY b) FROM t1; +a bit_or(min(a)) OVER (ORDER BY b) +NULL 0 +# No implicit grouping here +SELECT a, bit_or(a) OVER (ORDER BY b) FROM t1; +a bit_or(a) OVER (ORDER BY b) +SELECT a, sum(a), bit_or(a) OVER (ORDER BY b) FROM t1; +a sum(a) bit_or(a) OVER (ORDER BY b) +NULL NULL 0 +DROP TABLE t1; diff --git a/mysql-test/main/win.test b/mysql-test/main/win.test index 824c5f9fa56..b5d00cd80da 100644 --- a/mysql-test/main/win.test +++ b/mysql-test/main/win.test @@ -2512,3 +2512,14 @@ create table t1 (a int); insert into t1 values (1),(2),(3); SELECT row_number() OVER (order by a) FROM t1 order by NAME_CONST('myname',NULL); drop table t1; + +--echo # +--echo # MDEV-22702: Assertion `!field->is_null()' failed in my_decimal::my_decimal +--echo # + +CREATE TABLE t1(a INT, b DECIMAL(10, 0) NOT NULL); +SELECT a, bit_or(min(a)) OVER (ORDER BY b) FROM t1; +--echo # No implicit grouping here +SELECT a, bit_or(a) OVER (ORDER BY b) FROM t1; +SELECT a, sum(a), bit_or(a) OVER (ORDER BY b) FROM t1; +DROP TABLE t1; diff --git a/sql/sql_lex.h b/sql/sql_lex.h index 0983cea44d0..16a0b3b08a6 100644 --- a/sql/sql_lex.h +++ b/sql/sql_lex.h @@ -1510,6 +1510,8 @@ class st_select_lex: public st_select_lex_node } bool have_window_funcs() const { return (window_funcs.elements !=0); } + uint32 get_number_of_window_funcs() const + { return (uint32)window_funcs.elements; } ORDER *find_common_window_func_partition_fields(THD *thd); bool cond_pushdown_is_allowed() const diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 0ca5ab23288..05463efe9a5 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -1201,6 +1201,18 @@ JOIN::prepare(TABLE_LIST *tables_init, break; } } + /* + If the query has a window function with an aggregate function, + then also we have a mix of elements with and without grouping. + Window function can be in the ORDER BY clause too so the check + is made separately. + Window function is inherited from Item_sum so each window function is + also registered as a sum item, so need to check that we have an + explicit aggregate function also in the query. + */ + if (select_lex->have_window_funcs() && + select_lex->get_number_of_window_funcs() < select_lex->n_sum_items) + mixed_implicit_grouping= true; } table_count= select_lex->leaf_tables.elements;