Skip to content

Commit ffdb764

Browse files
committed
Bug#37243446: Assertion: item->contextualized in Item::Item
In the patch for bug 36531144, we changed some constructors for subquery items so that we passed an improved definition text to their constructors. Unfortunately, the constructors with and without POS arguments are quite different. Those with a POS argument usually rely upon a contextualization function to add them to the THD chain of items, as well as set the contextualized flag. Thus, some special tests might assert because the contextualized was never set. The fix here is to add explicit function calls to set the flag. Change-Id: I6e836bac0beb322b1129d3edeeffc749637120e3
1 parent c73602f commit ffdb764

8 files changed

+68
-3
lines changed

mysql-test/r/subquery_bugs.result

+21
Original file line numberDiff line numberDiff line change
@@ -2459,3 +2459,24 @@ HAVING 1 IN (SELECT min(x.x) FROM (SELECT 1) AS w(w))
24592459
)
24602460
WHERE 1 = 2;
24612461
col
2462+
#
2463+
# Bug#37243446: Assertion: item->contextualized in Item::Item
2464+
#
2465+
CREATE TABLE t (c integer);
2466+
SELECT (SELECT t.c FROM t) AS f FROM t
2467+
UNION ALL
2468+
SELECT 1 FROM t;
2469+
f
2470+
SELECT c IN (SELECT t.c FROM t) AS f FROM t
2471+
UNION ALL
2472+
SELECT 1 FROM t;
2473+
f
2474+
SELECT c >ANY (SELECT t.c FROM t) AS f FROM t
2475+
UNION ALL
2476+
SELECT 1 FROM t;
2477+
f
2478+
SELECT EXISTS (SELECT t.c FROM t) AS f FROM t
2479+
UNION ALL
2480+
SELECT 1 FROM t;
2481+
f
2482+
DROP TABLE t;

mysql-test/t/subquery_bugs.test

+28
Original file line numberDiff line numberDiff line change
@@ -2075,3 +2075,31 @@ FROM (SELECT 1) AS y(y) JOIN (SELECT 1) AS z(z)
20752075
HAVING 1 IN (SELECT min(x.x) FROM (SELECT 1) AS w(w))
20762076
)
20772077
WHERE 1 = 2;
2078+
2079+
--echo #
2080+
--echo # Bug#37243446: Assertion: item->contextualized in Item::Item
2081+
--echo #
2082+
2083+
CREATE TABLE t (c integer);
2084+
2085+
# This crashes:
2086+
SELECT (SELECT t.c FROM t) AS f FROM t
2087+
UNION ALL
2088+
SELECT 1 FROM t;
2089+
2090+
# This works:
2091+
SELECT c IN (SELECT t.c FROM t) AS f FROM t
2092+
UNION ALL
2093+
SELECT 1 FROM t;
2094+
2095+
# This works, but does not set contextualized flag
2096+
SELECT c >ANY (SELECT t.c FROM t) AS f FROM t
2097+
UNION ALL
2098+
SELECT 1 FROM t;
2099+
2100+
# This crashes:
2101+
SELECT EXISTS (SELECT t.c FROM t) AS f FROM t
2102+
UNION ALL
2103+
SELECT 1 FROM t;
2104+
2105+
DROP TABLE t;

sql/item_subselect.cc

+3-1
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,9 @@ bool Item_subselect::fix_fields(THD *thd, Item **) {
597597

598598
assert(!fixed);
599599
assert(indexsubquery_engine == nullptr);
600-
600+
#ifndef NDEBUG
601+
assert(contextualized);
602+
#endif
601603
if (check_stack_overrun(thd, STACK_MIN_SIZE, (uchar *)&res)) return true;
602604

603605
if (!query_expr()->is_prepared() &&

sql/item_subselect.h

+10
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ class Item_subselect : public Item_result_field {
8383
public:
8484
Query_expression *query_expr() const { return m_query_expr; }
8585

86+
/**
87+
For Item_subselect constructor with POS parameter, the contextualized
88+
field must be transitioned explicitly.
89+
*/
90+
void set_contextualized() {
91+
#ifndef NDEBUG
92+
assert(!contextualized);
93+
contextualized = true;
94+
#endif // NDEBUG
95+
}
8696
/**
8797
If !=NO_PLAN_IDX: this Item is in the condition attached to the JOIN_TAB
8898
having this index in the parent JOIN.

sql/parse_tree_items.cc

+2
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ bool PTI_singlerow_subselect::do_itemize(Parse_context *pc, Item **res) {
329329
*res = new (pc->mem_root) Item_singlerow_subselect(m_pos, subselect->value());
330330
if (*res == nullptr) return true;
331331

332+
down_cast<Item_subselect *>(*res)->set_contextualized();
332333
pc->thd->add_item(*res);
333334
pc->select->n_scalar_subqueries++;
334335

@@ -340,6 +341,7 @@ bool PTI_exists_subselect::do_itemize(Parse_context *pc, Item **res) {
340341
*res = new (pc->mem_root) Item_exists_subselect(m_pos, subselect->value());
341342
if (*res == nullptr) return true;
342343

344+
down_cast<Item_subselect *>(*res)->set_contextualized();
343345
pc->thd->add_item(*res);
344346

345347
return false;

sql/parse_tree_node_base.h

+1
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ class Show_parse_tree {
230230
template <typename Context>
231231
class Parse_tree_node_tmpl {
232232
friend class Item; // for direct access to the "contextualized" field
233+
friend class Item_subselect;
233234

234235
Parse_tree_node_tmpl(const Parse_tree_node_tmpl &); // undefined
235236
void operator=(const Parse_tree_node_tmpl &); // undefined

sql/parser_yystype.h

-2
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ class PT_start_option_value_list_following_option_type;
125125
class PT_sub_partition;
126126
class PT_subpartition;
127127
class PT_subquery;
128-
class PT_subselect;
129128
class PT_table_constraint_def;
130129
class PT_table_element;
131130
class PT_table_reference;
@@ -426,7 +425,6 @@ union MY_SQL_PARSER_STYPE {
426425
} trg_characteristics;
427426
Index_hint *key_usage_element;
428427
List<Index_hint> *key_usage_list;
429-
PT_subselect *subselect;
430428
PT_item_list *item_list2;
431429
PT_order_expr *order_expr;
432430
PT_order_list *order_list;

sql/sql_parse.cc

+3
Original file line numberDiff line numberDiff line change
@@ -6672,11 +6672,13 @@ Item *all_any_subquery_creator(THD *thd, const POS &pos, Item *left_expr,
66726672
item = new (thd->mem_root) Item_in_subselect(pos, left_expr, query_block);
66736673
if (item == nullptr) return nullptr;
66746674

6675+
down_cast<Item_subselect *>(item)->set_contextualized();
66756676
thd->add_item(item);
66766677
} else if (cmp == &comp_ne_creator && all) { // <> ALL <=> NOT IN
66776678
item = new Item_in_subselect(pos, left_expr, query_block);
66786679
if (item == nullptr) return nullptr;
66796680

6681+
down_cast<Item_subselect *>(item)->set_contextualized();
66806682
thd->add_item(item);
66816683

66826684
Item *negated = item->truth_transformer(nullptr, Item::BOOL_NEGATED);
@@ -6687,6 +6689,7 @@ Item *all_any_subquery_creator(THD *thd, const POS &pos, Item *left_expr,
66876689
Item_allany_subselect(pos, left_expr, cmp, query_block, all);
66886690
if (it == nullptr) return nullptr;
66896691

6692+
it->set_contextualized();
66906693
thd->add_item(it);
66916694

66926695
if (all) { // ALL

0 commit comments

Comments
 (0)