diff --git a/mysql-test/r/hash_join.result b/mysql-test/r/hash_join.result index c3651efd30e..c01f6f78928 100644 --- a/mysql-test/r/hash_join.result +++ b/mysql-test/r/hash_join.result @@ -3915,6 +3915,9 @@ EXPLAIN -> Table scan on table1 (cost=0.35 rows=1) -> Hash -> Table scan on t1 (cost=0.35 rows=1) + -> Select #2 (subquery in extra conditions; dependent) + -> Filter: (table1.a <> 6) (cost=0.35 rows=1) + -> Table scan on t1 (cost=0.35 rows=1) Warnings: Note 1276 Field or reference 'test.table1.a' of SELECT #2 was resolved in SELECT #1 @@ -3929,6 +3932,9 @@ EXPLAIN -> Table scan on table1 (cost=0.35 rows=1) -> Hash -> Table scan on t1 (cost=0.35 rows=1) + -> Select #2 (subquery in condition; dependent) + -> Filter: (table1.a <> 6) (cost=0.35 rows=1) + -> Table scan on t1 (cost=0.35 rows=1) Warnings: Note 1276 Field or reference 'test.table1.a' of SELECT #2 was resolved in SELECT #1 @@ -4244,3 +4250,30 @@ NULL NULL NULL DROP TABLE t1, t2; +CREATE TABLE t1 (a int not null, b int not null); +CREATE TABLE t2 (a int not null, b int not null); +EXPLAIN FORMAT=TREE +SELECT * FROM t1, t2 WHERE t2.a = (SELECT MIN(t2.a) FROM t2 WHERE t1.a = t2.a); +EXPLAIN +-> Inner hash join (t2.a = (select #2)) (cost=0.7 rows=1) + -> Table scan on t2 (cost=0.35 rows=1) + -> Hash + -> Table scan on t1 (cost=0.35 rows=1) + -> Select #2 (subquery in condition; dependent) + -> Aggregate: min(t2.a) (cost=0.45 rows=1) + -> Filter: (t1.a = t2.a) (cost=0.35 rows=1) + -> Table scan on t2 (cost=0.35 rows=1) + +EXPLAIN FORMAT=TREE +SELECT * FROM t1 LEFT JOIN t2 ON t1.b = t2.b AND t2.a < (SELECT MIN(t2.a) FROM t2 WHERE t1.a = t2.a); +EXPLAIN +-> Left hash join (t2.b = t1.b), extra conditions: (t2.a < (select #2)) (cost=0.7 rows=1) + -> Table scan on t1 (cost=0.35 rows=1) + -> Hash + -> Table scan on t2 (cost=0.35 rows=1) + -> Select #2 (subquery in extra conditions; dependent) + -> Aggregate: min(t2.a) (cost=0.45 rows=1) + -> Filter: (t1.a = t2.a) (cost=0.35 rows=1) + -> Table scan on t2 (cost=0.35 rows=1) + +DROP TABLE t1, t2; diff --git a/mysql-test/t/hash_join.test b/mysql-test/t/hash_join.test index d5cbdd3b18e..0a1a40c7652 100644 --- a/mysql-test/t/hash_join.test +++ b/mysql-test/t/hash_join.test @@ -2694,3 +2694,16 @@ ANALYZE TABLE t1, t2; --eval $query DROP TABLE t1, t2; + +# Bug111564: Missing subquery in HashJoin condition. +CREATE TABLE t1 (a int not null, b int not null); +CREATE TABLE t2 (a int not null, b int not null); + +--disable_warnings +EXPLAIN FORMAT=TREE +SELECT * FROM t1, t2 WHERE t2.a = (SELECT MIN(t2.a) FROM t2 WHERE t1.a = t2.a); +EXPLAIN FORMAT=TREE +SELECT * FROM t1 LEFT JOIN t2 ON t1.b = t2.b AND t2.a < (SELECT MIN(t2.a) FROM t2 WHERE t1.a = t2.a); + +--enable_warnings +DROP TABLE t1, t2; diff --git a/sql/join_optimizer/explain_access_path.cc b/sql/join_optimizer/explain_access_path.cc index 69c1b3e78e5..a044ef18424 100644 --- a/sql/join_optimizer/explain_access_path.cc +++ b/sql/join_optimizer/explain_access_path.cc @@ -1277,6 +1277,14 @@ static std::unique_ptr SetObjectMembers( error |= AddMemberToObject(obj, "join_algorithm", "hash"); children->push_back({path->hash_join().outer}); children->push_back({path->hash_join().inner, "Hash"}); + const RelationalExpression *join_predicate = + path->hash_join().join_predicate->expr; + for (Item_eq_base *cond : join_predicate->equijoin_conditions) { + GetAccessPathsFromItem(cond, "condition", children); + } + for (Item *cond : join_predicate->join_conditions) { + GetAccessPathsFromItem(cond, "extra conditions", children); + } break; } case AccessPath::FILTER: {