Skip to content

[Compatibility]: DISTINCT aggregates are rejected in correlated scalar subqueries #28300

Closed

Description

@Ariznawlll

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

main

Commit ID

74668fc

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

  1. added this to the MO-202609 milestone on Sep 7, 2026
  2. added
    kind/bug
    on Sep 7, 2026
  3. added theissue type on Sep 7, 2026
  4. added
    severity/s0
    and removed on Sep 10, 2026
  5. added a commit that references this issue on Sep 11, 2026
    5c79a2b
  6. assigned and unassigned on Sep 12, 2026
  7. Ariznawlll commented on Sep 14, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:07b25969bfea9d4b76d3bf05d5b0497e4f36eba0
    • 部署:本地独立 1 CN / 1 TN / 1 LogService;每轮均重新启动服务后执行。
    • 执行:每项 SQL 回归连续 3 轮,输出逐轮一致。

    原问题验证

    • 相关标量子查询中的 DISTINCT 聚合已被接受。

    Happy Path 覆盖

    • 覆盖 parent=1 的 DISTINCT 关联聚合,count=2、sum=30。

    Unhappy Path 覆盖

    • 覆盖无匹配 parent,count=0、sum=NULL,查询正常完成。

    白盒验证

    • 以 MOTR 固定相关 DISTINCT 聚合结果。

    回归测试

    测试证据

    • 以上结果均在该 main commit 的独立环境获得;每轮无 stderr,三轮结果一致。

    测试结论

    测试通过。

  8. added 3 commits that reference this issue on Sep 17, 2026
    e62d56c
    9bf41e5
    83fc304
  9. dengn commented on Oct 10, 2026

    @dengn
    Contributor

    Cloud-instance reproduction on 2026-10-10

    The correlated GROUP_CONCAT(DISTINCT ...) case is still
    reproducible 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 current main, and the commit
    of 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:

    CREATE TABLE tasks (
        id INT PRIMARY KEY,
        state VARCHAR(16) NOT NULL DEFAULT 'PENDING'
    );
    
    CREATE TABLE documents (
        id INT PRIMARY KEY,
        task_id INT,
        status VARCHAR(16),
        present TINYINT
    );
    
    INSERT INTO tasks(id) VALUES (1), (2), (3);
    INSERT INTO documents VALUES
        (1, 1, 'READY', 1),
        (2, 1, 'READY', 1),
        (3, 1, 'FAILED', 1),
        (4, 2, 'READY', 1);
    
    SELECT t.id,
           (SELECT GROUP_CONCAT(DISTINCT d.status)
              FROM documents d
             WHERE d.task_id = t.id AND d.present = 1) AS file_states
    FROM tasks t
    ORDER BY t.id;

    MatrixOne returns:

    20102: aggregate group_concat in a correlated scalar projection will be supported in a future version is not yet implemented
    

    MySQL succeeds: task 1 has the distinct set
    {FAILED, READY}, task 2 has READY, and task 3 has NULL.
    String order is not part of the expectation because the
    aggregate has no ORDER BY clause.

    Controls and verified workaround

    Removing only DISTINCT from the correlated subquery works
    on both servers: task 1 returns READY,READY,FAILED, task 2
    returns READY, and task 3 returns NULL.

    Pre-aggregating and then joining also works on both servers:

    SELECT t.id, d.file_states
    FROM tasks t
    LEFT JOIN (
        SELECT task_id,
               GROUP_CONCAT(DISTINCT status) AS file_states
        FROM documents
        WHERE present = 1
        GROUP BY task_id
    ) d ON d.task_id = t.id
    ORDER BY t.id;

    The workaround preserves the distinct sets and empty-input
    NULL result. This query shape is used to summarize several
    document 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?

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

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions