Skip to content

[Bug]: LEFT and RIGHT JOIN ON conditions still reject subqueries after #24996 #25973

Closed

Description

@Ariznawlll

Summary

LEFT JOIN and RIGHT JOIN still reject subqueries in the ON condition on the latest main, although the general JOIN-ON subquery issue #24996 was closed after support was added for INNER JOIN.

Both correlated and non-correlated scalar subqueries are rejected. EXISTS in an outer-join condition is rejected as well.

Environment

  • Branch: main
  • Commit: 3046f14d94b3f1a25839eeeb65729b64f85ac647
  • MatrixOne version: 8.0.30-MatrixOne-v1.3.0
  • MySQL comparison: 8.0.45

Reproduction

CREATE TABLE l(id INT PRIMARY KEY, k INT);
CREATE TABLE r(id INT PRIMARY KEY, k INT);

INSERT INTO l VALUES (1,1),(2,2),(3,NULL);
INSERT INTO r VALUES (11,1),(12,1),(13,3),(14,NULL);

-- Correlated scalar subquery in LEFT JOIN ON.
SELECT l.id,r.id
FROM l LEFT JOIN r
  ON l.k=r.k
 AND r.id=(SELECT MIN(r2.id) FROM r r2 WHERE r2.k=l.k)
ORDER BY l.id,r.id;

-- Correlated scalar subquery in RIGHT JOIN ON.
SELECT l.id,r.id
FROM l RIGHT JOIN r
  ON l.k=r.k
 AND l.id=(SELECT MIN(l2.id) FROM l l2 WHERE l2.k=r.k)
ORDER BY r.id,l.id;

Actual result

Both statements fail during planning:

ERROR 20102 (HY000): subquery in JOIN condition is not yet implemented

The behavior reproduced 3/3 times for LEFT JOIN and 3/3 times for RIGHT JOIN. A non-correlated scalar subquery and a correlated EXISTS predicate in LEFT JOIN ON fail with the same error.

An equivalent INNER JOIN query succeeds on the same server, confirming that #24996's INNER JOIN path is active.

Expected result

The valid outer-join statements should plan and execute with normal null-extension semantics, matching MySQL 8.0.

White-box evidence

In pkg/sql/plan/query_builder.go, buildJoinTable selects NewJoinOnBinder only for plan.Node_INNER; every other join type uses NewTableBinder. TableBinder.BindSubquery returns the NYI error when allowSubquery is false.

The INNER JOIN implementation introduced for #24996 moves subquery predicates to a filter above the join. That transformation is not semantically valid for outer joins, so LEFT/RIGHT JOIN require a separate lowering strategy that preserves unmatched rows rather than reusing the INNER JOIN rewrite.

Impact

Standard MySQL-compatible queries that select a best/latest matching row inside an outer-join condition cannot run. Moving the predicate to WHERE is not an equivalent workaround because it can remove null-extended rows.

Related: #24996

Activity

  1. added this to the MO-202607 milestone on Jul 21, 2026
  2. added
    kind/bug
    on Jul 21, 2026
  3. added theissue type on Jul 21, 2026
  4. added
    ai-light
    on Aug 10, 2026
  5. removed theissue type on Aug 10, 2026
  6. Ariznawlll commented on Aug 10, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:3c7392a0f86b23712da71f31d7a9198a2c8591a9
    • 部署方式:本地单节点 Log/TN/CN,独立数据目录;测试日期:2026-08-10。

    原问题验证

    原始相关标量子查询分别置于 LEFT/RIGHT JOIN ON,连续 3 次均成功执行并保留未匹配侧的 NULL 扩展行,不再报 subquery in JOIN condition is not yet implemented。

    Happy Path 覆盖

    1. LEFT JOIN:返回 (1,11)、(2,NULL)、(3,NULL),验证相关 MIN 子查询及左侧保留。
    2. RIGHT JOIN:返回 (1,11)、(1,12)、(NULL,13)、(NULL,14),验证右侧保留。

    Unhappy Path 覆盖

    1. ON 中非关联标量子查询正常执行,未匹配左行仍 NULL 扩展。
    2. ON 中相关 EXISTS 正常执行,匹配行和 NULL 扩展行均正确;这覆盖 issue 报告的另一失败形式。
    3. 只读 JOIN,无失败写入原子性维度。

    白盒验证

    不适用:通过公开 SQL 的 LEFT/RIGHT JOIN 语义验证。

    回归测试

    已加入 motr PR #89 的 suites/14_issue_regression/t/issue_26269_25973_25389_25327_25326_sql_semantics.test(commit a6a50dfcf7053027dab5e9117fde7bdd7b5cdefb);非关联/EXISTS 扩展对照已独立连续验证 3 次。

    测试证据

    MOTR 固定回归连续 3 轮通过;关联 5 个场景合计 15/15 成功、171 条断言成功。

    测试结论

    测试通过。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

ai-lightkind/bugphase/testing

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions