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 rejects COUNT(DISTINCT ...), SUM(DISTINCT ...),
AVG(DISTINCT ...), and GROUP_CONCAT(DISTINCT ...) when they are returned by
an equality-correlated scalar subquery:
aggregate count in a correlated scalar projection will be supported in a future version is not yet implemented
aggregate sum in a correlated scalar projection will be supported in a future version is not yet implemented
aggregate avg in a correlated scalar projection will be supported in a future version is not yet implemented
aggregate group_concat in a correlated scalar projection will be supported in a future version is not yet implemented
The same DISTINCT aggregates work when the subquery is uncorrelated, including
their empty-input results. Their non-DISTINCT correlated forms also work and
correctly restore 0 or NULL for an outer row with no inner match.
MIN(DISTINCT ...) and MAX(DISTINCT ...) are accepted because DISTINCT does
not change their result. The gap affects aggregate implementations with a
distinct state, whose IDs are not classified by the correlated scalar
projection's empty-result restoration logic.
Expected Behavior
The correlated DISTINCT aggregate queries should produce the same results as
MySQL 8.0.45. For outer row id=3, which has no matching inner row,
COUNT(DISTINCT ...) should return 0, while SUM, AVG, and GROUP_CONCAT
should return NULL.
Steps to Reproduce
CREATE DATABASE codex_correlated_distinct_0907;
USE codex_correlated_distinct_0907;
CREATE TABLE outer_t(id INT PRIMARY KEY);
CREATE TABLE inner_t(id INT PRIMARY KEY, v INT);
INSERT INTO outer_t VALUES (1),(2),(3);
INSERT INTO inner_t VALUES (1,101),(2,202),(4,404);
-- Controls: uncorrelated DISTINCT aggregates are supported.
SELECT COUNT(DISTINCT i.v), SUM(DISTINCT i.v), AVG(DISTINCT i.v),
GROUP_CONCAT(DISTINCT i.v)
FROM inner_t i
WHERE i.id=999;
-- Correlated DISTINCT aggregates are rejected.
SELECT o.id,
(SELECT COUNT(DISTINCT i.v) FROM inner_t i WHERE i.id=o.id) AS v
FROM outer_t o ORDER BY o.id;
SELECT o.id,
(SELECT SUM(DISTINCT i.v) FROM inner_t i WHERE i.id=o.id) AS v
FROM outer_t o ORDER BY o.id;
SELECT o.id,
(SELECT AVG(DISTINCT i.v) FROM inner_t i WHERE i.id=o.id) AS v
FROM outer_t o ORDER BY o.id;
SELECT o.id,
(SELECT GROUP_CONCAT(DISTINCT i.v)
FROM inner_t i WHERE i.id=o.id) AS v
FROM outer_t o ORDER BY o.id;
-- A mixed expression is rejected at the first DISTINCT aggregate as well.
SELECT o.id,
(SELECT COUNT(DISTINCT i.v) + COALESCE(SUM(DISTINCT i.v),0)
FROM inner_t i WHERE i.id=o.id) AS v
FROM outer_t o ORDER BY o.id;Additional information
The rejection is emitted by the empty-result restoration path in
pkg/sql/plan/flatten_subquery.go. The base aggregate IDs are classified in
pkg/sql/colexec/aggexec/empty_result.go, but the corresponding DISTINCT
aggregate IDs are not.
Activity
fix(plan): restore empty correlated DISTINCT aggregate results (#28642)
Ariznawlll commentedon Sep 14, 2026
测试版本
原问题验证
Happy Path 覆盖
Unhappy Path 覆盖
白盒验证
回归测试
./motr --repeat 3 14_issue_regression.issue_28187_28772_latest_main连续 3/3 通过(213/213 statements matched)。测试证据
测试结论
测试通过。
fix(plan): restore empty correlated DISTINCT aggregate results (matri…
fix(plan): restore empty correlated DISTINCT aggregate results (matri…
fix(plan): restore empty correlated DISTINCT aggregate results (matri…
dengn commentedon Oct 10, 2026
Cloud-instance reproduction on 2026-10-10
The correlated
GROUP_CONCAT(DISTINCT ...)case is stillreproducible on a MatrixOne Cloud instance reporting
8.0.30-MatrixOne-v4.2.1.The comparison server reports MySQL
8.3.0.The client is PyMySQL over the MySQL text protocol.
This issue already records a successful fix verification
on
main. I have not tested currentmain, and the commitof the cloud deployment is unknown. Please treat this as a
deployed-version/fix-availability follow-up.
Standalone synthetic reproduction
Run in a fresh scratch database:
MatrixOne returns:
MySQL succeeds: task 1 has the distinct set
{FAILED, READY}, task 2 hasREADY, and task 3 hasNULL.String order is not part of the expectation because the
aggregate has no
ORDER BYclause.Controls and verified workaround
Removing only
DISTINCTfrom the correlated subquery workson both servers: task 1 returns
READY,READY,FAILED, task 2returns
READY, and task 3 returnsNULL.Pre-aggregating and then joining also works on both servers:
The workaround preserves the distinct sets and empty-input
NULLresult. This query shape is used to summarize severaldocument states per application in a workflow inbox.
Both scratch databases were removed after the test.
Could the fix/backport and rollout availability for this
cloud deployment be checked?