Repository navigation
Description
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
Fix correlated scalar CTE aggregates across equality and range (#29413)
Ariznawlll commentedon Oct 2, 2026
测试版本
maind50a2d475a406829f67127fef7b2ad4abc1c1d3dmo-service,独立数据目录;MySQL 文本协议与 server-side PREPARE原问题验证
按 issue 原始语义在上述 commit 重新执行并重复 3 次。空输入上的无 GROUP BY 聚合即使包含 computed/nested expression 仍保留单行结果。
Happy Path 覆盖
覆盖 COUNT、SUM、表达式嵌套与 CTAS 物化。
Unhappy Path 覆盖
覆盖空表、过滤为空、NULL 表达式;不再错误返回零行或 panic。
白盒验证
关联修复 #29413、#29305 已进入当前
main。同时将本 issue 的可公开观察契约固化为独立回归,避免只依赖实现细节或共享测试间接判断。回归测试
suites/14_issue_regression/t/issue_recent_sql_semantics.test714/714条语句通过。测试证据
d50a2d475a406829f67127fef7b2ad4abc1c1d3d14_issue_regression中观察到的旧 GOROOT/历史 golden/并发锁冲突属于既有无关失败,以上新增用例单独重跑均稳定通过。测试结论
测试通过。