Skip to content

Commit 28ec5fd

Browse files
Chaithra Gopalareddydahlerlend
authored andcommitted
Bug#32863713: RECENT REGRESSION: CTE CRASH IN
QUERY_BLOCK::MASTER_QUERY_EXPRESSION To clone a derived table expression, we re-parse it. To re-parse the expression, a new query block is created and used temporarily. However, as part of an earlier fix, the query block to be used for parsing was set to the query block provided in the context (derived table query block or in case of a view ref, the merged derived table query block). But, the query expression to use was still the query expression created temporarily for parsing. Because of the mismatch between the query block and the query expression that it belongs to, parser tries to access an invalid query expression resulting in server exit. We now set the query expression along with the query block to parse the derived table expression. Change-Id: If65b5b2d359d1c35c7e3984f301cb089e54e65d1
1 parent cd6aeb5 commit 28ec5fd

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

mysql-test/r/derived_condition_pushdown.result

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,3 +1157,22 @@ SELECT g, ROW_NUMBER() OVER (PARTITION BY g) AS r FROM t1
11571157
) w ON w.g=t1.g AND w.r=1 WHERE w.g IS NULL;
11581158
g
11591159
DROP TABLE t1;
1160+
#
1161+
# BUG#32863713: CTE CRASH IN QUERY_BLOCK::MASTER_QUERY_EXPRESSION
1162+
#
1163+
CREATE TABLE t(f1 INTEGER);
1164+
EXPLAIN SELECT a1, a2
1165+
FROM (SELECT MAX(2) AS a1 FROM t) as dt1,
1166+
(SELECT @a AS a2 FROM t) as dt2
1167+
WHERE dt1.a1 <= dt2.a2;
1168+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1169+
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL NULL no matching row in const table
1170+
2 DERIVED t NULL ALL NULL NULL NULL NULL 1 100.00 NULL
1171+
Warnings:
1172+
Note 1003 /* select#1 */ select NULL AS `a1`,(@`a`) AS `a2` from (/* select#2 */ select max(2) AS `a1` from `test`.`t` having (max(2) <= <cache>((@`a`)))) `dt1` join `test`.`t`
1173+
SELECT a1, a2
1174+
FROM (SELECT MAX(f1) AS a1 FROM t) as dt1,
1175+
(SELECT @a AS a2 FROM t) as dt2
1176+
WHERE dt1.a1 <= dt2.a2;
1177+
a1 a2
1178+
DROP TABLE t;

mysql-test/t/derived_condition_pushdown.test

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,3 +680,22 @@ SELECT g, ROW_NUMBER() OVER (PARTITION BY g) AS r FROM t1
680680
) w ON w.g=t1.g AND w.r=1 WHERE w.g IS NULL;
681681

682682
DROP TABLE t1;
683+
684+
--echo #
685+
--echo # BUG#32863713: CTE CRASH IN QUERY_BLOCK::MASTER_QUERY_EXPRESSION
686+
--echo #
687+
688+
CREATE TABLE t(f1 INTEGER);
689+
# Checking if condition pushdown to a derived table does not
690+
# crash the server when one of derived table is merged.
691+
EXPLAIN SELECT a1, a2
692+
FROM (SELECT MAX(2) AS a1 FROM t) as dt1,
693+
(SELECT @a AS a2 FROM t) as dt2
694+
WHERE dt1.a1 <= dt2.a2;
695+
696+
SELECT a1, a2
697+
FROM (SELECT MAX(f1) AS a1 FROM t) as dt1,
698+
(SELECT @a AS a2 FROM t) as dt2
699+
WHERE dt1.a1 <= dt2.a2;
700+
701+
DROP TABLE t;

sql/sql_derived.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ Item *TABLE_LIST::get_clone_for_derived_expr(THD *thd, Item *item,
565565
// Set the correct query block to parse the item. In some cases, like
566566
// fulltext functions, parser needs to add them to ftfunc_list of the
567567
// query block.
568+
thd->lex->unit = context->query_block->master_query_expression();
568569
thd->lex->set_current_query_block(context->query_block);
569570
bool result = parse_sql(thd, &parser_state, nullptr);
570571

0 commit comments

Comments
 (0)