shehabgamin commented on code in PR #15588: URL: https://github.com/apache/datafusion/pull/15588#discussion_r2033892685
########## datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs: ########## @@ -1997,6 +2010,78 @@ fn is_exactly_true(expr: Expr, info: &impl SimplifyInfo) -> Result<Expr> { } } +// i.e. A * 1 -> A +// Move this function body out of the large match branch avoid stack overflow +fn simplify_right_is_one_case<S: SimplifyInfo>( + info: &S, + left: Box<Expr>, + op: &Operator, + right: &Expr, +) -> Result<Transformed<Expr>> { + // Check if resulting type would be different due to coercion + let left_type = info.get_data_type(&left)?; + let right_type = info.get_data_type(right)?; + match BinaryTypeCoercer::new(&left_type, op, &right_type).get_result_type() { + Ok(result_type) => { + // Only cast if the types differ + if left_type != result_type { + Ok(Transformed::yes(Expr::Cast(Cast::new(left, result_type)))) + } else { + Ok(Transformed::yes(*left)) + } + } + Err(_) => Ok(Transformed::yes(*left)), + } +} + +// A * null -> null +fn simplify_right_is_null_case<S: SimplifyInfo>( + info: &S, + left: &Expr, + op: &Operator, + right: Box<Expr>, +) -> Result<Transformed<Expr>> { + // Check if resulting type would be different due to coercion + let left_type = info.get_data_type(left)?; + let right_type = info.get_data_type(&right)?; + match BinaryTypeCoercer::new(&left_type, op, &right_type).get_result_type() { + Ok(result_type) => { + // Only cast if the types differ + if right_type != result_type { + Ok(Transformed::yes(Expr::Cast(Cast::new(right, result_type)))) + } else { + Ok(Transformed::yes(*right)) + } + } + Err(_) => Ok(Transformed::yes(*right)), + } +} + +// null / A --> null +// Move this function body out of the large match branch avoid stack overflow +fn simplify_left_is_null_case<S: SimplifyInfo>( + info: &S, + left: Box<Expr>, + right: &Expr, +) -> Result<Transformed<Expr>> { + // Check if resulting type would be different due to coercion + let left_type = info.get_data_type(&left)?; + let right_type = info.get_data_type(right)?; + match BinaryTypeCoercer::new(&left_type, &Operator::Divide, &right_type) Review Comment: @jayzhan211 Got it! Was going to suggest to rename the function for clarity but it looks like you did that in your latest commit! -- 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