Repository navigation
fix(plan): fold contradictory same-column predicates to near-zero estimates - #32502
Conversation
…imates A contradictory same-column predicate pair (k2 = 5 AND k2 = 6), which matches zero rows by definition, was estimated as the full table (selectivity 1.0 on a 1M-row table): the falsification verdict sat behind the same-column-range bypass, so a surviving BETWEEN kept the contradictory equalities unfolded, and a folded constant-false filter was itself estimated at full-table rows by estimateExprSelectivity's literal case. The planner then picked full scans and sized every operator above the filter for 1M rows while the true result was empty. - pkg/sql/plan/expr_opt.go: normalizeColumnDomain decides the point-domain satisfiability verdict before the hasRange bypass - an empty intersection of same-column point sets (distinct equality literals, disjoint IN lists) or one fully excluded by the column's NOT-IN points folds the filter list to constant false; only the duplicate-point merging stays behind the range bypass. The value keys that decide point identity now follow runtime SQL equality at both constLiteralKey and blockFilterLiteralKey: floating signed zero, text keys in the bound collation domain, family-specific declaration metadata stripping (string width, temporal Width/Scale, decimal width, integral and FLOAT/DOUBLE Width/Scale), FLOAT32 normalization by its own scale mirroring runtime comparison, and the retained bind-time fold source (Literal.Src) classified by static evaluability - statically evaluable sources key by payload while execution-dependent sources decline the domain decision instead of proving a false contradiction. Low-precision float casts stay declined: the cast rounds, so the source literal is not their value. - pkg/sql/plan/stats.go: a folded constant FALSE or NULL literal filter is estimated at the near-zero floor (matching the impossible-interval convention) instead of selectivity 1.0, so a falsified filter no longer sizes scans and operators for the full table. - Tests: pkg/sql/plan/stats_test.go adds the focused planner tests for the estimate defect and every identity seam (contradictory-equality scan estimate through real parse/bind/build, folded-boolean bounds, OR/IN consolidated controls, signed zero, Unicode collation, temporal precision, decimal widths and provenance, integral cast metadata, double/float cast spellings, folded-source point domains); pkg/sql/plan/mock.go adds the fpts fixture for float cast-shape filters; test/distributed/cases/optimizer/contradictory_selectivity.sql and .result add the public regression pinning zero results with near-zero estimates for contradictory predicates and kept predicates with positive estimates and matching rows for satisfiable duplicates. Related to matrixorigin#32068
|
Unrelated CI blockers tracked for this PR:
A single failed-jobs rerun of run 42893155021 (attempt 1) was requested under this loop's CI handling rules; its result has now been inspected: Rerun outcome (inspected 2026-11-11): run 42893155021 attempt 2 completed |
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
|
Queue requested — the merge queue status continues in this comment ↓. |
|
mo-bot review · Commit: Review completed. Read the assessment. |
XuPeng-SH
left a comment
There was a problem hiding this comment.
mo-bot review · mo-bot-local · review
Commit: 98ebb0eaaf97959deac1028f2228497b989b77ab
Assessment: approve
No actionable correctness or design defects remain. The fix reuses existing domain normalization and runtime comparison helpers, with appropriate safeguards for SQL-equal literal representations.
Model: gpt-6.1-sol / max
Session: 01a1a004-90c3-73b1-acc9-3d4636452003 · slot 0 · generation 0
Validation:
-
Full planner and types suites passed with Go 1.27.1 and the supplied isolated executor, including all 11 added test families. Evidence: owning-suites.log and checks/1794349924116206068-4.json.
-
All 11 additional parser/BuildPlan boundary probes passed, covering decimal scales, FLOAT signed-zero rounding and distinct-value controls. Evidence: value-boundary.log and checks/1794349974888229460-3.json.
-
Isolation preflight, native provenance, source cleanliness and discussion coverage verified. Receipts and retained logs reconciled in review-validation.json; final audit receipt checks/1794350173701637824-3.json.
Limitations:
-
Two preexisting unrelated planner subcases skipped. Distributed BVT was not rerun locally; completed frozen same-head CI evidence was reviewed.
-
Later CI lanes still running in the frozen snapshot were not counted as passes.
✅ Merged into
|
What type of PR is this?
Which issue(s) this PR fixes:
Related to #32068.
What this PR does / why we need it:
Root cause
A same-column contradictory pair (
k2 = 5 AND k2 = 6, zero matching rows by definition) was estimated as the full table - selectivity 1.0, outcnt 1,000,000 on a 1M-row table - so the planner chose full scans and sized every operator above the filter for 1M rows while the true result was empty (#32068). Two defects stacked:normalizeColumnDomain(pkg/sql/plan/expr_opt.go) both point-domain falsification verdicts sat behind thehasRangebypass: a surviving same-column range (BETWEEN) kept contradictory equalities unfolded and left them to the selectivity arithmetic.estimateExprSelectivity's literal case (pkg/sql/plan/stats.go) returned selectivity 1.0, estimating an always-empty filter as a full scan.Review traced ten further identity defects at the two value-key seams that decide same-column point identity (
constLiteralKey,blockFilterLiteralKey): raw signed-zero float bits, raw text bytes instead of the bound collation domain, temporal Width/Scale, decimal declaration width and normalized-spelling provenance, integral Width/Scale, DOUBLE/FLOAT(M,D) declaration metadata (FLOAT32 additionally needs payload normalization by its own declared scale, mirroring runtime comparison), and the retained bind-time fold source (Literal.Src) marshaled into the keys. Each could prove a false contradiction and fold a genuinely satisfiable filter to constant false.Exact change
pkg/sql/plan/expr_opt.go: the point-domain satisfiability verdict now resolves before thehasRangebypass - an empty intersection of same-column point sets (distinct equality literals, disjoint IN lists) or an intersection fully excluded by the column's NOT-IN points folds the filter list to constant false; the range bypass now only skips duplicate-point merging, and satisfiable duplicates still merge into one predicate. Both value-key seams key by runtime SQL value: floating signed zero, text in the bound collation domain (binary payloads keep exact bytes), family-specific declaration-metadata stripping (literalSemanticKeyType), FLOAT32 self-scale normalization (sqlEqualFloat32ScaledLiteral), and fold-source classification (literalDomainValueIdentity: statically evaluableLiteral.Srckeys by payload; execution-dependent sources decline the domain decision). Low-precision float casts stay declined (unwrapConstLiteral) because the cast rounds the value; the intentional partial float-family coverage inliteralSemanticKeyTypecarries the required// typeswitch:partialannotation.pkg/sql/plan/stats.go: a folded constant FALSE or NULL literal filter takes the near-zero floor (matching the impossible-interval convention) instead of selectivity 1.0.pkg/sql/plan/mock.go:fptsfixture (INT32 primary key, DOUBLE, FLOAT) for cast-shape planner tests.pkg/sql/plan/stats_test.go: eleven focused planner tests including full parse/bind/build companions.test/distributed/cases/optimizer/contradictory_selectivity.sql(+ regenerated.result): public regression over integer, temporal, decimal, string, folded-DIV and double/float fixture blocks.Issue-to-test proof
o_orderkey = 19 AND o_orderkey = 81(with and without a coveringBETWEEN) folds to a constant-false filter with selectivity 0.0000 and returns 0 rows, while the duplicateo_orderkey = 19 AND o_orderkey = 19keeps one real predicate with a positive point estimate and returns exactly its rows (TestContradictoryPredicateScanEstimatethrough real parse/bind/build; BVT pins both with EXPLAIN selectivity regexes and COUNT companions; measured live on an isolated single-instance MatrixOne).TestFoldedBooleanLiteralSelectivityBounds,TestConsolidatedOrAndInListSelectivityControls.CAST('19.00' AS DECIMAL(8,2)) DIV 1); genuinely distinct values still fold. Each defect was first reproduced by a focused failing test on the pre-fix tree, then green after the fix.TestTypeSwitchFamiliesgate (./pkg/container/types) passes with the annotated partial switch.Test commands
Residual risk