Skip to content

fix: normalize per-column IN/NOT IN/=/<> domains in scan filters - #24229

Merged
8 commits merged into
matrixorigin:3.0-devfrom
ck89119:bigsql
Apr 29, 2026
Merged

8 commits merged into
matrixorigin:3.0-devfrom
ck89119:bigsql

Conversation

@ck89119

@ck89119 ck89119 commented Apr 27, 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:

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.

ck89119 and others added 2 commits April 27, 2026 17:38
Generalize the single-table scan filter rewrite into `normalizeColumnDomain`:
merge positive points (IN / =), negative points (NOT IN / <>), and IS NULL
per column, then emit a single canonical IN / = / FALSE. The outer domain is
also propagated into OR branches and nested NOT(IN), which is the main
bigsql optimization case. BETWEEN is left as-is; list-with-NULL and
cross-column predicates are skipped to preserve 3-valued logic.

Fixes: matrixorigin#24227

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@mergify
mergify Bot requested a review from XuPeng-SH April 27, 2026 11:05
@Ariznawlll

Copy link
Copy Markdown
Collaborator

PR #24229 测试覆盖分析

变更摘要

本次 PR 对 SQL 优化器的单表扫描过滤条件(scan filters)进行了归一化处理,主要在 pkg/sql/plan/expr_opt.go 新增和重构了 normalizeColumnDomain 相关逻辑,实现了对同一列的 IN/NOT IN/=/<> 等谓词的合并与规约。例如:col IN S1 AND col IN S2 自动归约为 col IN (S1 ∩ S2),col IN S1 AND col <> v 归约为 col IN (S1 \ {v}),并能正确处理 NULL、OR 分支等复杂场景。UT 覆盖了典型场景。涉及文件为 pkg/sql/plan/expr_opt.go、pkg/sql/plan/filter_domain_test.go、pkg/sql/plan/opt_misc.go。

6 类测试覆盖情况

测试类型 覆盖状态 说明
BVT ⚠️ 涉及 SQL 计划优化(scan filter 规约),需补充覆盖 IN/NOT IN/<>/IS NULL 等组合场景的 SQL 回归测试。
稳定性 ✅ 归一化逻辑影响查询计划,TPCH 等稳定性测试会覆盖典型复杂查询。
Chaos ➖ 仅优化器层逻辑,无需故障注入验证。
大数据 ⚠️ 大数据量下复杂谓词规约可能影响执行计划选择,建议补充大数据量下的 IN/NOT IN 组合查询。
PITR ➖ 不涉及备份恢复流程。
Snapshot ➖ 不涉及快照流程。

图例: ✅ 已覆盖 ⚠️ 需补充 ➖ 不相关

建议

BVT 测试建议:
补充如下 SQL 场景的回归用例(test/distributed/cases/optimizer/):

-- IN + NOT IN
SELECT * FROM t1 WHERE col IN (1,2,3) AND col NOT IN (2);

-- IN + <>
SELECT * FROM t1 WHERE col IN (1,2,3) AND col <> 3;

-- IN + IN
SELECT * FROM t1 WHERE col IN (1,2,3) AND col IN (2,3,4);

-- IN + IS NULL
SELECT * FROM t1 WHERE col IN (1,2) AND col IS NULL;

-- IN + OR + NOT IN
SELECT * FROM t1 WHERE col IN (1,2,3) AND (col = 1 OR col NOT IN (2));

每个 case 需验证结果与预期一致,建议对比 MySQL 行为。

大数据测试建议:
在大数据量表(如 1000 万行)上补充如下查询(tools/mo-regression-test/cases/{customer}/qXX.sql):

SELECT COUNT(*) FROM big_table WHERE col IN (100,200,300,400,500) AND col NOT IN (200,400);
SELECT COUNT(*) FROM big_table WHERE col IN (100,200,300) AND col <> 300;

用于验证大数据下谓词规约的正确性和执行效率。

🤖 自动生成的测试 PR

❌ BVT PR 提交失败:command failed (exit 1): gh pr create --repo matrixorigin/matrixone --base main --head Ariznawlll:auto/bvt-scan_filter_in_notin_domain_normalization-pr24229 --title test(bvt): 验证 Table Scan filter IN/NOT IN/<> 组合的 domain 归一化优化效果 (from PR #24229) --body Auto-generated BVT cases for matrixone PR #24229.

说明: 验证 Table Scan filter IN/NOT IN/<> 组合的 domain 归一化优化效果

新增文件:

  • test/distributed/cases/optimizer/scan_filter_in_notin_domain_normalization.sql
  • test/distributed/cases/optimizer/scan_filter_in_notin_domain_normalization.result

由 auto-test-pr bot 自动生成,请人工 review 后合并
❌ 大数据 PR 生成失败(LLM 解析错误):Unterminated string starting at: line 7 column 18 (char 228)


由 AI Test Analyzer 自动生成 · workflow run

- UT: outer IN(..,NULL) must not collapse `<>`/NOT IN against the
  nullable domain; cast(uint8 as int64) <> 256 must not shrink the
  uint8 IN domain; cast(uint8 as int64) = -1 must not poison the
  point set.
- BVT: make the cross-column case actually exercise `b <> v` (previous
  data could not distinguish the predicate being dropped); add
  `NOT IN (..,NULL)` variants (top-level and under OR) to lock
  3-valued-logic semantics end-to-end.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
This pull request was closed.

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.

4 participants