Skip to content

Generalize null-rejection checks into a reusable expression trait #21597

@SubhamSinghal

Description

@SubhamSinghal

Is your feature request related to a problem or challenge?

Discussion thread

Several optimizer rules need to determine whether an expression "rejects nulls" — i.e., returns NULL/false when one or more input columns are NULL. Today this logic lives in EliminateOuterJoin's extract_non_nullable_columns() with explicit pattern matching for each expression type (comparison, IN, BETWEEN, LIKE, IS TRUE/FALSE, etc.). Every new expression type must be added manually.

Describe the solution you'd like

Introduce a NullRejection trait on expressions, similar to Apache Calcite's Strong class:

 trait PhysicalExpr {
     // ... existing methods ...

     /// Returns whether this expression is guaranteed to be not-true
     /// (i.e., NULL or false) when all given columns are NULL.
     ///
     /// - `Some(true)`:  definitely rejects nulls (safe to eliminate outer join)
     /// - `Some(false)`: definitely does NOT reject nulls
     /// - `None`:        unknown (conservative default, assume not null-rejecting)
     fn is_not_true(&self, all_null_cols: &[&Column]) -> Option<bool> {
         None
     }
 }

Each expression type overrides with simple structural logic:

// Comparison (=, >, <, etc.): NULL on either side → NULL result
impl PhysicalExpr for BinaryExpr {
    fn is_not_true(&self, all_null_cols: &[&Column]) -> Option<bool> {
        match (self.left.is_not_true(all_null_cols), self.right.is_not_true(all_null_cols)) {
            (Some(true), _) | (_, Some(true)) => Some(true),
            (Some(false), Some(false)) => Some(false),
            _ => None,
        }
    }
}

Describe alternatives you've considered

No response

Additional context

No response

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request
No fields configured for Feature.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions