JasonLi-cn commented on code in PR #13249: URL: https://github.com/apache/datafusion/pull/13249#discussion_r1828609903
########## datafusion/optimizer/src/utils.rs: ########## @@ -129,52 +128,32 @@ pub fn log_plan(description: &str, plan: &LogicalPlan) { /// `c0 > 8` return true; /// `c0 IS NULL` return false. pub fn is_restrict_null_predicate<'a>( + input_schema: &DFSchema, predicate: Expr, - join_cols_of_predicate: impl IntoIterator<Item = &'a Column>, + cols_of_predicate: impl IntoIterator<Item = &'a Column>, ) -> Result<bool> { if matches!(predicate, Expr::Column(_)) { return Ok(true); } - static DUMMY_COL_NAME: &str = "?"; - let schema = Schema::new(vec![Field::new(DUMMY_COL_NAME, DataType::Null, true)]); - let input_schema = DFSchema::try_from(schema.clone())?; - let column = new_null_array(&DataType::Null, 1); - let input_batch = RecordBatch::try_new(Arc::new(schema.clone()), vec![column])?; let execution_props = ExecutionProps::default(); - let null_column = Column::from_name(DUMMY_COL_NAME); + let replace_columns = cols_of_predicate.into_iter().collect(); + let replaced_predicate = replace_expr_with_null(predicate, &replace_columns)?; + let coerced_predicate = coerce(replaced_predicate, input_schema)?; - let join_cols_to_replace = join_cols_of_predicate - .into_iter() - .map(|column| (column, &null_column)) - .collect::<HashMap<_, _>>(); + let info = SimplifyContext::new(&execution_props) + .with_schema(Arc::new(input_schema.clone())); + let simplifier = ExprSimplifier::new(info).with_canonicalize(false); + let expr = simplifier.simplify(coerced_predicate)?; - let replaced_predicate = replace_col(predicate, &join_cols_to_replace)?; - let coerced_predicate = coerce(replaced_predicate, &input_schema)?; - let phys_expr = - create_physical_expr(&coerced_predicate, &input_schema, &execution_props)?; + let ret = match &expr { + Expr::Literal(scalar) if scalar.is_null() => true, + Expr::Literal(ScalarValue::Boolean(Some(b))) => !b, + _ if matches!(expr.get_type(input_schema)?, DataType::Null) => true, + _ => false, + }; - let result_type = phys_expr.data_type(&schema)?; - if !matches!(&result_type, DataType::Boolean) { - return Ok(false); - } - - // If result is single `true`, return false; - // If result is single `NULL` or `false`, return true; - Ok(match phys_expr.evaluate(&input_batch)? { - ColumnarValue::Array(array) => { Review Comment: Unlike before, we have now added the logic of 'expression simplification'. In `ExprSimplifier`, the previous `evaluate ` is implemented by `ConstEvaluator`. https://github.com/apache/datafusion/blob/0458d30b4bf078251ac4c4dfc4669e8beec8bc3b/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs#L633-L650 -- 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: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org