Skip to content

[Bug]: COALESCE inside an empty correlated aggregate subquery returns NULL #25959

Closed

Description

@Ariznawlll

MatrixOne version

Latest main at 3046f14d94b3f1a25839eeeb65729b64f85ac647.

What happened?

When a correlated scalar aggregate subquery has no matching input rows, COALESCE or IFNULL inside the subquery is not evaluated. MatrixOne returns NULL instead of the requested fallback value.

The same aggregate works when the subquery is uncorrelated, and moving COALESCE outside the scalar subquery also works. This indicates that the correlated-subquery rewrite drops the aggregate result row for an empty input.

How to reproduce

CREATE TABLE parent(id INT PRIMARY KEY);
CREATE TABLE child(parent_id INT, v INT);
INSERT INTO parent VALUES (1), (2);
INSERT INTO child VALUES (1, 10);

SELECT p.id,
       (SELECT COALESCE(SUM(c.v), 0)
        FROM child c
        WHERE c.parent_id = p.id) AS total
FROM parent p
ORDER BY p.id;

Actual result

1  10
2  NULL

Expected result

1  10
2  0

MySQL 8.0.45 returns the expected result.

Scope

  • The issue is reproducible with SUM, AVG, MIN, and MAX wrapped by COALESCE.
  • COUNT(*) returns 0 correctly.
  • Wrapping the correlated source in a CTE has the same incorrect result:
SELECT p.id,
       (WITH x AS (SELECT v FROM child c WHERE c.parent_id = p.id)
        SELECT COALESCE(SUM(v), 0) FROM x)
FROM parent p;
  • Moving the fallback outside the subquery is a working control:
SELECT p.id,
       COALESCE((SELECT SUM(c.v) FROM child c WHERE c.parent_id = p.id), 0)
FROM parent p;

Activity

  1. added this to the MO-202607 milestone on Jul 21, 2026
  2. added
    kind/bug
    on Jul 21, 2026
  3. added theissue type on Jul 21, 2026
  4. VioletQwQ-0 commented on Aug 3, 2026

    @VioletQwQ-0
    Collaborator

    QA handoff

    PR #26510 merged with final head 55203b022b21cc79c73b64e2b65c032e82c41725 as merge commit d6800b98855b871459c3c4bad0ce73aa7321610f. The merge is present in current main d34a6c896f5f0c07f378861704520db548f404b8.

    Automated evidence:

    • canonical scalar_correlated_projection BVT passed 40/40 on the PR implementation head;
    • exact-head coverage, Proxy BVT, pessimistic standalone BVT, arm64 SCA, and build checks passed;
    • TKE BVT, SSB + TPCH, SYSBENCH, and TPCC completed successfully;
    • the exact-head Ubuntu UT job remains red from the previously diagnosed shared MOProtocolVersion test-state leak, not from the planner change.

    Remaining QA on an identified MatrixOne version/environment:

    1. Re-run the original [Bug]: COALESCE inside an empty correlated aggregate subquery returns NULL #25959 CTE query and require the missing-key result to be 0, not NULL.
    2. Verify the correlated JSON aggregate empty-input fallback returns [].
    3. Verify present, empty, and all-NULL inner-input controls and check that ordinary correlated aggregate behavior does not regress.

    PASS requires the tested version/SHA, environment, and terminal results for all scenarios. FAIL requires preserving the exact SQL, observed output/error, version, and environment. Keep the issue open and assigned to VioletQwQ-0 until terminal QA evidence is recorded.

  5. Ariznawlll commented on Aug 5, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:e31ee06042bc708ac5620a579715e07ec31c21b6
    • 部署方式:本地单 CN,默认配置
    • 测试日期:2026-08-05

    原问题验证

    重跑原始 CTE + 空相关聚合场景,三个外层键分别得到 12,0,0;缺失匹配键返回 0 而不是 NULL。

    Happy Path 覆盖

    1. 相关输入存在时,COALESCE(SUM(...),0) 返回正确合计 12。
    2. 相关输入为空时返回 0。
    3. 全 NULL 输入与空输入分别验证,普通相关聚合语义未回归。
    4. JSON 聚合分别返回 [5, 7]、[null]、[],区分有值、全 NULL 和空输入。

    Unhappy Path 覆盖

    1. 不适用:原问题覆盖的是合法空集/NULL 边界,不是非法输入;边界结果、NULL 状态以及后续会话可用性均已断言,无 panic 或连接中断。

    白盒验证

    本轮不以白盒作为终态依据;原始 CTE、相关聚合和 JSON 空集语义均通过公开 SQL 端到端验证。

    回归测试

    测试证据

    • motr commit:b770136206c88435a51599e4b317e522eddfd085。
    • 修复提交:d6800b98855b871459c3c4bad0ce73aa7321610f。

    测试结论

    测试通过。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugphase/testing

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions