Apache DataFusion SQL Query Engine
ARROW-12437: [Rust] [Ballista] Create DataFusion context without repartition
Andy Grove92f462ec18f2
diff --git a/rust/ballista/rust/client/src/context.rs b/rust/ballista/rust/client/src/context.rsindex 0556c2948d..400f6b6183 100644--- a/rust/ballista/rust/client/src/context.rs+++ b/rust/ballista/rust/client/src/context.rs@@ -33,11 +33,11 @@ use ballista_core::{datasource::DFTableAdapter,error::{BallistaError, Result},memory_stream::MemoryStream,+ utils::create_datafusion_context,};use arrow::datatypes::Schema;use datafusion::catalog::TableReference;-use datafusion::execution::context::ExecutionContext;use datafusion::logical_plan::{DFSchema, Expr, LogicalPlan, Partitioning};use datafusion::physical_plan::csv::CsvReadOptions;use datafusion::{dataframe::DataFrame, physical_plan::RecordBatchStream};@@ -94,7 +94,7 @@ impl BallistaContext {let path = fs::canonicalize(&path)?;// use local DataFusion context for now but later this might call the scheduler- let mut ctx = ExecutionContext::new();+ let mut ctx = create_datafusion_context();let df = ctx.read_parquet(path.to_str().unwrap())?;Ok(BallistaDataFrame::from(self.state.clone(), df))}@@ -111,7 +111,7 @@ impl BallistaContext {let path = fs::canonicalize(&path)?;// use local DataFusion context for now but later this might call the scheduler- let mut ctx = ExecutionContext::new();+ let mut ctx = create_datafusion_context();let df = ctx.read_csv(path.to_str().unwrap(), options)?;Ok(BallistaDataFrame::from(self.state.clone(), df))}@@ -143,7 +143,7 @@ impl BallistaContext {/// Create a DataFrame from a SQL statementpub fn sql(&self, sql: &str) -> Result<BallistaDataFrame> {// use local DataFusion context for now but later this might call the scheduler- let mut ctx = ExecutionContext::new();+ let mut ctx = create_datafusion_context();// register tableslet state = self.state.lock().unwrap();for (name, plan) in &state.tables {diff --git a/rust/ballista/rust/core/src/utils.rs b/rust/ballista/rust/core/src/utils.rsindex d1c239a585..ee9c9557e7 100644--- a/rust/ballista/rust/core/src/utils.rs+++ b/rust/ballista/rust/core/src/utils.rs@@ -33,7 +33,11 @@ use arrow::datatypes::{DataType, Field};use arrow::ipc::reader::FileReader;use arrow::ipc::writer::FileWriter;use arrow::record_batch::RecordBatch;+use datafusion::execution::context::{ExecutionConfig, ExecutionContext};use datafusion::logical_plan::Operator;+use datafusion::physical_optimizer::coalesce_batches::CoalesceBatches;+use datafusion::physical_optimizer::merge_exec::AddMergeExec;+use datafusion::physical_optimizer::optimizer::PhysicalOptimizerRule;use datafusion::physical_plan::coalesce_batches::CoalesceBatchesExec;use datafusion::physical_plan::csv::CsvExec;use datafusion::physical_plan::expressions::{BinaryExpr, Column, Literal};@@ -307,3 +311,17 @@ fn build_exec_plan_diagram(}Ok(node_id)}++/// Create a DataFusion context that is compatible with Ballista+pub fn create_datafusion_context() -> ExecutionContext {+ // remove Repartition rule because that isn't supported yet+ let rules: Vec<Arc<dyn PhysicalOptimizerRule + Send + Sync>> = vec![+ Arc::new(CoalesceBatches::new()),+ Arc::new(AddMergeExec::new()),+ ];+ let config = ExecutionConfig::new()+ .with_concurrency(1)+ .with_repartition_joins(false)+ .with_physical_optimizer_rules(rules);+ ExecutionContext::with_config(config)+}diff --git a/rust/ballista/rust/scheduler/src/lib.rs b/rust/ballista/rust/scheduler/src/lib.rsindex 54733e32e5..de49bc01f3 100644--- a/rust/ballista/rust/scheduler/src/lib.rs+++ b/rust/ballista/rust/scheduler/src/lib.rs@@ -60,12 +60,12 @@ impl parse_arg::ParseArgFromStr for ConfigBackend {use crate::planner::DistributedPlanner;-use datafusion::execution::context::ExecutionContext;use log::{debug, error, info, warn};use rand::{distributions::Alphanumeric, thread_rng, Rng};use tonic::{Request, Response};use self::state::{ConfigBackendClient, SchedulerState};+use ballista_core::utils::create_datafusion_context;use datafusion::physical_plan::parquet::ParquetExec;use std::time::{Instant, SystemTime, UNIX_EPOCH};@@ -254,7 +254,7 @@ impl SchedulerGrpc for SchedulerServer {Query::Sql(sql) => {//TODO we can't just create a new context because we need a context that has// tables registered from previous SQL statements that have been executed- let mut ctx = ExecutionContext::new();+ let mut ctx = create_datafusion_context();let df = ctx.sql(&sql).map_err(|e| {let msg = format!("Error parsing SQL: {}", e);error!("{}", msg);@@ -303,7 +303,7 @@ impl SchedulerGrpc for SchedulerServer {let job_id_spawn = job_id.clone();tokio::spawn(async move {// create physical plan using DataFusion- let datafusion_ctx = ExecutionContext::new();+ let datafusion_ctx = create_datafusion_context();macro_rules! fail_job {($code :expr) => {{match $code {