Repository navigation
Description
Summary
Two contradictory equality predicates on the same column — WHERE k2 = 5 AND k2 = 6, which matches zero rows by definition — are estimated as the full table (outcnt 1,000,000 on a 1M-row table, selectivity 1.0). The contradiction is never folded (no constant-folding/falsification pass on filter trees), and the AND-composition path produces 1.0 instead of ~0 for the pair.
Reproduction (main@HEAD, c6683bd)
CREATE TABLE big1m (k INT PRIMARY KEY, k2 INT, KEY idx_k2 (k2)); -- 1M rows, k2 ∈ [0, 999]| predicate | est. outcnt | actual | note |
|---|---|---|---|
k2 = 5 |
1,000 | 1,000 | ✓ |
k2 = 5 AND k2 = 5 |
1,000 | 1,000 | ✓ duplicate folded |
k2 = 5 AND k2 = 6 |
1,000,000 | 0 | worse than no filter |
k2 < 100 AND k2 > 900 (empty interval) |
1,000 | 0 | andSelectivity(0.1, 0.1) floor keeps 0.1% — wrong sign but bounded |
IN (5, 6) |
10.1 | 2,000 | (separately filed, #32059) |
Root cause
- No falsification pass: constant predicates like
a = 5 AND a = 6are trivially foldable to FALSE (equal-column distinct-literal contradiction) at bind/plan time, but no such rewrite exists in the plan pipeline — the pair flows into selectivity estimation intact. - Composition produces the maximum: in
estimateExprSelectivity(stats.go:1017), each=is estimated 0.001; thenestimateAndSelectivity→andSelectivity(0.001, 0.001)should give ≈0.001 (or the conservative branch ~min), yet EXPLAIN shows selectivity=1.0. The path taken is evidently not the product branch:rangeSelectivityGroupKey(stats.go:1147) only recognizes> >= < <= between—=is not grouped, so each equality takes the singleton pathret = andSelectivity(ret, ...), and the observed 1.0 means the first conjunct'sexpr.Selectivitycache (:1008-1010, the same suspicious write-back flagged in [Performance]: same-column OR estimates below a single term, 200x off #32059) or the surrounding group logic corrupted the accumulation. Whatever the internal mis-wiring, the black-box invariant is broken: AND-composition returned a value above both operands (1.0 ≫ 0.001), which a correct AND composition cannot do. - Wider observation: literal-contradiction detection would also fix the
k2 < 100 AND k2 > 900case (empty interval →estimateConjunctiveRangeSelectivityalready computeslower > upper, but the group path apparently only fires for the ≥2-same-column case when all conjuncts are range operators — it did fire here yet still estimated 0.1%, not 0).
Downstream damage
A contradictory predicate usually appears from OR-mangled app filters, date-range bugs, or trigger-generated WHERE clauses. With selectivity 1.0 the planner treats the query as a full scan and sizes every operator above it for 1M rows: it will pick nested-loop joins against this "1M-row" input, choose full-scan access paths, and skip indexes. All while the true cost is near-zero. The query gets the worst possible plan for the best possible filter.
Fix direction
- Cheap and complete: a constant-folding pass over filter lists that recognizes same-column distinct-literal contradictions (
=x AND =y,x≠y→ FALSE;>a AND <b,a≥b→ FALSE) and replaces the filter with a constant false, short-circuiting execution entirely. - Independent hardening:
andSelectivity/estimateAndSelectivitymust be monotone — never return more than the minimum operand's selectivity; and=should participate inrangeSelectivityGroupKeyso=5 AND >=4collapses to=5.
Activity
XuPeng-SH commentedon Nov 9, 2026
Consolidating #32059 here under compound-predicate selectivity correctness. Keep separate OR and AND invariants: OR selectivity must not fall below either operand for disjoint alternatives; AND selectivity must not exceed the tighter operand, and contradictions should estimate zero. The current evidence points to the same estimator/cache area but does not yet prove one underlying defect, so retain both reproductions and determine the common cause before choosing the fix.
[-][Performance]: contradictory same-column predicates estimate as full table[/-][+][Performance]: compound predicate selectivity violates AND/OR bounds[/+]gouhongshen commentedon Nov 10, 2026
PR #32502 is trying to fix this issue.
fix(plan): fold contradictory same-column predicates to near-zero est…
gouhongshen commentedon Nov 10, 2026
PR #32502 has merged and is awaiting manual validation. The issue remains open.
XuPeng-SH commentedon Nov 10, 2026
合并范围核对:#32502 已于 2026-11-10 23:50:03 UTC 合入 main,merge SHA 为 45917e8。它修正同列点域矛盾判定、SQL 等价值标识及常量 FALSE/NULL 的选择率估计;不可能条件使用现有 1e-8 近零下限,而非精确零。
本总项暂保留研发:评论6732084599已将 #32059 的 OR/IN 选择率问题并入这里,并要求保留 OR、AND 两组验收;新 PR 的 Residual risk 仍明确把 #32059 的 IN-list estimate 缺陷列为范围外。新加 OR/IN lower-bound controls 不能直接证明该合并范围内的完整复现已修复。需继续核对并完成这部分,或明确其独立追踪范围后再进行整个 issue 的 QA 交接。当前保持负责人和开放状态。
验证边界:作者报告完整 planner 包、11组聚焦测试,以及 contradictory_selectivity 199条、between_selectivity 22条 SQL 各两轮比较通过;审查者报告 planner/types 套件与11个额外边界探针通过,但两个原有子用例跳过、未本地重跑分布式BVT。队列报告 CI Required 成功。以上均为远端报告,本轮没有运行本地测试或独立审计原始日志,不代表整项验收通过。
Ariznawlll commentedon Nov 18, 2026
Note
结论:同列矛盾谓词选择率修复(#32502)实测通过(AND 组验收完成)。#32059 并入的 OR/IN 选择率缺陷不在本次合并范围(PR 的 Residual risk 已明确列为范围外),该部分仍未验收——issue 建议保持 OPEN 跟踪 OR/IN 组,或由负责人把 OR/IN 拆分为独立跟踪项后再评估整体关闭。
QA 独立复验结论(#32068 / #32502,main)
修复 PR #32502,merge SHA
45917e803f2915f6f0838b2b2fe9f591fc5b6018(2026-11-10 合入 main)。main 修复按规则只验 main、motr 只提 main。修复存在性
本地 main 构建含
pkg/sql/plan/stats.go矛盾折叠注释与折叠逻辑、白盒TestContradictoryPredicateScanEstimate(#32068 端到端复现注释)、BVTtest/distributed/cases/optimizer/contradictory_selectivity.sql;实例git_version()=517e169538。SQL 黑盒(15/15 PASS,真实 main 实例)
废弃值断言比较器在首轮跑批中被修正(数据本身首轮即正确,FAIL 为脚本比较结构错误,已排除脚本误报风险后重验):
矛盾组全部折叠(0 行 + explain selectivity=0.0000):
v=19 AND v=81(同列矛盾等值,issue 主例)→ 0 行,selectivity=0.0000 ✅(修复前估算全表 1,000,000/选择率 1.0)d=19.00 AND d=81.00(DECIMAL 同款)✅v=19 AND v=19 AND v=81(重复+矛盾)✅v=19 AND v<>19(等值+不等)✅s='a' AND s IS NULL(值域+NULL 域)✅v<20 AND v>40(空区间)→ 0 行,selectivity=0.0000 ✅v=19 AND v=81 AND v BETWEEN 0 AND 100(BETWEEN 组合不顺延折叠)→ 0 行 + selectivity=0.0000 ✅健康对照组不受影响:
v BETWEEN 19 AND 81→ 2 行 ✅v=19单独 /v=19 AND v=19重复 → 1 行 ✅v=19的估算选择率 0.25,非近似零 ✅(证明折叠没有把正常谓词误伤)Go 白盒(66/66 PASS)
pkg/sql/plan定向:TestContradictoryPredicateScanEstimate(端到端)、TestEstimateAndSelectivityNormalizesSameColumnRanges、TestBoundBetweenSelectivityClipsToDomain、TestRangeSelectivityClipsToDomain、TestEstimateIntervalSelectivityPreservesClippedBoundaryMembership等全过。回归交付(motr main)
14_issue_regression.issue_32068_contradictory_selectivity(14 断言):7 组矛盾折叠(行数+selectivity 双通道)+ 4 组健康对照 + 正常谓词非近似零防误伤断言。faf1f99):https://github.com/matrixorigin/motr/pull/358 ,CI:https://github.com/matrixorigin/motr/actions/runs/44273719716范围外(与 XuPeng-SH 合并范围核对一致)
contradictory_selectivity.sql的 IN 断言)与 motr 交付的对照断言中,OR/IN 修复落地后可立即复验。Ariznawlll commentedon Nov 19, 2026
Warning
OR/IN 剩余范围在最新 main 仍有真实反例,#32068 不能关闭。实际行数正确不代表优化器估算已修复;AND 矛盾折叠的既有验收结论不变。
QA 独立复验:#32059 并入的 OR/IN 范围(main)
git_version()=035af99680,对应源码 HEAD035af996806917b23d9428695e0295b65cb16356。45917e803f2915f6f0838b2b2fe9f591fc5b6018是该 HEAD 的祖先;未找到新的 OR/IN 专项修复 PR。k2=i%1000、NDV=1000、二级索引idx_k2(k2),ANALYZE TABLE big1m(k2) FULLSCAN。另有 100k 行、整数/varchar、无二级索引对照。1. 原始带索引场景仍低估
EXPLAIN VERBOSE 的以下数值来自实际 Index Table Scan 节点(不是脚本假设,也不是 COUNT 执行结果):
k2=42k2=43k2=42 OR k2=43k2=43 OR k2=42k2 IN (42,43)k2=42 OR k2=42k2=5 OR k2=625 组探针中的 SQL
COUNT(*)均与预期一致;失败点是计划基数/选择率,不是执行结果。AND 控制k2=42 AND k2=43返回 0 行、估算 outcnt=0.01(selectivity 显示 0.0000),未被本次 OR/IN 反例回退。2. 索引/非索引对照缩小了问题范围
idx_k2idx_k2idx_k2idx_k2无索引路径的二值估算符合当前均匀 NDV 近似;不能据此宣称原始带索引问题已修复。varchar
pad的普通 Table Scan 二值 OR/IN 为 200.40 / 0.0020,亦正常。3. 白盒定位线索(已结合真实计划核对)
带索引 OR 和 IN 的实际 Filter Cond 都变成
prefix_in(__mo_index_idx_col):当前
pkg/sql/plan/stats.go的case "in", "prefix_in"对prefix_in仍执行card *= 10;prefix_eq则保留固定 0.0001 的估算分支。单值索引扫描的 outcnt=992.06 不能证明使用了原始k2NDV=1000;应重点检查隐藏索引表__mo_index_idx_col的统计/NDV 映射,以及编码键 IN 列表长度对prefix_in的估算语义。以上是代码和真实计划共同指出的排查线索,不声称已完成产品修复。现有
TestConsolidatedOrAndInListSelectivityControls在本基线定向运行通过,mock 记录per-value=0.001000, or=0.002000, in=0.002000,但它没有覆盖上述隐藏索引表/prefix_in计划,因此不能替代真实 SQL 验收。4. 回归用例与交接
严格 main-only 草稿
14_issue_regression.issue_32068_or_in_selectivity已实际执行:单值行数与单值估算通过;正序 OR、逆序 OR、等价 IN、重复 OR、三值 OR 共 5 个选择率断言失败,对应 outcnt=72.37 / selectivity=0.0001。已排除初轮脚本%格式化异常及仅检查 Filter 节点的解析遗漏后重跑。不 record 失败输出为通过 golden,不提交 motr PR,也不宣称 record/replay/repeat/CI 通过。 严格草稿保留在隔离工作区
/tmp/motr-qa-32068-or-in;修复落地后可直接用于 main 的完整回归交付。建议继续由研发处理 OR/IN 索引扫描估算及相应真实索引计划回归;本 issue 保持 OPEN。