Skip to content

[Performance]: compound predicate selectivity violates AND/OR bounds #32068

Todo

Description

@Ariznawlll

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

  1. No falsification pass: constant predicates like a = 5 AND a = 6 are 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.
  2. Composition produces the maximum: in estimateExprSelectivity (stats.go:1017), each = is estimated 0.001; then estimateAndSelectivity → 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 path ret = andSelectivity(ret, ...), and the observed 1.0 means the first conjunct's expr.Selectivity cache (: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.
  3. Wider observation: literal-contradiction detection would also fix the k2 < 100 AND k2 > 900 case (empty interval → estimateConjunctiveRangeSelectivity already computes lower > 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

  1. 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.
  2. Independent hardening: andSelectivity/estimateAndSelectivity must be monotone — never return more than the minimum operand's selectivity; and = should participate in rangeSelectivityGroupKey so =5 AND >=4 collapses to =5.

Activity

  1. added
    kind/bug
    on Nov 3, 2026
  2. added this to the MO-202611 milestone on Nov 3, 2026
  3. added theissue type on Nov 3, 2026
  4. XuPeng-SH commented on Nov 9, 2026

    @XuPeng-SH
    Contributor

    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.

  5. changed the title [-][Performance]: contradictory same-column predicates estimate as full table[/-] [+][Performance]: compound predicate selectivity violates AND/OR bounds[/+] on Nov 9, 2026
  6. added
    severity/s0
    ai-easy
    and removed on Nov 9, 2026
  7. gouhongshen commented on Nov 10, 2026

    @gouhongshen
    Contributor

    PR #32502 is trying to fix this issue.

  8. gouhongshen commented on Nov 10, 2026

    @gouhongshen
    Contributor

    PR #32502 has merged and is awaiting manual validation. The issue remains open.

  9. XuPeng-SH commented on Nov 10, 2026

    @XuPeng-SH
    Contributor

    合并范围核对:#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 成功。以上均为远端报告,本轮没有运行本地测试或独立审计原始日志,不代表整项验收通过。

  10. added and removed
    severity/s0
    on Nov 18, 2026
  11. Ariznawlll commented on Nov 18, 2026

    @Ariznawlll
    CollaboratorAuthor

    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 端到端复现注释)、BVT test/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)

    范围外(与 XuPeng-SH 合并范围核对一致)

  12. Ariznawlll commented on Nov 19, 2026

    @Ariznawlll
    CollaboratorAuthor

    Warning

    OR/IN 剩余范围在最新 main 仍有真实反例,#32068 不能关闭。实际行数正确不代表优化器估算已修复;AND 矛盾折叠的既有验收结论不变。

    QA 独立复验:#32059 并入的 OR/IN 范围(main)

    • 隔离实例 git_version()=035af99680,对应源码 HEAD 035af996806917b23d9428695e0295b65cb16356。
    • 补齐完整 Git 历史后确认 AND 修复 PR fix(plan): fold contradictory same-column predicates to near-zero estimates #32502 的 merge commit 45917e803f2915f6f0838b2b2fe9f591fc5b6018 是该 HEAD 的祖先;未找到新的 OR/IN 专项修复 PR。
    • 复现原 issue 数据形态:1M 行、k2=i%1000、NDV=1000、二级索引 idx_k2(k2),ANALYZE TABLE big1m(k2) FULLSCAN。另有 100k 行、整数/varchar、无二级索引对照。

    1. 原始带索引场景仍低估

    EXPLAIN VERBOSE 的以下数值来自实际 Index Table Scan 节点(不是脚本假设,也不是 COUNT 执行结果):

    场景 实际行数 估算 outcnt 估算 selectivity 结论
    1M:k2=42 1000 992.06 0.0010 单值健康
    1M:k2=43 1000 992.06 0.0010 单值健康
    1M:k2=42 OR k2=43 2000 72.37 0.0001 低估 27.64 倍,且低于任一操作数
    1M:k2=43 OR k2=42 2000 72.37 0.0001 与顺序无关,仍违反 OR 下界
    1M:k2 IN (42,43) 2000 72.37 0.0001 与错误 OR 一致,未达到二值 union
    1M:三值 OR 3000 72.37 0.0001 低估 41.45 倍
    1M:重复 k2=42 OR k2=42 1000 72.37 0.0001 同样低于单值估算
    100k:k2=5 OR k2=6 200 10.00 0.0001 原报告反例稳定复现,低估 20 倍

    25 组探针中的 SQL COUNT(*) 均与预期一致;失败点是计划基数/选择率,不是执行结果。AND 控制 k2=42 AND k2=43 返回 0 行、估算 outcnt=0.01(selectivity 显示 0.0000),未被本次 OR/IN 反例回退。

    2. 索引/非索引对照缩小了问题范围

    表形态 二值 OR outcnt / selectivity 二值 IN outcnt / selectivity
    1M + idx_k2 72.37 / 0.0001 72.37 / 0.0001
    1M 同数据无 idx_k2 1984.13 / 0.0020 1984.13 / 0.0020
    100k + idx_k2 10.00 / 0.0001 10.00 / 0.0001
    100k 同数据无 idx_k2 198.41 / 0.0020 198.41 / 0.0020

    无索引路径的二值估算符合当前均匀 NDV 近似;不能据此宣称原始带索引问题已修复。varchar pad 的普通 Table Scan 二值 OR/IN 为 200.40 / 0.0020,亦正常。

    3. 白盒定位线索(已结合真实计划核对)

    带索引 OR 和 IN 的实际 Filter Cond 都变成 prefix_in(__mo_index_idx_col):

    Project[1] (outcnt=72.37 selectivity=0.0001)
      -> Index Table Scan[0] on big1m.idx_k2 (outcnt=72.37 selectivity=0.0001)
           Filter Cond: prefix_in(__mo_index_idx_col)
           Block 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 不能证明使用了原始 k2 NDV=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。

  13. removed their assignment
    on Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

ai-easykind/bugneeds-triage

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions