diff --git a/datafusion/src/execution/context.rs b/datafusion/src/execution/context.rs index deec84d5a0ff8..fb271a1a7e568 100644 --- a/datafusion/src/execution/context.rs +++ b/datafusion/src/execution/context.rs @@ -1347,44 +1347,6 @@ mod tests { )); } - #[test] - fn optimize_explain() { - let schema = Schema::new(vec![Field::new("id", DataType::Int32, false)]); - - let plan = LogicalPlanBuilder::scan_empty(Some("employee"), &schema, None) - .unwrap() - .explain(true, false) - .unwrap() - .build() - .unwrap(); - - if let LogicalPlan::Explain(e) = &plan { - assert_eq!(e.stringified_plans.len(), 1); - } else { - panic!("plan was not an explain: {:?}", plan); - } - - // now optimize the plan and expect to see more plans - let optimized_plan = ExecutionContext::new().optimize(&plan).unwrap(); - if let LogicalPlan::Explain(e) = &optimized_plan { - // should have more than one plan - assert!( - e.stringified_plans.len() > 1, - "plans: {:#?}", - e.stringified_plans - ); - // should have at least one optimized plan - let opt = e - .stringified_plans - .iter() - .any(|p| matches!(p.plan_type, PlanType::OptimizedLogicalPlan { .. })); - - assert!(opt, "plans: {:#?}", e.stringified_plans); - } else { - panic!("plan was not an explain: {:?}", plan); - } - } - #[tokio::test] async fn parallel_projection() -> Result<()> { let partition_count = 4; diff --git a/datafusion/tests/sql/explain.rs b/datafusion/tests/sql/explain.rs new file mode 100644 index 0000000000000..00842b5eb8abf --- /dev/null +++ b/datafusion/tests/sql/explain.rs @@ -0,0 +1,60 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use arrow::datatypes::{DataType, Field, Schema}; +use datafusion::{ + logical_plan::{LogicalPlan, LogicalPlanBuilder, PlanType}, + prelude::ExecutionContext, +}; + +#[test] +fn optimize_explain() { + let schema = Schema::new(vec![Field::new("id", DataType::Int32, false)]); + + let plan = LogicalPlanBuilder::scan_empty(Some("employee"), &schema, None) + .unwrap() + .explain(true, false) + .unwrap() + .build() + .unwrap(); + + if let LogicalPlan::Explain(e) = &plan { + assert_eq!(e.stringified_plans.len(), 1); + } else { + panic!("plan was not an explain: {:?}", plan); + } + + // now optimize the plan and expect to see more plans + let optimized_plan = ExecutionContext::new().optimize(&plan).unwrap(); + if let LogicalPlan::Explain(e) = &optimized_plan { + // should have more than one plan + assert!( + e.stringified_plans.len() > 1, + "plans: {:#?}", + e.stringified_plans + ); + // should have at least one optimized plan + let opt = e + .stringified_plans + .iter() + .any(|p| matches!(p.plan_type, PlanType::OptimizedLogicalPlan { .. })); + + assert!(opt, "plans: {:#?}", e.stringified_plans); + } else { + panic!("plan was not an explain: {:?}", plan); + } +} diff --git a/datafusion/tests/sql/mod.rs b/datafusion/tests/sql/mod.rs index ea6829969462c..95623d45e467d 100644 --- a/datafusion/tests/sql/mod.rs +++ b/datafusion/tests/sql/mod.rs @@ -96,6 +96,7 @@ pub mod udf; pub mod union; pub mod window; +mod explain; pub mod information_schema; #[cfg_attr(not(feature = "unicode_expressions"), ignore)] pub mod unicode;