Repository navigation
Description
Is there an existing issue for the same bug?
- I have checked the existing issues.
Branch Name
main
Commit ID
Other Environment Information
- MatrixOne was built from the commit above and started locally with etc/launch/launch.toml.
- MySQL comparison version: 8.0.45.
- Every query was repeated three times with the same result.Actual Behavior
MatrixOne supports a simple non-equality correlated scalar aggregate such as
SELECT MAX(i.val) ... WHERE i.grp <= o.grp, but rejects several ordinary
extensions of the same query that MySQL 8.0 accepts:
- Wrapping the aggregate result in an expression, including
MAX(i.val) + 1,
COALESCE(MAX(i.val), -1), andCASE ... MAX(i.val) ... END. - Adding
HAVING,DISTINCT, orGROUP BY ... ORDER BY ... LIMIT 1inside
the scalar aggregate subquery. - Reading the outer row from a derived table.
- Referencing columns from two outer join inputs.
- Applying
COUNT(*)to a derivedUNION ALLinner input.
The first group is rejected with:
aggregation with non equal predicate in scalar subquery will be supported in future version is not yet implemented
The remaining groups are rejected by related NYI guards for derived outer
tables, multiple outer tables, correlated LIMIT, or an inner shape without a
reachable Row_ID.
The planner's non-equality scalar-aggregate rewrite only accepts a narrow
PROJECT -> AGG -> inner form whose first projection is a direct aggregate
column. It also requires exactly one base-table outer binding with a hidden
Row_ID and a reachable inner Row_ID for COUNT(*). These structural
restrictions reject semantically valid scalar queries rather than producing a
result.
Expected Behavior
These queries should produce the same scalar result as MySQL 8.0.45. For the
sample data below, representative expected results include:
MAX(i.val) + 1: (1,6), (2,6), (3,11), (4,11)
derived outer table + MAX: (1,5), (2,5), (3,10), (4,10)
UNION ALL inner + COUNT(*): (1,4), (2,4), (3,8), (4,8)
Steps to Reproduce
CREATE DATABASE codex_correlated_noneq_aggregate_0907;
USE codex_correlated_noneq_aggregate_0907;
CREATE TABLE outer_t (
id INT PRIMARY KEY,
grp INT NOT NULL,
val INT NULL
);
CREATE TABLE inner_t (
id INT PRIMARY KEY,
grp INT NOT NULL,
val INT NULL
);
INSERT INTO outer_t VALUES
(1,1,5),(2,1,NULL),(3,2,10),(4,3,7);
INSERT INTO inner_t VALUES
(10,1,5),(11,1,NULL),(12,2,8),(13,2,10),(14,4,NULL);
-- Control: supported by both MatrixOne and MySQL.
SELECT o.id,
(SELECT MAX(i.val) FROM inner_t i WHERE i.grp <= o.grp) AS v
FROM outer_t o ORDER BY o.id;
-- Aggregate-result expressions.
SELECT o.id,
(SELECT MAX(i.val) + 1 FROM inner_t i WHERE i.grp <= o.grp) AS v
FROM outer_t o ORDER BY o.id;
SELECT o.id,
(SELECT COALESCE(MAX(i.val), -1)
FROM inner_t i WHERE i.grp <= o.grp) AS v
FROM outer_t o ORDER BY o.id;
SELECT o.id,
(SELECT CASE WHEN MAX(i.val) IS NULL THEN -1 ELSE MAX(i.val) END
FROM inner_t i WHERE i.grp <= o.grp) AS v
FROM outer_t o ORDER BY o.id;
-- HAVING / DISTINCT / grouped Top-1.
SELECT o.id,
(SELECT MAX(i.val) FROM inner_t i
WHERE i.grp <= o.grp HAVING MAX(i.val) >= 5) AS v
FROM outer_t o ORDER BY o.id;
SELECT o.id,
(SELECT DISTINCT MAX(i.val) FROM inner_t i WHERE i.grp <= o.grp) AS v
FROM outer_t o ORDER BY o.id;
SELECT o.id,
(SELECT MAX(i.val) FROM inner_t i WHERE i.grp <= o.grp
GROUP BY i.grp ORDER BY i.grp DESC LIMIT 1) AS v
FROM outer_t o ORDER BY o.id;
-- Derived outer input.
SELECT d.id,
(SELECT MAX(i.val) FROM inner_t i WHERE i.grp <= d.grp) AS v
FROM (SELECT id, grp FROM outer_t) d ORDER BY d.id;
-- Two outer inputs.
SELECT o.id, x.id,
(SELECT MAX(i.val) FROM inner_t i
WHERE i.grp <= o.grp AND i.id <= x.id) AS v
FROM outer_t o JOIN outer_t x ON x.id = o.id
ORDER BY o.id;
-- Derived UNION ALL inner input with COUNT(*).
SELECT o.id,
(SELECT COUNT(*)
FROM (SELECT id,grp FROM inner_t
UNION ALL
SELECT id+100,grp FROM inner_t) i
WHERE i.grp <= o.grp) AS v
FROM outer_t o ORDER BY o.id;Additional information
This is a remaining coverage gap after the simple form fixed for #23942 / PR
#24053. The original #23942 shape now works; the cases above exercise planner
shapes that are still explicitly rejected in pkg/sql/plan/flatten_subquery.go.
Activity
dengn commentedon Oct 12, 2026
Additional BI regression evidence for the derived outer input / non-equality correlated aggregate coverage gap in this issue.
Tested environment
23d8f61252f3017dff218b530817a240206fd762(2026-10-09).SELECT git_version(), version()returned23d8f61252/8.0.30-MatrixOne-v.Observed impact
A 20,000-case analytical suite (100 SQL templates × 200 deterministic data scenarios; 14 tables / 356,840 rows) completed with 19,800 passes and 200 explicit rejections. All 200 rejections were the same rolling-distinct-visitors template: a scalar aggregate correlated to a DISTINCT day CTE via date-range conditions. These are 200 scenario variants of one unsupported query family, not 200 distinct bugs.
Each rejected case was then replayed twice serially: 400/400 reproduced error 20102. A
SELECT 1immediately after every error succeeded on the same connection. No process crash or wrong result was observed. Equivalent LEFT JOIN + GROUP BY rewrites matched the original independent expected results for all 200 scenarios, including empty input, duplicates, NULL-heavy, leap-day, and 2038 timestamp fixtures.Self-contained read-only reproducer (verified on the stated build)
Actual:
Independent expected result:
The oracle uses elapsed whole-unit timestamp differences (
trunc((epoch(end)-epoch(start))/86400)) rather than calendar-boundary counting. All loaded fixture rows/columns were independently compared and hashed against the oracle. A separate MySQL server comparison was not run for this follow-up, so this comment reports a reproducible NYI / functionality gap rather than a new correctness regression.This supports the existing issue's derived-outer-input restriction; no duplicate issue is needed. It should not be attributed to correlated DISTINCT aggregate issue #28300, which is closed and has a different rejection path.
Ariznawlll commentedon Oct 14, 2026
当前 main 的 MySQL 对照补充(2026-10-14)
2ce4e58af6f03b943ee288ffad81012766cf830amysql-compatibility-contract/README.md,未找到此类非等值相关聚合/相关 LIMIT 的已知限制条目。在最小数据集上,普通基表的非等值相关
MAX、等值相关MAX(基表、CTE、派生表)、非等值基表聚合、等值 CTE 标量子查询均与 MySQL 一致。将非等值相关聚合放入 CTE/派生表后则被 MO 明确拒绝:MySQL 返回
(1,10),(2,20),(3,30),(4,30);MO 每次返回ERROR 20102: non-equality correlated aggregate with outer composition cannot be safely decorrelated is not yet implemented。把同一查询改为派生表输入也稳定被拒绝,错误为non-equality correlated aggregate requires an accessible inner row marker is not yet implemented。同一 CTE 形态的相关非聚合
ORDER BY ... LIMIT 1也被correlated LIMIT with non-equality predicates is not yet implemented拒绝,而 MySQL 返回正确标量值。上述拒绝在 SELECT 投影、WHERE、ORDER BY、CTAS、VIEW 和 SQL PREPARE/EXECUTE 路径均复现;没有部分结果或数据修改。这补充了本 issue 的 CTE/派生表与查询消费路径证据;不是新建重复 issue,也未修改产品代码或添加测试用例。
XuPeng-SH commentedon Oct 23, 2026
Additional reproduction consolidated from #30872: a non-equality correlated aggregate with
GROUP BYandHAVINGis rejected with error 20102 both when the outer and inner inputs are a recursive CTE and when the same shape is run over a plain table. The equality-predicate recursive CTE control succeeds. This is additional coverage for the non-equality correlated aggregate planner restriction already tracked here; it is not specific to recursive CTEs.Recursive CTE reproducer:
The source report and its plain-table reproducer remain available at #30872. Consolidating tracking here does not claim a fix.
fix(plan): support complex non-equality scalar aggregates (#31275)
XuPeng-SH commentedon Oct 28, 2026
QA status for merged main
d5d1c5c8cc30055635840874768286457ceb5b08(#31275): this environment has not run the affected planner/compiler tests or current-head SQL. Current headroom under the existing guard is insufficient for a safe large link or service build. Original non-equality correlated aggregate cases, empty/NULL and duplicate semantics, prepared reuse and error recovery remain pending independent acceptance here. The PR's author-reported SQL and benchmark results are not local results. Assignee unchanged.slyunnn commentedon Nov 2, 2026
Regression and extended verification for #28298 (2026-11-02)
Verification Environment
mainf0a6ed145e07c84ecc80566ac659212c4a20debd—fix(function): preserve 64-bit complement of unsigned zero (#32009); freshly built runtime reportedgit_version() = f0a6ed1.2 CN / 1 TN / 1 LogService / Proxy. Proxy26833, CN127151, and CN227152were reachable using the MySQL CLI over TCP.8.0.46control. Derived-input, empty-input, and SQLPREPARE/EXECUTEreuse checks were also run through each CN.What Was Covered
MAX,COALESCE,CASE,HAVING,DISTINCT, groupedORDER BY ... LIMIT, derived outer input, two outer bindings, and derivedUNION ALLwithCOUNT(*).COUNT(*) = 0andMAX = NULL; SQL prepared reuse produced the expected results for matching and nonmatching parameters across all three endpoints.ERROR 1242 (21000)on both MatrixOne and MySQL.20,000-case BI suite, binary prepared protocol, full distributed BVT, or an asserted engine-parallel/multi-CN query plan.Findings
The reported original SQL shapes execute with the expected values on the pinned
main. The relevant merged fix is PR #31275, merge commit423b44495e8b91df703726a68f6950ba96963384, which selects dependent execution when the previous rewrite cannot safely preserve the correlated relation.MOTR / BVT Coverage
No new MOTR case was added; the merged fix already includes regression coverage. This run used real SQL against the isolated deployment and did not rerun the full BVT suite.
Final Conclusion
Fixed for the original issue SQL and the stated extensions on this pinned
main. Large-row-count regression is not required to establish this planner-shape and scalar-semantics fix; this run does not establish large-workload performance.