Skip to content

Apache DataFusion SQL Query Engine

The CLI panics when passing an invalid explain query (#4429)

comphead02534597248c
diff --git a/datafusion/core/tests/sql/explain_analyze.rs b/datafusion/core/tests/sql/explain_analyze.rs
index c4802b43f6..5479bf529f 100644
--- a/datafusion/core/tests/sql/explain_analyze.rs
+++ b/datafusion/core/tests/sql/explain_analyze.rs
@@ -907,3 +907,25 @@ async fn explain_physical_plan_only() {
]];
assert_eq!(expected, actual);
}
+
+#[tokio::test]
+async fn explain_nested() {
+ async fn test_nested_explain(explain_phy_plan_flag: bool) {
+ let config = SessionConfig::new()
+ .set_bool(OPT_EXPLAIN_PHYSICAL_PLAN_ONLY, explain_phy_plan_flag);
+ let ctx = SessionContext::with_config(config);
+ let sql = "EXPLAIN explain select 1";
+ let plan = ctx.create_logical_plan(sql).unwrap();
+
+ let plan = ctx.create_physical_plan(&plan).await;
+
+ assert!(plan
+ .err()
+ .unwrap()
+ .to_string()
+ .contains("Explain must be root of the plan"));
+ }
+
+ test_nested_explain(true).await;
+ test_nested_explain(false).await;
+}
diff --git a/datafusion/expr/src/utils.rs b/datafusion/expr/src/utils.rs
index c84f4b8d70..71088d2db9 100644
--- a/datafusion/expr/src/utils.rs
+++ b/datafusion/expr/src/utils.rs
@@ -563,16 +563,20 @@ pub fn from_plan(
}
LogicalPlan::Explain(_) => {
// Explain should be handled specially in the optimizers;
- // If this assert fails it means some optimizer pass is
+ // If this check cannot pass it means some optimizer pass is
// trying to optimize Explain directly
- assert!(
- expr.is_empty(),
- "Explain can not be created from utils::from_expr"
- );
- assert!(
- inputs.is_empty(),
- "Explain can not be created from utils::from_expr"
- );
+ if expr.is_empty() {
+ return Err(DataFusionError::Plan(
+ "Invalid EXPLAIN command. Expression is empty".to_string(),
+ ));
+ }
+
+ if inputs.is_empty() {
+ return Err(DataFusionError::Plan(
+ "Invalid EXPLAIN command. Inputs are empty".to_string(),
+ ));
+ }
+
Ok(plan.clone())
}
LogicalPlan::EmptyRelation(_)