Skip to content

Commit d6ff3db

Browse files
committed
sql: export function that makes explain from the plan
It is need in the situation when there is a custom plan under `EXPLAIN` and we want plan it to logical plan by ourself and want to wrap it with explain node the same way as DF does.
1 parent 47d8edc commit d6ff3db

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

datafusion/sql/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ mod relation;
4343
mod select;
4444
mod set_expr;
4545
mod statement;
46+
pub use statement::make_explain;
4647
#[cfg(feature = "unparser")]
4748
pub mod unparser;
4849
pub mod utils;

datafusion/sql/src/statement.rs

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,26 +1102,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
11021102
return plan_err!("Nested EXPLAINs are not supported");
11031103
}
11041104
let plan = Arc::new(plan);
1105-
let schema = LogicalPlan::explain_schema();
1106-
let schema = schema.to_dfschema_ref()?;
1107-
1108-
if analyze {
1109-
Ok(LogicalPlan::Analyze(Analyze {
1110-
verbose,
1111-
input: plan,
1112-
schema,
1113-
}))
1114-
} else {
1115-
let stringified_plans =
1116-
vec![plan.to_stringified(PlanType::InitialLogicalPlan)];
1117-
Ok(LogicalPlan::Explain(Explain {
1118-
verbose,
1119-
plan,
1120-
stringified_plans,
1121-
schema,
1122-
logical_optimization_succeeded: false,
1123-
}))
1124-
}
1105+
make_explain(verbose, analyze, plan)
11251106
}
11261107

11271108
fn show_variable_to_plan(&self, variable: &[Ident]) -> Result<LogicalPlan> {
@@ -1599,3 +1580,30 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
15991580
.is_ok()
16001581
}
16011582
}
1583+
1584+
/// Make an explain node using provided plan and explan parameters.
1585+
pub fn make_explain(
1586+
verbose: bool,
1587+
analyze: bool,
1588+
plan: Arc<LogicalPlan>,
1589+
) -> Result<LogicalPlan> {
1590+
let schema = LogicalPlan::explain_schema();
1591+
let schema = schema.to_dfschema_ref()?;
1592+
1593+
if analyze {
1594+
Ok(LogicalPlan::Analyze(Analyze {
1595+
verbose,
1596+
input: plan,
1597+
schema,
1598+
}))
1599+
} else {
1600+
let stringified_plans = vec![plan.to_stringified(PlanType::InitialLogicalPlan)];
1601+
Ok(LogicalPlan::Explain(Explain {
1602+
verbose,
1603+
plan,
1604+
stringified_plans,
1605+
schema,
1606+
logical_optimization_succeeded: false,
1607+
}))
1608+
}
1609+
}

0 commit comments

Comments
 (0)