Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor: extract merge_schema() function. #5203

Merged
merged 1 commit into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions datafusion/expr/src/logical_plan/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,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, .. }) => {
Expand All @@ -201,19 +200,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, .. })
Expand Down
10 changes: 2 additions & 8 deletions datafusion/optimizer/src/type_coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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 =
Expand Down
15 changes: 4 additions & 11 deletions datafusion/optimizer/src/unwrap_cast_in_comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());

Expand All @@ -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(),
Expand Down
13 changes: 12 additions & 1 deletion datafusion/optimizer/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::*;
Expand Down