-
Couldn't load subscription status.
- Fork 1.7k
fix NamedStructField should be rewritten in OperatorToFunction in subquery regression (change ApplyFunctionRewrites to use TreeNode API
#10032
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5f0023b
fix NamedStructField should be rewritten in OperatorToFunction in sub…
alamb a546e69
Use TreeNode rewriter
alamb a8cddad
Merge remote-tracking branch 'apache/main' into alamb/fix_rewrite2
alamb cf4e79c
Merge remote-tracking branch 'apache/main' into alamb/fix_rewrite2
alamb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,11 +19,13 @@ | |
|
|
||
| use super::AnalyzerRule; | ||
| use datafusion_common::config::ConfigOptions; | ||
| use datafusion_common::tree_node::{Transformed, TreeNodeRewriter}; | ||
| use datafusion_common::tree_node::{Transformed, TreeNode}; | ||
| use datafusion_common::{DFSchema, Result}; | ||
| use datafusion_expr::expr_rewriter::{rewrite_preserving_name, FunctionRewrite}; | ||
|
|
||
| use crate::utils::NamePreserver; | ||
| use datafusion_expr::expr_rewriter::FunctionRewrite; | ||
| use datafusion_expr::utils::merge_schema; | ||
| use datafusion_expr::{Expr, LogicalPlan}; | ||
| use datafusion_expr::LogicalPlan; | ||
| use std::sync::Arc; | ||
|
|
||
| /// Analyzer rule that invokes [`FunctionRewrite`]s on expressions | ||
|
|
@@ -37,86 +39,53 @@ impl ApplyFunctionRewrites { | |
| pub fn new(function_rewrites: Vec<Arc<dyn FunctionRewrite + Send + Sync>>) -> Self { | ||
| Self { function_rewrites } | ||
| } | ||
| } | ||
|
|
||
| impl AnalyzerRule for ApplyFunctionRewrites { | ||
| fn name(&self) -> &str { | ||
| "apply_function_rewrites" | ||
| } | ||
|
|
||
| fn analyze(&self, plan: LogicalPlan, options: &ConfigOptions) -> Result<LogicalPlan> { | ||
| self.analyze_internal(&plan, options) | ||
| } | ||
| } | ||
|
|
||
| impl ApplyFunctionRewrites { | ||
| fn analyze_internal( | ||
| /// Rewrite a single plan, and all its expressions using the provided rewriters | ||
| fn rewrite_plan( | ||
| &self, | ||
| plan: &LogicalPlan, | ||
| plan: LogicalPlan, | ||
| options: &ConfigOptions, | ||
| ) -> Result<LogicalPlan> { | ||
| // optimize child plans first | ||
| let new_inputs = plan | ||
| .inputs() | ||
| .iter() | ||
| .map(|p| self.analyze_internal(p, options)) | ||
| .collect::<Result<Vec<_>>>()?; | ||
|
|
||
| ) -> Result<Transformed<LogicalPlan>> { | ||
| // get schema representing all available input fields. This is used for data type | ||
| // resolution only, so order does not matter here | ||
| let mut schema = merge_schema(new_inputs.iter().collect()); | ||
| let mut schema = merge_schema(plan.inputs()); | ||
|
|
||
| if let LogicalPlan::TableScan(ts) = plan { | ||
| if let LogicalPlan::TableScan(ts) = &plan { | ||
| let source_schema = DFSchema::try_from_qualified_schema( | ||
| ts.table_name.clone(), | ||
| &ts.source.schema(), | ||
| )?; | ||
| schema.merge(&source_schema); | ||
| } | ||
|
|
||
| let mut expr_rewrite = OperatorToFunctionRewriter { | ||
| function_rewrites: &self.function_rewrites, | ||
| options, | ||
| schema: &schema, | ||
| }; | ||
| let name_preserver = NamePreserver::new(&plan); | ||
|
|
||
| plan.map_expressions(|expr| { | ||
| let original_name = name_preserver.save(&expr)?; | ||
|
|
||
| let new_expr = plan | ||
| .expressions() | ||
| .into_iter() | ||
| .map(|expr| { | ||
| // ensure names don't change: | ||
| // https://github.com/apache/arrow-datafusion/issues/3555 | ||
| rewrite_preserving_name(expr, &mut expr_rewrite) | ||
| }) | ||
| .collect::<Result<Vec<_>>>()?; | ||
| // recursively transform the expression, applying the rewrites at each step | ||
| let result = expr.transform_up(&|expr| { | ||
| let mut result = Transformed::no(expr); | ||
| for rewriter in self.function_rewrites.iter() { | ||
| result = result.transform_data(|expr| { | ||
| rewriter.rewrite(expr, &schema, options) | ||
| })?; | ||
| } | ||
| Ok(result) | ||
| })?; | ||
|
|
||
| plan.with_new_exprs(new_expr, new_inputs) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this copies the plan + expressions |
||
| result.map_data(|expr| original_name.restore(expr)) | ||
| }) | ||
| } | ||
| } | ||
| struct OperatorToFunctionRewriter<'a> { | ||
| function_rewrites: &'a [Arc<dyn FunctionRewrite + Send + Sync>], | ||
| options: &'a ConfigOptions, | ||
| schema: &'a DFSchema, | ||
| } | ||
|
|
||
| impl<'a> TreeNodeRewriter for OperatorToFunctionRewriter<'a> { | ||
| type Node = Expr; | ||
|
|
||
| fn f_up(&mut self, mut expr: Expr) -> Result<Transformed<Expr>> { | ||
| // apply transforms one by one | ||
| let mut transformed = false; | ||
| for rewriter in self.function_rewrites.iter() { | ||
| let result = rewriter.rewrite(expr, self.schema, self.options)?; | ||
| if result.transformed { | ||
| transformed = true; | ||
| } | ||
| expr = result.data | ||
| } | ||
| impl AnalyzerRule for ApplyFunctionRewrites { | ||
| fn name(&self) -> &str { | ||
| "apply_function_rewrites" | ||
| } | ||
|
|
||
| Ok(if transformed { | ||
| Transformed::yes(expr) | ||
| } else { | ||
| Transformed::no(expr) | ||
| }) | ||
| fn analyze(&self, plan: LogicalPlan, options: &ConfigOptions) -> Result<LogicalPlan> { | ||
| plan.transform_up_with_subqueries(&|plan| self.rewrite_plan(plan, options)) | ||
| .map(|res| res.data) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using the new map_expressions API it is quite straightforward to rewrite these expressions (and it doesn't copy them!)