revision-id: 5fdcd669fa78962ccafc9f7ea843878118ad3d0d (mariadb-10.5.2-545-g5fdcd669fa7) parent(s): c2a63ac526bf4cd269def30a3d55ff29fdba8f86 author: Sergei Petrunia committer: Sergei Petrunia timestamp: 2021-04-07 19:26:58 +0300 message: MDEV-25362: Incorrect name resolution for subqueries in ON expressions When adding a subquery, the outer Name_resolution_context should be the top context on the context stack, not the WHERE-clause level Name_resolution_context of the parent select. This makes a difference when the subquery is in the ON expression: it should follow the name resolution rules of the ON expression, not of the WHERE. --- mysql-test/main/join_outer.result | 28 ++++++++++++++++++++++++++++ mysql-test/main/join_outer.test | 33 +++++++++++++++++++++++++++++++++ mysql-test/main/join_outer_jcl6.result | 28 ++++++++++++++++++++++++++++ sql/sql_lex.cc | 2 +- 4 files changed, 90 insertions(+), 1 deletion(-) diff --git a/mysql-test/main/join_outer.result b/mysql-test/main/join_outer.result index e882973043e..9f5ed79154b 100644 --- a/mysql-test/main/join_outer.result +++ b/mysql-test/main/join_outer.result @@ -2786,3 +2786,31 @@ a b c d e f 1 4 NULL NULL NULL NULL 2 4 NULL NULL NULL NULL drop table t1,t2,t3,t4,t5,t6; +# +# MDEV-25362: Incorrect name resolution for subqueries in ON expressions +# +create table t1 (a int, b int); +create table t2 (c int, d int); +create table t3 (e int, f int); +create table t4 (g int, h int); +explain +select * +from +t1 left join +(t2 +join +t3 on +(t3.f=t1.a) +) on (t2.c=t1.a ); +ERROR 42S22: Unknown column 't1.a' in 'on clause' +explain +select * +from +t1 left join +(t2 +join +t3 on +(t3.f=(select max(g) from t4 where t4.h=t1.a)) +) on (t2.c=t1.a ); +ERROR 42S22: Unknown column 't1.a' in 'where clause' +drop table t1,t2,t3,t4; diff --git a/mysql-test/main/join_outer.test b/mysql-test/main/join_outer.test index f835d8af5a8..a93b865eed3 100644 --- a/mysql-test/main/join_outer.test +++ b/mysql-test/main/join_outer.test @@ -2287,3 +2287,36 @@ FROM LEFT JOIN t6 ON t3.c = t6.f; drop table t1,t2,t3,t4,t5,t6; + +--echo # +--echo # MDEV-25362: Incorrect name resolution for subqueries in ON expressions +--echo # +create table t1 (a int, b int); +create table t2 (c int, d int); +create table t3 (e int, f int); +create table t4 (g int, h int); + +--error ER_BAD_FIELD_ERROR +explain +select * +from + t1 left join + (t2 + join + t3 on + (t3.f=t1.a) + ) on (t2.c=t1.a ); + +# This must produce an error: +--error ER_BAD_FIELD_ERROR +explain +select * +from + t1 left join + (t2 + join + t3 on + (t3.f=(select max(g) from t4 where t4.h=t1.a)) + ) on (t2.c=t1.a ); + +drop table t1,t2,t3,t4; diff --git a/mysql-test/main/join_outer_jcl6.result b/mysql-test/main/join_outer_jcl6.result index 9cbfa5d0fe5..89417e79003 100644 --- a/mysql-test/main/join_outer_jcl6.result +++ b/mysql-test/main/join_outer_jcl6.result @@ -2793,3 +2793,31 @@ a b c d e f 1 4 NULL NULL NULL NULL 2 4 NULL NULL NULL NULL drop table t1,t2,t3,t4,t5,t6; +# +# MDEV-25362: Incorrect name resolution for subqueries in ON expressions +# +create table t1 (a int, b int); +create table t2 (c int, d int); +create table t3 (e int, f int); +create table t4 (g int, h int); +explain +select * +from +t1 left join +(t2 +join +t3 on +(t3.f=t1.a) +) on (t2.c=t1.a ); +ERROR 42S22: Unknown column 't1.a' in 'on clause' +explain +select * +from +t1 left join +(t2 +join +t3 on +(t3.f=(select max(g) from t4 where t4.h=t1.a)) +) on (t2.c=t1.a ); +ERROR 42S22: Unknown column 't1.a' in 'where clause' +drop table t1,t2,t3,t4; diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index 02652dd095c..f8654c454c5 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -10080,7 +10080,7 @@ SELECT_LEX *LEX::parsed_subselect(SELECT_LEX_UNIT *unit) (curr_sel == NULL && current_select == &builtin_select)); if (curr_sel) { - curr_sel->register_unit(unit, &curr_sel->context); + curr_sel->register_unit(unit, context_stack.head()); curr_sel->add_statistics(unit); }