Description
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 return30; a constant left branch plus table-backed right branch returns30. - 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
Ariznawlll commentedon Oct 12, 2026
补充:错误列值不只影响聚合,还会写入表
测试版本:
main/5a5501b3e1048f3d19d96824cc68a62b7f0fcb07;对照:MySQL 8.0.46。下面每个最小查询重复 3 次,结果一致。1. 普通 SELECT 已读错列
2, 1010, 20右分支本应返回第二列
20,实际返回了第一列2。即使把条件改成WHERE 1=1,问题仍出现;不需要真的过滤掉行。反转两个分支的顺序后,右分支的10也会被错换成1。2. 字符串错值会持久化
'2','ten''ten','twenty'显式
CAST后查询成功,但右分支的'twenty'被写成'2'。同一表达式经普通 View、UPDATE 也会传播错值;SQL PreparedStatement 的参数从0改成1后,MO 返回'2',MySQL 返回'twenty'。3.
NULL也会被错换并经 CTAS 写入2,10NULL,10这里的
2同样来自右分支第一列;预期的第二列是NULL。相关边界: 相近形态的
UNION(去重)、INTERSECT、EXCEPT报投影宽度不匹配,见 #29989;字符串不经CAST直接投影会触发内部错误并影响同一连接的后续响应,见 #30010。本 issue 仅追踪查询成功但返回或写入错误数据的情况。fix(sql): preserve filtered UNION ALL right branch columns (#30015)
Ariznawlll commentedon Oct 13, 2026
测试版本
main;commit:175a645a85994f2c95992d3aa8d6dacb6d9e167f;2026-10-13。原问题验证
过滤的 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 运行。
测试结论
测试通过。
fix: preserve set-operation columns through remapping (#30028)