Skip to content

[Bug]: correlated local CTE computed and nested aggregates lose empty-input row #29412

Closed

Description

@XuPeng-SH

Problem

A correlated local CTE with an ungrouped aggregate can lose the aggregate's mandatory one-row result when the CTE output is computed or passed through another ungrouped aggregate. This is distinct from #29129's supported single-level non-equality aggregate. PR #29305 now rejects these shapes explicitly to prevent silent wrong results, but the SQL remains unsupported.

Reproduction

Tested on main 5be6cd90b501da225be591ffffe6000bf01f515e and PR #29305 candidate based on 947c504b5dc1883b997d8f6525bf99e525f45f2c:

CREATE DATABASE qa_cte_empty_row;
USE qa_cte_empty_row;
CREATE TABLE o (n INT);
CREATE TABLE i (n INT);
INSERT INTO o VALUES (0),(1);
INSERT INTO i VALUES (1);

-- Expected: (0,1), (1,2). The inner COUNT still emits one row on empty input.
SELECT o.n,
  (WITH x AS (SELECT COUNT(*) AS c FROM i WHERE i.n = o.n)
   SELECT c + 1 FROM x) AS computed_count
FROM o ORDER BY o.n;

-- Expected: (0,1), (1,1). The lower MAX emits one row even on empty input.
SELECT o.n,
  (WITH a AS (SELECT MAX(i.n) AS m FROM i WHERE i.n = o.n),
        x AS (SELECT COUNT(*) AS c FROM a)
   SELECT c FROM x) AS nested_count
FROM o ORDER BY o.n;

DROP DATABASE qa_cte_empty_row;

Observed and root cause

On the PR #29305 candidate both statements return SQLSTATE HY000 / error 20102 (NYI): respectively non-transparent correlated aggregate projection cannot preserve empty-input result and nested correlated aggregate cannot preserve empty-input rows. Before the guard, the nested equality shape could silently return 0 for a missing inner key instead of 1. The correlated predicate pullup groups the lower aggregate by the inner correlation key; that grouped aggregate has no row for an absent key, whereas the original ungrouped aggregate has one. A simple outer COALESCE(COUNT,0) cannot recreate the lower row before the upper aggregate or final expression runs.

The direct correlated aggregate empty-input bug #25959 was fixed and closed; this CTE projection/nesting shape needs separate row-presence handling. #29129 covers the single-level range aggregate duplication and should not be treated as fixing this follow-up.

Acceptance

  • Preserve the one-row empty-input contract at every ungrouped aggregate level, then evaluate final projections against that row.
  • Verify empty, present, all-NULL, duplicate outer keys; COUNT(*), COUNT(col), MAX, SUM, and their computed/nested combinations against an independent SQL oracle.
  • Retain correct scalar cardinality errors and explicit safe rejection for shapes not covered by the fix.
  • Compare plan shape and memory/time on larger input; avoid per-outer-row execution or unbounded buffering.

Activity

  1. self-assigned this
    on Sep 27, 2026
  2. added
    kind/bug
    severity/s0
    on Sep 27, 2026
  3. added a commit that references this issue on Sep 28, 2026
    0782faf
  4. Ariznawlll commented on Oct 2, 2026

    @Ariznawlll
    Collaborator

    测试版本

    • 分支:main
    • Commit:d50a2d475a406829f67127fef7b2ad4abc1c1d3d
    • 部署方式:本地全新单 CN mo-service,独立数据目录;MySQL 文本协议与 server-side PREPARE
    • 测试日期:2026-10-02

    原问题验证

    按 issue 原始语义在上述 commit 重新执行并重复 3 次。空输入上的无 GROUP BY 聚合即使包含 computed/nested expression 仍保留单行结果。

    Happy Path 覆盖

    覆盖 COUNT、SUM、表达式嵌套与 CTAS 物化。

    Unhappy Path 覆盖

    覆盖空表、过滤为空、NULL 表达式;不再错误返回零行或 panic。

    白盒验证

    关联修复 #29413、#29305 已进入当前 main。同时将本 issue 的可公开观察契约固化为独立回归,避免只依赖实现细节或共享测试间接判断。

    回归测试

    测试证据

    • 精确测试版本:d50a2d475a406829f67127fef7b2ad4abc1c1d3d
    • 当前结果:空输入上的无 GROUP BY 聚合即使包含 computed/nested expression 仍保留单行结果。
    • 回归代码与 golden result 已提交到 MOTR #209;本地全量 14_issue_regression 中观察到的旧 GOROOT/历史 golden/并发锁冲突属于既有无关失败,以上新增用例单独重跑均稳定通过。

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions