Skip to content

Commit 3491f6b

Browse files
authored
Implement From<Arc<LogicalPlan>> for LogicalPlanBuilder (#10466)
* implement From<Arc<LogicalPlan>> for LogicalPlanBuilder * make fmt happy * added test case and doc comment
1 parent 53de994 commit 3491f6b

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

datafusion/expr/src/logical_plan/builder.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ use crate::utils::{
4242
expand_wildcard, find_valid_equijoin_key_pair, group_window_expr_by_sort_keys,
4343
};
4444
use crate::{
45-
and, binary_expr, DmlStatement, Expr, ExprSchemable, Operator, RecursiveQuery,
46-
TableProviderFilterPushDown, TableSource, WriteOp,
45+
and, binary_expr, logical_plan::tree_node::unwrap_arc, DmlStatement, Expr,
46+
ExprSchemable, Operator, RecursiveQuery, TableProviderFilterPushDown, TableSource,
47+
WriteOp,
4748
};
4849

4950
use arrow::datatypes::{DataType, Field, Fields, Schema, SchemaRef};
@@ -1138,6 +1139,31 @@ impl LogicalPlanBuilder {
11381139
)?))
11391140
}
11401141
}
1142+
1143+
/// Converts a `Arc<LogicalPlan>` into `LogicalPlanBuilder`
1144+
/// fn employee_schema() -> Schema {
1145+
/// Schema::new(vec![
1146+
/// Field::new("id", DataType::Int32, false),
1147+
/// Field::new("first_name", DataType::Utf8, false),
1148+
/// Field::new("last_name", DataType::Utf8, false),
1149+
/// Field::new("state", DataType::Utf8, false),
1150+
/// Field::new("salary", DataType::Int32, false),
1151+
/// ])
1152+
/// }
1153+
/// let plan = table_scan(Some("employee_csv"), &employee_schema(), Some(vec![3, 4]))?
1154+
/// .sort(vec![
1155+
/// Expr::Sort(expr::Sort::new(Box::new(col("state")), true, true)),
1156+
/// Expr::Sort(expr::Sort::new(Box::new(col("salary")), false, false)),
1157+
/// ])?
1158+
/// .build()?;
1159+
/// let plan_builder: LogicalPlanBuilder = Arc::new(plan).into();
1160+
1161+
impl From<Arc<LogicalPlan>> for LogicalPlanBuilder {
1162+
fn from(plan: Arc<LogicalPlan>) -> Self {
1163+
LogicalPlanBuilder::from(unwrap_arc(plan))
1164+
}
1165+
}
1166+
11411167
pub fn change_redundant_column(fields: &Fields) -> Vec<Field> {
11421168
let mut name_map = HashMap::new();
11431169
fields
@@ -2144,4 +2170,21 @@ mod tests {
21442170
);
21452171
Ok(())
21462172
}
2173+
2174+
#[test]
2175+
fn plan_builder_from_logical_plan() -> Result<()> {
2176+
let plan =
2177+
table_scan(Some("employee_csv"), &employee_schema(), Some(vec![3, 4]))?
2178+
.sort(vec![
2179+
Expr::Sort(expr::Sort::new(Box::new(col("state")), true, true)),
2180+
Expr::Sort(expr::Sort::new(Box::new(col("salary")), false, false)),
2181+
])?
2182+
.build()?;
2183+
2184+
let plan_expected = format!("{plan:?}");
2185+
let plan_builder: LogicalPlanBuilder = Arc::new(plan).into();
2186+
assert_eq!(plan_expected, format!("{:?}", plan_builder.plan));
2187+
2188+
Ok(())
2189+
}
21472190
}

0 commit comments

Comments
 (0)