alamb commented on code in PR #3923: URL: https://github.com/apache/arrow-datafusion/pull/3923#discussion_r1002113077
########## datafusion/optimizer/src/inline_table_scan.rs: ########## @@ -0,0 +1,184 @@ +// 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. + +//! Optimizer rule to replace TableScan references +//! such as DataFrames and Views and inlines the LogicalPlan Review Comment: 👍 ########## datafusion/optimizer/src/inline_table_scan.rs: ########## @@ -0,0 +1,184 @@ +// 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. + +//! Optimizer rule to replace TableScan references +//! such as DataFrames and Views and inlines the LogicalPlan +//! to support further optimization +use crate::{OptimizerConfig, OptimizerRule}; +use datafusion_common::Result; +use datafusion_expr::{ + logical_plan::LogicalPlan, utils::from_plan, Expr, LogicalPlanBuilder, TableScan, +}; + +/// Optimization rule that inlines TableScan that provide a [LogicalPlan] +/// ([DataFrame] / [ViewTable]) +#[derive(Default)] +pub struct InlineTableScan; + +impl InlineTableScan { + #[allow(missing_docs)] + pub fn new() -> Self { + Self {} + } +} + +/// Inline +fn inline_table_scan(plan: &LogicalPlan) -> Result<LogicalPlan> { + match plan { + // Match only on scans without filter/projection + // As DataFrames / Views don't have those Review Comment: I don't see why we couldn't also create a projection / limit node as part of this rewrite as well if the table scan had them -- maybe we could file that as a future optimization ########## datafusion/optimizer/src/inline_table_scan.rs: ########## @@ -0,0 +1,184 @@ +// 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. + +//! Optimizer rule to replace TableScan references +//! such as DataFrames and Views and inlines the LogicalPlan +//! to support further optimization +use crate::{OptimizerConfig, OptimizerRule}; +use datafusion_common::Result; +use datafusion_expr::{ + logical_plan::LogicalPlan, utils::from_plan, Expr, LogicalPlanBuilder, TableScan, +}; + +/// Optimization rule that inlines TableScan that provide a [LogicalPlan] +/// ([DataFrame] / [ViewTable]) +#[derive(Default)] +pub struct InlineTableScan; + +impl InlineTableScan { + #[allow(missing_docs)] + pub fn new() -> Self { + Self {} + } +} + +/// Inline +fn inline_table_scan(plan: &LogicalPlan) -> Result<LogicalPlan> { + match plan { + // Match only on scans without filter/projection + // As DataFrames / Views don't have those + LogicalPlan::TableScan(TableScan { + source, + table_name, + filters, + fetch: None, + projected_schema, + projection: None, + }) if filters.is_empty() => { Review Comment: Likewise, if it has filters, we could add a LogicalPlan::Filter here I think ########## datafusion/optimizer/src/optimizer.rs: ########## @@ -148,6 +149,7 @@ impl Optimizer { /// Create a new optimizer using the recommended list of rules pub fn new(config: &OptimizerConfig) -> Self { let mut rules: Vec<Arc<dyn OptimizerRule + Sync + Send>> = vec![ + Arc::new(InlineTableScan::new()), Review Comment: I agree -- 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]
