Skip to content

[Bug]: COALESCE and IFNULL CHAR keys lose PAD SPACE semantics #28024

Closed

Description

@Ariznawlll

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' returns 1 for ids 1 and 2.
  • GROUP BY COALESCE(ch, '') returns three groups, and DISTINCT COALESCE(ch, '') returns three rows. IFNULL has 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 VARCHAR promotion 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, '') and IFNULL(ch, '') split in GROUP BY, DISTINCT, and windows in every run.
  • CASE WHEN id > 0 THEN ch ELSE '' END has 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

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

  1. added
    kind/bug
    on Sep 2, 2026
  2. added theissue type on Sep 2, 2026
  3. added this to the MO-202609 milestone on Sep 2, 2026
  4. XuPeng-SH commented on Nov 22, 2026

    @XuPeng-SH
    Contributor

    QA 交接:COALESCE/IFNULL 选择 CHAR 值时的 PAD SPACE 键语义

    #32002 已合入 main,合并 SHA d9a43b3156dd6ee54f14bdd4197071e522efd285,最终 PR head a2b88df5。

    独立定向报告确认 COALESCE/IFNULL/CASE 的相等比较、GROUP BY、DISTINCT、UNION 和窗口键对照通过。 上述独立 QA 证据对应较早候选 005ea33b,不是当前合并版的实测。正式评审针对较早 head b1bc02f6,最终 head 的 Iceberg E2E 通过;合并队列记录未显示 Required 已满足。未在当前合并提交复跑,不推定 4.2 或整体 CI 通过。

    请在合入树验原始结果、窗口 peer/分区、显式 VARCHAR 对照;INTERSECT 未独立验证,另行纳入;不扩到独立的 #28023。 转测不等于验收通过,工单保持开放。

  5. Ariznawlll commented on Nov 23, 2026

    @Ariznawlll
    CollaboratorAuthor

    结论

    通过。官方 main 在测试开始时的最新提交 c8805c3e2613bcb53f279035fdf2e2337c86a68e 上,所测 CHAR 值选择表达式在相等比较、聚合键、去重、窗口和集合运算中均保留 PAD SPACE 等价关系。本次仅验证 main。

    关键证据

    • 环境:macOS arm64 隔离单机 MO,1 CN / 1 TN / 1 LogService;git_version() 与二进制构建记录为上述完整 SHA,#32002 合并提交包含在测试版本中。
    • Happy:原始三行 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。
    • 扩展:对三种表达式分别执行 UNION 与 INTERSECT,带空格和不带空格的值各归为一个逻辑键;显示值可能保留不同数量的尾随空格,测试同时检查等价类而不强求任意代表值。显式 VARCHAR 转换控制得到两组。
    • Unhappy:NULL 经 COALESCE 回退为空字符串,测试后源表行数仍正确。白盒执行 TestMixedPadSpaceGroupKeyRetention、TestPromotedPadSpaceComparisonBuiltinsUseCanonicalArguments 各 3 次通过,覆盖键语义及规范化的计划控制。
    • 正式 MOTR runner 3/3 通过,0 失败、0 跳过;回归追加到同一个 MOTR PR #374,提交 1b6d0977d6612c2d392573bf69877b65486fdb97,限定 main。该提交的 PR CI 尚在执行,不能表述为已通过;按用户要求回归 PR 提交后先关闭。

    待解决点

    无本 Issue 所测范围内的阻塞项;未以此替代独立问题 #28023 或声称 4.2-dev 已验收。

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

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions