alamb commented on a change in pull request #1699:
URL: https://github.com/apache/arrow-datafusion/pull/1699#discussion_r794748183



##########
File path: datafusion/src/execution/dataframe_impl.rs
##########
@@ -62,6 +68,65 @@ impl DataFrameImpl {
     }
 }
 
+#[async_trait]
+impl TableProvider for DataFrameImpl {
+    fn as_any(&self) -> &dyn Any {
+        self
+    }
+
+    fn schema(&self) -> SchemaRef {
+        Arc::new(Schema::new(
+            self.plan
+                .schema()
+                .fields()
+                .iter()
+                .map(|f| f.field().clone())
+                .collect(),
+        ))
+    }

Review comment:
       I think you can use some rust-fu to make this shorter if you want
   
   ```suggestion
       fn schema(&self) -> SchemaRef {
           let schema: Schema = self.plan.schema().as_ref().into();
           Arc::new(schema)
       }
   
   ```

##########
File path: datafusion/src/execution/dataframe_impl.rs
##########
@@ -62,6 +68,65 @@ impl DataFrameImpl {
     }
 }
 
+#[async_trait]
+impl TableProvider for DataFrameImpl {
+    fn as_any(&self) -> &dyn Any {
+        self
+    }
+
+    fn schema(&self) -> SchemaRef {
+        Arc::new(Schema::new(
+            self.plan
+                .schema()
+                .fields()
+                .iter()
+                .map(|f| f.field().clone())
+                .collect(),
+        ))
+    }
+
+    fn table_type(&self) -> TableType {
+        TableType::View
+    }
+
+    async fn scan(
+        &self,
+        projection: &Option<Vec<usize>>,
+        filters: &[Expr],
+        limit: Option<usize>,
+    ) -> Result<Arc<dyn ExecutionPlan>> {
+        let expr = projection
+            .as_ref()
+            // construct projections
+            .map_or_else(
+                || Ok(Arc::new(Self::new(self.ctx_state.clone(), &self.plan)) 
as Arc<_>),
+                |projection| {
+                    let schema = 
TableProvider::schema(self).project(projection)?;
+                    let names = schema
+                        .fields()
+                        .iter()
+                        .map(|field| field.name().as_str())
+                        .collect::<Vec<_>>();
+                    self.select_columns(names.as_slice())

Review comment:
       this is cool

##########
File path: datafusion/src/execution/dataframe_impl.rs
##########
@@ -488,6 +553,17 @@ mod tests {
         Ok(())
     }
 
+    #[tokio::test]
+    async fn register_table() -> Result<()> {
+        let df = test_table().await?.select_columns(&["c1", "c3"])?;
+        let mut ctx = ExecutionContext::new();
+        let df_impl = DataFrameImpl::new(ctx.state.clone(), 
&df.to_logical_plan());
+        ctx.register_table("test_table", Arc::new(df_impl))?;
+        let table = ctx.table("test_table")?;
+        assert_same_plan(&table.to_logical_plan(), &table.to_logical_plan());
+        Ok(())

Review comment:
       I would also recommend at test that runs the plan too (either via `SQL` 
or via `df_impl.collect()`) to exercise the `scan` code.




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