lyne7-sc opened a new pull request, #23148:
URL: https://github.com/apache/datafusion/pull/23148

   ## Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and 
enhancements and this helps us generate change logs for our releases. You can 
link an issue to this PR using the GitHub syntax. For example `Closes #123` 
indicates that this PR will close issue #123.
   -->
   
   - Related to #13232.
   
   ## Rationale for this change
   
   <!--
    Why are you proposing this change? If this is already explained clearly in 
the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your 
changes and offer better suggestions for fixes.  
   -->
   
   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.
   
   ```sql
   SELECT t1.a
   FROM t1 LEFT JOIN t2 ON t1.a = t2.x
   WHERE abs(t2.y) > 5;
   ```
   
   Rows produced by the unmatched side of the left join have `t2.y = NULL`. 
Since `abs(NULL)` is also `NULL`, the predicate `abs(t2.y) > 5` cannot 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 is 
`false` so 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 INPUT` on 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 INPUT`
     https://www.postgresql.org/docs/current/sql-createfunction.html
   
   - DuckDB `FunctionNullHandling` definition
     
https://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?
   
   - Adds `ScalarUDFImpl::is_strict()`, defaulting to false.
   - Adds `ScalarUDF::is_strict()` as the public forwarding API.
   - Marks `abs` as strict.
   - Uses strict scalar functions in predicate/nullability reasoning and outer 
join elimination.
   - Propagates `is_strict` through `datafusion-ffi::FFI_ScalarUDF`.
   - Adds unit and slt coverage for strict and non-strict scalar UDF behavior.
   
   <!--
   There is no need to duplicate the description in the issue here but it is 
sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   ## Are these changes tested?
   
   Yes.
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are 
they covered by existing tests)?
   -->
   
   ## 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.
   
   <!--
   If there are user-facing changes then we may require documentation to be 
updated before approving the PR.
   -->
   
   <!--
   If there are any breaking changes to public APIs, please add the `api 
change` label.
   -->
   
   ## Future work
   
   - Mark more built-in scalar functions as strict where the behavior is 
clearly NULL-in/NULL-out.
   - Reuse `is_strict()` in other optimizer rules to infer `arg IS NOT NULL` 
from predicates like `strict_func(arg) > 0`.
   - Use inferred non-null predicates to simplify redundant `IS NOT NULL` 
checks and push filters closer to scans.
   - Use strictness in statistics/selectivity estimation by deriving tighter 
nullability information for function outputs.
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to