jayzhan211 commented on code in PR #15588:
URL: https://github.com/apache/datafusion/pull/15588#discussion_r2032398015
##########
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:
the function is only used in Divide not other op, different op may behave
differently.
In Multiple case, I switch left and right instead.
--
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]