Repository navigation
Description
Description
A scalar subquery whose local CTE contains a non-equality correlation (<= or <) loses its aggregate semantics. MAX, SUM, and COUNT are evaluated once per matching inner row, so one outer row is expanded into multiple result rows instead of receiving one scalar aggregate value.
The equivalent correlated scalar aggregate without the local CTE is correct, and the local-CTE form with an equality predicate is also correct.
Environment
- Branch:
main - Commit:
d8ddce92b1c5c172111b50aefe6b6b200b2589cb - Deployment: local 1 LogService / 1 TN / 2 CN (
16001,16002) - Test date: 2026-09-20
Steps to reproduce
DROP DATABASE IF EXISTS cte_corr_non_eq;
CREATE DATABASE cte_corr_non_eq;
USE cte_corr_non_eq;
CREATE TABLE t(n INT PRIMARY KEY);
INSERT INTO t VALUES (1),(2),(3);
SELECT o.n,
(WITH x AS (
SELECT MAX(i.n) AS m
FROM t i
WHERE i.n <= o.n
)
SELECT m FROM x) AS m
FROM t o
ORDER BY o.n;The same behavior occurs when MAX(i.n) is replaced with SUM(i.n) or COUNT(*).
Actual behavior
MAX returns six rows and exposes the matching inner rows:
1 1
2 1
2 2
3 1
3 2
3 3
SUM returns the same six row shape rather than cumulative sums. COUNT(*) returns one 1 per matching row.
EXPLAIN shows that the aggregate is grouped by the inner value and then joined with the outer row:
Join Type: LEFT
Join Cond: (i.n <= o.n)
-> Table Scan on t
-> Aggregate
Group Key: i.n
Aggregate Functions: max(i.n)
-> Table Scan on t
Expected behavior
The scalar aggregate must return exactly one row per outer row:
1 1
2 2
3 3
For the other aggregates, COUNT(*) should return 1,2,3, and SUM(i.n) should return 1,3,6.
Stability and controls
- Reproducer: 3 independent databases × 2 CN endpoints,
6/6returned the same wrong rows. - Without the local CTE, the equivalent
MAX,SUM, andCOUNTcorrelated scalar subqueries return the expected three rows. - With the local CTE and
i.n = o.n, the query returns the expected three rows. - The statements are read-only; the source table remained
(1),(2),(3)after every run.
Evidence
The full two-CN result matrix and plan are retained in evidence/cte_correlated_nonnequality_aggregate_repro.out for the stated commit.
Code analysis
The logical rewrite decorrelates the CTE aggregate with i.n as a grouping key, then applies i.n <= o.n as the outer LEFT JOIN condition. That grouping is valid for an equality correlation, where at most one group matches, but not for a non-equality correlation, where several inner groups match one outer row. The aggregate must remain correlated per outer row or group by a stable outer-row identity after the non-equality join.
Regression coverage
Add BVT coverage for local CTEs inside correlated scalar subqueries with =, <, <=, >, and >=, covering MAX, MIN, SUM, COUNT, empty matches, NULL values, and multiple outer rows. Assert both row count and scalar values.
Related
- [Bug]: correlated column in CTE not supported #3304 is closed and concerns declaration-scope resolution/errors; it does not cover wrong aggregate results from a supported correlated local CTE.
test/distributed/cases/subquery/scalar_correlated_projection.sqlalready treats equality-correlated local CTE aggregates as supported.
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 次。相关子查询中的 MAX/SUM/COUNT 对外层每行返回正确结果,空集合聚合语义也正确。
Happy Path 覆盖
覆盖相关键有匹配、多匹配和普通聚合。
Unhappy Path 覆盖
覆盖无匹配、NULL 相关键与空集合;MAX/SUM 为 NULL、COUNT 为 0。
白盒验证
关联修复 #29413、#29305 已进入当前
main。同时将本 issue 的可公开观察契约固化为独立回归,避免只依赖实现细节或共享测试间接判断。回归测试
suites/14_issue_regression/t/issue_recent_sql_semantics.test714/714条语句通过。测试证据
d50a2d475a406829f67127fef7b2ad4abc1c1d3d14_issue_regression中观察到的旧 GOROOT/历史 golden/并发锁冲突属于既有无关失败,以上新增用例单独重跑均稳定通过。测试结论
测试通过。