Skip to content

[Bug]: Window sort path splits PAD SPACE CHAR values into different peers #28023

Closed

Description

@Ariznawlll

Description

Window PARTITION BY and ORDER BY on a direct CHAR column treat trailing-space variants as distinct, while the same column is equal under =/WHERE and forms one group under GROUP BY. This changes window partition membership, peer ranking, and RANGE aggregate results.

Environment

  • Branch: main
  • Commit: 3ec2c2524f88cb3365b9bc464d02458c9a77798a
  • Deployment: local official-main build, 1 CN / 1 TN / 1 Log

Steps to reproduce

CREATE DATABASE char_window_key_probe;
USE char_window_key_probe;
CREATE TABLE t (id INT PRIMARY KEY, ch CHAR(8), v INT);
INSERT INTO t VALUES (1, 'a', 10), (2, 'a  ', 20), (3, 'b', 30);

-- The ordinary relational keys agree that rows 1 and 2 are equal.
SELECT id, ch = 'a' AS eq_a FROM t ORDER BY id;
SELECT COUNT(*) AS group_count FROM (SELECT ch FROM t GROUP BY ch) g;

-- Direct CHAR window keys disagree with those relational keys.
SELECT id,
       COUNT(*) OVER (PARTITION BY ch) AS partition_count,
       RANK() OVER (ORDER BY ch) AS rank_value,
       SUM(v) OVER (ORDER BY ch RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS range_sum
FROM t ORDER BY id;

-- Explicit VARCHAR promotion is a control that uses the PAD SPACE comparison cast.
SELECT id,
       COUNT(*) OVER (PARTITION BY CAST(ch AS VARCHAR(8))) AS partition_count,
       RANK() OVER (ORDER BY CAST(ch AS VARCHAR(8))) AS rank_value,
       SUM(v) OVER (ORDER BY CAST(ch AS VARCHAR(8)) RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS range_sum
FROM t ORDER BY id;

Actual behavior

  • ch = 'a' is 1 for rows 1 and 2, and GROUP BY ch produces two groups (a and b).
  • The direct-CHAR window query returns (id, partition_count, rank_value, range_sum) as (1,1,1,10), (2,1,2,30), (3,1,3,60).
  • The VARCHAR control returns (1,2,1,30), (2,2,1,30), (3,1,3,60).

Thus the raw window path splits rows that the server itself considers equal, and also makes them different RANGE peers. The source table remains three rows with SUM(v)=60 after every query.

Expected behavior

Window partition and ordering keys should use the same PAD SPACE equality relation as = and GROUP BY for a direct CHAR key. Rows 1 and 2 should share the partition and peer group.

Stability and controls

  • Reproducer: 3/3 identical results on current main.
  • Controls: equality, WHERE, and GROUP BY merge the two values in all three runs; explicit CAST(ch AS VARCHAR(8)) gives the merged window result in all three runs.
  • Query-boundary variants: direct CHAR also remains split through both a derived table and a CTE (3/3).
  • Failure atomicity: read-only queries left the three source rows and aggregate total unchanged.

Code analysis

bindWindowSpec calls appendPadSpaceComparisonCastIfNeeded for PARTITION BY and ORDER BY (pkg/sql/plan/window_binder.go). That helper only adds the comparison cast when the expression is VARCHAR/TEXT with PAD SPACE provenance; a direct CHAR expression is returned unchanged. The Node_PARTITION plan consequently contains raw t.ch, unlike the explicit-VARCHAR control. Existing TestWindowPadSpaceKeysUseCanonicalArguments covers a promoted CHAR/VARCHAR expression, not a bare CHAR column.

Related

Regression coverage

A focused planner/runtime regression should cover direct CHAR PARTITION BY, RANK() OVER (ORDER BY ...), and an implicit/RANGE peer aggregate, with the equality and GROUP BY controls. 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. modified the milestones: MO-202608, MO-202609 on Sep 2, 2026
  4. added
    ai-easy
    on Sep 4, 2026
  5. Ariznawlll commented on Sep 4, 2026

    @Ariznawlll
    CollaboratorAuthor

    验证结论(最新 main):

    • 基线:MatrixOne main e6b25ee7726596514f520925815692c1a44031e1,129 TKE,3 CN + 1 DN + 3 Log + 2 Proxy;2026-09-04。
    • Happy case:CHAR(8) 值 a 与 a 在 PARTITION BY、RANK 及 RANGE window 中按 PAD SPACE 规则归为同一组,结果分别为 partition_count=2、rank=1、range_sum=30;b 为独立组,结果正确。
    • Unhappy/control:等值比较和 GROUP BY 同样将 a/a 合并,返回 eq_a=1,1,0、分组数 2;未见错误或数据副作用。
    • MOTR:已加入 PR #143 的 issue_28023_char_window,并行执行,3/3 成功。
    • 不需要 big-data、PITR、snapshot、stability 或 chaos;问题是确定性的 CHAR PAD SPACE window 语义。

    测试通过。

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

Metadata

Metadata

Assignees

Labels

ai-easykind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions