Skip to content

Commit db5b9ce

Browse files
author
Chaithra Gopalareddy
committed
Bug#36313072: mysqld crash in Item_ref::real_item
While resolving an expression in QUALIFY clause, if the expression is resolved against a field in a table or a merged view, it is added to the select expression list as a hidden item. In the case when the resolver later reduces the QUALIFY clause to an always false or an always true condition, it removes the expressions present in the QUALIFY clause and replaces them with a TRUE or FALSE condition. Care is taken to not remove these expressions when there are still references to these objects. However, for the case where the hidden item is added to the select expression list, reference count is not incremented correctly resulting in removal of the expressions. This leads to problems later when the added hidden item is looked at. Solution is to increment the reference count when the hidden item is added to the select expression list. We already increment the ref count once, which accounts for the reference from Item_ref object we are trying to resolve. Now we account for the presence in the select expression list. Change-Id: Ife3e51f1f38fe030ab2381c2ef7354d033dff5bf
1 parent 16bc3aa commit db5b9ce

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

mysql-test/r/qualify_hypergraph.result

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,3 +555,10 @@ GROUP BY id, x WITH ROLLUP WINDOW w AS (ORDER BY id) QUALIFY x <> 1;
555555
id ROW_NUMBER() OVER w
556556
5 10
557557
DROP TABLE t;
558+
#
559+
# Bug#36313072: mysqld crash in Item_ref::real_item
560+
#
561+
CREATE TABLE t1 (f1 INTEGER);
562+
SELECT NTILE(1) OVER() FROM (SELECT * FROM t1) as dt QUALIFY f1 AND FALSE;
563+
NTILE(1) OVER()
564+
DROP TABLE t1;

mysql-test/t/qualify_hypergraph.test

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,3 +377,13 @@ SELECT id, ROW_NUMBER() OVER w FROM (SELECT * FROM t) AS dt
377377
GROUP BY id, x WITH ROLLUP WINDOW w AS (ORDER BY id) QUALIFY x <> 1;
378378

379379
DROP TABLE t;
380+
381+
--echo #
382+
--echo # Bug#36313072: mysqld crash in Item_ref::real_item
383+
--echo #
384+
385+
CREATE TABLE t1 (f1 INTEGER);
386+
387+
SELECT NTILE(1) OVER() FROM (SELECT * FROM t1) as dt QUALIFY f1 AND FALSE;
388+
389+
DROP TABLE t1;

sql/item.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8325,12 +8325,17 @@ bool Item_ref::fix_fields(THD *thd, Item **reference) {
83258325
// Add the view reference to the select expression list as hidden
83268326
// item.
83278327
m_ref_item = qb->add_hidden_item(*reference);
8328+
// Increment the reference count as the expression is now part
8329+
// of the select list. The call to link_referenced_item()
8330+
// later will account for the reference from this Item_ref object.
8331+
(*reference)->increment_ref_count();
83288332
*reference = this;
83298333
} else {
83308334
Item_field *fld = new Item_field(
83318335
thd, context, from_field->table->pos_in_table_list, from_field);
83328336
if (fld == nullptr) return true;
83338337
m_ref_item = qb->add_hidden_item(fld);
8338+
fld->increment_ref_count();
83348339
}
83358340
}
83368341
}

0 commit comments

Comments
 (0)