jackwener commented on code in PR #5570: URL: https://github.com/apache/arrow-datafusion/pull/5570#discussion_r1134968294
########## datafusion/optimizer/src/analyzer.rs: ########## @@ -0,0 +1,196 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use crate::rewrite::TreeNodeRewritable; +use datafusion_common::config::ConfigOptions; +use datafusion_common::{DataFusionError, Result}; +use datafusion_expr::expr_visitor::inspect_expr_pre; +use datafusion_expr::{Expr, LogicalPlan}; +use log::{debug, trace}; +use std::sync::Arc; +use std::time::Instant; + +/// `AnalyzerRule` transforms the unresolved ['LogicalPlan']s and unresolved ['Expr']s into +/// the resolved form. +pub trait AnalyzerRule { Review Comment: I feel we should add a new dir for analyzer. Because we can pull some analyzer rule into it. ########## datafusion/optimizer/src/analyzer.rs: ########## @@ -0,0 +1,196 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use crate::rewrite::TreeNodeRewritable; +use datafusion_common::config::ConfigOptions; +use datafusion_common::{DataFusionError, Result}; +use datafusion_expr::expr_visitor::inspect_expr_pre; +use datafusion_expr::{Expr, LogicalPlan}; +use log::{debug, trace}; +use std::sync::Arc; +use std::time::Instant; + +/// `AnalyzerRule` transforms the unresolved ['LogicalPlan']s and unresolved ['Expr']s into +/// the resolved form. +pub trait AnalyzerRule { + /// Rewrite `plan` + fn analyze(&self, plan: &LogicalPlan, config: &ConfigOptions) -> Result<LogicalPlan>; + + /// A human readable name for this analyzer rule + fn name(&self) -> &str; +} +/// A rule-based Analyzer. +#[derive(Clone)] +pub struct Analyzer { + /// All rules to apply + pub rules: Vec<Arc<dyn AnalyzerRule + Send + Sync>>, +} + +impl Default for Analyzer { + fn default() -> Self { + Self::new() + } +} + +impl Analyzer { + /// Create a new analyzer using the recommended list of rules + pub fn new() -> Self { + let rules = vec![]; + Self::with_rules(rules) + } + + /// Create a new analyzer with the given rules + pub fn with_rules(rules: Vec<Arc<dyn AnalyzerRule + Send + Sync>>) -> Self { + Self { rules } + } + + /// Analyze the logical plan by applying analyzer rules, and + /// do necessary check and fail the invalid plans + pub fn execute_and_check( + &self, + plan: &LogicalPlan, + config: &ConfigOptions, + ) -> Result<LogicalPlan> +where { + let start_time = Instant::now(); + let mut new_plan = plan.clone(); + + // TODO add common rule executor for Analyzer and Optimizer + for rule in &self.rules { + new_plan = rule.analyze(&new_plan, config)?; + } + check_plan(&new_plan)?; + log_plan("Final analyzed plan", &new_plan); + debug!("Analyzer took {} ms", start_time.elapsed().as_millis()); + Ok(new_plan) + } +} + +/// Log the plan in debug/tracing mode after some part of the optimizer runs +fn log_plan(description: &str, plan: &LogicalPlan) { + debug!("{description}:\n{}\n", plan.display_indent()); + trace!("{description}::\n{}\n", plan.display_indent_schema()); +} + +/// Do necessary check and fail the invalid plan +fn check_plan(plan: &LogicalPlan) -> Result<()> { + plan.for_each_up(&|plan: &LogicalPlan| { + plan.expressions().into_iter().try_for_each(|expr| { + // recursively look for subqueries + inspect_expr_pre(&expr, |expr| match expr { + Expr::Exists { subquery, .. } + | Expr::InSubquery { subquery, .. } + | Expr::ScalarSubquery(subquery) => { + check_subquery_expr(plan, &subquery.subquery, expr) + } + _ => Ok(()), + }) + }) + }) +} + +fn check_subquery_expr( Review Comment: in the future, we also can move type_coercion into analyzer -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
