Skip to content

Apache DataFusion SQL Query Engine

minor: extract `merge_schema()` function. (#5203)

jakevin04b13672cf80
diff --git a/datafusion/expr/src/logical_plan/plan.rs b/datafusion/expr/src/logical_plan/plan.rs
index 96fbeb94e6..47d658535f 100644
--- a/datafusion/expr/src/logical_plan/plan.rs
+++ b/datafusion/expr/src/logical_plan/plan.rs
@@ -179,7 +179,6 @@ impl LogicalPlan {
LogicalPlan::TableScan(TableScan {
projected_schema, ..
}) => vec![projected_schema],
- LogicalPlan::Values(Values { schema, .. }) => vec![schema],
LogicalPlan::Window(Window { input, schema, .. })
| LogicalPlan::Projection(Projection { input, schema, .. })
| LogicalPlan::Aggregate(Aggregate { input, schema, .. })
@@ -205,19 +204,16 @@ impl LogicalPlan {
schemas
}
LogicalPlan::Subquery(Subquery { subquery, .. }) => subquery.all_schemas(),
- LogicalPlan::SubqueryAlias(SubqueryAlias { schema, .. }) => {
- vec![schema]
- }
- LogicalPlan::Union(Union { schema, .. }) => {
- vec![schema]
- }
LogicalPlan::Extension(extension) => vec![extension.node.schema()],
LogicalPlan::Explain(Explain { schema, .. })
| LogicalPlan::Analyze(Analyze { schema, .. })
| LogicalPlan::EmptyRelation(EmptyRelation { schema, .. })
| LogicalPlan::CreateExternalTable(CreateExternalTable { schema, .. })
| LogicalPlan::CreateCatalogSchema(CreateCatalogSchema { schema, .. })
- | LogicalPlan::CreateCatalog(CreateCatalog { schema, .. }) => {
+ | LogicalPlan::CreateCatalog(CreateCatalog { schema, .. })
+ | LogicalPlan::Values(Values { schema, .. })
+ | LogicalPlan::SubqueryAlias(SubqueryAlias { schema, .. })
+ | LogicalPlan::Union(Union { schema, .. }) => {
vec![schema]
}
LogicalPlan::Limit(Limit { input, .. })
diff --git a/datafusion/optimizer/src/type_coercion.rs b/datafusion/optimizer/src/type_coercion.rs
index f42b7520f5..82c563765f 100644
--- a/datafusion/optimizer/src/type_coercion.rs
+++ b/datafusion/optimizer/src/type_coercion.rs
@@ -43,7 +43,7 @@ use datafusion_expr::{
};
use datafusion_expr::{ExprSchemable, Signature};
-use crate::utils::rewrite_preserving_name;
+use crate::utils::{merge_schema, rewrite_preserving_name};
use crate::{OptimizerConfig, OptimizerRule};
#[derive(Default)]
@@ -82,13 +82,7 @@ fn optimize_internal(
.collect::<Result<Vec<_>>>()?;
// get schema representing all available input fields. This is used for data type
// resolution only, so order does not matter here
- let mut schema = new_inputs.iter().map(|input| input.schema()).fold(
- DFSchema::empty(),
- |mut lhs, rhs| {
- lhs.merge(rhs);
- lhs
- },
- );
+ let mut schema = merge_schema(new_inputs.iter().collect());
if let LogicalPlan::TableScan(ts) = plan {
let source_schema =
diff --git a/datafusion/optimizer/src/unwrap_cast_in_comparison.rs b/datafusion/optimizer/src/unwrap_cast_in_comparison.rs
index 17065433d5..604f353141 100644
--- a/datafusion/optimizer/src/unwrap_cast_in_comparison.rs
+++ b/datafusion/optimizer/src/unwrap_cast_in_comparison.rs
@@ -19,12 +19,12 @@
//! of expr can be added if needed.
//! This rule can reduce adding the `Expr::Cast` the expr instead of adding the `Expr::Cast` to literal expr.
use crate::optimizer::ApplyOrder;
-use crate::utils::rewrite_preserving_name;
+use crate::utils::{merge_schema, rewrite_preserving_name};
use crate::{OptimizerConfig, OptimizerRule};
use arrow::datatypes::{
DataType, TimeUnit, MAX_DECIMAL_FOR_EACH_PRECISION, MIN_DECIMAL_FOR_EACH_PRECISION,
};
-use datafusion_common::{DFSchema, DFSchemaRef, DataFusionError, Result, ScalarValue};
+use datafusion_common::{DFSchemaRef, DataFusionError, Result, ScalarValue};
use datafusion_expr::expr::{BinaryExpr, Cast, TryCast};
use datafusion_expr::expr_rewriter::{ExprRewriter, RewriteRecursion};
use datafusion_expr::utils::from_plan;
@@ -85,15 +85,7 @@ impl OptimizerRule for UnwrapCastInComparison {
plan: &LogicalPlan,
_config: &dyn OptimizerConfig,
) -> Result<Option<LogicalPlan>> {
- let inputs: Vec<LogicalPlan> = plan.inputs().into_iter().cloned().collect();
-
- let mut schema = inputs.iter().map(|input| input.schema()).fold(
- DFSchema::empty(),
- |mut lhs, rhs| {
- lhs.merge(rhs);
- lhs
- },
- );
+ let mut schema = merge_schema(plan.inputs());
schema.merge(plan.schema());
@@ -107,6 +99,7 @@ impl OptimizerRule for UnwrapCastInComparison {
.map(|expr| rewrite_preserving_name(expr, &mut expr_rewriter))
.collect::<Result<Vec<_>>>()?;
+ let inputs: Vec<LogicalPlan> = plan.inputs().into_iter().cloned().collect();
Ok(Some(from_plan(
plan,
new_exprs.as_slice(),
diff --git a/datafusion/optimizer/src/utils.rs b/datafusion/optimizer/src/utils.rs
index 4b09ab9562..4d9d10d51a 100644
--- a/datafusion/optimizer/src/utils.rs
+++ b/datafusion/optimizer/src/utils.rs
@@ -18,8 +18,8 @@
//! Collection of utility functions that are leveraged by the query optimizer rules
use crate::{OptimizerConfig, OptimizerRule};
-use datafusion_common::Result;
use datafusion_common::{plan_err, Column, DFSchemaRef};
+use datafusion_common::{DFSchema, Result};
use datafusion_expr::expr::{BinaryExpr, Sort};
use datafusion_expr::expr_rewriter::{ExprRewritable, ExprRewriter};
use datafusion_expr::expr_visitor::inspect_expr_pre;
@@ -457,6 +457,17 @@ fn add_alias_if_changed(original_name: String, expr: Expr) -> Result<Expr> {
})
}
+/// merge inputs schema into a single schema.
+pub fn merge_schema(inputs: Vec<&LogicalPlan>) -> DFSchema {
+ inputs
+ .iter()
+ .map(|input| input.schema())
+ .fold(DFSchema::empty(), |mut lhs, rhs| {
+ lhs.merge(rhs);
+ lhs
+ })
+}
+
#[cfg(test)]
mod tests {
use super::*;