Skip to content

perf(plan): cut proto text marshalling from filter rewrite; normalize filter domain before composite-key folding - #24280

Merged
mergify[bot] merged 7 commits into
matrixorigin:mainfrom
ck89119:cp-bigsql-to-main
May 7, 2026
Merged

mergify[bot] merged 7 commits into
matrixorigin:mainfrom
ck89119:cp-bigsql-to-main

Conversation

@ck89119

@ck89119 ck89119 commented May 6, 2026 •

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:

issue #24227

What this PR does / why we need it:

Cherry-pick of #24229 and #24261 from 3.0-dev to main.

  1. 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.).

  2. 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 in applyDistributivity, and packs literal proto binary form directly into constLiteralKey. Also reorders optimizeFilters so domain normalization runs before composite-key folding, allowing nested NOT IN / <> conjuncts to be collapsed before the outer IN is folded into prefix_in.

Measured ~5× latency improvement on BPC-style reproducer (2.9s → 0.52s).

ck89119 and others added 2 commits May 6, 2026 14:59
…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]>
@ck89119
ck89119 requested review from aunjgr and ouyuanning as code owners May 6, 2026 07:13
@ck89119
ck89119 requested a review from a user May 6, 2026 07:13
@qodo-code-review

Copy link
Copy Markdown
ⓘ 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 aunjgr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@mergify mergify Bot added the queued label May 7, 2026
@mergify

mergify Bot commented May 7, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-05-07 06:26 UTC · Rule: main
  • ✅ Checks started · in-place
  • 🚫 Left the queue — 2026-05-07 07:02 UTC · at 50ca71bdcb3ba6f832a29e6cd2b8f868fa8ccef6

This pull request spent 35 minutes 6 seconds in the queue, with no time running CI.

Waiting for
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-skipped = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-success = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
    • check-skipped = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
    • check-success = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
    • check-success = Matrixone Utils CI / Coverage
All conditions
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-skipped = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-success = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
    • check-skipped = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
    • check-success = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
    • check-success = Matrixone Utils CI / Coverage
  • #approved-reviews-by >= 1 [🛡 GitHub branch protection]
  • #changes-requested-reviews-by = 0 [🛡 GitHub branch protection]
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • branch-protection-review-decision = APPROVED [🛡 GitHub branch protection]
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / SCA Test on Ubuntu/x86
    • check-neutral = Matrixone CI / SCA Test on Ubuntu/x86
    • check-skipped = Matrixone CI / SCA Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)

Reason

The merge conditions cannot be satisfied due to failing checks

Hint

You may have to fix your CI before adding the pull request to the queue again.
If you update this pull request, to fix the CI, it will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue instead, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

@mergify

mergify Bot commented May 7, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-05-07 08:02 UTC · Rule: main
  • ✅ Checks skipped · PR is already up-to-date
  • ✅ Merged — 2026-05-07 08:02 UTC · at 1e5f56e9d0afdb7490b1d0e1ebbc379ab92a5a62 · squash

This pull request spent 20 seconds in the queue, including 3 seconds running CI.

Required conditions to merge
  • #approved-reviews-by >= 1 [🛡 GitHub branch protection]
  • #changes-requested-reviews-by = 0 [🛡 GitHub branch protection]
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • branch-protection-review-decision = APPROVED [🛡 GitHub branch protection]
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-neutral = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-skipped = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / SCA Test on Ubuntu/x86
    • check-neutral = Matrixone CI / SCA Test on Ubuntu/x86
    • check-skipped = Matrixone CI / SCA Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
    • check-neutral = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
    • check-skipped = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Utils CI / Coverage
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage

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/enhancement size/XXL Denotes a PR that changes 2000+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants