jorgecarleitao commented on a change in pull request #8662:
URL: https://github.com/apache/arrow/pull/8662#discussion_r523421514



##########
File path: rust/datafusion/tests/sql.rs
##########
@@ -1399,3 +1399,22 @@ async fn query_on_string_dictionary() -> Result<()> {
 
     Ok(())
 }
+
+#[tokio::test]
+async fn query_without_from() -> Result<()> {
+    // Test for SELECT <expression> without FROM.
+    // Should evaluate expressions in project position.
+    let mut ctx = ExecutionContext::new();
+
+    let sql = "SELECT 1";
+    let actual = execute(&mut ctx, sql).await;
+    let expected = vec![vec!["1"]];
+    assert_eq!(expected, actual);
+
+    let sql = "SELECT 1+2, 3/4, cos(0)";
+    let actual = execute(&mut ctx, sql).await;
+    let expected = vec![vec!["3", "0", "1"]];

Review comment:
       💯
   
   I think that this could even be used to simplify many of our tests for 
expressions, as it can be more idiomatic than creating a DataFrame with data 
and running a projection against it.

##########
File path: rust/datafusion/src/physical_plan/projection.rs
##########
@@ -114,14 +117,33 @@ impl ExecutionPlan for ProjectionExec {
     }
 
     async fn execute(&self, partition: usize) -> 
Result<SendableRecordBatchStream> {
+        let input = if 
self.input.as_any().downcast_ref::<EmptyExec>().is_some() {

Review comment:
       I agree with @alamb : this design does not scale as we end up with `if` 
statements scattered around the code.

##########
File path: rust/datafusion/src/physical_plan/projection.rs
##########
@@ -114,14 +117,33 @@ impl ExecutionPlan for ProjectionExec {
     }
 
     async fn execute(&self, partition: usize) -> 
Result<SendableRecordBatchStream> {
+        let input = if 
self.input.as_any().downcast_ref::<EmptyExec>().is_some() {
+            placeholder_stream(self.schema.clone())?
+        } else {
+            self.input.execute(partition).await?
+        };
+
         Ok(Box::pin(ProjectionStream {
             schema: self.schema.clone(),
             expr: self.expr.iter().map(|x| x.0.clone()).collect(),
-            input: self.input.execute(partition).await?,
+            input,
         }))
     }
 }
 
+// Makes a stream only contains one null element
+fn placeholder_stream(schema: Arc<Schema>) -> 
Result<SendableRecordBatchStream> {

Review comment:
       I agree: ingenious 💯 




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

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to