Skip to content

fix(sql): preserve MIN/MAX DISTINCT input values - #32372

Merged
XuPeng-SH merged 2 commits into
mainfrom
codex/fix-distinct-minmax-pad-values
Nov 8, 2026
Merged

XuPeng-SH merged 2 commits into
mainfrom
codex/fix-distinct-minmax-pad-values

Conversation

@XuPeng-SH

@XuPeng-SH XuPeng-SH commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • BUG

Which issue(s) this PR fixes:

Fixes #32373

Discovered while investigating #32060; this PR addresses a separate correctness defect.

What this PR does / why we need it:

The binder already discards DISTINCT for MIN and MAX, because duplicate elimination cannot change their result. It nevertheless applies DISTINCT-only PAD SPACE normalization to the argument. For c CHAR(8) = NULL and v VARCHAR(8) = 'a ', HEX(MAX(COALESCE(c, v))) returns 6120, but HEX(MAX(DISTINCT COALESCE(c, v))) returns 61.

Only apply that DISTINCT-only normalization when the bound aggregate actually retains the DISTINCT flag. The existing ordinary MIN/MAX input and comparison path is then shared by their DISTINCT forms; aggregates that still retain DISTINCT keep their existing normalization.

Validation:

  • Planner regression for both MIN and MAX fails on the base and passes with this change; existing true DISTINCT controls remain covered.
  • Public SQL BVT: 36/36 statements on an isolated single-CN instance twice and on both coordinators of an isolated two-CN instance twice each. Includes null/empty input, character modes, prepared-parameter type changes, and volatile arguments.
  • A forced-AP two-CN workload checks partial/final aggregation on 131,072 rows; a separate ordering and materialized-column control checks the selected representatives.
  • Full pkg/sql/plan normal and race tests passed. After a test-local preallocation adjustment, the changed tests passed normal and race again. Incremental gofmt, vet, molint, and golangci-lint passed for pkg/sql/plan and pkg/sql/compile.
  • Baseline/candidate SQL latency samples were noisy and the buggy baseline returns different values for padded DISTINCT inputs; they do not establish a performance improvement or a general latency bound.

The performance report in #32060 and other PAD SPACE grouping and DISTINCT-aggregate equality defects found during that investigation remain outside the scope of this change. CI is pending.

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@XuPeng-SH

XuPeng-SH commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor Author

mo-bot review · mo-bot-local · superseded

Commit: d2f2a658bed6df3c9f6cf378dccd598021c165a1

Review linked issue or PR description changed before publication; a fresh contextual review is queued.

@XuPeng-SH

XuPeng-SH commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor Author

mo-bot review · mo-bot-local · done

Commit: d2f2a658bed6df3c9f6cf378dccd598021c165a1

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mo-bot review · mo-bot-local · review

Commit: d2f2a658bed6df3c9f6cf378dccd598021c165a1

Assessment: approve

The guard makes argument normalization follow the existing retained DISTINCT flag, restoring ordinary MIN/MAX value semantics while preserving true DISTINCT paths. It reuses existing behavior without adding mechanisms; no actionable findings remain.

Model: gpt-6.1-sol / max

Session: 01a1a004-90c3-73b1-acc9-3d4636452003 · slot 0 · generation 0

Validation:

  • Reviewed all four changed files, affected owners and consumers, SQL value and evaluation-count oracles, both linked issues and all three discussion items.

  • Isolation preflight passed: checks/1794151892343461269-3.json and preflight.log.

  • Static audit passed: frozen head, clean source, patch content, git diff --check, gofmt, 36 SQL/result statement echoes and complete discussion coverage; checks/1794152121964531005-3.json and static-audit-final.log.

  • Author reports planner normal/race tests and repeated 1CN/2CN SQL validation; these are reported evidence, not independent local passes.

Limitations:

  • Focused Go tests could not start: installed Go 1.26.6 cannot satisfy required Go 1.27.1. Automatic toolchain recovery timed out after 60 seconds; receipts and logs are retained in validation-ledger.json.

  • No local SQL cluster execution; author execution logs were not supplied and CI remains partly pending.

@XuPeng-SH
XuPeng-SH merged commit d7185c9 into main Nov 8, 2026
@XuPeng-SH
XuPeng-SH deleted the codex/fix-distinct-minmax-pad-values branch November 8, 2026 15:47
@XuPeng-SH
XuPeng-SH restored the codex/fix-distinct-minmax-pad-values branch November 8, 2026 15:47
@XuPeng-SH
XuPeng-SH deleted the codex/fix-distinct-minmax-pad-values branch November 8, 2026 15:47
XuPeng-SH added a commit that referenced this pull request Nov 8, 2026
## What type of PR is this?

- [x] Test and CI

## Which issue(s) this PR fixes:

Refs #32373, #32369, #32372 and #32368.

## What this PR does / why we need it:

Add 4.2 regression coverage for the aggregate input contracts addressed
by the two main-branch PRs. Their production patches do not apply to the
current 4.2 implementation:

- MIN/MAX discard DISTINCT without adding the main branch's
DISTINCT-only PAD SPACE normalization. The SQL case checks exact
returned bytes, NULL/empty inputs, and prepared execution; a planner
test checks that DISTINCT preserves the bound input.
- Group evaluates a batch once and returns build/evaluation errors; it
spills after a complete batch rather than retrying a partially processed
batch. Tests inject capacity errors during evaluation and after one
completed hash chunk, and exercise ordinary spill/reload with exact
results and evaluation counts. The errors are synthetic; this branch
does not have the upstream capacity-recovery path.

There are no production-code or query hot-path changes.

Validation on 4.2-dev base `0351bc89d7478ccd908fbc7b084ff0dd71a0ee80`
(Go 1.26.4, Linux/amd64):

- Full normal tests for `pkg/sql/plan` and `pkg/sql/colexec/group`;
focused new tests in normal and race modes passed.
- Local single-CN SQL: exact trailing-space bytes and session-mode
restoration verified. The 19-statement BVT passed in normal comparison
and with CI flags on the same instance; database teardown was checked
after each run.
- Incremental gofmt, go vet, and golangci-lint v2.6.2 passed for the two
changed Go packages. Pinned molint emitted three pre-existing
`checkrecover` diagnostics, identical on a clean copy of the base; it
reported no new diagnostics.
- The BVT took approximately 0.07 seconds per local comparison run. CI
remains pending.

This branch was previously deployed

1 inactive deployment
ci — 16107467 Deployed Nov 8, 2026 by XuPeng-SH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants