datafusion_optimizer/analyzer/
function_rewrite.rs1use super::AnalyzerRule;
21use datafusion_common::config::ConfigOptions;
22use datafusion_common::tree_node::{Transformed, TreeNode};
23use datafusion_common::{DFSchema, Result};
24
25use crate::utils::NamePreserver;
26use datafusion_expr::expr_rewriter::FunctionRewrite;
27use datafusion_expr::utils::merge_schema;
28use datafusion_expr::{DmlStatement, LogicalPlan, WriteOp};
29use std::sync::Arc;
30
31#[derive(Default, Debug)]
33pub struct ApplyFunctionRewrites {
34 function_rewrites: Vec<Arc<dyn FunctionRewrite + Send + Sync>>,
36}
37
38impl ApplyFunctionRewrites {
39 pub fn new(function_rewrites: Vec<Arc<dyn FunctionRewrite + Send + Sync>>) -> Self {
40 Self { function_rewrites }
41 }
42
43 fn rewrite_plan(
45 &self,
46 plan: LogicalPlan,
47 options: &ConfigOptions,
48 ) -> Result<Transformed<LogicalPlan>> {
49 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 if let LogicalPlan::Dml(DmlStatement {
65 op: WriteOp::MergeInto(_),
66 table_name,
67 target,
68 ..
69 }) = &plan
70 {
71 let target_schema = DFSchema::try_from_qualified_schema(
72 table_name.clone(),
73 &target.schema(),
74 )?;
75 schema.merge(&target_schema);
76 }
77
78 let name_preserver = NamePreserver::new(&plan);
79
80 plan.map_expressions(|expr| {
81 let original_name = name_preserver.save(&expr);
82
83 let transformed_expr = expr.transform_up(|expr| {
85 let mut result = Transformed::no(expr);
86 for rewriter in self.function_rewrites.iter() {
87 result = result.transform_data(|expr| {
88 rewriter.rewrite(expr, &schema, options)
89 })?;
90 }
91 Ok(result)
92 })?;
93
94 Ok(transformed_expr.update_data(|expr| original_name.restore(expr)))
95 })
96 }
97}
98
99impl AnalyzerRule for ApplyFunctionRewrites {
100 fn name(&self) -> &str {
101 "apply_function_rewrites"
102 }
103
104 fn analyze(&self, plan: LogicalPlan, options: &ConfigOptions) -> Result<LogicalPlan> {
105 plan.transform_up_with_subqueries(|plan| self.rewrite_plan(plan, options))
106 .map(|res| res.data)
107 }
108}