Skip to content

fix: General boolean constant folding in filter optimizer#703

Merged
adsharma merged 1 commit into
mainfrom
fix/issue-698-null-contradiction
Jul 18, 2026
Merged

fix: General boolean constant folding in filter optimizer#703
adsharma merged 1 commit into
mainfrom
fix/issue-698-null-contradiction

Conversation

@adsharma

@adsharma adsharma commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

Add a new BoolFoldingOptimizer pass that simplifies boolean filter predicates through constant folding and contradiction detection.

Replaces the earlier NULL-specific isNullContradiction helper with a general-purpose foldBool() function and an areNegations() helper that recognizes:

  - NOT(p) and p
  - IS_NULL(x) and IS_NOT_NULL(x)

Rules:

  NOT(true) / NOT(false)               -> false / true
  AND(false, ...)                      -> false
  AND(p, NOT(p))                       -> false (contradiction)
  AND(IS_NULL(x), IS_NOT_NULL(x))      -> false
  AND() / all-true conjuncts           -> true
  OR(true, ...)                        -> true

When a predicate folds to false/null the filter operator is replaced with an EmptyResult, avoiding unnecessary plan scans.

Fixes #698.

Add a BoolFoldingOptimizer pass that simplifies filter predicates
through bottom-up tree propagation of boolean constant folding.

Unlike the earlier NULL-specific isNullContradiction helper, the
new foldBool() function recursively simplifies children first so
that collapsed subtrees enable parent-level rules:

  AND(OR(IS_NULL(x), IS_NOT_NULL(x)), y > 5)
    -> AND(true, y > 5)        [OR tautology folded]
    -> y > 5                   [AND(true, x) collapsed]

  OR(AND(IS_NULL(x), IS_NOT_NULL(x)), n.k = 1)
    -> OR(false, n.k = 1)      [AND contradiction folded]
    -> n.k = 1                 [OR(false, x) collapsed]

The areNegations() helper recognises three negation forms:
  NOT(p) and p
  IS_NULL(x) and IS_NOT_NULL(x)

Files:
  + src/include/optimizer/bool_folding_optimizer.h
  + src/optimizer/bool_folding_optimizer.cpp
  + test/test_files/issue/issue698.test
  M src/optimizer/CMakeLists.txt
  M src/optimizer/optimizer.cpp

Fixes #698.
@adsharma
adsharma force-pushed the fix/issue-698-null-contradiction branch from 3ac35e2 to 2455a35 Compare July 18, 2026 18:01
@adsharma
adsharma merged commit 109d0b2 into main Jul 18, 2026
4 checks passed
@adsharma
adsharma deleted the fix/issue-698-null-contradiction branch July 18, 2026 18:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NULL contradiction scans the node table instead of producing an empty result

1 participant