Apache DataFusion SQL Query Engine
ARROW-9815: [Rust][DataFusion] Remove the use of Arc/Mutex to protect plan time structures
alamb2b047efa9c91
diff --git a/rust/datafusion/src/datasource/datasource.rs b/rust/datafusion/src/datasource/datasource.rsindex 044a8ab429..5d5b419bba 100644--- a/rust/datafusion/src/datasource/datasource.rs+++ b/rust/datafusion/src/datasource/datasource.rs@@ -28,8 +28,7 @@ pub trait TableProvider {/// Get a reference to the schema for this tablefn schema(&self) -> SchemaRef;- /// Perform a scan of a table and return a sequence of iterators over the data (one- /// iterator per partition)+ /// Create an ExecutionPlan that will scan the table.fn scan(&self,projection: &Option<Vec<usize>>,diff --git a/rust/datafusion/src/execution/context.rs b/rust/datafusion/src/execution/context.rsindex 83be3d4692..03c03e7ade 100644--- a/rust/datafusion/src/execution/context.rs+++ b/rust/datafusion/src/execution/context.rs@@ -21,8 +21,10 @@ use std::collections::{HashMap, HashSet};use std::fs;use std::path::Path;use std::string::String;-use std::sync::{Arc, Mutex};-use std::thread::{self, JoinHandle};+use std::{+ sync::Arc,+ thread::{self, JoinHandle},+};use arrow::csv;use arrow::datatypes::*;@@ -93,7 +95,7 @@ use crate::sql::{/// ```pub struct ExecutionContext {/// Internal state for the context- pub state: Arc<Mutex<ExecutionContextState>>,+ pub state: ExecutionContextState,}impl ExecutionContext {@@ -105,27 +107,23 @@ impl ExecutionContext {/// Create a new execution context using the provided configurationpub fn with_config(config: ExecutionConfig) -> Self {let ctx = Self {- state: Arc::new(Mutex::new(ExecutionContextState {+ state: ExecutionContextState {datasources: HashMap::new(),scalar_functions: HashMap::new(),config,- })),+ },};ctx}/// Create a context from existing context state- pub(crate) fn from(state: Arc<Mutex<ExecutionContextState>>) -> Self {+ pub(crate) fn from(state: ExecutionContextState) -> Self {Self { state }}/// Get the configuration of this execution context- pub fn config(&self) -> ExecutionConfig {- self.state- .lock()- .expect("failed to lock mutex")- .config- .clone()+ pub fn config(&self) -> &ExecutionConfig {+ &self.state.config}/// Execute a SQL query and produce a Relation (a schema-aware iterator over a series@@ -178,15 +176,15 @@ impl ExecutionContext {}// create a query planner- let state = self.state.lock().expect("failed to lock mutex");- let query_planner = SqlToRel::new(&*state);+ let query_planner = SqlToRel::new(&self.state);Ok(query_planner.statement_to_plan(&statements[0])?)}/// Register a scalar UDFpub fn register_udf(&mut self, f: ScalarFunction) {- let mut state = self.state.lock().expect("failed to lock mutex");- state.scalar_functions.insert(f.name.clone(), Arc::new(f));+ self.state+ .scalar_functions+ .insert(f.name.clone(), Arc::new(f));}/// Creates a DataFrame for reading a CSV data source.@@ -251,26 +249,17 @@ impl ExecutionContext {/// Register a table using a custom TableProvider so that it can be referenced from SQL/// statements executed against this context.- pub fn register_table(- &mut self,- name: &str,- provider: Box<dyn TableProvider + Send + Sync>,- ) {- let mut ctx_state = self.state.lock().expect("failed to lock mutex");- ctx_state.datasources.insert(name.to_string(), provider);+ pub fn register_table(&mut self, name: &str, provider: Box<dyn TableProvider>) {+ self.state+ .datasources+ .insert(name.to_string(), provider.into());}/// Retrieves a DataFrame representing a table previously registered by calling the/// register_table function. An Err result will be returned if no table has been/// registered with the provided name.pub fn table(&mut self, table_name: &str) -> Result<Arc<dyn DataFrame>> {- match self- .state- .lock()- .expect("failed to lock mutex")- .datasources- .get(table_name)- {+ match self.state.datasources.get(table_name) {Some(provider) => {let schema = provider.schema().as_ref().clone();let table_scan = LogicalPlan::TableScan {@@ -294,13 +283,7 @@ impl ExecutionContext {/// The set of available tables. Use `table` to get a specific table.pub fn tables(&self) -> HashSet<String> {- self.state- .lock()- .expect("failed to lock mutex")- .datasources- .keys()- .cloned()- .collect()+ self.state.datasources.keys().cloned().collect()}/// Optimize the logical plan by applying optimizer rules@@ -316,12 +299,12 @@ impl ExecutionContext {&self,logical_plan: &LogicalPlan,) -> Result<Arc<dyn ExecutionPlan>> {- let planner: Arc<dyn PhysicalPlanner> = match self.config().physical_planner {- Some(planner) => planner,- None => Arc::new(DefaultPhysicalPlanner::default()),- };- let ctx_state = self.state.lock().expect("Failed to aquire lock");- planner.create_physical_plan(logical_plan, &ctx_state)+ if let Some(planner) = &self.config().physical_planner {+ planner.create_physical_plan(logical_plan, &self.state)+ } else {+ let planner = DefaultPhysicalPlanner::default();+ planner.create_physical_plan(logical_plan, &self.state)+ }}/// Execute a physical plan and collect the results in memory@@ -334,14 +317,7 @@ impl ExecutionContext {}_ => {// merge into a single partition- let plan = MergeExec::new(- plan.clone(),- self.state- .lock()- .expect("failed to lock mutex")- .config- .concurrency,- );+ let plan = MergeExec::new(plan.clone(), self.state.config.concurrency);// MergeExec must produce a single partitionassert_eq!(1, plan.output_partitioning().partition_count());common::collect(plan.execute(0)?)@@ -395,11 +371,7 @@ impl ExecutionContext {impl ScalarFunctionRegistry for ExecutionContext {fn lookup(&self, name: &str) -> Option<Arc<ScalarFunction>> {- self.state- .lock()- .expect("failed to lock mutex")- .scalar_functions- .lookup(name)+ self.state.scalar_functions.lookup(name)}}@@ -451,9 +423,10 @@ impl ExecutionConfig {}/// Execution context for registering data sources and executing queries+#[derive(Clone)]pub struct ExecutionContextState {/// Data sources that are registered with the context- pub datasources: HashMap<String, Box<dyn TableProvider + Send + Sync>>,+ pub datasources: HashMap<String, Arc<dyn TableProvider>>,/// Scalar functions that are registered with the contextpub scalar_functions: HashMap<String, Arc<ScalarFunction>>,/// Context configuration@@ -578,14 +551,7 @@ mod tests {let tmp_dir = TempDir::new("execute")?;let ctx = create_ctx(&tmp_dir, 1)?;- let schema = ctx- .state- .lock()- .expect("failed to lock mutex")- .datasources- .get("test")- .unwrap()- .schema();+ let schema = ctx.state.datasources.get("test").unwrap().schema();assert_eq!(schema.field_with_name("c1")?.is_nullable(), false);let plan = LogicalPlanBuilder::scan("default", "test", schema.as_ref(), None)?diff --git a/rust/datafusion/src/execution/dataframe_impl.rs b/rust/datafusion/src/execution/dataframe_impl.rsindex edaf7ae0f9..b41c567b9a 100644--- a/rust/datafusion/src/execution/dataframe_impl.rs+++ b/rust/datafusion/src/execution/dataframe_impl.rs@@ -17,7 +17,7 @@//! Implementation of DataFrame API-use std::sync::{Arc, Mutex};+use std::sync::Arc;use crate::arrow::record_batch::RecordBatch;use crate::dataframe::*;@@ -28,13 +28,13 @@ use arrow::datatypes::Schema;/// Implementation of DataFrame APIpub struct DataFrameImpl {- ctx_state: Arc<Mutex<ExecutionContextState>>,+ ctx_state: ExecutionContextState,plan: LogicalPlan,}impl DataFrameImpl {/// Create a new Table based on an existing logical plan- pub fn new(ctx_state: Arc<Mutex<ExecutionContextState>>, plan: &LogicalPlan) -> Self {+ pub fn new(ctx_state: ExecutionContextState, plan: &LogicalPlan) -> Self {Self {ctx_state,plan: plan.clone(),diff --git a/rust/datafusion/src/execution/physical_plan/planner.rs b/rust/datafusion/src/execution/physical_plan/planner.rsindex 1c24cd3f30..84806ea074 100644--- a/rust/datafusion/src/execution/physical_plan/planner.rs+++ b/rust/datafusion/src/execution/physical_plan/planner.rs@@ -345,23 +345,19 @@ impl DefaultPhysicalPlanner {}Expr::Literal(value) => Ok(Arc::new(Literal::new(value.clone()))),Expr::BinaryExpr { left, op, right } => {- let lhs =- self.create_physical_expr(left, input_schema, ctx_state.clone())?;- let rhs =- self.create_physical_expr(right, input_schema, ctx_state.clone())?;+ let lhs = self.create_physical_expr(left, input_schema, ctx_state)?;+ let rhs = self.create_physical_expr(right, input_schema, ctx_state)?;binary(lhs, op.clone(), rhs, input_schema)}Expr::Cast { expr, data_type } => expressions::cast(- self.create_physical_expr(expr, input_schema, ctx_state.clone())?,+ self.create_physical_expr(expr, input_schema, ctx_state)?,input_schema,data_type.clone(),),Expr::ScalarFunction { fun, args } => {let physical_args = args.iter()- .map(|e| {- self.create_physical_expr(e, input_schema, ctx_state.clone())- })+ .map(|e| self.create_physical_expr(e, input_schema, ctx_state)).collect::<Result<Vec<_>>>()?;functions::create_physical_expr(fun, &physical_args, input_schema)}@@ -376,7 +372,7 @@ impl DefaultPhysicalPlanner {physical_args.push(self.create_physical_expr(e,input_schema,- ctx_state.clone(),+ ctx_state,)?);}Ok(Arc::new(ScalarFunctionExpr::new(