Skip to content

Preserve NULL matches in extended CASE filters - #30992

Draft
bvolpato wants to merge 1 commit into
trinodb:masterfrom
bvolpato:bvolpato/fix-extended-case-null-filter
Draft

Preserve NULL matches in extended CASE filters#30992
bvolpato wants to merge 1 commit into
trinodb:masterfrom
bvolpato:bvolpato/fix-extended-case-null-filter

Conversation

@bvolpato

@bvolpato bvolpato commented Sep 3, 2026

Copy link
Copy Markdown
Member

Description

Preserve extended CASE clauses that can match a NULL operand when simplifying filters. The old shortcut always selected ELSE for a NULL operand, which is invalid for predicates such as IS NOT DISTINCT FROM.

SELECT x
FROM UNNEST(ARRAY[NULL, 1]) t(x)
WHERE CASE CAST(NULL AS integer)
    WHEN IS NOT DISTINCT FROM x THEN true
    ELSE false
END;

Before the fix this returns no rows; afterward it returns the required NULL row. Swapping THEN and ELSE previously admitted the NULL row incorrectly.

Remove the obsolete shortcut and leave predicate-aware evaluation to the IR optimizer. The rule retains its existing simplifications when all result branches agree.

Additional context and related issues

The regression tests cover both filter outcomes, with projection, searched CASE, and ordinary equality CASE controls. Two existing rule expectations are updated to stop asserting the obsolete shortcut.

Before the production change, the selected rule and query tests had three failures and two passing controls. Afterward, all 30 tests in TestSimplifyFilterPredicate, TestExtendedCase, and TestEvaluateMatch passed. Module validation, including Checkstyle and Airstyle, passed.

./mvnw -pl core/trino-main -Dtest=TestSimplifyFilterPredicate,TestExtendedCase,TestEvaluateMatch -Dair.check.skip-all=true test
./mvnw -pl core/trino-main validate

Release notes

(x) Release notes are required, with the following suggested text:

## General
* Fix missing or unexpected rows when filtering with extended CASE expressions whose operand is NULL.

Remove the filter simplifier's assumption that a NULL CASE operand
always selects ELSE. Extended CASE predicates can match NULL, so
predicate-aware evaluation must decide which branch is selected.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

2 participants