Repository navigation
Description
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'is1for rows 1 and 2, andGROUP BY chproduces two groups (aandb).- The direct-
CHARwindow query returns(id, partition_count, rank_value, range_sum)as(1,1,1,10),(2,1,2,30),(3,1,3,60). - The
VARCHARcontrol 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, andGROUP BYmerge the two values in all three runs; explicitCAST(ch AS VARCHAR(8))gives the merged window result in all three runs. - Query-boundary variants: direct
CHARalso 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
- Closed [Bug]: Hash window partition changes CHAR trailing-space partition membership #27952 identified a HASH-versus-SORT discrepancy for the same class of values. This report is distinct: current official
mainselects the legacy Sort/Partition path and that path itself disagrees with ordinary equality and grouping semantics. - Open [Bug]: CHAR column retrieval preserves trailing spaces unlike MySQL #25240 concerns CHAR retrieval retaining trailing spaces; this report concerns window key semantics and incorrect result rows.
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
Ariznawlll commentedon Sep 4, 2026
验证结论(最新 main):
e6b25ee7726596514f520925815692c1a44031e1,129 TKE,3 CN + 1 DN + 3 Log + 2 Proxy;2026-09-04。CHAR(8)值a与a在PARTITION BY、RANK及RANGEwindow 中按 PAD SPACE 规则归为同一组,结果分别为 partition_count=2、rank=1、range_sum=30;b为独立组,结果正确。a/a合并,返回eq_a=1,1,0、分组数 2;未见错误或数据副作用。issue_28023_char_window,并行执行,3/3 成功。测试通过。