Skip to content

[Feature Request]: Support correlated subquery with multi levels #7559

Description

@domingozhang

Is there an existing issue for the same feature request?

  • I have checked the existing issues.

Is your feature request related to a problem?

No response

Describe the feature you'd like

Currently, MO supports correlated subquery with only 1 level. To accomplish this feature, have to support correlated subquery with more than 1 level

Describe implementation you've considered

No response

Documentation, Adoption, Use Case, Migration Strategy

No response

Additional information

No response

Activity

  1. added
    priority/p0
    source/on-demand
    on Jan 10, 2023
  2. added this to the V0.8.0-Backlog milestone on Jan 10, 2023
  3. added
    priority/p1
    and removed
    priority/p0
    on Feb 9, 2023
  4. added theissue type on Apr 24, 2025
  5. 5 remaining items

  6. assigned and unassigned on Sep 29, 2026
  7. slyunnn commented on Sep 30, 2026

    @slyunnn

    Verification Environment

    • Verified on MatrixOne origin/main commit 19e01d261b078c172585a3ca46da86eb7e446921 (test: reduce race fixture work and strengthen regression coverage (#29520)).
    • The running mo-service binary was freshly built from that exact worktree; its SHA-256 was 1e03ca96c78d8a11f2807c30aee5d92cee3a3d62ce10fad0d776763926397c48, matching the binary mounted in CN1.
    • Topology: 2 CN / 1 TN / 1 Log service / Proxy. The isolated MySQL endpoints were Proxy 6621 (service port 6009), CN1 16621 (service port 16001), and CN2 16622 (service port 16002). All endpoints were reachable and passed post-query health checks.
    • Client protocol: MySQL text protocol via mysql CLI. Endpoint cases ran in parallel with isolated databases and no shared test objects.
    • Related prerequisite PR: a97f9c940bba4bcbc6d7c615f789fccab1ac25f9, fix(sql): preserve join locality and numeric CAST metadata (#29452). Its design and PR description explicitly state that it is prerequisite work only and does not implement or close [Feature Request]: Support correlated subquery with multi levels #7559.

    What Was Covered

    • OK One-level correlated EXISTS control returned 1,2,3 through Proxy, CN1, and CN2.
    • OK Two-level correlated EXISTS returned the expected 1 through Proxy, CN1, and CN2.
    • OK Two-level correlated IN returned the expected 1 through Proxy, CN1, and CN2.
    • Failed Two-level correlated scalar-subquery form returned ERROR 20102: correlated columns in SCALAR subquery deeper than 1 level ... is not yet implemented, consistently through all three endpoints.
    • OK Each endpoint completed SELECT 1 after the tested statements.
    • OK Focused planner test TestNestedCorrelatedScalarStillRejectsUnsafeShapes passed with exit status 0; it asserts this current explicit rejection.
    • Not covered General multilevel decorrelation across all subquery categories, aggregates, FROM-subqueries, arbitrary parameter domains, demand/cardinality handling, and materialization budgets. The issue is a broad feature request without a finalized general implementation design.

    Findings

    MatrixOne currently has partial multilevel correlation support: the representative two-level EXISTS and IN forms execute correctly, while a representative two-level scalar correlation is intentionally rejected by pkg/sql/plan/flatten_subquery.go. Therefore, the requested general capability is not implemented. The linked timeline event only assigned the issue; it did not indicate a completed implementation.

    Current main supports the tested multilevel EXISTS and IN forms but does not support the tested multilevel scalar-subquery form; #29452 provides physical prerequisites only and must not be treated as a fix for the general feature.

    MOTR / BVT Coverage

    No MOTR case, branch, recording, replay, commit, or PR was created. This is an open kind/feature request rather than a fixed bug regression, and adding a MOTR expectation for a deliberately unsupported feature would not provide valid coverage. Existing planner coverage explicitly verifies the current rejection; the real SQL endpoint matrix above verifies the current public behavior.

    Large-data regression is not required. The observed limitation is a planner/decorrelation capability gate that occurs before data-volume-dependent execution; row count, block/batch boundaries, memory/spill, concurrency, skew, network transfer, and resource scale do not change the support decision.

  8. assigned and unassigned on Oct 8, 2026
  9. XuPeng-SH commented on Oct 18, 2026

    @XuPeng-SH
    Contributor

    continue20 独立实测:#30116 对本 issue“超过一层的关联子查询”报告范围已经通过,转测试。

    main 2508ce7029b03e6bb3ac0bd8e66a994d12593664,Linux/amd64、Go1.27.1、CPU/CGo,隔离worktree;使用仓库mo-cgo-test入口及测试Go overlay,未改产品源代码。

    • 真实MySQL CN0/CN1:两层/三层祖先引用、嵌套聚合与空标量NULL、未选择CASE分支不触发多行错误、LIMIT/OFFSET延迟求值、被求值标量多行明确拒绝且随后SQL正常、PREPARE参数复用、INSERT SELECT的精确持久化值,全部通过。
    • 补充公开取消:通过现有测试fault barrier确认dependent查询进入执行,再用另一会话KILL QUERY,两个CN均返回1317/context canceled;connection_id前后一致,原连接SELECT 1成功。无sleep猜测取消时机。夹具首轮未启用fault导致setup失败,已修正并保留原exit1;不当作数据库回归。
    • dependent包完整normal,以及planner/compiler/process的27个consumer根测试(167个命名结果)通过;四包对应race通过。相同SHA的UT+consumer profile并集覆盖dependent包958/1131语句=84.7%,不冒充195个变更文件或整个PR的覆盖率。
    • frontend的dependent PREPARE导出协议、低版本目标拒绝前状态保留、GROUP_CONCAT输出元数据3根/7命名结果通过;这些是typed迁移边界测试,不声称执行了完整Proxy线上迁移部署。
    • planner与MySQL parser完整normal:4092根、18897命名PASS,零FAIL(包含其他功能回归,不将这些数量当作本feature独有用例)。

    证据 /workspace/work/pr-audit-continue20/logs/:public-matrix.log、dependent-public-cancel-enabled.log(exit0)、dependent-full.log、dependent-consumers.log、dependent-race.log、dependent-frontend.log、planner-parser-full.log。coverage/dependent-union.json保留分母与同SHA profile。公开命令:MO_TEST_CLUSTER_TCP_ONLY=1 .agents/skills/mo-dev/scripts/mo-cgo-test -overlay=/workspace/work/pr-audit-continue20/public-overlay.json -p 1 -v -count=1 -timeout=10m -run '^TestAudit20PublicMatrix$/^CN[01]$/^30116_multilevel_correlated$' ./pkg/tests/issues;race选择TestDependent.*|TestCorrelation.*|TestNativeKernelTerminalCause|TestSourceEdgeRetiresRoundControlBudget|TestPublicDependentStreamingExecution,覆盖dependent/plan/compile/process四包,exit0。

    按main报告范围转 @Ariznawlll,移除原开发指派;不自动要求pick到4.2。此结果不表示最近100个PR已全部完成高覆盖实测。

  10. slyunnn commented on Oct 22, 2026

    @slyunnn

    Regression and extended verification for #7559 (2026-10-22)

    Verification Environment

    • Executed git fetch origin main in the running-source worktree. HEAD and origin/main both resolved to ada0bde326adec3c3906d2f417c266d0ada5b054 — feat: control TPC-H comparison resources and paired measurement (#30599).
    • The actual mo-service binary was built from this ref: Go 1.27.1, commit ada0bde326, running from the local current-main source worktree.
    • Complete topology: 2 CN / 1 TN / 1 Log service / Proxy, launched with the local proxy configuration. Proxy 6001, CN1 16001, and CN2 16002 were reachable.
    • All SQL used the MySQL TCP protocol via the MySQL CLI. Original multilevel, empty-scalar, Prepared Statement, cardinality-error, cleanup, and health checks ran through Proxy 6001; core execution and health checks ran directly through CN1 16001 and CN2 16002.
    • Related implementation: PR #30116, feat(sql): support multilevel correlated subqueries, merged as 94760aa93223924df314c12d7079323f2e89c2b1. Its main implementation commit is 7010830b375096c5f3683f261cf68bedbc157d63. The merge commit is an ancestor of the verified current main.

    What Was Covered

    • OK — Two-level correlated scalar query through Proxy, CN1, and CN2 returned (1,2), (2,3), and (3,4) for SELECT o.k, (SELECT (SELECT o.k + 1)).
    • OK — Three-level ancestor reference through Proxy returned (1,2), (2,4), and (3,6) for SELECT o.k, (SELECT (SELECT (SELECT o.k * 2))).
    • OK — Correlated aggregate with nested EXISTS returned (1,21), (2,20), and (3,30) through Proxy, CN1, and CN2.
    • OK — Empty nested scalar result preserved SQL NULL semantics: every empty_scalar_is_null result was 1.
    • OK — Prepared Statement through Proxy preserved multilevel correlation and parameter binding: k > 1 with delta 10 returned (2,12) and (3,13).
    • OK — Unhappy path: a demanded nested scalar returning two rows was rejected with standard ERROR 1242 (Subquery returns more than 1 row); subsequent Proxy SELECT 1 succeeded.
    • OK — CN1 and CN2 each completed post-query SELECT 1; both direct endpoints produced the same functional results as Proxy.
    • Not covered — Proxy connection migration/capability downgrade, cancellation fault injection, authorization/revocation, and full DML/CTE/window/set-operation matrix. These are broader PR feat(sql): support multilevel correlated subqueries #30116 execution-scope areas, not part of the original minimal feature request or this independent SQL verification.
    • Not covered — Large-data performance and throughput characterization. The feature is not a row-count, LIMIT, block/batch, spill, index, skew, or network-transfer defect. The small multi-row/duplicate-inner fixture exercises demanded invocation and cardinality behavior; no large-data regression is required for this functional conclusion.
    • Not covered — No engine worker-parallel claim is made. The verification ran serially; MOTR -j N was not used as a proxy for engine behavior.

    Findings

    The requested multilevel correlated-subquery capability is now present in verified current main for nested scalar ancestor references, correlated aggregates, empty scalar results, Prepared Statement parameters, and standard scalar-cardinality errors. The historical unsupported deeper-than-one scalar rejection is not reproduced. Proxy, CN1, and CN2 returned consistent results.

    MOTR / BVT Coverage

    No new MOTR/BVT case was needed or created. PR #30116 already contains extensive owned planner/execution and public SQL coverage for this large feature. The independent verification above exercised the issue contracts directly against the actual latest-main multi-CN service, so no test branch, recording/replay, commit, push, or daily-summary entry applies.

    Final Conclusion

    Fixed — PR #30116 is merged in verified origin/main, and actual Proxy/CN1/CN2 MySQL-protocol validation confirms the requested multilevel correlated-subquery support within the tested functional scope.

  11. XuPeng-SH commented on Oct 22, 2026

    @XuPeng-SH
    Contributor

    Verified on main@3ad9adbb4c (PR #30116 merged) — no transfer, slyunnn already owns testing

    Built mo-service from current main locally (CGo, arm64) and ran adversarial multi-level correlated cases against a fresh isolated instance, using an equivalent JOIN/GROUP BY rewrite as the oracle (independent of the subquery decorrelation path):

    create table t1(a int, b int);
    create table t2(a int, c int);
    create table t3(c int, d int);
    insert into t1 values (1,10),(2,5),(3,1);
    insert into t2 values (1,100),(1,200),(2,50),(3,7),(3,9);
    insert into t3 values (100,11),(200,9),(50,6),(7,0),(9,20);
    
    -- two-level correlation: inner EXISTS references the outermost table
    select a, (select max(c) from t2
               where t2.a = t1.a
                 and exists (select 1 from t3 where t3.c = t2.c and t3.d > t1.b)) as v
    from t1 order by a;

    Actual on HEAD: 1|100, 2|50, 3|9 — identical to the oracle

    select t1.a, max(t2.c) from t1
      join t2 on t2.a=t1.a
      join t3 on t3.c=t2.c and t3.d>t1.b
     group by t1.a order by t1.a;

    Nested scalar-inside-scalar with two outer references also produces the manually verified values (1|1, 2|1, 3|0; for a=3 the only c=7 row has d=0 < b=1, so 0 is correct).

    Assignees left unchanged: slyunnn already handles testing on this issue, so per policy no transfer is performed — this comment is verification evidence only.

    Not covered here: 3+ level nesting, correlation through LATERAL/set operations, and performance at scale.

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

Metadata

Metadata

Assignees

Labels

kind/featurelong-term-jobpriority/p1source/on-demand

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions