diff --git a/mysql-test/r/subquery_bugs.result b/mysql-test/r/subquery_bugs.result index c6135878dd8..611bab362d0 100644 --- a/mysql-test/r/subquery_bugs.result +++ b/mysql-test/r/subquery_bugs.result @@ -2333,3 +2333,35 @@ SELECT * FROM (SELECT c0 AS col0, c1 AS col1, c2 AS col2, c3 AS col3, c4 AS col ERROR 42000: Row size too large (> 8126). Changing some columns to TEXT or BLOB may help. In current row format, BLOB prefix of 0 bytes is stored inline. SET GLOBAL log_error_verbosity = @old_val; drop table t; +# +# Bug#118512 The missing semi join condition causes incorrect result +# +CREATE TABLE t1 (id INT); +CREATE TABLE t2 (id INT); +CREATE VIEW v1 AS SELECT abs(t1.id) AS id FROM t1 LEFT JOIN t2 ON t1.id = t2.id; +INSERT INTO t1 VALUES (1), (2); +INSERT INTO t2 VALUES (1), (2); +SET optimizer_switch='loosescan=off,firstmatch=off,duplicateweedout=off'; +EXPLAIN FORMAT=tree SELECT /*+ JOIN_SUFFIX(t1, t2) */ * FROM v1 WHERE v1.id IN (SELECT id FROM v1); +EXPLAIN +-> Left hash join (t2.id = t1.id) (cost=2.08 rows=16) + -> Inner hash join (abs(t1.id) = ``.id) (cost=4.88 rows=8) + -> Table scan on t1 (cost=0.45 rows=2) + -> Hash + -> Table scan on (cost=1.91..3.83 rows=4) + -> Materialize with deduplication (cost=1.28..1.28 rows=4) + -> Filter: (abs(t1.id) is not null) (cost=0.875 rows=4) + -> Left hash join (t1.id = t2.id) (cost=0.875 rows=4) + -> Table scan on t1 (cost=0.45 rows=2) + -> Hash + -> Table scan on t2 (cost=0.225 rows=2) + -> Hash + -> Table scan on t2 (cost=0.225 rows=2) + +SELECT /*+ JOIN_SUFFIX(t1, t2) */ * FROM v1 WHERE v1.id IN (SELECT id FROM v1); +id +1 +2 +DROP TABLE t1,t2; +DROP VIEW v1; +SET optimizer_switch = default; diff --git a/mysql-test/t/subquery_bugs.test b/mysql-test/t/subquery_bugs.test index 7a8e4a7036a..d792f9c2a8b 100644 --- a/mysql-test/t/subquery_bugs.test +++ b/mysql-test/t/subquery_bugs.test @@ -2002,3 +2002,19 @@ SET GLOBAL log_error_verbosity = 1; --eval $query SET GLOBAL log_error_verbosity = @old_val; drop table t; + +--echo # +--echo # Bug#118512 The missing semi join condition causes incorrect result +--echo # + +CREATE TABLE t1 (id INT); +CREATE TABLE t2 (id INT); +CREATE VIEW v1 AS SELECT abs(t1.id) AS id FROM t1 LEFT JOIN t2 ON t1.id = t2.id; +INSERT INTO t1 VALUES (1), (2); +INSERT INTO t2 VALUES (1), (2); +SET optimizer_switch='loosescan=off,firstmatch=off,duplicateweedout=off'; +EXPLAIN FORMAT=tree SELECT /*+ JOIN_SUFFIX(t1, t2) */ * FROM v1 WHERE v1.id IN (SELECT id FROM v1); +SELECT /*+ JOIN_SUFFIX(t1, t2) */ * FROM v1 WHERE v1.id IN (SELECT id FROM v1); +DROP TABLE t1,t2; +DROP VIEW v1; +SET optimizer_switch = default; diff --git a/sql/item_cmpfunc.cc b/sql/item_cmpfunc.cc index 57bfa408362..f61d2736280 100644 --- a/sql/item_cmpfunc.cc +++ b/sql/item_cmpfunc.cc @@ -7362,7 +7362,7 @@ Item *Item_equal::equality_substitution_transformer(uchar *arg) { // Iterate over the fields selected from the subquery uint fieldno = 0; for (Item *existing : sj_nest->nested_join->sj_inner_exprs) { - if (existing->real_item()->eq(item, false)) + if (existing->real_item()->eq(item->real_item(), false)) added_fields.push_back(sj_nest->nested_join->sjm.mat_fields[fieldno]); fieldno++; } @@ -7393,7 +7393,7 @@ Item *Item_func_eq::equality_substitution_transformer(uchar *arg) { // Iterate over the fields selected from the subquery uint fieldno = 0; for (Item *existing : sj_nest->nested_join->sj_inner_exprs) { - if (existing->real_item()->eq(args[1], false) && + if (existing->real_item()->eq(args[1]->real_item(), false) && (args[0]->used_tables() & ~sj_nest->sj_inner_tables)) current_thd->change_item_tree( args + 1, sj_nest->nested_join->sjm.mat_fields[fieldno]);