Skip to content

[4.2-dev] fix DISTINCT inserts for ENUM and SET - #32808

Merged
XuPeng-SH merged 2 commits into
matrixorigin:4.2-devfrom
XuPeng-SH:fix/enum-set-distinct-4.2-dev
Nov 16, 2026
Merged

XuPeng-SH merged 2 commits into
matrixorigin:4.2-devfrom
XuPeng-SH:fix/enum-set-distinct-4.2-dev

Conversation

@XuPeng-SH

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

Related to #30771. Follow-up to #32723 on 4.2-dev.

What this PR does / why we need it:

An INSERT ... SELECT DISTINCT can write two rows when the SELECT returns one: ENUM error ordinal 0 and a declared empty member display identically, but a hidden ordinal was added to the DISTINCT key. Hidden SET bitmaps could split the same visible tuple as well.

Keep hidden storage out of ambiguous DISTINCT keys and recover a canonical value from the visible result after duplicate elimination. ENUM recovery uses exact member bytes, including framed domains and case-distinct members. Plain projections and the non-DISTINCT branches of UNION ALL retain their stored ordinals; incompatible SET domains fall back to visible-value conversion.

When a DISTINCT result cannot distinguish error ordinal 0 from a declared empty ENUM member, recovery chooses the declared empty member. This follows the existing visible-value semantics at set-operation boundaries; an ENUM domain without an empty member retains ordinal 0.

Validation

  • Local 4.2-dev base 2bc965d972e3, macOS arm64, Go 1.26.4: pkg/sql/plan and pkg/sql/plan/function short UT passed with matrixone_test.
  • Focused planner, ENUM recovery and function-ID tests passed under -race.
  • The maintained enum_domain_roundtrip SQL case passed twice on an isolated local mo-service: 137/137 statements each time, with no ignored or abnormal statements. Coverage includes direct/derived/CTE DISTINCT, self-UNION, mixed UNION ALL, NULL, case-distinct/framed ENUM members, domain remapping, and combined ENUM/SET keys.
  • Incremental gofmt, go vet, golangci-lint, and git diff --check passed. Hosted CI is separate.

@mergify

mergify Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

This pull request is merged.

@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 16, 2026 •

Copy link
Copy Markdown
Contributor Author

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

Commit: dbbcded0cc2c57b0d91162cf010acd2081813f26

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: dbbcded0cc2c57b0d91162cf010acd2081813f26

Assessment: approve

No actionable defects found. The fix preserves visible DISTINCT keys and reuses existing provenance, materialization and codec owners with bounded local state.

Model: gpt-6.1-sol / max

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

Validation:

  • Go 1.26.4 focused planner/function tests passed; receipt checks/1794823324771438223-2.json and focused-tests-official.jsonl.

  • Both owning packages passed -short -vet=all: 9,781 test/subtest passes and 3 skips; receipt checks/1794823396453383304-2.json and owning-tests.jsonl.

  • All 14 reviewer planner probes passed on the candidate and failed for hidden DISTINCT keys with exact-baseline Go overlays; receipts checks/1794823381817315353-2.json and checks/1794823460350514759-2.json.

  • Formatting, frozen-source checks, discussion coverage and an independent oracle for all 15 added SQL result blocks passed; receipt checks/1794823012783555143-2.json and source-audit.json.

  • Final receipt/log/source reconciliation passed; receipt checks/1794823712920047119-2.json, validation-audit.json and validation-manifest.json.

Limitations:

  • No fresh live-service SQL or race run. The author's reported SQL and race runs have no retained raw receipts in the supplied evidence.

  • Hosted CI had five checks in progress in the supplied snapshot.

@XuPeng-SH

XuPeng-SH commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor Author

QA challenge — dbbcded0cc2c vs 2bc965d972e3 (4.2-dev)

Result: I found no PR-introduced correctness blocker for the stated INSERT ... SELECT DISTINCT fix. Please keep merge gated on the hosted checks still in progress below.

Correctness and side effects

  • Reproduced the bug on the exact base: SELECT DISTINCT returned one visible ENUM group, but INSERT ... SELECT DISTINCT stored two rows with ordinals 0,1. On the PR head the same SQL stores one row, canonicalized to the declared empty member.
  • The maintained enum_domain_roundtrip BVT passed 137/137 twice on the PR head, with zero ignored or abnormal statements. This covers direct, derived, CTE, UNION/UNION ALL, NULL, framed/comma labels, ENUM remapping, and mixed ENUM/SET grouping.
  • Additional real-service SQL passed for REPLACE, prepared INSERT, non-leading source projection, incompatible SET domains, and same-domain utf8mb4_bin ENUM members. The same-domain case retains distinct A/a ordinals and collapses only error-zero with the declared empty member.
  • Full pkg/sql/plan and pkg/sql/plan/function UTs passed; the focused planner and ENUM recovery tests passed. The PR head also built with the 4.2 Makefile target.

Separate existing limitation

With a differently ordered target ENUM under utf8mb4_bin, both A and a are converted to target label a. I reproduced this on the base with ordinary INSERT ... SELECT; the PR head has the same behavior for ordinary and DISTINCT inserts. It is not introduced by this patch, but it means this PR should not be treated as closing the broader binary-collation remapping issue in #30771.

Attribution of the binary-collation limitation

The A/a target remapping is not introduced by base commit 2bc965d972e3. Its parent (04610fe5f4e2) already used strings.EqualFold in ParseEnum/parseEnumName, and ordinary ENUM assignment called that lookup. 2bc965d refactors parsing through decoded EnumMembers but retains the same case-folded lookup. The comma-bearing label in the exact QA fixture became representable with 2bc965d's new tagged encoding; removing that label leaves the same pre-existing case-distinct mapping limitation. The #32723 change description also explicitly leaves binary-collation identity tracked by #30771.

Source inspection of both release tags, v4.2.8 and v4.2.8-hotfix-20261114, finds the same strings.EqualFold member lookup and types.ParseEnum assignment path. So the case-distinct target-mapping limitation is present in those release code lines too. The exact comma-bearing fixture used above is only representable after the tagged encoding added on 4.2-dev; this release conclusion is source-level, not a runtime test of those tags.

Performance and static checks

A function-level synthetic benchmark on Linux/amd64 (Go 1.26.6, i7-11700) measured an ambiguous 65,535-member domain: 63 rows on the linear path took 12.63 ms / 1.05 MB; 64 rows on the indexed path took 3.74 ms / 4.54 MB; 8,192 rows took 3.21 ms / 4.54 MB. This is a worst-case function microbenchmark, not end-to-end SQL throughput; the index setup adds about 4.5 MB per evaluated batch at the maximum domain size.

go vet, incremental golangci-lint (0 issues), gofmt, and git diff --check passed. The configured molint analyzer emitted the same unchanged-file recover() diagnostics on base and head; it reported nothing in changed files.

Hosted CI status at review time

Passed: shared build, UT coverage, MongoDB E2E, Iceberg E2E, and CI preflight. Still running: Ubuntu/x86 UT, Linux/arm64 SCA, Compose multi-CN BVT (PROXY), and standalone multi-CN BVT (COMPOSE/PESSIMISTIC). Release and target compatibility jobs were skipped.

@XuPeng-SH

XuPeng-SH commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor Author

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

Commit: dbbcded0cc2c57b0d91162cf010acd2081813f26

New discussion was assessed; the previous review remains unchanged.

@XuPeng-SH

XuPeng-SH commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor Author

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

Commit: dbbcded0cc2c57b0d91162cf010acd2081813f26

New discussion was assessed; the previous review remains unchanged.

This branch was successfully deployed

No deployments
ci — 1204cbde Deployed Nov 16, 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 kind/test-ci size/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants