Skip to content

Apache DataFusion SQL Query Engine

ARROW-9899: [Rust] [DataFusion] Switch from Box<Schema> --> SchemaRef (Arc<Schema>) to be consistent with the rest of Arrow

alamb2f8ef93fc155
diff --git a/rust/datafusion/src/execution/context.rs b/rust/datafusion/src/execution/context.rs
index a2d740a089..2933d1a486 100644
--- a/rust/datafusion/src/execution/context.rs
+++ b/rust/datafusion/src/execution/context.rs
@@ -197,11 +197,11 @@ impl ExecutionContext {
let table_scan = LogicalPlan::CsvScan {
path: filename.to_string(),
- schema: Box::new(csv.schema().as_ref().to_owned()),
+ schema: csv.schema().clone(),
has_header: options.has_header,
delimiter: Some(options.delimiter),
projection: None,
- projected_schema: Box::new(csv.schema().as_ref().to_owned()),
+ projected_schema: csv.schema().clone(),
};
Ok(Arc::new(DataFrameImpl::new(
@@ -216,9 +216,9 @@ impl ExecutionContext {
let table_scan = LogicalPlan::ParquetScan {
path: filename.to_string(),
- schema: Box::new(parquet.schema().as_ref().to_owned()),
+ schema: parquet.schema().clone(),
projection: None,
- projected_schema: Box::new(parquet.schema().as_ref().to_owned()),
+ projected_schema: parquet.schema().clone(),
};
Ok(Arc::new(DataFrameImpl::new(
@@ -265,12 +265,12 @@ impl ExecutionContext {
pub fn table(&mut self, table_name: &str) -> Result<Arc<dyn DataFrame>> {
match self.state.datasources.get(table_name) {
Some(provider) => {
- let schema = provider.schema().as_ref().clone();
+ let schema = provider.schema().clone();
let table_scan = LogicalPlan::TableScan {
schema_name: "".to_string(),
table_name: table_name.to_string(),
- table_schema: Box::new(schema.to_owned()),
- projected_schema: Box::new(schema),
+ table_schema: schema.clone(),
+ projected_schema: schema,
projection: None,
};
Ok(Arc::new(DataFrameImpl::new(
@@ -578,18 +578,19 @@ mod tests {
Field::new("b", DataType::Int32, false),
Field::new("c", DataType::Int32, false),
]);
+ let schema = SchemaRef::new(schema);
let plan = LogicalPlanBuilder::from(&LogicalPlan::InMemoryScan {
data: vec![vec![RecordBatch::try_new(
- Arc::new(schema.clone()),
+ schema.clone(),
vec![
Arc::new(Int32Array::from(vec![1, 10, 10, 100])),
Arc::new(Int32Array::from(vec![2, 12, 12, 120])),
Arc::new(Int32Array::from(vec![3, 12, 12, 120])),
],
)?]],
- schema: Box::new(schema.clone()),
+ schema: schema.clone(),
projection: None,
- projected_schema: Box::new(schema.clone()),
+ projected_schema: schema.clone(),
})
.project(vec![col("b")])?
.build()?;
diff --git a/rust/datafusion/src/logical_plan/mod.rs b/rust/datafusion/src/logical_plan/mod.rs
index 7a060f286f..caa0e6d3c8 100644
--- a/rust/datafusion/src/logical_plan/mod.rs
+++ b/rust/datafusion/src/logical_plan/mod.rs
@@ -23,7 +23,7 @@
use std::{fmt, sync::Arc};
-use arrow::datatypes::{DataType, Field, Schema};
+use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
use crate::datasource::csv::{CsvFile, CsvReadOptions};
use crate::datasource::parquet::ParquetTable;
@@ -775,7 +775,7 @@ pub enum LogicalPlan {
/// The incoming logical plan
input: Arc<LogicalPlan>,
/// The schema description of the output
- schema: Box<Schema>,
+ schema: SchemaRef,
},
/// Filters rows from its input that do not match an
/// expression (essentially a WHERE clause with a predicate
@@ -801,7 +801,7 @@ pub enum LogicalPlan {
/// Aggregate expressions
aggr_expr: Vec<Expr>,
/// The schema description of the aggregate output
- schema: Box<Schema>,
+ schema: SchemaRef,
},
/// Sorts its input according to a list of sort expressions.
Sort {
@@ -818,40 +818,40 @@ pub enum LogicalPlan {
/// The name of the table
table_name: String,
/// The schema of the CSV file(s)
- table_schema: Box<Schema>,
+ table_schema: SchemaRef,
/// Optional column indices to use as a projection
projection: Option<Vec<usize>>,
/// The schema description of the output
- projected_schema: Box<Schema>,
+ projected_schema: SchemaRef,
},
/// Produces rows that come from a `Vec` of in memory `RecordBatch`es
InMemoryScan {
/// Record batch partitions
data: Vec<Vec<RecordBatch>>,
/// The schema of the record batches
- schema: Box<Schema>,
+ schema: SchemaRef,
/// Optional column indices to use as a projection
projection: Option<Vec<usize>>,
/// The schema description of the output
- projected_schema: Box<Schema>,
+ projected_schema: SchemaRef,
},
/// Produces rows by scanning Parquet file(s)
ParquetScan {
/// The path to the files
path: String,
/// The schema of the Parquet file(s)
- schema: Box<Schema>,
+ schema: SchemaRef,
/// Optional column indices to use as a projection
projection: Option<Vec<usize>>,
/// The schema description of the output
- projected_schema: Box<Schema>,
+ projected_schema: SchemaRef,
},
/// Produces rows by scanning a CSV file(s)
CsvScan {
/// The path to the files
path: String,
/// The underlying table schema
- schema: Box<Schema>,
+ schema: SchemaRef,
/// Whether the CSV file(s) have a header containing column names
has_header: bool,
/// An optional column delimiter. Defaults to `b','`
@@ -859,12 +859,12 @@ pub enum LogicalPlan {
/// Optional column indices to use as a projection
projection: Option<Vec<usize>>,
/// The schema description of the output
- projected_schema: Box<Schema>,
+ projected_schema: SchemaRef,
},
/// Produces no rows: An empty relation with an empty schema
EmptyRelation {
/// The schema description of the output
- schema: Box<Schema>,
+ schema: SchemaRef,
},
/// Produces the first `n` tuples from its input and discards the rest.
Limit {
@@ -876,7 +876,7 @@ pub enum LogicalPlan {
/// Creates an external table.
CreateExternalTable {
/// The table schema
- schema: Box<Schema>,
+ schema: SchemaRef,
/// The table name
name: String,
/// The physical location
@@ -896,13 +896,13 @@ pub enum LogicalPlan {
/// Represent the various stages plans have gone through
stringified_plans: Vec<StringifiedPlan>,
/// The output schema of the explain (2 columns of text)
- schema: Box<Schema>,
+ schema: SchemaRef,
},
}
impl LogicalPlan {
/// Get a reference to the logical plan's schema
- pub fn schema(&self) -> &Box<Schema> {
+ pub fn schema(&self) -> &SchemaRef {
match self {
LogicalPlan::EmptyRelation { schema } => &schema,
LogicalPlan::InMemoryScan {
@@ -928,8 +928,8 @@ impl LogicalPlan {
}
/// Returns the (fixed) output schema for explain plans
- pub fn explain_schema() -> Box<Schema> {
- Box::new(Schema::new(vec![
+ pub fn explain_schema() -> SchemaRef {
+ SchemaRef::new(Schema::new(vec![
Field::new("plan_type", DataType::Utf8, false),
Field::new("plan", DataType::Utf8, false),
]))
@@ -1103,7 +1103,7 @@ impl LogicalPlanBuilder {
/// Create an empty relation
pub fn empty() -> Self {
Self::from(&LogicalPlan::EmptyRelation {
- schema: Box::new(Schema::empty()),
+ schema: SchemaRef::new(Schema::empty()),
})
}
@@ -1123,7 +1123,7 @@ impl LogicalPlanBuilder {
.to_owned(),
};
- let projected_schema = Box::new(
+ let projected_schema = SchemaRef::new(
projection
.clone()
.map(|p| {
@@ -1135,8 +1135,8 @@ impl LogicalPlanBuilder {
Ok(Self::from(&LogicalPlan::CsvScan {
path: path.to_owned(),
- schema: Box::new(schema),
- has_header: has_header,
+ schema: SchemaRef::new(schema),
+ has_header,
delimiter: Some(delimiter),
projection,
projected_schema,
@@ -1146,17 +1146,19 @@ impl LogicalPlanBuilder {
/// Scan a Parquet data source
pub fn scan_parquet(path: &str, projection: Option<Vec<usize>>) -> Result<Self> {
let p = ParquetTable::try_new(path)?;
- let schema = p.schema().as_ref().to_owned();
+ let schema = p.schema().clone();
+
let projected_schema = projection
.clone()
.map(|p| Schema::new(p.iter().map(|i| schema.field(*i).clone()).collect()));
+ let projected_schema =
+ projected_schema.map_or(schema.clone(), |s| SchemaRef::new(s));
+
Ok(Self::from(&LogicalPlan::ParquetScan {
path: path.to_owned(),
- schema: Box::new(schema.clone()),
+ schema,
projection,
- projected_schema: Box::new(
- projected_schema.or(Some(schema.clone())).unwrap(),
- ),
+ projected_schema,
}))
}
@@ -1167,16 +1169,18 @@ impl LogicalPlanBuilder {
table_schema: &Schema,
projection: Option<Vec<usize>>,
) -> Result<Self> {
+ let table_schema = SchemaRef::new(table_schema.clone());
let projected_schema = projection.clone().map(|p| {
Schema::new(p.iter().map(|i| table_schema.field(*i).clone()).collect())
});
+ let projected_schema =
+ projected_schema.map_or(table_schema.clone(), |s| SchemaRef::new(s));
+
Ok(Self::from(&LogicalPlan::TableScan {
schema_name: schema_name.to_owned(),
table_name: table_name.to_owned(),
- table_schema: Box::new(table_schema.clone()),
- projected_schema: Box::new(
- projected_schema.or(Some(table_schema.clone())).unwrap(),
- ),
+ table_schema,
+ projected_schema,
projection,
}))
}
@@ -1204,7 +1208,7 @@ impl LogicalPlanBuilder {
Ok(Self::from(&LogicalPlan::Projection {
expr: projected_expr,
input: Arc::new(self.plan.clone()),
- schema: Box::new(schema),
+ schema: SchemaRef::new(schema),
}))
}
@@ -1243,7 +1247,7 @@ impl LogicalPlanBuilder {
input: Arc::new(self.plan.clone()),
group_expr,
aggr_expr,
- schema: Box::new(aggr_schema),
+ schema: SchemaRef::new(aggr_schema),
}))
}
diff --git a/rust/datafusion/src/optimizer/projection_push_down.rs b/rust/datafusion/src/optimizer/projection_push_down.rs
index 7a6485676b..71034c0bac 100644
--- a/rust/datafusion/src/optimizer/projection_push_down.rs
+++ b/rust/datafusion/src/optimizer/projection_push_down.rs
@@ -22,7 +22,7 @@ use crate::error::{ExecutionError, Result};
use crate::logical_plan::LogicalPlan;
use crate::optimizer::optimizer::OptimizerRule;
use crate::optimizer::utils;
-use arrow::datatypes::{Field, Schema};
+use arrow::datatypes::{Field, Schema, SchemaRef};
use arrow::error::Result as ArrowResult;
use std::{collections::HashSet, sync::Arc};
use utils::optimize_explain;
@@ -60,7 +60,7 @@ fn get_projected_schema(
projection: &Option<Vec<usize>>,
required_columns: &HashSet<String>,
has_projection: bool,
-) -> Result<(Vec<usize>, Schema)> {
+) -> Result<(Vec<usize>, SchemaRef)> {
if projection.is_some() {
return Err(ExecutionError::General(
"Cannot run projection push-down rule more than once".to_string(),
@@ -103,7 +103,7 @@ fn get_projected_schema(
projected_fields.push(schema.fields()[*i].clone());
}
- Ok((projection, Schema::new(projected_fields)))
+ Ok((projection, SchemaRef::new(Schema::new(projected_fields))))
}
/// Recursively transverses the logical plan removing expressions and that are not needed.
@@ -154,7 +154,7 @@ fn optimize_plan(
Ok(LogicalPlan::Projection {
expr: new_expr,
input: Arc::new(new_input),
- schema: Box::new(Schema::new(new_fields)),
+ schema: SchemaRef::new(Schema::new(new_fields)),
})
}
}
@@ -209,7 +209,7 @@ fn optimize_plan(
&new_required_columns,
true,
)?),
- schema: Box::new(new_schema),
+ schema: SchemaRef::new(new_schema),
})
}
// scans:
@@ -234,7 +234,7 @@ fn optimize_plan(
table_name: table_name.to_string(),
table_schema: table_schema.clone(),
projection: Some(projection),
- projected_schema: Box::new(projected_schema),
+ projected_schema: projected_schema,
})
}
LogicalPlan::InMemoryScan {
@@ -253,7 +253,7 @@ fn optimize_plan(
data: data.clone(),
schema: schema.clone(),
projection: Some(projection),
- projected_schema: Box::new(projected_schema),
+ projected_schema: projected_schema,
})
}
LogicalPlan::CsvScan {
@@ -277,7 +277,7 @@ fn optimize_plan(
schema: schema.clone(),
delimiter: *delimiter,
projection: Some(projection),
- projected_schema: Box::new(projected_schema),
+ projected_schema: projected_schema,
})
}
LogicalPlan::ParquetScan {
@@ -297,7 +297,7 @@ fn optimize_plan(
path: path.to_owned(),
schema: schema.clone(),
projection: Some(projection),
- projected_schema: Box::new(projected_schema),
+ projected_schema: projected_schema,
})
}
LogicalPlan::Explain {
diff --git a/rust/datafusion/src/optimizer/utils.rs b/rust/datafusion/src/optimizer/utils.rs
index b00b987fda..1c5f5104e3 100644
--- a/rust/datafusion/src/optimizer/utils.rs
+++ b/rust/datafusion/src/optimizer/utils.rs
@@ -19,7 +19,7 @@
use std::{collections::HashSet, sync::Arc};
-use arrow::datatypes::Schema;
+use arrow::datatypes::{Schema, SchemaRef};
use super::optimizer::OptimizerRule;
use crate::error::{ExecutionError, Result};
@@ -89,7 +89,7 @@ pub fn optimize_explain(
PlanType::OptimizedLogicalPlan { optimizer_name },
format!("{:#?}", plan),
));
- let schema = Box::new(schema.clone());
+ let schema = SchemaRef::new(schema.clone());
Ok(LogicalPlan::Explain {
verbose,
diff --git a/rust/datafusion/src/sql/planner.rs b/rust/datafusion/src/sql/planner.rs
index 9ec2107bbe..849a450b5e 100644
--- a/rust/datafusion/src/sql/planner.rs
+++ b/rust/datafusion/src/sql/planner.rs
@@ -128,7 +128,7 @@ impl<'a, S: SchemaProvider> SqlToRel<'a, S> {
FileType::NdJson => {}
};
- let schema = Box::new(self.build_schema(&columns)?);
+ let schema = SchemaRef::new(self.build_schema(&columns)?);
Ok(LogicalPlan::CreateExternalTable {
schema,