Skip to content

Commit

Permalink
Only recompute schema in TypeCoercion when necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
alamb committed May 3, 2024
1 parent 9b73f66 commit 8f45ee2
Show file tree
Hide file tree
Showing 5 changed files with 712 additions and 515 deletions.
2 changes: 1 addition & 1 deletion datafusion/expr/src/expr_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ fn field_for_index(

/// cast subquery in InSubquery/ScalarSubquery to a given type.
pub fn cast_subquery(subquery: Subquery, cast_to_type: &DataType) -> Result<Subquery> {
if subquery.subquery.schema().field(0).data_type() == cast_to_type {
if subquery.data_type() == cast_to_type {
return Ok(subquery);
}

Expand Down
5 changes: 5 additions & 0 deletions datafusion/expr/src/logical_plan/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2685,6 +2685,11 @@ impl Subquery {
outer_ref_columns: self.outer_ref_columns.clone(),
}
}

/// Returns the type of the first column of the subquery
pub fn data_type(&self) -> &DataType {
self.subquery.schema().fields()[0].data_type()
}
}

impl Debug for Subquery {
Expand Down
13 changes: 12 additions & 1 deletion datafusion/optimizer/src/analyzer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use log::debug;

use datafusion_common::config::ConfigOptions;
use datafusion_common::instant::Instant;
use datafusion_common::tree_node::{TreeNode, TreeNodeRecursion};
use datafusion_common::tree_node::{Transformed, TreeNode, TreeNodeRecursion};
use datafusion_common::{DataFusionError, Result};
use datafusion_expr::expr::Exists;
use datafusion_expr::expr::InSubquery;
Expand Down Expand Up @@ -62,6 +62,17 @@ pub trait AnalyzerRule {
/// Rewrite `plan`
fn analyze(&self, plan: LogicalPlan, config: &ConfigOptions) -> Result<LogicalPlan>;

/// Rewrite a plan indicating if the plan was modified
/// The default implementation calls `analyze`
fn rewrite(
&self,
plan: LogicalPlan,
config: &ConfigOptions,
) -> Result<Transformed<LogicalPlan>> {
let new_plan = self.analyze(plan, config)?;
Ok(Transformed::yes(new_plan))
}

/// A human readable name for this analyzer rule
fn name(&self) -> &str;
}
Expand Down
Loading

0 comments on commit 8f45ee2

Please sign in to comment.