Skip to content

feat(sql): support EXCEPT ALL multiset subtraction - #29228

Merged
XuPeng-SH merged 8 commits into
matrixorigin:mainfrom
XuPeng-SH:codex/issue-28234-except-all
Sep 22, 2026
Merged

XuPeng-SH merged 8 commits into
matrixorigin:mainfrom
XuPeng-SH:codex/issue-28234-except-all

Conversation

@XuPeng-SH

@XuPeng-SH XuPeng-SH commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • Feature
  • BUG
  • Test and CI

Which issue(s) this PR fixes:

Fixes #28234

What this PR does / why we need it:

Implement EXCEPT ALL / MINUS ALL as multiset subtraction: each key, including NULL, is emitted max(left_count - right_count, 0) times. The dedicated MinusAll operator builds mpool-accounted right-side multiplicities, consumes them while probing, and returns surviving original left rows.

Parallel input work is preserved, but both complete streams feed one subtraction owner; the existing broadcast set-operation path must not multiply results. Planner support includes INSERT SELECT, physical PAD SPACE equality keys and prepared-plan key refresh. The new VM opcode is appended without renumbering existing opcodes.

Remote decoding restores the compiler-generated two-merge input shape for all binary set operators: Minus, MinusAll, Intersect, and IntersectAll. Restoring only MinusAll was insufficient: an actual two-CN execution of (L EXCEPT ALL R) INTERSECT ALL L with the owner placed away from ingress panicked because the ancestor had only one decoded child. The shared restoration fixes this boundary without changing the wire format or placement policy; malformed shapes are rejected before mutating the tree.

Validation

PAD SPACE-aware set operations retain outer predicates above the set boundary: filtering byte-distinct but key-equivalent rows before subtraction produces wrong multiplicities. This also protects normalized UNION/INTERSECT/MINUS; branch-local filters, exact-key operations and UNION ALL retain existing optimization. See design revision 2; maintainer design approval remains pending.

Latest follow-up validation:

  • Before the fix, public SQL with two VARCHAR 'a ' rows minus one CHAR 'a', filtered by HEX(v)='6120', returned 2 instead of 1, twice on the same instance; a LIMIT control returned 1. After the fix, HEX/LENGTH predicates and LIMIT/no-LIMIT controls pass on private 1CN/2CN clusters (43.917s), including EXCEPT ALL, its MINUS ALL alias, distinct EXCEPT, INTERSECT, INTERSECT ALL and UNION ALL.
  • Full pkg/sql/plan suite 5.400s PASS, including a six-operator × keyed/unkeyed boundary matrix, branch-local pushdown and LIMIT residual preservation.
  • Exact set_operator.test normal mo-tester comparison twice on the same private CN: 33/33 each, zero failed/ignored/abnormal, including two new explicit teardown statements. Independent schema/table/account inventories before and after each pass match; cluster Close and subsequent listener refusal pass (11.669s). This is direct embedded-CN evidence, not proxy CI.
  • Production build, final changed-package vet, incremental lint (0 issues), formatting and diff checks pass.

Earlier feature validation (historical where not exercised above):

  • Private live one-CN and two-CN embedded clusters, accessed over the MySQL protocol. The two-CN case marks ingress draining so the subtraction owner itself executes on the other CN, with executed physical plans captured. Both clusters close successfully.
  • Both spellings, duplicate-heavy inputs crossing batches, exact per-key/NULL counts, empty inputs, nested subtraction on either side, distinct/intersect ancestors, outer LIMIT and binary prepared reuse pass.
  • Full pkg/sql/compile tests, including complete remote scope encode/decode with nested PreScopes, all four binary ancestors, key expressions, receiver ranges, connector identity, and malformed nested wire fixtures.
  • Full pkg/sql/colexec/minusall tests, including injected build/probe failures, Reset/reuse and zero mpool balance after teardown.
  • Changed production package build, changed-package vet, incremental configured lint and formatting checks.

The initial implementation also passed planner/VM package tests and MinusAll race tests; these are historical checks, not a claim that the full CI matrix was rerun locally after the follow-up.

Limits

  • Subtraction has one owner; upstream input computation remains parallel.
  • Right-side multiplicities remain in memory and do not spill.
  • The conservative predicate barrier can increase input volume and memory for normalized set operations; no equivalence-preserving predicate classifier is introduced.
  • Maintainer design approval is pending; model reviews do not replace it.
  • Live local tests use embedded CN/TN/log services, not the standalone proxy BVT deployment. Full CI is pending for the updated revision.

@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 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.

Local exact-head fallback review (Astra thread capacity unavailable), substantive result REQUEST_CHANGES. Reviewed head 194d1f4 against base 3727c5c. Source audit found no confirmed semantic or cleanup defect in duplicate/NULL subtraction, mpool Reset/Free, single-owner consolidation, or remote shape restoration. However, this PR changes distributed and remote execution paths while explicitly lacking local live one-CN/two-CN BVT execution. Before merge, attach exact-head remote/two-CN normal-comparison evidence with teardown, covering nested remote MINUS ALL, duplicate/NULL cardinality, malformed instruction rejection, operator reset/reuse, and mpool balance. The existing unit and codec tests are not sufficient evidence for the distributed contract.

@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.

GPT-6 high force review at exact head 2955326 (base 1634996): substantive REQUEST_CHANGES. Formal review is COMMENT because this PR is self-authored.

[P2] Predicate pushdown can break PAD SPACE multiset subtraction. The new MINUS_ALL physical key treats 'a ' and 'a' as the same subtraction key, but the existing pushdown path in pkg/sql/plan/pushdown.go:879 pushes an outer predicate into both inputs without checking that it preserves the equivalence class. Reproduced on the reviewed head with left VARCHAR(8) containing two 'a ' rows and right CHAR(8) containing one 'a': EXCEPT ALL returns 1 row, but adding outer WHERE HEX(v)='6120' returns 2 rows instead of 1 because the right 'a' is filtered before subtraction. A LIMIT control blocks pushdown and returns 1. Restrict non-equivalence-preserving pushdown and add this public SQL regression.

[P2] Design approval is still not recorded. docs/rfcs/00000000_except_all.md still says maintainer design approval is pending, while this adds public SQL semantics, a new operator, remote tree restoration, and a single-owner/in-memory capacity model.

[P2] set_operator.test has one successful 31/31 run but no same-instance rerun and independent post-teardown checks; its DROP TABLE IF EXISTS setup can hide residue. Add the required repeat and explicit cleanup verification.

The implementation review otherwise closed NULL/count/batch/parallel/remote-shape/prepared/reset-cleanup concerns. Current CI is green, including two-CN integration and 118/118 JSON BVT, but those tests did not catch the PAD SPACE pushdown counterexample. Coverage was not used as a criterion.

@XuPeng-SH

Copy link
Copy Markdown
Contributor Author

Addressed the latest review at 2955326b6d:

  1. PAD SPACE pushdown correctness fixed. Reproduced the exact public SQL counterexample twice: outer HEX(v)='6120' returned 2 instead of 1, while the LIMIT control returned 1. The shared set-operation planner now retains incoming predicates when physical equality keys normalize visible values, while continuing branch-local optimization. UNION ALL and unkeyed operations retain pushdown. This is an equality-boundary correction, not a HEX-specific exception.
  2. Regression coverage: full planner suite PASS (5.400s); six-operator × keyed/unkeyed builder matrix covers branch-local filtering and LIMIT residuals. Public private 1CN/2CN tests PASS (43.917s), with HEX and LENGTH, LIMIT controls, EXCEPT/MINUS ALL, distinct EXCEPT, INTERSECT distinct/all, UNION ALL, same-instance repeats, remote ownership and service/listener teardown. Earlier tests exercised keys/output bytes but missed predicates distinguishing members of the same equality class; this regression closes that gap.
  3. BVT repetition/cleanup: added explicit final DROP TABLE for t1/t2. Exact set_operator.test normal comparison passed twice on one private CN, 33/33 each, zero failed/ignored/abnormal. Independent schema/table/account inventories match before and after each invocation, before subsequent setup cleanup. Cluster Close succeeds and listener refuses connections. Outer test/package 11.63s/11.669s. No expected-result regeneration, ignore mode, or nometa; temporary harness not committed. This is direct-CN local evidence, not a proxy rerun claim.
  4. Design approval: RFC revision 2 records predicate placement and the conservative input-volume/memory tradeoff. Maintainer approval remains explicitly pending; it cannot be replaced with model approval.

Production build, final vet, incremental lint (0 issues), gofmt and diff checks pass. GPT-6 medium design and separate overall review found no material blocker in this correction. New-head CI remains pending; no merge performed.

@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.

Force review(GPT-6 high,head df3908e5f856e54260b000432625c1b63d3ac8d3)

结论:当前未确认新的实现缺陷;此前发现的 PAD SPACE predicate-pushdown 计数问题已修复并独立验证。planner/operator/transport focused tests、真实 1CN/2CN 集成、同实例 equality/prepared reuse 均通过。

仍有两个验收门槛未闭合:

  1. RFC docs/rfcs/00000000_except_all.md 仍标记为 maintainer design approval pending。该功能新增公共 SQL、算子及远端执行契约,需维护者确认 owner、内存计数和混合版本限制。
  2. 当前 exact-head 的 UT、SCA、proxy BVT、两 CN pessimistic BVT 当时仍在运行,需取得终态成功证据。

因此本次是 substantive REQUEST_CHANGES 结论,但由于这是作者本人 PR,按规则以 COMMENT 提交。

@XuPeng-SH
XuPeng-SH merged commit 66da387 into matrixorigin:main Sep 22, 2026
16 of 20 checks passed
@XuPeng-SH
XuPeng-SH deleted the codex/issue-28234-except-all branch September 22, 2026 14:46

This branch was previously deployed

1 inactive deployment
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/feature kind/test-ci size/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Compatibility]: EXCEPT ALL multiset subtraction is not implemented

2 participants