Skip to content

Incorrect result when optimizing expressions with IN list containing NULL since 50.0.0 #17681

Closed as not planned

Description

@ddupg

Describe the bug

When using an IN clause containing NULL (e.g., col IN (1, NULL)), ExprSimplifier appears to return incorrect results.

To Reproduce

Reproduce test:

mod tests {
    use std::sync::Arc;
    use arrow_schema::{DataType, Field, SchemaRef};
    use datafusion_common::{Column, DFSchema, ScalarValue};
    use datafusion_expr::{BinaryExpr, Cast, Expr, Operator};
    use datafusion_expr::execution_props::ExecutionProps;
    use datafusion_expr::simplify::SimplifyContext;
    use datafusion_optimizer::simplify_expressions::ExprSimplifier;

    pub fn optimize_expr(schema: SchemaRef, expr: Expr) -> datafusion_common::Result<Expr> {
        let df_schema = Arc::new(DFSchema::try_from(schema.as_ref().clone())?);

        // DataFusion needs the simplify and coerce passes to be applied before
        // expressions can be handled by the physical planner.
        let props = ExecutionProps::default();
        let simplify_context = SimplifyContext::new(&props).with_schema(df_schema.clone());
        let simplifier = ExprSimplifier::new(simplify_context);

        let expr = simplifier.simplify(expr)?;
        let expr = simplifier.coerce(expr, &df_schema)?;

        Ok(expr)
    }

    #[test]
    fn test() {
        let schema = Arc::new(arrow_schema::Schema::new(vec![
            Field::new("x", DataType::Int32, true),
        ]));
        // x IN (1, NULL)
        let expr = Expr::BinaryExpr(BinaryExpr::new(
            Box::new(Expr::BinaryExpr(BinaryExpr::new(
                Box::new(Expr::Column(Column::new(None::<String>, "x"))),
                Operator::Eq,
                Box::new(Expr::Literal(ScalarValue::Int32(Some(1)), None)),
            ))),
            Operator::Or,
            Box::new(Expr::BinaryExpr(BinaryExpr::new(
                Box::new(Expr::Column(Column::new(None::<String>, "x"))),
                Operator::Eq,
                Box::new(Expr::Cast(Cast::new(Box::new(Expr::Literal(ScalarValue::Null, None)), DataType::Int32))),
            ))),
        ));
        println!("expr: {:#?}", expr);

        let logical_expr = optimize_expr(schema.clone(), expr).unwrap();
        println!("logical_expr: {:#?}", logical_expr);
    }
}

the output:

expr: BinaryExpr(
    BinaryExpr {
        left: BinaryExpr(
            BinaryExpr {
                left: Column(
                    Column {
                        relation: None,
                        name: "x",
                    },
                ),
                op: Eq,
                right: Literal(
                    Int32(1),
                    None,
                ),
            },
        ),
        op: Or,
        right: BinaryExpr(
            BinaryExpr {
                left: Column(
                    Column {
                        relation: None,
                        name: "x",
                    },
                ),
                op: Eq,
                right: Cast(
                    Cast {
                        expr: Literal(
                            NULL,
                            None,
                        ),
                        data_type: Int32,
                    },
                ),
            },
        ),
    },
)
logical_expr: BinaryExpr(
    BinaryExpr {
        left: BinaryExpr(
            BinaryExpr {
                left: Column(
                    Column {
                        relation: None,
                        name: "x",
                    },
                ),
                op: Eq,
                right: Literal(
                    Int32(1),
                    None,
                ),
            },
        ),
        op: Or,
        right: Literal(
            Boolean(NULL),
            None,
        ),
    },
)

the output of datafusion 49.0.2 is:

logical_expr: BinaryExpr(
    BinaryExpr {
        left: BinaryExpr(
            BinaryExpr {
                left: Column(
                    Column {
                        relation: None,
                        name: "x",
                    },
                ),
                op: Eq,
                right: Literal(
                    Int32(1),
                    None,
                ),
            },
        ),
        op: Or,
        right: BinaryExpr(
            BinaryExpr {
                left: Column(
                    Column {
                        relation: None,
                        name: "x",
                    },
                ),
                op: Eq,
                right: Literal(
                    Int32(NULL),
                    None,
                ),
            },
        ),
    },
)

Expected behavior

No response

Additional context

No response

Activity

  1. added theissue type on Sep 20, 2025
  2. added
    bug
    on Sep 20, 2025
  3. Jefffrey commented on Sep 20, 2025

    @Jefffrey
    Contributor

    This seems to be the intended behaviour per #17088 which introduced this change; could you elaborate on what is incorrect here?

    FYI @findepi

  4. findepi commented on Sep 20, 2025

    @findepi
    Member

    When using an IN clause containing NULL (e.g., col IN (1, NULL)), ExprSimplifier appears to return incorrect results.

    It would help to determine value of col for which expression before simplification and after simplification return different results.

    We could also have SLT tests runs that run with and without simplifications -- results should always be the same.

  5. ddupg commented on Sep 22, 2025

    @ddupg
    MemberAuthor

    Thanks for @Jefffrey @findepi patience in explaining, the change in ExprSimplifier behavior is in line with the semantics of datafusion's handling of NULL.

  6. westonpace commented on Sep 22, 2025

    @westonpace
    Member

    What does NULL as a standalone expression mean? Is it a NULL literal? If so, is it coerced into a null boolean literal when used as part of a WHERE clause? Because SELECT ... FROM ... WHERE NULL is valid but SELECT ... FROM ... WHERE 5 is not.

  7. westonpace commented on Sep 22, 2025

    @westonpace
    Member

    Nvm, I think I answered my own questions. It is indeed a null literal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions