[Commits] fbe35f63afb: MDEV-12575: Server crash in AGGR_OP::put_record or in JOIN_CACHE::free or Invalid write in JOIN::make_aggr_tables_info
revision-id: fbe35f63afb537f5f4eba10375c4b0b0e613d438 (mariadb-10.2.16-127-gfbe35f63afb) parent(s): 1372596f5e6e8b12a709e5eeacac2a2d6acd4ff1 author: Varun Gupta committer: Varun Gupta timestamp: 2018-09-17 20:48:16 +0530 message: MDEV-12575: Server crash in AGGR_OP::put_record or in JOIN_CACHE::free or Invalid write in JOIN::make_aggr_tables_info During the optimize state of a query, we come know that the result set would atmost contain one row, then for such a query we don't need to compute GROUP BY, ORDER BY and DISTINCT. --- mysql-test/r/distinct.result | 12 ++++++++++++ mysql-test/r/win.result | 31 +++++++++++++++++++++++++++++++ mysql-test/t/distinct.test | 14 ++++++++++++++ mysql-test/t/win.test | 33 +++++++++++++++++++++++++++++++++ sql/sql_select.cc | 11 +++++++++++ 5 files changed, 101 insertions(+) diff --git a/mysql-test/r/distinct.result b/mysql-test/r/distinct.result index e1bbf5adb79..2edb3f96d12 100644 --- a/mysql-test/r/distinct.result +++ b/mysql-test/r/distinct.result @@ -1049,4 +1049,16 @@ b1+'0' b2+'0' b3+'0' b4+'0' b5+'0' b6 +'0' 1 0 0 1 0 1 0 1 0 0 1 0 DROP TABLE t1; +# +# MDEV-12575: Server crash in AGGR_OP::put_record or in JOIN_CACHE::free +# or Invalid write in JOIN::make_aggr_tables_info +# +CREATE TABLE t1 (pk INT PRIMARY KEY); +INSERT INTO t1 VALUES (1),(2); +( SELECT DISTINCT 1 FROM t1 ORDER BY BENCHMARK(1, MIN(pk)) ) +UNION +( SELECT DISTINCT 1 FROM t1 ORDER BY BENCHMARK(1, MIN(pk)) ); +1 +1 +drop table t1; End of 5.5 tests diff --git a/mysql-test/r/win.result b/mysql-test/r/win.result index aef9b613433..e6802075266 100644 --- a/mysql-test/r/win.result +++ b/mysql-test/r/win.result @@ -3326,3 +3326,34 @@ SELECT DISTINCT BIT_AND(0) OVER (), MAX(1) FROM t1; BIT_AND(0) OVER () MAX(1) 0 1 drop table t1; +# +# MDEV-12575: Server crash in AGGR_OP::put_record or in JOIN_CACHE::free +# or Invalid write in JOIN::make_aggr_tables_info +# +SELECT DISTINCT BIT_OR(100) OVER () FROM dual +GROUP BY LEFT('2018-08-24', 100) +HAVING @A := 'qwerty'; +BIT_OR(100) OVER () +SELECT DISTINCT BIT_OR(100) OVER () FROM dual +GROUP BY LEFT('2018-08-24', 100) order by 1+2; +BIT_OR(100) OVER () +100 +CREATE TABLE t1 (i INT); +INSERT INTO t1 VALUES (1),(2); +SELECT * FROM ( +SELECT +ROW_NUMBER() OVER(), i, sum(i) +FROM t1 +WHERE 1=0 +limit 0 +) AS sq; +ROW_NUMBER() OVER() i sum(i) +SELECT * FROM ( +SELECT +ROW_NUMBER() OVER(), i, sum(i) +FROM t1 +WHERE 1=0 +GROUP BY i +) AS sq; +ROW_NUMBER() OVER() i sum(i) +drop table t1; diff --git a/mysql-test/t/distinct.test b/mysql-test/t/distinct.test index c11f8b501bc..5b49c2db728 100644 --- a/mysql-test/t/distinct.test +++ b/mysql-test/t/distinct.test @@ -798,4 +798,18 @@ CREATE TABLE t1 (b1 BIT, b2 BIT, b3 BIT, b4 BIT , b5 BIT, b6 BIT); INSERT INTO t1 VALUES (1,0,0,1,0,1),(0,1,0,0,1,0); SELECT DISTINCT b1+'0', b2+'0', b3+'0', b4+'0', b5+'0', b6 +'0' FROM t1; DROP TABLE t1; + +--echo # +--echo # MDEV-12575: Server crash in AGGR_OP::put_record or in JOIN_CACHE::free +--echo # or Invalid write in JOIN::make_aggr_tables_info +--echo # + +CREATE TABLE t1 (pk INT PRIMARY KEY); +INSERT INTO t1 VALUES (1),(2); + +( SELECT DISTINCT 1 FROM t1 ORDER BY BENCHMARK(1, MIN(pk)) ) +UNION +( SELECT DISTINCT 1 FROM t1 ORDER BY BENCHMARK(1, MIN(pk)) ); +drop table t1; + --echo End of 5.5 tests diff --git a/mysql-test/t/win.test b/mysql-test/t/win.test index 1617e85caf4..46345b65831 100644 --- a/mysql-test/t/win.test +++ b/mysql-test/t/win.test @@ -2092,3 +2092,36 @@ insert into t1 values (1),(2); SELECT DISTINCT row_number() OVER (), MAX(1) FROM t1; SELECT DISTINCT BIT_AND(0) OVER (), MAX(1) FROM t1; drop table t1; + +--echo # +--echo # MDEV-12575: Server crash in AGGR_OP::put_record or in JOIN_CACHE::free +--echo # or Invalid write in JOIN::make_aggr_tables_info +--echo # + +SELECT DISTINCT BIT_OR(100) OVER () FROM dual +GROUP BY LEFT('2018-08-24', 100) +HAVING @A := 'qwerty'; + +SELECT DISTINCT BIT_OR(100) OVER () FROM dual +GROUP BY LEFT('2018-08-24', 100) order by 1+2; + +CREATE TABLE t1 (i INT); +INSERT INTO t1 VALUES (1),(2); + +SELECT * FROM ( + SELECT + ROW_NUMBER() OVER(), i, sum(i) + FROM t1 + WHERE 1=0 + limit 0 +) AS sq; + +SELECT * FROM ( + SELECT + ROW_NUMBER() OVER(), i, sum(i) + FROM t1 + WHERE 1=0 + GROUP BY i +) AS sq; +drop table t1; + diff --git a/sql/sql_select.cc b/sql/sql_select.cc index efc13276424..9343fbd1bb1 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -2245,6 +2245,17 @@ JOIN::optimize_inner() if (!tables_list || !table_count) { choose_tableless_subquery_plan(); + + /* The output has atmost one row */ + if (group_list) + { + group_list= 0; + group_optimized_away= 1; + } + order=0; + simple_order=1; + select_distinct=0; + if (select_lex->have_window_funcs()) { if (!(join_tab= (JOIN_TAB*) thd->alloc(sizeof(JOIN_TAB))))
participants (1)
-
varunraiko1803@gmail.com