You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/04/26 16:59:34 UTC

[GitHub] [arrow-datafusion] andygrove opened a new pull request, #2344: Add SQL planner support for EXISTS subqueries

andygrove opened a new pull request, #2344:
URL: https://github.com/apache/arrow-datafusion/pull/2344

   # Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Closes https://github.com/apache/arrow-datafusion/issues/2219
   
    # Rationale for this change
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   
   Add SQL query planner support for EXISTS subqueries
   
   # What changes are included in this PR?
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   The SQL query planner has been updated so that it passes information about the outer schema to subqueries, so that correlated subqueries can resolve fields from the outer query.
   
   # Are there any user-facing changes?
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   Yes, SQL queries with EXISTS subqueries will now result in valid logical plan instead of an error.
   
   <!--
   If there are any breaking changes to public APIs, please add the `api change` label.
   -->
   


-- 
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: github-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-datafusion] alamb merged pull request #2344: Add SQL planner support for EXISTS subqueries

Posted by GitBox <gi...@apache.org>.
alamb merged PR #2344:
URL: https://github.com/apache/arrow-datafusion/pull/2344


-- 
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: github-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-datafusion] andygrove commented on a diff in pull request #2344: Add SQL planner support for EXISTS subqueries

Posted by GitBox <gi...@apache.org>.
andygrove commented on code in PR #2344:
URL: https://github.com/apache/arrow-datafusion/pull/2344#discussion_r859206506


##########
datafusion/core/src/sql/planner.rs:
##########
@@ -4182,4 +4264,36 @@ mod tests {
             \n    TableScan: test projection=None";
         quick_test(sql, expected);
     }
+
+    #[test]
+    fn exists_subquery() {
+        let sql = "SELECT id FROM person p WHERE EXISTS \
+            (SELECT first_name FROM person \
+            WHERE last_name = p.last_name \
+            AND state = p.state)";
+
+        let expected = "Projection: #p.id\
+        \n  Filter: EXISTS (Subquery: Projection: #person.first_name\
+        \n  Filter: #person.last_name = #p.last_name AND #person.state = #p.state\
+        \n    TableScan: person projection=None)\
+        \n    SubqueryAlias: p\
+        \n      TableScan: person projection=None";
+        quick_test(sql, expected);
+    }
+
+    #[test]
+    fn exists_subquery_wildcard() {
+        let sql = "SELECT id FROM person p WHERE EXISTS \
+            (SELECT * FROM person \
+            WHERE last_name = p.last_name \
+            AND state = p.state)";
+
+        let expected = "Projection: #p.id\
+        \n  Filter: EXISTS (Subquery: Projection: #person.id, #person.first_name, #person.last_name, #person.age, #person.state, #person.salary, #person.birth_date, #person.😀\
+        \n  Filter: #person.last_name = #p.last_name AND #person.state = #p.state\
+        \n    TableScan: person projection=None)\
+        \n    SubqueryAlias: p\
+        \n      TableScan: person projection=None";

Review Comment:
   The query is hard to parse here and I have pushed a change to improve the formatting in the test. There is one filter in the subquery and one filter in the outer query.
   
   My plan was to implement an optimizer rule to handle re-writing to a semi-join. This rule would be applied regardless of whether the query was created by the SQL planner or via the DataFrame API. Does that make sense?



-- 
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: github-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-datafusion] alamb commented on pull request #2344: Add SQL planner support for EXISTS subqueries

Posted by GitBox <gi...@apache.org>.
alamb commented on PR #2344:
URL: https://github.com/apache/arrow-datafusion/pull/2344#issuecomment-1110217652

   I plan to review this shortly


-- 
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: github-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-datafusion] andygrove commented on a diff in pull request #2344: Add SQL planner support for EXISTS subqueries

Posted by GitBox <gi...@apache.org>.
andygrove commented on code in PR #2344:
URL: https://github.com/apache/arrow-datafusion/pull/2344#discussion_r859205980


##########
datafusion/core/src/sql/planner.rs:
##########
@@ -443,16 +463,21 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
         &self,
         t: TableWithJoins,
         ctes: &mut HashMap<String, LogicalPlan>,
+        outer_schema: Option<&DFSchema>,

Review Comment:
   That makes sense. I renamed this.



-- 
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: github-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #2344: Add SQL planner support for EXISTS subqueries

Posted by GitBox <gi...@apache.org>.
alamb commented on code in PR #2344:
URL: https://github.com/apache/arrow-datafusion/pull/2344#discussion_r859124195


##########
datafusion/core/src/sql/planner.rs:
##########
@@ -443,16 +463,21 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
         &self,
         t: TableWithJoins,
         ctes: &mut HashMap<String, LogicalPlan>,
+        outer_schema: Option<&DFSchema>,

Review Comment:
   Given that `outer` means something already in terms of joins, I wonder if a name like `outer_query_schema` might potentially cause less confusion



##########
datafusion/core/src/sql/planner.rs:
##########
@@ -4182,4 +4264,36 @@ mod tests {
             \n    TableScan: test projection=None";
         quick_test(sql, expected);
     }
+
+    #[test]
+    fn exists_subquery() {
+        let sql = "SELECT id FROM person p WHERE EXISTS \
+            (SELECT first_name FROM person \
+            WHERE last_name = p.last_name \
+            AND state = p.state)";
+
+        let expected = "Projection: #p.id\
+        \n  Filter: EXISTS (Subquery: Projection: #person.first_name\
+        \n  Filter: #person.last_name = #p.last_name AND #person.state = #p.state\
+        \n    TableScan: person projection=None)\
+        \n    SubqueryAlias: p\
+        \n      TableScan: person projection=None";
+        quick_test(sql, expected);
+    }
+
+    #[test]
+    fn exists_subquery_wildcard() {
+        let sql = "SELECT id FROM person p WHERE EXISTS \
+            (SELECT * FROM person \
+            WHERE last_name = p.last_name \
+            AND state = p.state)";
+
+        let expected = "Projection: #p.id\
+        \n  Filter: EXISTS (Subquery: Projection: #person.id, #person.first_name, #person.last_name, #person.age, #person.state, #person.salary, #person.birth_date, #person.😀\
+        \n  Filter: #person.last_name = #p.last_name AND #person.state = #p.state\
+        \n    TableScan: person projection=None)\
+        \n    SubqueryAlias: p\
+        \n      TableScan: person projection=None";

Review Comment:
   I am not sure what this plan is doing (as it seems to have a `LogicalPlan::Filter` node that has multiple inputs which I didn't think was possible 🤔 )
   
   With a correlated subquery like this I think the classic plan uses a `Semi-Join` like:
   
   ```
   "Projection: #p.id\
     Join(type=Semi): #person.last_name = #p.last_name AND #person.state = #p.state\
       TableScan: person projection=None)\
       SubqueryAlias: p\
         TableScan: person projection=None";
   ```
   
   



-- 
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: github-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-datafusion] andygrove commented on pull request #2344: Add SQL planner support for EXISTS subqueries

Posted by GitBox <gi...@apache.org>.
andygrove commented on PR #2344:
URL: https://github.com/apache/arrow-datafusion/pull/2344#issuecomment-1110125055

   @alamb Here is the first PR to actually support subqueries in the SQL planner. I'm sure there will be follow up work to make sure the optimizer rules work correctly but this at least enables the creation of a logical plan from a SQL query containing EXISTS subqueries.


-- 
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: github-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #2344: Add SQL planner support for EXISTS subqueries

Posted by GitBox <gi...@apache.org>.
alamb commented on code in PR #2344:
URL: https://github.com/apache/arrow-datafusion/pull/2344#discussion_r859653759


##########
datafusion/core/src/sql/planner.rs:
##########
@@ -4182,4 +4264,36 @@ mod tests {
             \n    TableScan: test projection=None";
         quick_test(sql, expected);
     }
+
+    #[test]
+    fn exists_subquery() {
+        let sql = "SELECT id FROM person p WHERE EXISTS \
+            (SELECT first_name FROM person \
+            WHERE last_name = p.last_name \
+            AND state = p.state)";
+
+        let expected = "Projection: #p.id\
+        \n  Filter: EXISTS (Subquery: Projection: #person.first_name\
+        \n  Filter: #person.last_name = #p.last_name AND #person.state = #p.state\
+        \n    TableScan: person projection=None)\
+        \n    SubqueryAlias: p\
+        \n      TableScan: person projection=None";
+        quick_test(sql, expected);
+    }
+
+    #[test]
+    fn exists_subquery_wildcard() {
+        let sql = "SELECT id FROM person p WHERE EXISTS \
+            (SELECT * FROM person \
+            WHERE last_name = p.last_name \
+            AND state = p.state)";
+
+        let expected = "Projection: #p.id\
+        \n  Filter: EXISTS (Subquery: Projection: #person.id, #person.first_name, #person.last_name, #person.age, #person.state, #person.salary, #person.birth_date, #person.😀\
+        \n  Filter: #person.last_name = #p.last_name AND #person.state = #p.state\
+        \n    TableScan: person projection=None)\
+        \n    SubqueryAlias: p\
+        \n      TableScan: person projection=None";

Review Comment:
   Thank you for the change. It is much easier to see what is going on now. 
   



-- 
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: github-unsubscribe@arrow.apache.org

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