Skip to content

Commit e5634b4

Browse files
committed
Bug#35785452: Table value constructor in subquery: Assertion
`!sl->order_list.first' failed. Redundant ORDER BY clauses in table value constructors are not removed during resolving. This could lead to assertions when transforming IN subqueries or quantified comparisons, as the code expects redundant ORDER BY clauses to have been removed before the transformation is performed. Fixed by adding a call to remove_redundant_subquery_clauses() while resolving table value constructors in subqueries. Change-Id: Ia8e33cf0d8dcacc62ec53a1a4f6cbb5c9a535f21
1 parent 8ff9e9d commit e5634b4

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

mysql-test/r/table_value_constructor.result

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,3 +770,14 @@ SELECT * FROM t WHERE a IN (VALUES ROW(5), ROW(55), ROW(NULL));
770770
a b
771771
5 6
772772
DROP TABLE t;
773+
#
774+
# Bug#35785452: Table value constructor in subquery:
775+
# Assertion `!sl->order_list.first' failed.
776+
#
777+
CREATE TABLE t(x INT);
778+
INSERT INTO t VALUES (1), (2), (3), (4);
779+
SELECT * FROM t WHERE x >= ALL (VALUES ROW(2), ROW(3) ORDER BY column_0);
780+
x
781+
3
782+
4
783+
DROP TABLE t;

mysql-test/t/table_value_constructor.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,3 +557,14 @@ ANALYZE TABLE t;
557557
--eval EXPLAIN FORMAT=TREE $query
558558
--eval $query
559559
DROP TABLE t;
560+
561+
--echo #
562+
--echo # Bug#35785452: Table value constructor in subquery:
563+
--echo # Assertion `!sl->order_list.first' failed.
564+
--echo #
565+
566+
CREATE TABLE t(x INT);
567+
INSERT INTO t VALUES (1), (2), (3), (4);
568+
--sorted_result
569+
SELECT * FROM t WHERE x >= ALL (VALUES ROW(2), ROW(3) ORDER BY column_0);
570+
DROP TABLE t;

sql/sql_lex.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2242,8 +2242,7 @@ class Query_block : public Query_term {
22422242
bool setup_group(THD *thd);
22432243
void fix_after_pullout(Query_block *parent_query_block,
22442244
Query_block *removed_query_block);
2245-
bool remove_redundant_subquery_clauses(THD *thd,
2246-
int hidden_group_field_count);
2245+
bool remove_redundant_subquery_clauses(THD *thd);
22472246
void repoint_contexts_of_join_nests(mem_root_deque<Table_ref *> join_list);
22482247
bool empty_order_list(Query_block *sl);
22492248
bool setup_join_cond(THD *thd, mem_root_deque<Table_ref *> *tables,

sql/sql_resolver.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,7 @@ bool Query_block::prepare(THD *thd, mem_root_deque<Item *> *insert_field_list) {
392392
if (unit->item && // 1)
393393
!thd->lex->is_view_context_analysis()) // 2)
394394
{
395-
if (remove_redundant_subquery_clauses(thd, hidden_group_field_count))
396-
return true;
395+
if (remove_redundant_subquery_clauses(thd)) return true;
397396
}
398397

399398
/*
@@ -705,6 +704,11 @@ bool Query_block::prepare_values(THD *thd) {
705704

706705
if (resolve_limits(thd)) return true;
707706

707+
// If this is a subquery, remove redundant clauses (ORDER BY in particular).
708+
if (unit->item != nullptr && !thd->lex->is_view_context_analysis()) {
709+
if (remove_redundant_subquery_clauses(thd)) return true;
710+
}
711+
708712
return false;
709713
}
710714

@@ -3834,13 +3838,10 @@ void Query_block::merge_contexts(Query_block *inner) {
38343838
for regular queries and on first execution of SP/PS
38353839
38363840
@param thd thread handler
3837-
@param hidden_group_field_count Number of hidden group fields added
3838-
by setup_group().
38393841
@return true on error
38403842
*/
38413843

3842-
bool Query_block::remove_redundant_subquery_clauses(
3843-
THD *thd, int hidden_group_field_count) {
3844+
bool Query_block::remove_redundant_subquery_clauses(THD *thd) {
38443845
Item_subselect *subq_predicate = master_query_expression()->item;
38453846
enum change {
38463847
REMOVE_NONE = 0,

0 commit comments

Comments
 (0)