Skip to content

correlated columns in SCALAR subquery deeper than 1 level not yet implemented (error 20102) — v4.0.0-rc2 #24997

Closed

Description

@dengn

Version

8.0.30-OmniFabric-v4.0.0-rc2 (output of SELECT version())

Description

A scalar subquery that contains a nested scalar subquery whose correlation references an outer table alias more than one level up fails to plan with:

ERROR 20102 (HY000): correlated columns in SCALAR subquery deeper than 1 level will be supported in future version is not yet implemented

This is a common "value at the most-recent timestamp per entity" pattern and runs on MySQL 8 / PostgreSQL.

Reproduction

CREATE TABLE j_dim  (id INT);
CREATE TABLE j_fact (id INT, dim_id INT, ts INT, val DOUBLE);

SELECT a.id,
       (SELECT MAX(x.val)
          FROM j_fact x
         WHERE x.ts = (SELECT MAX(y.ts)
                         FROM j_fact y
                        WHERE y.dim_id = a.id)   -- correlates to outer `a`, two levels up
       ) AS latest
FROM j_dim a;

Actual

ERROR 20102 (HY000): correlated columns in SCALAR subquery deeper than 1 level will be supported in future version is not yet implemented

Expected

The statement plans and runs, resolving the correlation to the outermost a from the doubly-nested scalar subquery.

Impact

Hit by a natural-language-to-SQL assistant. Questions like "give me the exact value at the latest timestamp for entity X" produce this nested-correlated scalar shape, which fails instead of running.

Activity

  1. added
    kind/bug
    severity/s0
    on Jun 16, 2026
  2. added theissue type on Jun 16, 2026
  3. added this to the MO-202606 milestone on Jun 16, 2026
  4. added
    severity/s1
    and removed
    severity/s0
    on Jun 30, 2026
  5. modified the milestones: MO-202606, MO-202607 on Jun 30, 2026
  6. added
    ai-easy
    on Aug 4, 2026
  7. Repository owner unassigned on Aug 6, 2026
  8. ghost assigned on Aug 6, 2026
  9. removed
    severity/s1
    on Aug 6, 2026
  10. slyunnn commented on Aug 7, 2026

    @slyunnn

    回归测试结论:通过:BVT 15/15。

  11. slyunnn commented on Aug 10, 2026

    @slyunnn

    Extended verification for #24997 (2026-08-10)

    Follow-up to the nested correlated scalar-subquery report: verified the fix from #26480 on the latest main branch.

    Verification environment

    • MatrixOne branch: main
    • MatrixOne commit: fa0ea95aa77b59a2947159baab31e7215d040e93
      fix(plan): support subqueries in left and right join ON clauses (#26763)
    • Fix under verification: ce384091488175e08410f0795379d217112421e3
      fix(plan): support nested correlated scalar aggregates (#26480)
    • MOTR branch: test/issue-24997-nested-correlated-scalar
    • MOTR commit: 8134ac43484bb8a4c20ef6cd7e6515214faa9132

    What was covered

    • Original two-level correlated scalar aggregate shape.
    • Per-dimension latest-value query, including a dimension with no fact rows.
    • AVG outer aggregate with a deeply correlated inner MAX.
    • Deep scalar aggregate used by an outer WHERE filter.
    • Prepared parameter execution for latest-value lookup.
    • Planner unit tests for nested correlated scalar aggregate pull-up and guarded unsupported shapes.

    Results

    • Planner tests: passed.
    • MOTR regression: 18/18 statements passed.
    • Original query result: 220.0, 300.0, NULL.
    • Per-dimension result: 200.0, 300.0, NULL.
    • Filtered result correctly returns dimensions 1 and 2.
    • The referenced issue comment also records the original BVT result: 15/15 passed.

    Findings

    • fix(plan): support nested correlated scalar aggregates #26480 resolves the original error 20102 for supported nested correlated scalar aggregate forms.
    • Empty inner input correctly produces NULL.
    • Unsafe forms involving COALESCE, non-NULL-on-empty aggregates, and LIMIT/OFFSET remain deliberately rejected,
      preventing semantic rewrites that could produce wrong results.

    Test-file changes

    • Added a MOTR .test file to permanently cover the original failure and supported extended query shapes.
    • Added the matching recorded .result file as expected output.
    • No MatrixOne service source code was changed.

    Suggested resolution

    Merge the MOTR regression PR and close #24997 as fixed by #26480.

  12. assigned and unassigned on Aug 10, 2026
  13. assigned and unassigned on Aug 12, 2026
  14. Ariznawlll commented on Aug 12, 2026

    @Ariznawlll
    Collaborator

    测试版本

    • 分支:main
    • commit:1c292cf20e113b296a3db2cb6a65e00af2ea2327
    • 部署方式:本地 launch-with-proxy 拓扑(2 CN + 1 TN + 1 Log + Proxy),MOTR 直连 CN 127.0.0.1:16001
    • 测试日期:2026-08-13

    原问题验证

    按 issue 的嵌套相关标量子查询场景创建 j_dim 3 行、j_fact 5 行,执行「外层维度行 -> MAX(val) -> 内层相关 MAX(ts)」的原始查询形状。期望 id/latest 为 1/220.0、2/300.0、3/NULL;在上述 main 上通过 MOTR --repeat 3 执行,3/3 轮结果与期望完全一致,未再出现错误结果或执行异常。完整 SQL 见下方回归文件。

    Happy Path 覆盖

    1. 原始嵌套相关聚合查询:返回 1/220.0、2/300.0、3/NULL。
    2. 内外层均按 dim_id 相关的 MAX 变体:返回 1/200.0、2/300.0、3/NULL,未匹配维度正确保留 NULL。
    3. AVG 聚合变体返回 200.0、300.0、NULL;相同嵌套子查询放入外层 WHERE > 150 时正确返回 id=1,2。
    4. SQL PREPARE 变体用 dim_id=1 和 dim_id=2 两组参数执行,结果分别为 200.0 和 300.0。

    Unhappy Path 覆盖

    1. 维度表中存在无事实行的 id=3,事实表中另有无匹配维度的 dim_id=99;相关结果仍为 id=3 -> NULL,没有被无关行污染。
    2. 分别执行当前不支持的 APPROX_COUNT、COUNT + COALESCE、COALESCE 及 LIMIT/OFFSET 嵌套形状,各 3/3 轮稳定拒绝;每次拒绝后继续执行普通查询均成功,连接可继续使用。

    白盒验证

    不适用:本 issue 验收的是 SQL 优化器对嵌套相关标量子查询的公开语义和结果正确性,可通过多组 SQL 输入及精确结果全量观测;本轮未额外执行聚焦 Go UT/故障注入。

    回归测试

    已由 motr PR #94 合入回归;精确测试文件:issue_24997_nested_correlated_scalar.test。本轮在最新 main 上对该 case 执行 MOTR --repeat 3,3/3 轮成功,每轮 18 条语句均与期望输出匹配。

    测试证据

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

Bug fixai-easykind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions