Skip to content

[Bug]: filtered UNION ALL feeds wrong column values to aggregates and persists incorrect results #30007

Description

@Ariznawlll

Description

A filtered UNION ALL with a no-FROM constant right branch can silently feed the right branch's first column into an outer aggregate over its second column. Unlike the width-mismatch error in #29989, this query succeeds and persists incorrect data through INSERT ... SELECT and CTAS.

Environment

  • Branch: main
  • Commit: 5a5501b3e1048f3d19d96824cc68a62b7f0fcb07 (fixed official-main checkout tested on 2026-10-12)
  • Deployment: local 1 LOG + 1 TN + 1 CN; MySQL 8.0.46 control

Steps to reproduce

SELECT SUM(eid) AS total
FROM (SELECT 1 AS point, 10 AS eid
      UNION ALL SELECT 2, 20) AS u
WHERE point > 0;

CREATE TABLE aggregate_target(total INT);
INSERT INTO aggregate_target
SELECT SUM(eid)
FROM (SELECT 1 AS point, 10 AS eid
      UNION ALL SELECT 2, 20) AS u
WHERE point > 0;
SELECT total FROM aggregate_target;

Actual behavior

MatrixOne returns and stores 12, not 30. With point > 1, a prepared statement returns 2, not 20. MIN(eid), MAX(eid), AVG(eid), and GROUP_CONCAT(eid ORDER BY eid) also consume incorrect values. CREATE TABLE ... AS SELECT SUM(eid) ... persists the wrong aggregate result.

EXPLAIN VERBOSE for the minimal SUM query shows the UNION ALL left child output 10, but its right child Project output 2 where the second-column value should be 20. The right branch has therefore been mapped to the wrong output position. This plan observation identifies the symptom; the exact faulty remapping step still needs code investigation.

Expected behavior

Both branches must preserve the set-operation column order: SUM(eid) and persisted rows should be 30.

Stability and controls

  • Minimal scalar aggregate: 3/3 wrong results. Prepared values 0 and 1: 3/3 each. INSERT and CTAS also persisted wrong values.
  • The same aggregate without the outer filter returns 30; two table-backed UNION ALL branches return 30; a constant left branch plus table-backed right branch returns 30.
  • The corresponding MySQL 8.0.46 queries return and persist 30.
  • The separate plain projection query fails with width-mismatch error 20101 instead of silently returning bad data; failed INSERT leaves its target unchanged and failed CTAS leaves no table.

Code analysis

The relevant set-operation remapping and width validation are in pkg/sql/plan/query_builder.go near remapAllColRefs (around lines 1496-1610 at this SHA). Hypothesis: the outer aggregate's single-column dependency allows the right constant branch to retain the wrong positional slot; verify during the fix.

Regression coverage

Add direct and prepared aggregate assertions, both branch orders, table-backed controls, and INSERT/CTAS persisted-value checks.

Related

#29989 covers an explicit width-mismatch error for a filtered set-operation projection. This issue tracks the distinct silent wrong-result and persisted-data outcome on the same general remapping path; they may share a root cause and fix.

Activity

  1. added this to the MO-202610 milestone on Oct 12, 2026
  2. added
    kind/bug
    on Oct 12, 2026
  3. added theissue type on Oct 12, 2026
  4. Ariznawlll commented on Oct 12, 2026

    @Ariznawlll
    CollaboratorAuthor

    补充:错误列值不只影响聚合,还会写入表

    测试版本:main / 5a5501b3e1048f3d19d96824cc68a62b7f0fcb07;对照:MySQL 8.0.46。下面每个最小查询重复 3 次,结果一致。

    1. 普通 SELECT 已读错列

    SELECT v
    FROM (SELECT 1 AS k, 10 AS v
          UNION ALL
          SELECT 2, 20) AS u
    WHERE k > 0
    ORDER BY v;
    MatrixOne 实际结果 MySQL 结果
    2, 10 10, 20

    右分支本应返回第二列 20,实际返回了第一列 2。即使把条件改成 WHERE 1=1,问题仍出现;不需要真的过滤掉行。反转两个分支的顺序后,右分支的 10 也会被错换成 1。

    2. 字符串错值会持久化

    CREATE TABLE dst (v VARCHAR(30));
    INSERT INTO dst
    SELECT CAST(v AS CHAR)
    FROM (SELECT 1 AS k, 'ten' AS v
          UNION ALL
          SELECT 2, 'twenty') AS u
    WHERE k > 0;
    SELECT v FROM dst ORDER BY v;
    MatrixOne 表中实际值 MySQL 表中值
    '2', 'ten' 'ten', 'twenty'

    显式 CAST 后查询成功,但右分支的 'twenty' 被写成 '2'。同一表达式经普通 View、UPDATE 也会传播错值;SQL PreparedStatement 的参数从 0 改成 1 后,MO 返回 '2',MySQL 返回 'twenty'。

    3. NULL 也会被错换并经 CTAS 写入

    CREATE TABLE dst_null AS
    SELECT v
    FROM (SELECT 1 AS k, 10 AS v
          UNION ALL
          SELECT 2, NULL) AS u
    WHERE k > 0;
    SELECT v FROM dst_null ORDER BY v;
    MatrixOne 表中实际值 MySQL 表中值
    2, 10 NULL, 10

    这里的 2 同样来自右分支第一列;预期的第二列是 NULL。

    相关边界: 相近形态的 UNION(去重)、INTERSECT、EXCEPT 报投影宽度不匹配,见 #29989;字符串不经 CAST 直接投影会触发内部错误并影响同一连接的后续响应,见 #30010。本 issue 仅追踪查询成功但返回或写入错误数据的情况。

  5. added a commit that references this issue on Oct 13, 2026
    175a645
  6. Ariznawlll commented on Oct 13, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main;commit:175a645a85994f2c95992d3aa8d6dacb6d9e167f;2026-10-13。
    • 环境:本地隔离 MatrixOne 服务;按用例使用真实 SQL、MySQL 二进制协议或异步索引。

    原问题验证

    过滤的 UNION ALL 数值投影和反转分支均返回 10、20;字符串投影 INSERT 后持久化 ten、twenty;普通 View 同值;NULL 投影经 CTAS 得到 2 行、1 个非 NULL、总和 10。 对应定向用例在 MOTR 入口重复执行 3 次均通过(每次脚本内再执行 3 轮)。

    Happy Path 覆盖

    无过滤对照与过滤结果一致;聚合和写入后的读取符合独立预期。

    Unhappy Path 覆盖

    专门检查曾被错换的右分支值和 NULL 持久化,未出现用 key 2 代替 value 的静默错值;同连接仍可查询。

    白盒验证

    本次以真实 SQL/协议行为直接判定;没有把仅凭源码推断当作运行结果。

    回归测试

    MOTR PR #234 中的 issue_30007_union_projection.py 已提交;离线 CI 通过,仍待正常 review/merge。该缺陷由确定性语义/类型/协议边界触发,不需要另加 big-data 或 chaos 才能验收。

    测试证据

    PR 验证记录及 CI 运行。

    测试结论

    测试通过。

  7. added a commit that references this issue on Oct 13, 2026
    de4bec0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s-1

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions