Repository navigation
Description
Description
COALESCE and IFNULL over a CHAR column lose PAD SPACE key semantics in GROUP BY, DISTINCT, and window keys. The same expression still compares equal to the unpadded literal, so logically equivalent values take different code paths depending on the SQL operator.
Environment
- Branch:
main - Commit:
3ec2c2524f88cb3365b9bc464d02458c9a77798a - Deployment: local official-main build, 1 CN / 1 TN / 1 Log
Steps to reproduce
CREATE DATABASE char_expression_key_probe;
USE char_expression_key_probe;
CREATE TABLE t (id INT PRIMARY KEY, ch CHAR(8));
INSERT INTO t VALUES (1, 'a'), (2, 'a '), (3, 'b');
-- Normal comparison recognizes ids 1 and 2 as equal.
SELECT id, COALESCE(ch, '') = 'a' AS eq_a FROM t ORDER BY id;
-- But COALESCE/IFNULL keys split the same two values.
SELECT COALESCE(ch, '') AS x, COUNT(*) FROM t GROUP BY x;
SELECT DISTINCT IFNULL(ch, '') AS x FROM t;
SELECT id, COUNT(*) OVER (PARTITION BY COALESCE(ch, '')) AS pc,
RANK() OVER (ORDER BY COALESCE(ch, '')) AS rk
FROM t ORDER BY id;
-- Explicit promotion is a control.
SELECT CAST(COALESCE(ch, '') AS VARCHAR(8)) AS x, COUNT(*) FROM t GROUP BY x;Actual behavior
COALESCE(ch, '') = 'a'returns1for ids 1 and 2.GROUP BY COALESCE(ch, '')returns three groups, andDISTINCT COALESCE(ch, '')returns three rows.IFNULLhas the same three-way split.- The window query returns
(id,pc,rk)as(1,1,1),(2,1,2),(3,1,3); rows 1 and 2 are different partitions and different peers. - Explicit
VARCHARpromotion returns two groups and merges ids 1 and 2 in the window control.
Expected behavior
For expressions that choose and return the original CHAR value, key operations should preserve the same PAD SPACE relation as ordinary equality. Since ids 1 and 2 compare equal under the expression, they should be one group, one DISTINCT value, one window partition, and one ordered peer group.
Stability and controls
- Reproducer: 3/3 identical results on current
main. - Affected expressions:
COALESCE(ch, '')andIFNULL(ch, '')split in GROUP BY, DISTINCT, and windows in every run. CASE WHEN id > 0 THEN ch ELSE '' ENDhas the same split in window keys; its GROUP BY/DISTINCT result is a separate planner path and remains two groups.- Controls: ordinary equality returns true for both padded variants; explicit
CAST(... AS VARCHAR(8))returns two groups and the merged window result. - Failure atomicity: read-only queries left the three source rows unchanged.
Code analysis
The planner records PAD SPACE provenance on value-selecting expressions only when the result type is VARCHAR or TEXT (pkg/sql/plan/base_binder.go, BindFuncExprImplByPlanExpr). These expressions are typed as CHAR after resolution (INFORMATION_SCHEMA.COLUMNS for a view exposes CHAR), so the physical-key construction used by GROUP BY/DISTINCT and the window-key normalization do not receive durable PAD SPACE metadata after expression materialization. This is a focused hypothesis supported by the explicit-VARCHAR control; the repair should preserve the metadata or derive a canonical comparison key without changing visible CHAR values.
Related
- [Bug]: Window sort path splits PAD SPACE CHAR values into different peers #28023: direct
CHARwindow keys are a separate current-main bug. This issue concerns value-selectingCHARexpressions and also affects GROUP BY/DISTINCT. - [Bug]: CHAR column retrieval preserves trailing spaces unlike MySQL #25240: open CHAR retrieval compatibility issue; this report concerns incorrect relational key semantics after expression evaluation.
Regression coverage
After a fix, add planner/runtime coverage for COALESCE, IFNULL, and CASE over padded CHAR values across equality, GROUP BY, DISTINCT, UNION/INTERSECT and window PARTITION BY/ORDER BY; include an explicit-VARCHAR control. No test file is added in this exploration pass.
Activity
Fix SQL compatibility regressions across aggregates, casts, and prepa…
XuPeng-SH commentedon Nov 22, 2026
QA 交接:COALESCE/IFNULL 选择 CHAR 值时的 PAD SPACE 键语义
#32002 已合入
main,合并 SHAd9a43b3156dd6ee54f14bdd4197071e522efd285,最终 PR heada2b88df5。独立定向报告确认 COALESCE/IFNULL/CASE 的相等比较、GROUP BY、DISTINCT、UNION 和窗口键对照通过。 上述独立 QA 证据对应较早候选
005ea33b,不是当前合并版的实测。正式评审针对较早 headb1bc02f6,最终 head 的 Iceberg E2E 通过;合并队列记录未显示 Required 已满足。未在当前合并提交复跑,不推定 4.2 或整体 CI 通过。请在合入树验原始结果、窗口 peer/分区、显式 VARCHAR 对照;INTERSECT 未独立验证,另行纳入;不扩到独立的 #28023。 转测不等于验收通过,工单保持开放。
Ariznawlll commentedon Nov 23, 2026
结论
通过。官方
main在测试开始时的最新提交c8805c3e2613bcb53f279035fdf2e2337c86a68e上,所测 CHAR 值选择表达式在相等比较、聚合键、去重、窗口和集合运算中均保留 PAD SPACE 等价关系。本次仅验证 main。关键证据
git_version()与二进制构建记录为上述完整 SHA,#32002 合并提交包含在测试版本中。CHAR(8)数据'a'、'a '、'b'上,COALESCE、IFNULL、CASE 三种表达式的等值比较结果分别为1,1,0;GROUP BY 分为a:2和b:1两组,DISTINCT 两行;窗口 PARTITION 计数为2,2,1,RANK 为1,1,3。TestMixedPadSpaceGroupKeyRetention、TestPromotedPadSpaceComparisonBuiltinsUseCanonicalArguments各 3 次通过,覆盖键语义及规范化的计划控制。1b6d0977d6612c2d392573bf69877b65486fdb97,限定 main。该提交的 PR CI 尚在执行,不能表述为已通过;按用户要求回归 PR 提交后先关闭。待解决点
无本 Issue 所测范围内的阻塞项;未以此替代独立问题 #28023 或声称 4.2-dev 已验收。