Repository navigation
perf(plan): cut proto text marshalling from filter rewrite; normalize filter domain before composite-key folding - #24261
4 commits merged into
Conversation
Large IN/OR queries (e.g. outer IN of 400+ values + nested NOT IN/<> list) were spending ~95% CPU in proto.(*TextMarshaler).writeStruct, driven by Expr.String() being used as a map key in applyDistributivity and as part of constLiteralKey in the domain rewriter. Both paths reflected over the full expression tree on every call. - Replace the String()-based map key in applyDistributivity with a structural FNV-64 hash over the expr's ColRef / Literal / Function / List shapes, and fall back to a structural equality check on collision. Right conjuncts are bucketed once so the left scan performs a single hash per cond. - constLiteralKey now 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. On the reproducer (BPC-style query, ~450-entry outer IN + nested cast IN/NOT IN), end-to-end latency drops from 2.7s to 0.52s and the pprof top goes from "TextMarshaler 96%" to "scan / prefix_in / mallocgc" — i.e. real work. Fixes: matrixorigin#24227 Co-Authored-By: Claude Opus 4.7 <[email protected]>
optimizeFilters used to run mergeFiltersOnCompositeKey before rewriteInDomainNotInFilters. When the outer IN sits on a composite primary key's first column, the composite-key pass folds it 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) (typically from a varchar PK compared against an integer list after the binder's IN expansion) survive unrewritten. This reproduces on BPC-style reports where the same column appears as an outer varchar IN and as an inner cast(col AS BIGINT) IN/NOT IN list. 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 hands the narrowed IN to the composite-key folder (which now 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. Fixes: matrixorigin#24227 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. |
|
/auto-test-pr |
PR #24261 测试覆盖分析变更摘要本 PR 针对 SQL 计划优化器(
6 类测试覆盖情况
图例: ✅ 已覆盖 建议
🤖 自动生成的测试 PR✅ 稳定性 PR 已提交:https://github.com/Ariznawlll/mo-nightly-regression/pull/6 由 AI Test Analyzer 自动生成 · workflow run |
…composite-pk cast Addresses two review items on the filter-rewrite stack: 1. exprStructuralHash / exprStructuralEqual used to only consider Function.Func.ObjName. Two distinct ObjectRef identities (different schema / obj id / overload) sharing a name would hash and compare equal, letting applyDistributivity factor them as a common subexpression. Feed the full ObjectRef proto bytes into the hash and compare ObjectRef byte-for-byte on equality, restoring the invariant the old String()-as-key path had. A regression test (TestExprStructuralHashDistinguishesObjectRef) locks this down. 2. The existing BVT cases covered only single-column int primary keys. The motivating composite-varchar-pk scenario (outer varchar IN + inner cast(col AS BIGINT) IN / NOT IN) had no checked-in case exercising the full optimizeFilters pipeline. Add an in_domain.test case over a composite varchar PK that asserts both the result rows and that the scan filter collapses into prefix_in / prefix_eq — confirming the order-swap rewrite-then-fold path keeps working end-to-end. Co-Authored-By: Claude Opus 4.7 <[email protected]>
Extend expr_hash_test.go with coverage for paths the first UT didn't reach: - every Literal variant enumerated by hashLitInto / literalEqual (i8/i16/i32/u8/u16/u32/double/fval/date/time/datetime/timestamp/ enum/jsonval/defaultval/updateval/vecval), plus cross-variant inequality (i64 vs u64) - null literal vs non-null, nil expr arguments - column relpos / colpos distinctness and column-vs-literal kind mismatch - Typ width distinctness for strings - Expr_F arg recursion and list arity differences - Expr_Sub fallback through the default Marshal-bytes branch - objectRefEqual nil handling Covers >=80% of every function in expr_hash.go; residual lines are unreachable Marshal-error fallbacks. Co-Authored-By: Claude Opus 4.7 <[email protected]>
XuPeng-SH
left a comment
There was a problem hiding this comment.
LGTM — thorough performance fix with solid test coverage.
Perf: Structural FNV-64a hash replacing proto TextMarshaler in applyDistributivity and binary-marshal key in constLiteralKey — measured 5x improvement on the BPC reproducer. Sound design with tag bytes preventing cross-variant collisions and marshal fallback for uncommon expr types.
Correctness: optimizeFilters ordering swap is well-reasoned — domain rewrite must see the raw IN before composite-key folding absorbs it into prefix_in. BVT updates confirm strictly better filter plans.
Minor nits (non-blocking):
- Manual byte loops could use
bytes.Equal writeUint64loops whilewriteUint32is unrolled (inconsistency)- String hashing uses null terminator rather than length prefix (safe due to equality fallback, but length prefix would be collision-proof)
… filter domain before composite-key folding (#24280) Cherry-pick of #24229 and #24261 from 3.0-dev to main. 1. **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** (#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). Approved by: @heni02, @aunjgr
… filter domain before composite-key folding (matrixorigin#24280) Cherry-pick of matrixorigin#24229 and matrixorigin#24261 from 3.0-dev to main. 1. **Normalize per-column IN/NOT IN/=/<> domains in scan filters** (matrixorigin#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** (matrixorigin#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). Approved by: @heni02, @aunjgr
… filter domain before composite-key folding (matrixorigin#24280) Cherry-pick of matrixorigin#24229 and matrixorigin#24261 from 3.0-dev to main. 1. **Normalize per-column IN/NOT IN/=/<> domains in scan filters** (matrixorigin#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** (matrixorigin#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). Approved by: @heni02, @aunjgr
… filter domain before composite-key folding (matrixorigin#24280) Cherry-pick of matrixorigin#24229 and matrixorigin#24261 from 3.0-dev to main. 1. **Normalize per-column IN/NOT IN/=/<> domains in scan filters** (matrixorigin#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** (matrixorigin#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). Approved by: @heni02, @aunjgr
What type of PR is this?
Which issue(s) this PR fixes:
issue #24227
What this PR does / why we need it:
Two independent changes on top of #24229:
1.
perf(plan): avoid proto text marshalling in hot pathsLarge IN/OR queries (e.g. an outer IN with ~450 varchar values
combined with a nested
cast(col AS BIGINT) IN/NOT INlist ofsimilar size) were spending ~95% CPU in
proto.(*TextMarshaler).writeStruct, driven byExpr.String()being used as a map key inside
applyDistributivityand insideconstLiteralKey(PR #24229). Both paths reflected over the fullexpression tree on every call.
applyDistributivitynow uses a structural FNV-64 hash(
exprStructuralHash) over theExpr'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.
constLiteralKeypacks the literal's proto binary form plusthe Type fields directly into the key, avoiding the
TextMarshalerin the per-literal hot loop of the domainrewrite.
Measured on a BPC-style reproducer (MatrixOne container cluster, 10-core host, 19.5M-row scan):
proto.TextMarshaler96%Marshal-as-keyproto.ProtoSize56%doPrefixIn/mallocgc/scanobject(real work)2.
fix(plan): normalize filter domains before composite-key foldingoptimizeFiltersused to runmergeFiltersOnCompositeKeybeforerewriteInDomainNotInFilters. When the outer IN sits on the firstcolumn of a composite primary key, the composite-key pass folds
that IN into
prefix_in(__mo_cpkey_col)and the IN disappearsfrom 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 #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_domainBVT cases on int primary keys now reporta 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.
Test plan
go test ./pkg/sql/plan/ -run TestRewriteInDomain -count=1— 29/29 passoptimizer/in_domain— 36/36 passmake && make static-checkclean