Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MDEV-32866 Debug assertion failure in Item_subselect::val_bool() #3708

Open
wants to merge 2 commits into
base: 10.5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions mysql-test/main/derived_cond_pushdown.result
Original file line number Diff line number Diff line change
Expand Up @@ -15567,7 +15567,6 @@ EXPLAIN
{
"query_block": {
"select_id": 1,
"const_condition": "1",
"table": {
"table_name": "t1",
"access_type": "ALL",
Expand Down Expand Up @@ -16751,7 +16750,6 @@ EXPLAIN
"lateral": 1,
"query_block": {
"select_id": 3,
"const_condition": "1",
"outer_ref_condition": "t4.c is not null",
"table": {
"table_name": "t3",
Expand Down
1 change: 0 additions & 1 deletion mysql-test/main/derived_split_innodb.result
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,6 @@ ANALYZE
"select_id": 1,
"r_loops": 1,
"r_total_time_ms": "REPLACED",
"const_condition": "1",
"table": {
"table_name": "t1",
"access_type": "ALL",
Expand Down
3 changes: 0 additions & 3 deletions mysql-test/main/explain_json.result
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,6 @@ EXPLAIN
{
"query_block": {
"select_id": 1,
"const_condition": "1",
"table": {
"table_name": "t1",
"access_type": "ALL",
Expand Down Expand Up @@ -1252,7 +1251,6 @@ EXPLAIN
{
"query_block": {
"select_id": 1,
"const_condition": "1",
"table": {
"table_name": "t2",
"access_type": "const",
Expand All @@ -1277,7 +1275,6 @@ ANALYZE
"select_id": 1,
"r_loops": 1,
"r_total_time_ms": "REPLACED",
"const_condition": "1",
"table": {
"table_name": "t2",
"access_type": "const",
Expand Down
6 changes: 0 additions & 6 deletions mysql-test/main/having_cond_pushdown.result
Original file line number Diff line number Diff line change
Expand Up @@ -3202,7 +3202,6 @@ EXPLAIN
{
"query_block": {
"select_id": 1,
"const_condition": "1",
"table": {
"table_name": "t1",
"access_type": "ALL",
Expand Down Expand Up @@ -3544,7 +3543,6 @@ EXPLAIN
{
"query_block": {
"select_id": 1,
"const_condition": "1 and 1",
"table": {
"table_name": "t1",
"access_type": "ALL",
Expand Down Expand Up @@ -3671,7 +3669,6 @@ EXPLAIN
{
"query_block": {
"select_id": 1,
"const_condition": "1",
"table": {
"table_name": "t1",
"access_type": "ALL",
Expand Down Expand Up @@ -3799,7 +3796,6 @@ EXPLAIN
{
"query_block": {
"select_id": 1,
"const_condition": "1",
"filesort": {
"sort_key": "t1.c",
"temporary_table": {
Expand Down Expand Up @@ -3867,7 +3863,6 @@ EXPLAIN
{
"query_block": {
"select_id": 1,
"const_condition": "1 and 1",
"table": {
"table_name": "t1",
"access_type": "ALL",
Expand Down Expand Up @@ -3985,7 +3980,6 @@ EXPLAIN
{
"query_block": {
"select_id": 1,
"const_condition": "1",
"table": {
"table_name": "t3",
"access_type": "ALL",
Expand Down
1 change: 0 additions & 1 deletion mysql-test/main/join_cache_debug.result
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ JSON
"select_id": 1,
"r_loops": 1,
"r_total_time_ms": "REPLACED",
"const_condition": "1",
"table": {
"table_name": "t1_t2",
"access_type": "ALL",
Expand Down
34 changes: 34 additions & 0 deletions mysql-test/main/subselect4.result
Original file line number Diff line number Diff line change
Expand Up @@ -3332,3 +3332,37 @@ Variable_name Value
Handler_read_rnd_next 0
drop table t1,t2,t3;
# End of 10.4 tests
#
# MDEV-32866: Debug assertion failure in Item_subselect::val_bool()
#
CREATE TABLE t1(a INT);
INSERT INTO t1(a) VALUES (1);
SELECT a FROM t1 WHERE (SELECT a,a) IN (SELECT a,a);
a
1
SELECT a FROM t1 WHERE (SELECT a,a UNION SELECT 1,a FROM t1) IN (SELECT 1,1);
a
1
SELECT a FROM t1 WHERE (SELECT a,a UNION SELECT 1,a FROM t1) IN (SELECT a,a);
a
1
DROP TABLE t1;
# Test case from duplicating MDEV-22375
CREATE TABLE t1 (a INT KEY,b CHAR(1));
INSERT INTO t1 VALUES (0,'1');
SELECT a FROM t1 WHERE '' IN (SELECT 'x' LIKE a HAVING a LIKE a);
a
0
Warnings:
Warning 1292 Truncated incorrect DECIMAL value: ''
DROP TABLE t1;
#
# MDEV-35654: Re-evaluation of constant subquery triggers debug assertion
#
CREATE TABLE t1 (a INT);
INSERT INTO t1 VALUES (
a IN (SELECT a IN (
SELECT 1 FROM (SELECT 1 AS a) AS dt WHERE a IN (SELECT a HAVING a=0)
)));
DROP TABLE t1;
# End of 10.5 tests
29 changes: 29 additions & 0 deletions mysql-test/main/subselect4.test
Original file line number Diff line number Diff line change
Expand Up @@ -2684,3 +2684,32 @@ show status like 'Handler_read_rnd_next%';
drop table t1,t2,t3;

--echo # End of 10.4 tests

--echo #
--echo # MDEV-32866: Debug assertion failure in Item_subselect::val_bool()
--echo #
CREATE TABLE t1(a INT);
INSERT INTO t1(a) VALUES (1);

SELECT a FROM t1 WHERE (SELECT a,a) IN (SELECT a,a);
SELECT a FROM t1 WHERE (SELECT a,a UNION SELECT 1,a FROM t1) IN (SELECT 1,1);
SELECT a FROM t1 WHERE (SELECT a,a UNION SELECT 1,a FROM t1) IN (SELECT a,a);
DROP TABLE t1;

--echo # Test case from duplicating MDEV-22375
CREATE TABLE t1 (a INT KEY,b CHAR(1));
INSERT INTO t1 VALUES (0,'1');
SELECT a FROM t1 WHERE '' IN (SELECT 'x' LIKE a HAVING a LIKE a);
DROP TABLE t1;

--echo #
--echo # MDEV-35654: Re-evaluation of constant subquery triggers debug assertion
--echo #
CREATE TABLE t1 (a INT);
INSERT INTO t1 VALUES (
a IN (SELECT a IN (
SELECT 1 FROM (SELECT 1 AS a) AS dt WHERE a IN (SELECT a HAVING a=0)
)));
DROP TABLE t1;

--echo # End of 10.5 tests
1 change: 0 additions & 1 deletion mysql-test/main/subselect_mat_analyze_json.result
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,6 @@ ANALYZE
"select_id": 1,
"r_loops": 1,
"r_total_time_ms": "REPLACED",
"const_condition": "1",
"table": {
"table_name": "t1",
"access_type": "ALL",
Expand Down
2 changes: 2 additions & 0 deletions sql/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -2197,6 +2197,8 @@ class Item: public Value_source,
If there is some, sets a bit for this key in the proper key map.
*/
virtual bool check_index_dependence(void *arg) { return 0; }
/* Mark all applicable items in the tree as constant */
virtual bool make_const_processor(void *arg) { return 0; }
/*============== End of Item processor list ======================*/

/*
Expand Down
7 changes: 7 additions & 0 deletions sql/item_subselect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,13 @@ bool Item_subselect::eliminate_subselect_processor(void *arg)
}


bool Item_subselect::make_const_processor(void *arg)
{
make_const();
return FALSE;
}


bool Item_subselect::mark_as_dependent(THD *thd, st_select_lex *select,
Item *item)
{
Expand Down
6 changes: 4 additions & 2 deletions sql/item_subselect.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,9 @@ class Item_subselect :public Item_result_field,
void recalc_used_tables(st_select_lex *new_parent, bool after_pullout);
virtual bool exec();
/*
If subquery optimization or execution determines that the subquery has
an empty result, mark the subquery predicate as a constant value.
Called if we've managed to calculate the value of this subquery
during optimization or execution, hence it can be considered constant
at all subsequent steps.
*/
void make_const()
{
Expand Down Expand Up @@ -262,6 +263,7 @@ class Item_subselect :public Item_result_field,
@retval FALSE otherwise
*/
bool is_expensive_processor(void *arg) override { return is_expensive(); }
bool make_const_processor(void *arg) override;
bool update_table_bitmaps_processor(void *arg) override;

/**
Expand Down
8 changes: 6 additions & 2 deletions sql/sql_select.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11819,6 +11819,7 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
{
trace_const_cond.add("evaluated", "false")
.add("cause", "expensive cond");
join->exec_const_cond= const_cond;
}
else
{
Expand All @@ -11827,16 +11828,19 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
Json_writer_array a(thd, "computing_condition");
const_cond_result= const_cond->val_int() != 0;
}
/*
Once we evaluated const_cond, we can mark all applicable items
in the tree as constant to avoid their re-evaluation
*/
const_cond->walk(&Item::make_const_processor, false, nullptr);
if (!const_cond_result)
{
DBUG_PRINT("info",("Found impossible WHERE condition"));
trace_const_cond.add("evaluated", "true")
.add("found", "impossible where");
join->exec_const_cond= NULL;
DBUG_RETURN(1);
}
}
join->exec_const_cond= const_cond;
}

if (join->table_count != join->const_tables)
Expand Down
6 changes: 3 additions & 3 deletions sql/sql_select.h
Original file line number Diff line number Diff line change
Expand Up @@ -1503,9 +1503,9 @@ class JOIN :public Sql_alloc
COND_EQUAL *cond_equal;
COND_EQUAL *having_equal;
/*
Constant codition computed during optimization, but evaluated during
join execution. Typically expensive conditions that should not be
evaluated at optimization time.
Constant part of conditions which is extracted during optimization, but
evaluated during join execution. Typically expensive conditions that
should not be evaluated at optimization time.
*/
Item *exec_const_cond;
/*
Expand Down