mingmwang commented on code in PR #3969:
URL: https://github.com/apache/arrow-datafusion/pull/3969#discussion_r1006891316
##########
datafusion/physical-expr/src/expressions/case.rs:
##########
@@ -286,6 +290,89 @@ impl PhysicalExpr for CaseExpr {
self.case_when_no_expr(batch)
}
}
+
+ fn children(&self) -> Vec<Arc<dyn PhysicalExpr>> {
+ let mut chileren = vec![];
+ match &self.expr {
+ Some(expr) => chileren.push(expr.clone()),
+ None => chileren.push(Arc::new(NoOp::new())),
+ }
+ self.when_then_expr.iter().for_each(|(cond, value)| {
+ chileren.push(cond.clone());
+ chileren.push(value.clone());
+ });
+
+ match &self.else_expr {
+ Some(expr) => chileren.push(expr.clone()),
+ None => chileren.push(Arc::new(NoOp::new())),
+ }
+ chileren
+ }
+
+ // For physical CaseExpr, we do not allow modifying children size
+ fn with_new_children(
+ self: Arc<Self>,
+ children: Vec<Arc<dyn PhysicalExpr>>,
+ ) -> Result<Arc<dyn PhysicalExpr>> {
+ if children.len() != self.children().len() {
+ Err(DataFusionError::Internal(
+ "CaseExpr: Wrong number of children".to_string(),
+ ))
+ } else {
+ assert_eq!(children.len() % 2, 0);
+ let expr = match
children[0].clone().as_any().downcast_ref::<NoOp>() {
+ Some(_) => None,
+ _ => Some(children[0].clone()),
+ };
+ let else_expr = match children[children.len() - 1]
+ .clone()
+ .as_any()
+ .downcast_ref::<NoOp>()
+ {
+ Some(_) => None,
+ _ => Some(children[children.len() - 1].clone()),
+ };
+
+ let branches = children[1..children.len() - 1].to_vec();
+ let mut when_then_expr: Vec<WhenThen> = vec![];
+ for (prev, next) in branches.into_iter().tuples() {
+ when_then_expr.push((prev, next));
+ }
+ Ok(Arc::new(CaseExpr::try_new(
+ expr,
+ when_then_expr,
+ else_expr,
+ )?))
+ }
+ }
+}
+
+impl PartialEq<dyn Any> for CaseExpr {
+ fn eq(&self, other: &dyn Any) -> bool {
+ down_cast_any_ref(other)
+ .downcast_ref::<Self>()
+ .map(|x| {
+ let expr_eq = match (&self.expr, &x.expr) {
+ (Some(expr1), Some(expr2)) => expr1.eq(expr2),
+ (None, None) => true,
+ _ => false,
Review Comment:
I would prefer the current implementation than returning from the Closure.
Returning from the Closure might
confuse people, does it mean return from the Closure or return from the
outer method? In Rust it is returned from the Closure but in Scala or Java(I
remember) it is returned from the outer method. In my opinion, it is not clear
and more lines of code.
````
fn eq(&self, other: &dyn Any) -> bool {
down_cast_any_ref(other)
.downcast_ref::<Self>()
.map(|x| {
match (&self.expr, &x.expr) {
(Some(expr1), Some(expr2)) =>{
if expr1.ne(expr2) {
return false;
}
},
(None, None) => {}
_ => return false,
};
match (&self.else_expr, &x.else_expr) {
(Some(expr1), Some(expr2)) => {
if expr1.ne(expr2) {
return false;
}
},
(None, None) => {}
_ => return false,
};
self.when_then_expr.len() == x.when_then_expr.len()
&&
self.when_then_expr.iter().zip(x.when_then_expr.iter()).all(
|((when1, then1), (when2, then2))| {
when1.eq(when2) && then1.eq(then2)
},
)
})
.unwrap_or(false)
}
````
--
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]