You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by jo...@apache.org on 2020/11/25 04:47:42 UTC

[arrow] branch master updated: ARROW-10666: [Rust][DataFusion] Support nested SELECT statements.

This is an automated email from the ASF dual-hosted git repository.

jorgecarleitao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/master by this push:
     new c0a6ab9  ARROW-10666: [Rust][DataFusion] Support nested SELECT statements.
c0a6ab9 is described below

commit c0a6ab996cee97e7149504f6b36324e9e30ac108
Author: Daniel Russo <da...@gmail.com>
AuthorDate: Wed Nov 25 05:46:51 2020 +0100

    ARROW-10666: [Rust][DataFusion] Support nested SELECT statements.
    
    [ARROW-10666](https://issues.apache.org/jira/browse/ARROW-10666) This PR enables nested `SELECT` statements. Note that table aliases remain unsupported, and no optimizations are made during the planning stages.
    
    Closes #8727 from drusso/ARROW-10666
    
    Authored-by: Daniel Russo <da...@gmail.com>
    Signed-off-by: Jorge C. Leitao <jo...@gmail.com>
---
 rust/datafusion/README.md          |  2 +-
 rust/datafusion/src/sql/planner.rs | 39 ++++++++++++++++++++++++++++++++++++--
 rust/datafusion/tests/sql.rs       | 28 +++++++++++++++++++++++++++
 3 files changed, 66 insertions(+), 3 deletions(-)

diff --git a/rust/datafusion/README.md b/rust/datafusion/README.md
index 114cc41..69517d6 100644
--- a/rust/datafusion/README.md
+++ b/rust/datafusion/README.md
@@ -67,7 +67,7 @@ DataFusion includes a simple command-line interactive SQL utility. See the [CLI
 - [x] Sorting
 - [ ] Nested types
 - [ ] Lists
-- [ ] Subqueries
+- [x] Subqueries
 - [ ] Joins
 
 ## Data Sources
diff --git a/rust/datafusion/src/sql/planner.rs b/rust/datafusion/src/sql/planner.rs
index 0d27539..002b53c 100644
--- a/rust/datafusion/src/sql/planner.rs
+++ b/rust/datafusion/src/sql/planner.rs
@@ -234,8 +234,9 @@ impl<'a, S: SchemaProvider> SqlToRel<'a, S> {
                     ))),
                 }
             }
-            _ => Err(DataFusionError::NotImplemented(
-                "Subqueries are still not supported".to_string(),
+            TableFactor::Derived { subquery, .. } => self.query_to_plan(subquery),
+            TableFactor::NestedJoin(_) => Err(DataFusionError::NotImplemented(
+                "Nested joins are not supported".to_string(),
             )),
         }
     }
@@ -705,6 +706,40 @@ mod tests {
     }
 
     #[test]
+    fn select_nested() {
+        let sql = "SELECT fn2, last_name
+                   FROM (
+                     SELECT fn1 as fn2, last_name, birth_date
+                     FROM (
+                       SELECT first_name AS fn1, last_name, birth_date, age
+                       FROM person
+                     )
+                   )";
+        let expected = "Projection: #fn2, #last_name\
+                        \n  Projection: #fn1 AS fn2, #last_name, #birth_date\
+                        \n    Projection: #first_name AS fn1, #last_name, #birth_date, #age\
+                        \n      TableScan: person projection=None";
+        quick_test(sql, expected);
+    }
+
+    #[test]
+    fn select_nested_with_filters() {
+        let sql = "SELECT fn1, age
+                   FROM (
+                     SELECT first_name AS fn1, age
+                     FROM person
+                     WHERE age > 20
+                   )
+                   WHERE fn1 = 'X' AND age < 30";
+        let expected = "Projection: #fn1, #age\
+                        \n  Filter: #fn1 Eq Utf8(\"X\") And #age Lt Int64(30)\
+                        \n    Projection: #first_name AS fn1, #age\
+                        \n      Filter: #age Gt Int64(20)\
+                        \n        TableScan: person projection=None";
+        quick_test(sql, expected);
+    }
+
+    #[test]
     fn select_binary_expr() {
         let sql = "SELECT age + salary from person";
         let expected = "Projection: #age Plus #salary\
diff --git a/rust/datafusion/tests/sql.rs b/rust/datafusion/tests/sql.rs
index fc35f4f..bbf8934 100644
--- a/rust/datafusion/tests/sql.rs
+++ b/rust/datafusion/tests/sql.rs
@@ -228,6 +228,34 @@ async fn parquet_list_columns() {
 }
 
 #[tokio::test]
+async fn csv_select_nested() -> Result<()> {
+    let mut ctx = ExecutionContext::new();
+    register_aggregate_csv(&mut ctx)?;
+    let sql = "SELECT o1, o2, c3
+               FROM (
+                 SELECT c1 AS o1, c2 + 1 AS o2, c3
+                 FROM (
+                   SELECT c1, c2, c3, c4
+                   FROM aggregate_test_100
+                   WHERE c1 = 'a' AND c2 >= 4
+                   ORDER BY c2 ASC, c3 ASC
+                 )
+               )";
+    let actual = execute(&mut ctx, sql).await;
+    let expected = vec![
+        vec!["a", "5", "-101"],
+        vec!["a", "5", "-54"],
+        vec!["a", "5", "-38"],
+        vec!["a", "5", "65"],
+        vec!["a", "6", "-101"],
+        vec!["a", "6", "-31"],
+        vec!["a", "6", "36"],
+    ];
+    assert_eq!(expected, actual);
+    Ok(())
+}
+
+#[tokio::test]
 async fn csv_count_star() -> Result<()> {
     let mut ctx = ExecutionContext::new();
     register_aggregate_csv(&mut ctx)?;