diff --git a/mysql-test/main/subselect_nulls.result b/mysql-test/main/subselect_nulls.result index 9d3b7b2cfd508..e57b02a4ead6a 100644 --- a/mysql-test/main/subselect_nulls.result +++ b/mysql-test/main/subselect_nulls.result @@ -154,3 +154,50 @@ drop table t1, t2; # # End of 10.10 tests # +# +# MDEV-32868 SELECT NULL,NULL IN (SUBQUERY) returns 0 instead of NULL +# +create table t1 (a int); +insert into t1 values (1); +create table t2 (a int, b int); +insert into t2 values (null, null); +# Scalar subquery returning NULLs as left part of IN +# All NULLs on left, no match possible -> NULL (subquery has rows) +select (select null, null) in (select 1, 2 from t1) from t1; +(select null, null) in (select 1, 2 from t1) +NULL +# One NULL on left, other column matches -> NULL +select (select null, 2) in (select 1, 2 from t1) from t1; +(select null, 2) in (select 1, 2 from t1) +NULL +# One NULL on left, other column doesn't match -> FALSE +select (select null, 3) in (select 1, 2 from t1) from t1; +(select null, 3) in (select 1, 2 from t1) +0 +# Table with NULLs as left part +select (select * from t2) in (select 1, 2 from t1) from t1; +(select * from t2) in (select 1, 2 from t1) +NULL +# Exact match -> TRUE +select (select 1, 2) in (select 1, 2 from t1) from t1; +(select 1, 2) in (select 1, 2 from t1) +1 +# No match, no NULLs -> FALSE +select (select 3, 4) in (select 1, 2 from t1) from t1; +(select 3, 4) in (select 1, 2 from t1) +0 +# Right side has NULLs +create table t3 (a int, b int); +insert into t3 values (1, null), (null, 2); +# Left matches one column, right has NULL in other -> NULL +select (select 1, 2) in (select * from t3) from t1; +(select 1, 2) in (select * from t3) +NULL +# Both sides have NULLs -> NULL +select (select null, 2) in (select * from t3) from t1; +(select null, 2) in (select * from t3) +NULL +drop table t1, t2, t3; +# +# End of 10.11 tests +# diff --git a/mysql-test/main/subselect_nulls.test b/mysql-test/main/subselect_nulls.test index 68575eca9e73a..53b68b63fd656 100644 --- a/mysql-test/main/subselect_nulls.test +++ b/mysql-test/main/subselect_nulls.test @@ -127,3 +127,48 @@ drop table t1, t2; --echo # --echo # End of 10.10 tests --echo # + +--echo # +--echo # MDEV-32868 SELECT NULL,NULL IN (SUBQUERY) returns 0 instead of NULL +--echo # + +create table t1 (a int); +insert into t1 values (1); + +create table t2 (a int, b int); +insert into t2 values (null, null); + +--echo # Scalar subquery returning NULLs as left part of IN +--echo # All NULLs on left, no match possible -> NULL (subquery has rows) +select (select null, null) in (select 1, 2 from t1) from t1; + +--echo # One NULL on left, other column matches -> NULL +select (select null, 2) in (select 1, 2 from t1) from t1; + +--echo # One NULL on left, other column doesn't match -> FALSE +select (select null, 3) in (select 1, 2 from t1) from t1; + +--echo # Table with NULLs as left part +select (select * from t2) in (select 1, 2 from t1) from t1; + +--echo # Exact match -> TRUE +select (select 1, 2) in (select 1, 2 from t1) from t1; + +--echo # No match, no NULLs -> FALSE +select (select 3, 4) in (select 1, 2 from t1) from t1; + +--echo # Right side has NULLs +create table t3 (a int, b int); +insert into t3 values (1, null), (null, 2); + +--echo # Left matches one column, right has NULL in other -> NULL +select (select 1, 2) in (select * from t3) from t1; + +--echo # Both sides have NULLs -> NULL +select (select null, 2) in (select * from t3) from t1; + +drop table t1, t2, t3; + +--echo # +--echo # End of 10.11 tests +--echo # diff --git a/sql/sql_select.cc b/sql/sql_select.cc index a025b7484b541..12ce910fa84d3 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -13307,12 +13307,7 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond) { Json_writer_object trace_const_cond(thd); trace_const_cond.add("condition_on_constant_tables", const_cond); - if (const_cond->is_expensive()) - { - trace_const_cond.add("evaluated", "false") - .add("cause", "expensive cond"); - } - else + if (const_cond->can_eval_in_optimize()) { bool const_cond_result; { @@ -13328,6 +13323,11 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond) DBUG_RETURN(1); } } + else + { + trace_const_cond.add("evaluated", "false") + .add("cause", "expensive cond"); + } join->exec_const_cond= const_cond; }