Rewrite a single plan, and all its expressions using the provided rewriters
(
&self,
plan: LogicalPlan,
options: &ConfigOptions,
)
| 42 | |
| 43 | /// Rewrite a single plan, and all its expressions using the provided rewriters |
| 44 | fn rewrite_plan( |
| 45 | &self, |
| 46 | plan: LogicalPlan, |
| 47 | options: &ConfigOptions, |
| 48 | ) -> Result<Transformed<LogicalPlan>> { |
| 49 | // get schema representing all available input fields. This is used for data type |
| 50 | // resolution only, so order does not matter here |
| 51 | let mut schema = merge_schema(&plan.inputs()); |
| 52 | |
| 53 | if let LogicalPlan::TableScan(ts) = &plan { |
| 54 | let source_schema = DFSchema::try_from_qualified_schema( |
| 55 | ts.table_name.clone(), |
| 56 | &ts.source.schema(), |
| 57 | )?; |
| 58 | schema.merge(&source_schema); |
| 59 | } |
| 60 | |
| 61 | let name_preserver = NamePreserver::new(&plan); |
| 62 | |
| 63 | plan.map_expressions(|expr| { |
| 64 | let original_name = name_preserver.save(&expr); |
| 65 | |
| 66 | // recursively transform the expression, applying the rewrites at each step |
| 67 | let transformed_expr = expr.transform_up(|expr| { |
| 68 | let mut result = Transformed::no(expr); |
| 69 | for rewriter in self.function_rewrites.iter() { |
| 70 | result = result.transform_data(|expr| { |
| 71 | rewriter.rewrite(expr, &schema, options) |
| 72 | })?; |
| 73 | } |
| 74 | Ok(result) |
| 75 | })?; |
| 76 | |
| 77 | Ok(transformed_expr.update_data(|expr| original_name.restore(expr))) |
| 78 | }) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | impl AnalyzerRule for ApplyFunctionRewrites { |
no test coverage detected