peter-toth commented on code in PR #10115:
URL:
https://github.com/apache/arrow-datafusion/pull/10115#discussion_r1570666526
##########
datafusion/optimizer/src/unwrap_cast_in_comparison.rs:
##########
@@ -140,140 +139,132 @@ struct UnwrapCastExprRewriter {
impl TreeNodeRewriter for UnwrapCastExprRewriter {
type Node = Expr;
- fn f_up(&mut self, expr: Expr) -> Result<Transformed<Expr>> {
- match &expr {
+ fn f_up(&mut self, mut expr: Expr) -> Result<Transformed<Expr>> {
+ match &mut expr {
// For case:
// try_cast/cast(expr as data_type) op literal
// literal op try_cast/cast(expr as data_type)
- Expr::BinaryExpr(BinaryExpr { left, op, right }) => {
- let left = left.as_ref().clone();
- let right = right.as_ref().clone();
- let left_type = left.get_type(&self.schema)?;
- let right_type = right.get_type(&self.schema)?;
- // Because the plan has been done the type coercion, the left
and right must be equal
- if is_support_data_type(&left_type)
- && is_support_data_type(&right_type)
- && is_comparison_op(op)
- {
- match (&left, &right) {
- (
- Expr::Literal(left_lit_value),
- Expr::TryCast(TryCast { expr, .. })
- | Expr::Cast(Cast { expr, .. }),
- ) => {
- // if the left_lit_value can be casted to the type
of expr
- // we need to unwrap the cast for cast/try_cast
expr, and add cast to the literal
- let expr_type = expr.get_type(&self.schema)?;
- let casted_scalar_value =
- try_cast_literal_to_type(left_lit_value,
&expr_type)?;
- if let Some(value) = casted_scalar_value {
- // unwrap the cast/try_cast for the right expr
- return Ok(Transformed::yes(binary_expr(
- lit(value),
- *op,
- expr.as_ref().clone(),
- )));
- }
- }
- (
- Expr::TryCast(TryCast { expr, .. })
- | Expr::Cast(Cast { expr, .. }),
- Expr::Literal(right_lit_value),
- ) => {
- // if the right_lit_value can be casted to the
type of expr
- // we need to unwrap the cast for cast/try_cast
expr, and add cast to the literal
- let expr_type = expr.get_type(&self.schema)?;
- let casted_scalar_value =
- try_cast_literal_to_type(right_lit_value,
&expr_type)?;
- if let Some(value) = casted_scalar_value {
- // unwrap the cast/try_cast for the left expr
- return Ok(Transformed::yes(binary_expr(
- expr.as_ref().clone(),
- *op,
- lit(value),
- )));
- }
- }
- (_, _) => {
- // do nothing
- }
+ Expr::BinaryExpr(BinaryExpr { left, op, right })
+ if {
+ let Ok(left_type) = left.get_type(&self.schema) else {
+ return Ok(Transformed::no(expr));
};
+ let Ok(right_type) = right.get_type(&self.schema) else {
+ return Ok(Transformed::no(expr));
+ };
+ is_support_data_type(&left_type)
+ && is_support_data_type(&right_type)
+ && is_comparison_op(op)
+ } =>
+ {
+ match (left.as_mut(), right.as_mut()) {
+ (
+ Expr::Literal(left_lit_value),
+ Expr::TryCast(TryCast {
+ expr: right_expr, ..
+ })
+ | Expr::Cast(Cast {
+ expr: right_expr, ..
+ }),
+ ) => {
+ // if the left_lit_value can be casted to the type of
expr
+ // we need to unwrap the cast for cast/try_cast expr,
and add cast to the literal
+ let Ok(expr_type) = right_expr.get_type(&self.schema)
else {
+ return Ok(Transformed::no(expr));
+ };
+ let Ok(Some(value)) =
+ try_cast_literal_to_type(left_lit_value,
&expr_type)
+ else {
+ return Ok(Transformed::no(expr));
+ };
+ **left = lit(value);
+ // unwrap the cast/try_cast for the right expr
+ **right =
+ mem::replace(right_expr,
Expr::Literal(ScalarValue::Null));
+ Ok(Transformed::yes(expr))
+ }
+ (
+ Expr::TryCast(TryCast {
+ expr: left_expr, ..
+ })
+ | Expr::Cast(Cast {
+ expr: left_expr, ..
+ }),
+ Expr::Literal(right_lit_value),
+ ) => {
+ // if the right_lit_value can be casted to the type of
expr
+ // we need to unwrap the cast for cast/try_cast expr,
and add cast to the literal
+ let Ok(expr_type) = left_expr.get_type(&self.schema)
else {
+ return Ok(Transformed::no(expr));
+ };
+ let Ok(Some(value)) =
+ try_cast_literal_to_type(right_lit_value,
&expr_type)
+ else {
+ return Ok(Transformed::no(expr));
+ };
+ // unwrap the cast/try_cast for the left expr
+ **left =
+ mem::replace(left_expr,
Expr::Literal(ScalarValue::Null));
Review Comment:
I've opened a minor PR to add the default `Expr`:
https://github.com/apache/arrow-datafusion/pull/10127, but as `Expr` is just an
enum, I don't know how to panic inside it.
--
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]