diff --git a/mysql-test/r/win.result b/mysql-test/r/win.result index c6707bd51bc..14628cd0d44 100644 --- a/mysql-test/r/win.result +++ b/mysql-test/r/win.result @@ -3173,3 +3173,15 @@ Nth_value(i,1) OVER() 1 1 DROP TABLE t1; +# +# A regression after MDEV-13351: +# MDEV-13374 : Server crashes in first_linear_tab / st_select_lex::set_explain_type +# upon UNION with aggregate function +# +CREATE TABLE t1 (i INT) ENGINE=MyISAM; +INSERT INTO t1 VALUES (1),(2); +SELECT i AS fld FROM t1 UNION SELECT COUNT(*) AS fld FROM t1; +fld +1 +2 +DROP TABLE t1; diff --git a/mysql-test/t/win.test b/mysql-test/t/win.test index 77ca755378d..3dedc1227fd 100644 --- a/mysql-test/t/win.test +++ b/mysql-test/t/win.test @@ -1954,3 +1954,14 @@ UNION ALL ; DROP TABLE t1; +--echo # +--echo # A regression after MDEV-13351: +--echo # MDEV-13374 : Server crashes in first_linear_tab / st_select_lex::set_explain_type +--echo # upon UNION with aggregate function +--echo # + +CREATE TABLE t1 (i INT) ENGINE=MyISAM; +INSERT INTO t1 VALUES (1),(2); +SELECT i AS fld FROM t1 UNION SELECT COUNT(*) AS fld FROM t1; +DROP TABLE t1; + diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 90f5e11dd16..ba3760dd948 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -8627,7 +8627,7 @@ JOIN_TAB *first_top_level_tab(JOIN *join, enum enum_with_const_tables const_tbls JOIN_TAB *tab= join->join_tab; if (const_tbls == WITHOUT_CONST_TABLES) { - if (join->const_tables == join->table_count) + if (join->const_tables == join->table_count || !tab) return NULL; tab += join->const_tables; } @@ -8650,6 +8650,10 @@ JOIN_TAB *first_linear_tab(JOIN *join, enum enum_with_const_tables const_tbls) { JOIN_TAB *first= join->join_tab; + + if (!first) + return NULL; + if (const_tbls == WITHOUT_CONST_TABLES) first+= join->const_tables; @@ -8736,7 +8740,7 @@ JOIN_TAB *first_depth_first_tab(JOIN* join) { JOIN_TAB* tab; /* This means we're starting the enumeration */ - if (join->const_tables == join->top_join_tab_count) + if (join->const_tables == join->top_join_tab_count || !join->join_tab) return NULL; tab= join->join_tab + join->const_tables;