andygrove commented on a change in pull request #8746:
URL: https://github.com/apache/arrow/pull/8746#discussion_r529223608



##########
File path: rust/datafusion/src/logical_plan/expr.rs
##########
@@ -342,6 +386,82 @@ impl Expr {
     }
 }
 
+pub struct CaseWhenBuilder {
+    expr: Option<Box<Expr>>,
+    when_expr: Vec<Expr>,
+    then_expr: Vec<Expr>,
+}
+
+pub struct CaseThenBuilder {
+    expr: Option<Box<Expr>>,
+    when_expr: Vec<Expr>,
+    then_expr: Vec<Expr>,
+}
+
+impl CaseWhenBuilder {
+    pub fn when(&mut self, expr: Expr) -> CaseThenBuilder {
+        self.when_expr.push(expr);
+        CaseThenBuilder {
+            expr: self.expr.clone(),
+            when_expr: self.when_expr.clone(),
+            then_expr: self.then_expr.clone(),
+        }
+    }
+    pub fn or_else(&mut self, else_expr: Expr) -> Expr {
+        Expr::Case {
+            expr: self.expr.clone(),
+            when_then_expr: self
+                .when_expr
+                .iter()
+                .zip(self.then_expr.iter())
+                .map(|(w, t)| (Box::new(w.clone()), Box::new(t.clone())))
+                .collect(),
+            else_expr: Some(Box::new(else_expr)),
+        }
+    }
+    pub fn end(&mut self) -> Expr {
+        Expr::Case {
+            expr: self.expr.clone(),
+            when_then_expr: self
+                .when_expr
+                .iter()
+                .zip(self.then_expr.iter())
+                .map(|(w, t)| (Box::new(w.clone()), Box::new(t.clone())))
+                .collect(),
+            else_expr: None,
+        }
+    }
+}

Review comment:
       I was aiming for a fluent-style API but this could work too. I will take 
a look at some other DataFrame APIs tomorrow (pandas, spark, etc) and see how 
they do 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.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to