sdf-jkl commented on code in PR #18789: URL: https://github.com/apache/datafusion/pull/18789#discussion_r2551075612
########## datafusion/optimizer/src/simplify_expressions/udf_preimage.rs: ########## @@ -0,0 +1,407 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::str::FromStr; + +use arrow::compute::kernels::cast_utils::IntervalUnit; +use datafusion_common::{internal_err, tree_node::Transformed, Result, ScalarValue}; +use datafusion_expr::{ + and, expr::ScalarFunction, lit, or, simplify::SimplifyInfo, BinaryExpr, Expr, + Operator, ScalarUDFImpl, +}; +use datafusion_functions::datetime::date_part::DatePartFunc; + +pub(super) fn preimage_in_comparison_for_binary( + info: &dyn SimplifyInfo, + udf_expr: Expr, + literal: Expr, + op: Operator, +) -> Result<Transformed<Expr>> { + let (func, args, lit_value) = match (udf_expr, literal) { + ( + Expr::ScalarFunction(ScalarFunction { func, args }), + Expr::Literal(lit_value, _), + ) => (func, args, lit_value), + _ => return internal_err!("Expect date_part expr and literal"), + }; + let expr = Box::new(args[1].clone()); + + let Ok(expr_type) = info.get_data_type(&expr) else { + return internal_err!("Can't get the data type of the expr {:?}", &expr); + }; + + let preimage_func = match func.name() { + "date_part" => DatePartFunc::new(), + _ => return internal_err!("Preimage is not supported for {:?}", func.name()), + }; + + let rewritten_expr = match op { + Operator::Lt | Operator::GtEq => { + let v = match preimage_func.preimage_cast(&lit_value, &expr_type, op) { + Some(v) => v, + None => { + return internal_err!("Could not cast literal to the column type") + } + }; Review Comment: These 6 lines are repeated in every match arm. -- 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]
