Apache DataFusion SQL Query Engine
feat: support temporary views in DataFrameTableProvider (#18158)
r1b0a650a0d27ca
diff --git a/datafusion/core/src/dataframe/mod.rs b/datafusion/core/src/dataframe/mod.rsindex 3186c5cb82..b164b050da 100644--- a/datafusion/core/src/dataframe/mod.rs+++ b/datafusion/core/src/dataframe/mod.rs@@ -1654,7 +1654,19 @@ impl DataFrame {/// Note: This discards the [`SessionState`] associated with this/// [`DataFrame`] in favour of the one passed to [`TableProvider::scan`]pub fn into_view(self) -> Arc<dyn TableProvider> {- Arc::new(DataFrameTableProvider { plan: self.plan })+ Arc::new(DataFrameTableProvider {+ plan: self.plan,+ table_type: TableType::Temporary,+ })+ }++ /// See [`Self::into_view`]. The returned [`TableProvider`] will+ /// create a transient table.+ pub fn into_temporary_view(self) -> Arc<dyn TableProvider> {+ Arc::new(DataFrameTableProvider {+ plan: self.plan,+ table_type: TableType::Temporary,+ })}/// Return a DataFrame with the explanation of its plan so far.@@ -2524,6 +2536,7 @@ macro_rules! dataframe {#[derive(Debug)]struct DataFrameTableProvider {plan: LogicalPlan,+ table_type: TableType,}#[async_trait]@@ -2549,7 +2562,7 @@ impl TableProvider for DataFrameTableProvider {}fn table_type(&self) -> TableType {- TableType::View+ self.table_type}async fn scan(diff --git a/datafusion/core/tests/dataframe/mod.rs b/datafusion/core/tests/dataframe/mod.rsindex 043f42b18c..e27a341485 100644--- a/datafusion/core/tests/dataframe/mod.rs+++ b/datafusion/core/tests/dataframe/mod.rs@@ -77,7 +77,7 @@ use datafusion_expr::var_provider::{VarProvider, VarType};use datafusion_expr::{cast, col, create_udf, exists, in_subquery, lit, out_ref_col, placeholder,scalar_subquery, when, wildcard, Expr, ExprFunctionExt, ExprSchemable, LogicalPlan,- LogicalPlanBuilder, ScalarFunctionImplementation, SortExpr, WindowFrame,+ LogicalPlanBuilder, ScalarFunctionImplementation, SortExpr, TableType, WindowFrame,WindowFrameBound, WindowFrameUnits, WindowFunctionDefinition,};use datafusion_physical_expr::aggregate::AggregateExprBuilder;@@ -1577,6 +1577,23 @@ async fn register_table() -> Result<()> {Ok(())}+#[tokio::test]+async fn register_temporary_table() -> Result<()> {+ let df = test_table().await?.select_columns(&["c1", "c12"])?;+ let ctx = SessionContext::new();+ let df_impl = DataFrame::new(ctx.state(), df.logical_plan().clone());++ let df_table_provider = df_impl.clone().into_temporary_view();++ // check that we set the correct table_type+ assert_eq!(df_table_provider.table_type(), TableType::Temporary);++ // check that we can register a dataframe as a temporary table+ ctx.register_table("test_table", df_table_provider)?;++ Ok(())+}+/// Compare the formatted string representation of two plans for equalityfn assert_same_plan(plan1: &LogicalPlan, plan2: &LogicalPlan) {assert_eq!(format!("{plan1:?}"), format!("{plan2:?}"));