isidentical commented on code in PR #3923:
URL: https://github.com/apache/arrow-datafusion/pull/3923#discussion_r1002065650


##########
datafusion/optimizer/src/inline_table_scan.rs:
##########
@@ -0,0 +1,188 @@
+// 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::{
+    col, logical_plan::LogicalPlan, utils::from_plan, 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 {
+        LogicalPlan::TableScan(TableScan {
+            source,
+            table_name,
+            filters,
+            fetch,
+            projected_schema,
+            ..
+        }) => {
+            if let Some(sub_plan) = source.get_logical_plan() {
+                // Recurse into scan
+                let plan = inline_table_scan(sub_plan)?;
+                let mut plan = 
LogicalPlanBuilder::from(plan).project_with_alias(
+                    projected_schema
+                        .fields()
+                        .iter()
+                        .map(|field| col(field.name())),
+                    Some(table_name.clone()),
+                )?;
+                for filter in filters {

Review Comment:
   Question: would it better if we can handle this logic inside 
`get_logical_plan` (pass limit/filter/projections)? If I am not missing 
anything, we do some tiny changes when building the plan for the view (e.g. 
avoiding redundant projections) for example and we might miss doing them here 
if we have two paths now (I guess the following optimizer rules can take care 
of this particular example, but just future proof-ing it). 



-- 
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]

Reply via email to