feat: add strictness metadata for scalar UDF null propagation and use it in outer join elimination#23148
Open
lyne7-sc wants to merge 4 commits into
Open
feat: add strictness metadata for scalar UDF null propagation and use it in outer join elimination#23148lyne7-sc wants to merge 4 commits into
lyne7-sc wants to merge 4 commits into
Conversation
|
Thank you for opening this pull request! Reviewer note: cargo-semver-checks reported the current version number is not SemVer-compatible with the changes in this pull request (compared against the base branch). Details |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Rationale for this change
Outer join elimination relies on proving that a filter rejects the NULL-padding rows introduced by an outer join. DataFusion already handles many built-in NULL-propagating expressions, but scalar UDFs did not expose whether they preserve the same property.
Rows produced by the unmatched side of the left join have
t2.y = NULL. Sinceabs(NULL)is alsoNULL, the predicateabs(t2.y) > 5cannot evaluate to true for those rows. That means the left join can be safely rewritten as an inner join. Without function-level null propagation metadata, the optimizer has to treat scalar functions conservatively and misses this rewrite.This PR adds
ScalarUDFImpl::is_strict()to let scalar UDF implementations declare that they always return NULL when any argument is NULL. The default isfalseso existing UDFs remain conservative. Optimizer rules can then use this metadata when reasoning about expression nullability and null-rejecting predicates.This design follows a pattern used by other query engines. PostgreSQL exposes
STRICT / RETURNS NULL ON NULL INPUTon functions, and documents that such functions are not executed when any argument is NULL; a NULL result is assumed automatically. DuckDB similarly has function null-handling metadata, with default NULL-in/NULL-out behavior and special handling for functions that do not follow that rule.References:
PostgreSQL
CREATE FUNCTION: STRICT / RETURNS NULL ON NULL INPUThttps://www.postgresql.org/docs/current/sql-createfunction.html
DuckDB
FunctionNullHandlingdefinitionhttps://github.com/duckdb/duckdb/blob/main/src/include/duckdb/function/function.hpp
DuckDB scalar function null-handling API
https://github.com/duckdb/duckdb/blob/main/src/include/duckdb/function/scalar_function.hpp
What changes are included in this PR?
ScalarUDFImpl::is_strict(), defaulting to false.ScalarUDF::is_strict()as the public forwarding API.absas strict.is_strictthroughdatafusion-ffi::FFI_ScalarUDF.Are these changes tested?
Yes.
Are there any user-facing changes?
Yes. Scalar UDF authors can now override ScalarUDFImpl::is_strict() to tell the optimizer that their function always returns NULL when any argument is NULL.
This PR also changes the FFI_ScalarUDF layout to propagate strictness across the FFI boundary, so it should carry the api change label.
Future work
is_strict()in other optimizer rules to inferarg IS NOT NULLfrom predicates likestrict_func(arg) > 0.IS NOT NULLchecks and push filters closer to scans.