Skip to content

[Bug]: MIN/MAX DISTINCT strips trailing spaces from promoted CHAR values #32373

Closed

Description

@XuPeng-SH

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

main

Commit ID

89ca3db

Actual Behavior

MIN(DISTINCT ...) and MAX(DISTINCT ...) can strip trailing spaces from the returned value when a value-selecting expression combines CHAR and VARCHAR. For the one-row reproduction below, the original expression and both ordinary aggregates return hex 6120, but both DISTINCT aggregates return 61.

Expected Behavior

MIN(DISTINCT E) and MAX(DISTINCT E) should preserve the same input value and return the same results as MIN(E) and MAX(E): 6120 in this example.

Steps to Reproduce

CREATE TABLE minmax_distinct_repro (c CHAR(8), v VARCHAR(8));
INSERT INTO minmax_distinct_repro VALUES (NULL, 'a ');

SET @saved_sql_mode = @@session.sql_mode;
SET SESSION sql_mode = '';

SELECT HEX(COALESCE(c, v)) AS raw_value FROM minmax_distinct_repro;
SELECT HEX(MIN(COALESCE(c, v))) AS min_plain,
       HEX(MIN(DISTINCT COALESCE(c, v))) AS min_distinct,
       HEX(MAX(COALESCE(c, v))) AS max_plain,
       HEX(MAX(DISTINCT COALESCE(c, v))) AS max_distinct
FROM minmax_distinct_repro;

SET SESSION sql_mode = @saved_sql_mode;
DROP TABLE minmax_distinct_repro;

Actual on the commit above: raw_value=6120, min_plain=6120, min_distinct=61, max_plain=6120, max_distinct=61.

Additional information

The binder removes the redundant DISTINCT flag for MIN/MAX but still applies DISTINCT-only PAD SPACE argument normalization. That normalization alters the value consumed by these value-returning aggregates. The problem was discovered during the separate performance investigation in #32060. Proposed fix: #32372.

Activity

  1. self-assigned this
    on Nov 8, 2026
  2. added
    kind/bug
    on Nov 8, 2026
  3. added a commit that references this issue on Nov 8, 2026
    d7185c9
  4. XuPeng-SH commented on Nov 8, 2026

    @XuPeng-SH
    ContributorAuthor

    QA 转测:#32372 已于 2026-11-08 15:47:02 UTC 合入 main,merge SHA d7185c9,修复本 issue 的 MIN/MAX DISTINCT 输入值被 DISTINCT-only PAD SPACE 规范化而丢失尾空格问题。

    已核对 issue 完整正文、全部评论(转测前为空)、合并范围及评审。未见当前适用的已验证反例或未完成的本问题开发 follow-up,交由 @Ariznawlll 验证;转测不代表验收通过。

    验证证据:作者报告 MIN/MAX planner 回归在基线失败、修复后通过,36/36 SQL BVT 在单 CN 两次及双 CN 两个协调端各两次通过,另有 131,072 行 forced-AP partial/final 聚合与值代表检查;planner normal/race 与增量静态检查通过。评审静态结论 approve,但其独立 Go 测试受工具链版本限制未启动,也未本地运行 SQL。以上为远端/作者报告,本轮未独立运行测试,未核验最新 CI 全量结果,不声称性能改善。

    建议 QA 核对原例 HEX 值 6120、MIN/MAX 与其 DISTINCT 形式一致性,并覆盖 NULL/空输入、字符模式、CHAR/VARCHAR 混合值、预处理参数类型变化、volatile 参数求值次数以及单/双 CN partial/final 聚合。仍保留 DISTINCT 的其他聚合规范化应不受影响。#32060 性能报告及其他 PAD SPACE 分组/聚合相等性缺陷不在本修复范围,不作为本 issue 已验收内容。

  5. MatrixAdventurer commented on Nov 9, 2026

    @MatrixAdventurer
    Contributor

    分级建议:
    severity/s2 —— DISTINCT 触发 PAD SPACE 归一化致 MIN/MAX 尾空格丢失,聚合正确性缺陷,需特定输入组合,面窄;修复 #32372 已入 main,本条剩验证收尾。
    模块:pkg/sql/plan(表达式折叠/DISTINCT 输入归一化)。
    疑似重复:未发现。
    建议负责人(/pkg/sql/plan CODEOWNERS):XuPeng-SH/aunjgr/aptend;验证由 assignee Ariznawlll 跟进。
    本操作由LW授权的MOI智能体提交

  6. Ariznawlll commented on Nov 9, 2026

    @Ariznawlll
    Collaborator

    结论

    测试通过。#32372 合入后的当前 main 未复现本 issue 的尾空格丢失。

    关键证据

    • 版本:main@c99234dcf4c94d1ff9d372917cd35041f1a3a682,隔离本地 CN,运行时 git_version() 为 c99234dcf4。
    • 按原始一行用例,在 3 个独立数据库重复验证:原表达式、普通 MIN/MAX、MIN(DISTINCT)/MAX(DISTINCT) 的 HEX 均为 6120。
    • 已确认修复 PR fix(sql): preserve MIN/MAX DISTINCT input values #32372 合入 main 的 merge SHA 为 d7185c9a926128ebbbc2de83902fdf37bbc3d959。

    待解决点

    • 本轮仅核验 issue 原始 CHAR/VARCHAR 尾空格用例,不代表覆盖其他 PAD SPACE 分组或聚合行为。
  7. slyunnn commented on Nov 11, 2026

    @slyunnn

    Regression and extended verification for #32373 (2026-11-11)

    Verification Environment

    • Frozen latest main at test start, 45917e803f2915f6f0838b2b2fe9f591fc5b6018, fix(plan): fold contradictory same-column predicates to near-zero estimates (#32502).
    • Fresh CPU binary with matching verified native-library inputs. Runtime git_version()=45917e803f through Proxy 6001, CN1 16001 and CN2 16002. All three endpoints executed the issue checks.
    • Co-hosted 2 CN / 1 TN / 1 Log service / Proxy, macOS arm64, Go 1.27.1.
    • PyMySQL text protocol and JDBC BVT. Actual Go database/sql + go-sql-driver/mysql, interpolateParams=false, supplied native prepared-statement evidence for MIN/MAX parameter changes and physical-index role changes.

    What Was Covered

    • OK — Original single-row expression and ordinary/DISTINCT MIN and MAX all return exact HEX value 6120 through every endpoint.
    • OK — Existing 36/36-statement BVT passed at each endpoint, including empty/NULL groups, CHAR/VARCHAR, padding mode, distinct-count controls, parameter type changes and volatile evaluation counts.
    • OK — Actual Go binary prepared execution reused one statement with text, bytes, NULL, numeric and text parameters again.
    • OK — 131,072 persisted rows preserve both extrema. A separate same-source native fixture used the existing forced-multi-CN switch and asserted an actual peer address and merge group in the physical plan, proving remote partial/final aggregation.
    • Not covered — Performance claims from [Performance]: COUNT(DISTINCT) forced to single stage and one CN, 34x slower than GROUP BY #32060 and other PAD SPACE grouping defects.

    Findings

    • Associated fix fix(sql): preserve MIN/MAX DISTINCT input values, merge d7185c9a926128ebbbc2de83902fdf37bbc3d959 is included in the frozen main. PR #32372

    MOTR / BVT Coverage

    • Reused the maintained product BVT described above. Existing coverage is sufficient, so no duplicate MOTR case was added.
    • Combined weekly branch passed 9 cases with -j 5 --repeat 2 through each endpoint, 54/54 executions total. The new cases use separate databases, roles and connections, with no global-variable changes. This is runner concurrency.
    • Targeted product tests recorded 199 test/subtest passes across 7 packages with selected tests. The independent forced-two-CN aggregate fixture also passed.

    Final Conclusion

    Fixed for the original issue contract on the verified build and paths.

    Large-data regression — Required for the partial/final aggregation boundary. 131,072 persisted rows and a forced two-CN native fixture completed. The external three-endpoint runs and the native forced-placement proof are recorded separately. No TKE performance run is needed for the value-preservation contract.

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

Metadata

Metadata

Assignees

Labels

kind/bugneeds-triage

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions