Repository navigation
perf(plan): cut proto text marshalling from filter rewrite; normalize filter domain before composite-key folding - #24280
Conversation
…rixorigin#24229) ## What type of PR is this? - [ ] API-change - [x] BUG - [x] Improvement - [ ] Documentation - [ ] Feature - [ ] Test and CI - [ ] Code Refactoring ## Which issue(s) this PR fixes: issue matrixorigin#24227 ## What this PR does / why we need it: Generalize the single-table scan filter rewrite into `normalizeColumnDomain` (`pkg/sql/plan/expr_opt.go`). For each column referenced by a scan's `FilterList`, we now collect constant-domain predicates and merge them into one canonical expression: - `col IN S1 AND col IN S2` → `col IN (S1 ∩ S2)` - `col = v AND col IN S` → `col = v` (or `FALSE`) - `col IN S1 AND col NOT IN S2` → `col IN (S1 \ S2)` - `col IN S1 AND col <> v1 AND col <> v2 ...` → `col IN (S1 \ {v1, v2, ...})` - `col IN S AND col IS NULL` → `FALSE` - empty intersection / difference → `FALSE` - outer `IN` domain is also propagated into OR branches and nested `NOT(IN)` so that `col IN S1 AND (col = 'x' OR col NOT IN S2)` collapses the inner `NOT IN` into `IN (S1 \ S2)` — the main bigsql scenario. Preserves 3-valued logic: `NOT IN` / `<>` rewrites only kick in when they sit under an outer `col IN S`, since the outer `IN` already excludes NULL. Lists containing NULL, non-constant lists, `BETWEEN`, and cross-column predicates are left untouched. UT: `pkg/sql/plan/filter_domain_test.go` covers - IN + NOT IN → difference IN - IN + multiple `<>` → difference IN - empty IN intersection → FALSE - IN + IS NULL → FALSE - `=` + IN → single `=` - IN × IN intersection - NOT IN with NULL in list → untouched - NOT IN inside OR branch → rewritten - cross-column predicates → not merged Local checks: `go test ./pkg/sql/plan/ -count=1`, `make`, `make static-check` all pass. --------- Co-authored-by: Claude Opus 4.7 <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: heni02 <[email protected]>
… filter domain before composite-key folding (matrixorigin#24261) - [ ] API-change - [x] BUG - [x] Improvement - [ ] Documentation - [ ] Feature - [ ] Test and CI - [ ] Code Refactoring issue matrixorigin#24227 Two independent changes on top of matrixorigin#24229: Large IN/OR queries (e.g. an outer IN with ~450 varchar values combined with a nested `cast(col AS BIGINT) IN/NOT IN` list of similar size) were spending ~95% CPU in `proto.(*TextMarshaler).writeStruct`, driven by `Expr.String()` being used as a map key inside `applyDistributivity` and inside `constLiteralKey` (PR matrixorigin#24229). Both paths reflected over the full expression tree on every call. - `applyDistributivity` now uses a structural FNV-64 hash (`exprStructuralHash`) over the `Expr`'s ColRef / Literal / Function / List shapes, and falls back to a structural equality check only on hash collision. Right conjuncts are bucketed once so the left scan performs a single hash per cond. - `constLiteralKey` packs the literal's proto **binary** form plus the Type fields directly into the key, avoiding the `TextMarshaler` in the per-literal hot loop of the domain rewrite. **Measured on a BPC-style reproducer (MatrixOne container cluster, 10-core host, 19.5M-row scan):** | Version | Query latency (steady) | CPU top | |---------|-----------------------|---------| | baseline (pre-PR) | 2.7 - 2.9 s | `proto.TextMarshaler` 96% | | after `Marshal`-as-key | 2.9 - 3.2 s | `proto.ProtoSize` 56% | | **this PR (structural hash + constLiteralKey fix)** | **0.52 s** | `doPrefixIn` / `mallocgc` / `scanobject` (real work) | folding `optimizeFilters` used to run `mergeFiltersOnCompositeKey` before `rewriteInDomainNotInFilters`. When the outer IN sits on the first column of a composite primary key, the composite-key pass folds that IN into `prefix_in(__mo_cpkey_col)` and the IN disappears from the scan's FilterList. The domain normalizer then has no outer IN to propagate, so nested `NOT IN` / `<>` / `cast(col)` conjuncts (typically introduced by the binder when a varchar PK is compared against an integer list) survive unrewritten. This reproduces on BPC-style reports where the same column appears as both an outer varchar IN and an inner `cast(col AS BIGINT)` IN/NOT IN list — the exact scenario that motivated PR matrixorigin#24229. Swapping the order lets the domain rewrite run against the still-bare IN, collapse matching conjuncts into a single IN / FALSE / equality, and only then hand the narrowed IN to the composite-key folder (which gets a smaller point set for the prefix_in seek). The existing `in_domain` BVT cases on int primary keys now report a single collapsed IN where they used to show two conjuncts; the expected output is updated accordingly. Behavior on non-PK tables is identical to before. - [x] UT: `go test ./pkg/sql/plan/ -run TestRewriteInDomain -count=1` — 29/29 pass - [x] BVT: `optimizer/in_domain` — 36/36 pass - [x] `make && make static-check` clean - [x] End-to-end check with the BPC reproducer — correct result, ~5× latency improvement, pprof top now shows real data-path work --------- Co-authored-by: Claude Opus 4.7 <[email protected]>
ⓘ You've reached your Qodo monthly free-tier limit. Reviews pause until next month — upgrade your plan to continue now, or link your paid account if you already have one. |
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
aunjgr
left a comment
There was a problem hiding this comment.
Structural hash approach is sound — FNV-64 with fallback to proto binary on collision/uncommon variants avoids correctness risk. IsBin fix and the reorder of normalizeColumnDomain before composite-key folding are both correct. Well tested.
Merge Queue Status
This pull request spent 35 minutes 6 seconds in the queue, with no time running CI. Waiting for
All conditions
ReasonThe merge conditions cannot be satisfied due to failing checks HintYou may have to fix your CI before adding the pull request to the queue again. |
Merge Queue Status
This pull request spent 20 seconds in the queue, including 3 seconds running CI. Required conditions to merge
|
What type of PR is this?
Which issue(s) this PR fixes:
issue #24227
What this PR does / why we need it:
Cherry-pick of #24229 and #24261 from 3.0-dev to main.
Normalize per-column IN/NOT IN/=/<> domains in scan filters (fix: normalize per-column IN/NOT IN/=/<> domains in scan filters #24229): Generalizes the single-table scan filter rewrite into
normalizeColumnDomain. For each column referenced by a scan's FilterList, collects constant-domain predicates and merges them into one canonical expression (IN intersections, IN \ NOT IN differences, = + IN collapse, empty → FALSE, etc.).Cut proto text marshalling from filter rewrite; normalize filter domain before composite-key folding (perf(plan): cut proto text marshalling from filter rewrite; normalize filter domain before composite-key folding #24261): Replaces
Expr.String()map keys with structural FNV-64 hash + equality check inapplyDistributivity, and packs literal proto binary form directly intoconstLiteralKey. Also reordersoptimizeFiltersso domain normalization runs before composite-key folding, allowing nested NOT IN / <> conjuncts to be collapsed before the outer IN is folded intoprefix_in.Measured ~5× latency improvement on BPC-style reproducer (2.9s → 0.52s).