This is an automated email from the ASF dual-hosted git repository.

alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git


The following commit(s) were added to refs/heads/main by this push:
     new 3491f6bd50 Implement `From<Arc<LogicalPlan>>` for `LogicalPlanBuilder` 
(#10466)
3491f6bd50 is described below

commit 3491f6bd5003624dc064db410eeaa41ef3f86acf
Author: Abrar Khan <[email protected]>
AuthorDate: Mon May 13 18:53:59 2024 +0530

    Implement `From<Arc<LogicalPlan>>` for `LogicalPlanBuilder` (#10466)
    
    * implement From<Arc<LogicalPlan>> for LogicalPlanBuilder
    
    * make fmt happy
    
    * added test case and doc comment
---
 datafusion/expr/src/logical_plan/builder.rs | 47 +++++++++++++++++++++++++++--
 1 file changed, 45 insertions(+), 2 deletions(-)

diff --git a/datafusion/expr/src/logical_plan/builder.rs 
b/datafusion/expr/src/logical_plan/builder.rs
index 2c6cfd8f9d..6055537ac5 100644
--- a/datafusion/expr/src/logical_plan/builder.rs
+++ b/datafusion/expr/src/logical_plan/builder.rs
@@ -42,8 +42,9 @@ use crate::utils::{
     expand_wildcard, find_valid_equijoin_key_pair, 
group_window_expr_by_sort_keys,
 };
 use crate::{
-    and, binary_expr, DmlStatement, Expr, ExprSchemable, Operator, 
RecursiveQuery,
-    TableProviderFilterPushDown, TableSource, WriteOp,
+    and, binary_expr, logical_plan::tree_node::unwrap_arc, DmlStatement, Expr,
+    ExprSchemable, Operator, RecursiveQuery, TableProviderFilterPushDown, 
TableSource,
+    WriteOp,
 };
 
 use arrow::datatypes::{DataType, Field, Fields, Schema, SchemaRef};
@@ -1138,6 +1139,31 @@ impl LogicalPlanBuilder {
         )?))
     }
 }
+
+/// Converts a `Arc<LogicalPlan>` into `LogicalPlanBuilder`
+/// fn employee_schema() -> Schema {
+///     Schema::new(vec![
+///         Field::new("id", DataType::Int32, false),
+///         Field::new("first_name", DataType::Utf8, false),
+///         Field::new("last_name", DataType::Utf8, false),
+///         Field::new("state", DataType::Utf8, false),
+///         Field::new("salary", DataType::Int32, false),
+///     ])
+/// }
+/// let plan = table_scan(Some("employee_csv"), &employee_schema(), 
Some(vec![3, 4]))?
+///     .sort(vec![
+///         Expr::Sort(expr::Sort::new(Box::new(col("state")), true, true)),
+///         Expr::Sort(expr::Sort::new(Box::new(col("salary")), false, false)),
+///      ])?
+///     .build()?;
+/// let plan_builder: LogicalPlanBuilder = Arc::new(plan).into();
+
+impl From<Arc<LogicalPlan>> for LogicalPlanBuilder {
+    fn from(plan: Arc<LogicalPlan>) -> Self {
+        LogicalPlanBuilder::from(unwrap_arc(plan))
+    }
+}
+
 pub fn change_redundant_column(fields: &Fields) -> Vec<Field> {
     let mut name_map = HashMap::new();
     fields
@@ -2144,4 +2170,21 @@ mod tests {
         );
         Ok(())
     }
+
+    #[test]
+    fn plan_builder_from_logical_plan() -> Result<()> {
+        let plan =
+            table_scan(Some("employee_csv"), &employee_schema(), Some(vec![3, 
4]))?
+                .sort(vec![
+                    Expr::Sort(expr::Sort::new(Box::new(col("state")), true, 
true)),
+                    Expr::Sort(expr::Sort::new(Box::new(col("salary")), false, 
false)),
+                ])?
+                .build()?;
+
+        let plan_expected = format!("{plan:?}");
+        let plan_builder: LogicalPlanBuilder = Arc::new(plan).into();
+        assert_eq!(plan_expected, format!("{:?}", plan_builder.plan));
+
+        Ok(())
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to