You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "jonathanc-n (via GitHub)" <gi...@apache.org> on 2024/11/20 02:14:17 UTC
[PR] feat: Wrap `TableScan` with Filter in Join Unparsing [datafusion]
jonathanc-n opened a new pull request, #13496:
URL: https://github.com/apache/datafusion/pull/13496
## 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 #13156 .
## 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.
-->
## What changes are included in this PR?
Pushes down filter to tablescan instead of having it apply on the join in unparsing.
<!--
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.
-->
## Are these changes tested?
Changed the previous test
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
-->
## Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be updated before approving the PR.
-->
<!--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: Wrap `TableScan` with `Filter` in Join Unparsing [datafusion]
Posted by "sgrebnov (via GitHub)" <gi...@apache.org>.
sgrebnov commented on PR #13496:
URL: https://github.com/apache/datafusion/pull/13496#issuecomment-2492691910
@jonathanc-n, @goldmedal - I've reviewed this change and it brings back the following issue (there is additional context of why filtering added this way produces incorrect result) https://github.com/apache/datafusion/pull/13132 . I really like the change but can we improve this to see if we can wrap TableScan with Filter as a subquery when it is required
Example query.
## Original query / LogicalPlan / Result
```
select
c_custkey,
count(o_orderkey)
from
customer left join orders on c_custkey = o_custkey and o_comment not like '%special%requests%'
group by
c_custkey
```
```console
| | Projection: customer.c_custkey, count(orders.o_orderkey) |
| | Aggregate: groupBy=[[customer.c_custkey]], aggr=[[count(orders.o_orderkey)]] |
| | Left Join: Filter: customer.c_custkey = orders.o_custkey |
| | TableScan: customer projection=[c_custkey] |
| | TableScan: orders projection=[o_orderkey, o_custkey], full_filters=[orders.o_comment NOT LIKE Utf8("%special%requests%")]
```
Result:
```
1489 29
1269 0
652 24
273 0
51 0
...
```
## Existing unparser (main)
```
select
"customer"."c_custkey",
count("orders"."o_orderkey")
from
"customer"
left join "orders" on
(("customer"."c_custkey" = "orders"."o_custkey")
and "orders"."o_comment" not like '%special%requests%')
group by
"customer"."c_custkey"
```
Result:
```
1489 29
1269 0
652 24
273 0
51 0
...
```
## Proposed change
```
select
"customer"."c_custkey",
count("orders"."o_orderkey")
from
"customer"
left join "orders" on
("customer"."c_custkey" = "orders"."o_custkey")
where
"orders"."o_comment" not like '%special%requests%'
group by
"customer"."c_custkey"
```
Result
```
1489 29
652 24
1091 1
70 15
839 14
```
If intent for filter to be moved it must be wrapped as subquery in this case:
```
select
c_custkey,
count(o_orderkey)
from
customer left join (select * from orders where o_comment not like '%special%requests%') on c_custkey = o_custkey
group by
c_custkey
```
Result
```
1489 29
1269 0
652 24
273 0
51 0
```
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: Wrap `TableScan` with `Filter` in Join Unparsing [datafusion]
Posted by "sgrebnov (via GitHub)" <gi...@apache.org>.
sgrebnov commented on code in PR #13496:
URL: https://github.com/apache/datafusion/pull/13496#discussion_r1853129524
##########
datafusion/sql/tests/cases/plan_to_sql.rs:
##########
@@ -1153,7 +1153,7 @@ fn test_join_with_table_scan_filters() -> Result<()> {
let sql = plan_to_sql(&join_plan_multiple_filters)?;
- let expected_sql = r#"SELECT * FROM left_table AS "left" JOIN right_table ON "left".id = right_table.id AND (("left".id > 5) AND (("left"."name" LIKE 'some_name' AND (right_table."name" = 'before_join_filter_val')) AND (age > 10))) WHERE ("left"."name" = 'after_join_filter_val')"#;
+ let expected_sql = r#"SELECT * FROM left_table AS "left" JOIN right_table ON "left".id = right_table.id AND ("left".id > 5) WHERE ("left"."name" = 'after_join_filter_val') AND "left"."name" LIKE 'some_name' AND ((right_table."name" = 'before_join_filter_val') AND (right_table.age > 10))"#;
Review Comment:
I'm under impression that this change will result in incorrect behavior we were trying to fix here:
https://github.com/apache/datafusion/pull/13132.
Filtering after join is not the same as filtering during join (I would expect filters should be in subquery during join/ not after). Let me double check this please
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: Wrap `TableScan` with `Filter` in Join Unparsing [datafusion]
Posted by "jeremyx04 (via GitHub)" <gi...@apache.org>.
jeremyx04 commented on PR #13496:
URL: https://github.com/apache/datafusion/pull/13496#issuecomment-2487196108
lgtm
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: Wrap `TableScan` with `Filter` in Join Unparsing [datafusion]
Posted by "goldmedal (via GitHub)" <gi...@apache.org>.
goldmedal commented on PR #13496:
URL: https://github.com/apache/datafusion/pull/13496#issuecomment-2496518022
> @jonathanc-n, @goldmedal - thank you, I've reviewed this change and it seems it brings back the following issue (there is additional context of why filtering added this way produces incorrect result) #13132 . I really like the change but can we improve this to see if we can wrap TableScan with Filter as a subquery when it is required
>
Thank @sgrebnov for checking this. Indeed, this looks like it brings us back to #13132. I agree with you that we should wrap them in the subquery if it's required. @jonathanc-n WDYT?
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: Wrap `TableScan` with `Filter` in Join Unparsing [datafusion]
Posted by "jonathanc-n (via GitHub)" <gi...@apache.org>.
jonathanc-n commented on PR #13496:
URL: https://github.com/apache/datafusion/pull/13496#issuecomment-2496557539
@jayzhan211 I agree, I'll try to wrap it in a subquery, i'll mark it as a draft for 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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: Wrap `TableScan` with `Filter` in Join Unparsing [datafusion]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] closed pull request #13496: feat: Wrap `TableScan` with `Filter` in Join Unparsing
URL: https://github.com/apache/datafusion/pull/13496
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: Wrap `TableScan` with `Filter` in Join Unparsing [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #13496:
URL: https://github.com/apache/datafusion/pull/13496#discussion_r1855189743
##########
datafusion/sql/tests/cases/plan_to_sql.rs:
##########
@@ -1153,7 +1153,7 @@ fn test_join_with_table_scan_filters() -> Result<()> {
let sql = plan_to_sql(&join_plan_multiple_filters)?;
- let expected_sql = r#"SELECT * FROM left_table AS "left" JOIN right_table ON "left".id = right_table.id AND (("left".id > 5) AND (("left"."name" LIKE 'some_name' AND (right_table."name" = 'before_join_filter_val')) AND (age > 10))) WHERE ("left"."name" = 'after_join_filter_val')"#;
+ let expected_sql = r#"SELECT * FROM left_table AS "left" JOIN right_table ON "left".id = right_table.id AND ("left".id > 5) WHERE ("left"."name" = 'after_join_filter_val') AND "left"."name" LIKE 'some_name' AND ((right_table."name" = 'before_join_filter_val') AND (right_table.age > 10))"#;
Review Comment:
Filtering after join is (semantically) the same as filtering before/during joins only for `INNER JOIN` / `JOIN`
You are correct that for `LEFT/RIGHT/FULL OUTER JOIN` and `SEMI/ANTI JOIN` the rules are more subtle
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: Wrap `TableScan` with `Filter` in Join Unparsing [datafusion]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #13496:
URL: https://github.com/apache/datafusion/pull/13496#issuecomment-2613718032
Thank you for your contribution. Unfortunately, this pull request is stale because it has been open 60 days with no activity. Please remove the stale label or comment or this will be closed in 7 days.
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: Wrap `TableScan` with `Filter` in Join Unparsing [datafusion]
Posted by "sgrebnov (via GitHub)" <gi...@apache.org>.
sgrebnov commented on code in PR #13496:
URL: https://github.com/apache/datafusion/pull/13496#discussion_r1853129524
##########
datafusion/sql/tests/cases/plan_to_sql.rs:
##########
@@ -1153,7 +1153,7 @@ fn test_join_with_table_scan_filters() -> Result<()> {
let sql = plan_to_sql(&join_plan_multiple_filters)?;
- let expected_sql = r#"SELECT * FROM left_table AS "left" JOIN right_table ON "left".id = right_table.id AND (("left".id > 5) AND (("left"."name" LIKE 'some_name' AND (right_table."name" = 'before_join_filter_val')) AND (age > 10))) WHERE ("left"."name" = 'after_join_filter_val')"#;
+ let expected_sql = r#"SELECT * FROM left_table AS "left" JOIN right_table ON "left".id = right_table.id AND ("left".id > 5) WHERE ("left"."name" = 'after_join_filter_val') AND "left"."name" LIKE 'some_name' AND ((right_table."name" = 'before_join_filter_val') AND (right_table.age > 10))"#;
Review Comment:
I'm under impression that this change will result in incorrect behavior we were trying to fix here:
https://github.com/apache/datafusion/pull/13132.
Filtering after join is not the same as filtering during/before join (I would expect filters should be in subquery during join/ not after). Let me double check this please
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org