You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "shivbhatia10 (via GitHub)" <gi...@apache.org> on 2026/03/19 16:07:48 UTC
[PR] Don't push down filter if sort has fetch, same as limit [datafusion]
shivbhatia10 opened a new pull request, #21057:
URL: https://github.com/apache/datafusion/pull/21057
## 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 #.
## 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?
<!--
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?
<!--
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] Don't push down filter if sort has fetch, same as limit [datafusion]
Posted by "shivbhatia10 (via GitHub)" <gi...@apache.org>.
shivbhatia10 commented on code in PR #21057:
URL: https://github.com/apache/datafusion/pull/21057#discussion_r2961166329
##########
datafusion/optimizer/src/push_down_filter.rs:
##########
@@ -832,6 +832,16 @@ impl OptimizerRule for PushDownFilter {
insert_below(LogicalPlan::Distinct(distinct), new_filter)
}
LogicalPlan::Sort(sort) => {
+ // If the sort has a fetch (limit), pushing a filter below
+ // it would change semantics: the limit should apply before
+ // the filter, not after.
+ if sort.fetch.is_some() {
Review Comment:
For logical nodes as far as I can see only Sort, Limit, and TableScan have fetch
--
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] Don't push down filter if sort has fetch, same as limit [datafusion]
Posted by "xanderbailey (via GitHub)" <gi...@apache.org>.
xanderbailey commented on code in PR #21057:
URL: https://github.com/apache/datafusion/pull/21057#discussion_r2961180779
##########
datafusion/optimizer/src/push_down_filter.rs:
##########
@@ -832,6 +832,16 @@ impl OptimizerRule for PushDownFilter {
insert_below(LogicalPlan::Distinct(distinct), new_filter)
}
LogicalPlan::Sort(sort) => {
+ // If the sort has a fetch (limit), pushing a filter below
+ // it would change semantics: the limit should apply before
+ // the filter, not after.
+ if sort.fetch.is_some() {
Review Comment:
Ah sorry yes, that sounds right
--
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] Fix push_down_filter for children with non-empty fetch fields [datafusion]
Posted by "adriangb (via GitHub)" <gi...@apache.org>.
adriangb commented on code in PR #21057:
URL: https://github.com/apache/datafusion/pull/21057#discussion_r2967300045
##########
datafusion/optimizer/src/push_down_filter.rs:
##########
@@ -832,6 +832,16 @@ impl OptimizerRule for PushDownFilter {
insert_below(LogicalPlan::Distinct(distinct), new_filter)
}
LogicalPlan::Sort(sort) => {
+ // If the sort has a fetch (limit), pushing a filter below
+ // it would change semantics: the limit should apply before
+ // the filter, not after.
+ if sort.fetch.is_some() {
Review Comment:
I think it would be reasonable to add a `fn fetch(&self) -> Option<usize>` to `LogicalPlan`. We could make the match exhaustive / have no fall-through so that anyone adding a new variant has to update it. At least then it will be in one place and also closer to the change -> better for human and machine to pick it up.
--
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] Fix push_down_filter for children with non-empty fetch fields [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #21057:
URL: https://github.com/apache/datafusion/pull/21057
--
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] Fix push_down_filter for children with non-empty fetch fields [datafusion]
Posted by "hareshkh (via GitHub)" <gi...@apache.org>.
hareshkh commented on code in PR #21057:
URL: https://github.com/apache/datafusion/pull/21057#discussion_r2961821291
##########
datafusion/optimizer/src/push_down_filter.rs:
##########
@@ -832,6 +832,16 @@ impl OptimizerRule for PushDownFilter {
insert_below(LogicalPlan::Distinct(distinct), new_filter)
}
LogicalPlan::Sort(sort) => {
+ // If the sort has a fetch (limit), pushing a filter below
+ // it would change semantics: the limit should apply before
+ // the filter, not after.
+ if sort.fetch.is_some() {
Review Comment:
Yes, will need to check physical plan too.
```rust
if self.fetch.is_some() {
return Ok(FilterDescription::all_unsupported(
&parent_filters,
&self.children(),
));
}
```
--
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] Fix push_down_filter for children with non-empty fetch fields [datafusion]
Posted by "hareshkh (via GitHub)" <gi...@apache.org>.
hareshkh commented on code in PR #21057:
URL: https://github.com/apache/datafusion/pull/21057#discussion_r2962430082
##########
datafusion/sqllogictest/test_files/push_down_filter_sort_fetch.slt:
##########
@@ -0,0 +1,64 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# Regression tests: filters must NOT be pushed below Sort with fetch (LIMIT).
+#
+# When a Sort node has a fetch (e.g. ORDER BY ... LIMIT N), pushing a filter
+# below it changes query semantics: the limit should apply before the filter.
+
+statement ok
+CREATE TABLE t(id INT, value INT) AS VALUES
+(1, 100),
+(2, 200),
+(3, 300),
+(4, 400),
+(5, 500);
+
+# Correct result: first take the 3 smallest values (100, 200, 300),
+# then filter value > 200 → only (3, 300).
+#
+# Without the fix the filter is pushed below the sort+limit, producing
+# the 3 smallest values *after* filtering: (300, 400, 500) — wrong.
+query II
+SELECT * FROM (SELECT * FROM t ORDER BY value LIMIT 3) sub WHERE sub.value > 200;
+----
+3 300
+
+# Same test with DESC ordering.
Review Comment:
nit: Can remove this line too
--
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] Fix push_down_filter for children with non-empty fetch fields [datafusion]
Posted by "hareshkh (via GitHub)" <gi...@apache.org>.
hareshkh commented on code in PR #21057:
URL: https://github.com/apache/datafusion/pull/21057#discussion_r2966432536
##########
datafusion/optimizer/src/push_down_filter.rs:
##########
@@ -1130,6 +1137,13 @@ impl OptimizerRule for PushDownFilter {
}
LogicalPlan::Join(join) => push_down_join(join, Some(&filter.predicate)),
LogicalPlan::TableScan(scan) => {
+ // If the scan has a fetch (limit), pushing filters into it
+ // would change semantics: the limit should apply before the
+ // filter, not after.
+ if scan.fetch.is_some() {
+ filter.input = Arc::new(LogicalPlan::TableScan(scan));
+ return Ok(Transformed::no(LogicalPlan::Filter(filter)));
+ }
Review Comment:
@alamb : Curious about your thoughts for this part of the change?
For a plan like:
```
FILTER col = val
|---- LIMIT 50
|---- TABLE_SCAN
```
After PushDownLimit folds the limit, we get FILTER -> TABLE_SCAN(fetch=50). This PR prevents pushing the filter into scan.filters when fetch is set.
Since there is no ordering specified, the LIMIT is essentially non-deterministic in the rows it returns. So the filter can be moved past it or run after it — both are semantically correct. But if the table scan has an underlying implicit ordering (due to the layout of data or such), then pushing the filter around may be incorrect.
--
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] Fix push_down_filter for children with non-empty fetch fields [datafusion]
Posted by "hareshkh (via GitHub)" <gi...@apache.org>.
hareshkh commented on code in PR #21057:
URL: https://github.com/apache/datafusion/pull/21057#discussion_r2966432536
##########
datafusion/optimizer/src/push_down_filter.rs:
##########
@@ -1130,6 +1137,13 @@ impl OptimizerRule for PushDownFilter {
}
LogicalPlan::Join(join) => push_down_join(join, Some(&filter.predicate)),
LogicalPlan::TableScan(scan) => {
+ // If the scan has a fetch (limit), pushing filters into it
+ // would change semantics: the limit should apply before the
+ // filter, not after.
+ if scan.fetch.is_some() {
+ filter.input = Arc::new(LogicalPlan::TableScan(scan));
+ return Ok(Transformed::no(LogicalPlan::Filter(filter)));
+ }
Review Comment:
@alamb : Curious about your thoughts for this part of the change?
For a plan like:
```
FILTER col = val
|---- LIMIT 50
|---- TABLE_SCAN
```
After PushDownLimit folds the limit, we get FILTER -> TABLE_SCAN(fetch=50). This PR prevents pushing the filter into scan.filters when fetch is set.
Since there is no ordering specified, the LIMIT is essentially non-deterministic in the rows it returns. So the filter can be moved past it or run after it — both are semantically correct - in which case we can remove this part of the change. But if the table scan has an underlying implicit ordering (due to the layout of data or such), then pushing the filter around may be incorrect.
--
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] Don't push down filter if sort has fetch, same as limit [datafusion]
Posted by "shivbhatia10 (via GitHub)" <gi...@apache.org>.
shivbhatia10 commented on code in PR #21057:
URL: https://github.com/apache/datafusion/pull/21057#discussion_r2961198838
##########
datafusion/optimizer/src/push_down_filter.rs:
##########
@@ -832,6 +832,16 @@ impl OptimizerRule for PushDownFilter {
insert_below(LogicalPlan::Distinct(distinct), new_filter)
}
LogicalPlan::Sort(sort) => {
+ // If the sort has a fetch (limit), pushing a filter below
+ // it would change semantics: the limit should apply before
+ // the filter, not after.
+ if sort.fetch.is_some() {
Review Comment:
Limit and TableScan already have special casing in this file
--
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] Fix push_down_filter for children with non-empty fetch fields [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #21057:
URL: https://github.com/apache/datafusion/pull/21057#discussion_r2969462639
##########
datafusion/optimizer/src/push_down_filter.rs:
##########
@@ -1130,6 +1137,13 @@ impl OptimizerRule for PushDownFilter {
}
LogicalPlan::Join(join) => push_down_join(join, Some(&filter.predicate)),
LogicalPlan::TableScan(scan) => {
+ // If the scan has a fetch (limit), pushing filters into it
+ // would change semantics: the limit should apply before the
+ // filter, not after.
+ if scan.fetch.is_some() {
+ filter.input = Arc::new(LogicalPlan::TableScan(scan));
+ return Ok(Transformed::no(LogicalPlan::Filter(filter)));
+ }
Review Comment:
> After PushDownLimit folds the limit, we get FILTER -> TABLE_SCAN(fetch=50). This PR prevents pushing the filter into scan.filters when fetch is set.
This is an excellent point
> . So the filter can be moved past it or run after it — both are semantically correct
I am not sure they are both semantically correct. I think limit is (should be) applied after Filters in table providers and there are some places where that already happens. For example
- https://github.com/apache/datafusion/blob/ee24f3c3cd320b88c5ea6a985cbc17d3a5b6b37b/datafusion/catalog-listing/src/table.rs#L511-L514
Also in the parquet data source I know the limit is applied *after* filters as well
However, that does not appear to be explicitly documented anywhere I could find -- I will make a PR to clarify the documentation
--
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] Fix push_down_filter for children with non-empty fetch fields [datafusion]
Posted by "shivbhatia10 (via GitHub)" <gi...@apache.org>.
shivbhatia10 commented on code in PR #21057:
URL: https://github.com/apache/datafusion/pull/21057#discussion_r2973929807
##########
datafusion/expr/src/logical_plan/plan.rs:
##########
@@ -1392,6 +1392,82 @@ impl LogicalPlan {
}
}
+ /// Returns the skip (offset) of this plan node, if it has one.
+ ///
+ /// Only [`LogicalPlan::Limit`] carries a skip value; all other variants
+ /// return `Ok(None)`. Returns `Ok(None)` for a zero skip.
+ pub fn skip(&self) -> Result<Option<usize>> {
Review Comment:
I think it might be confusing if limit returns an Option<usize>, but I could make it a boolean function like `fn has_internal_limit(&self) -> Result<bool>` instead if that's clearer than having two methods. Although I think these methods might be reusable in other contexts as they are now
##########
datafusion/expr/src/logical_plan/plan.rs:
##########
@@ -1392,6 +1392,82 @@ impl LogicalPlan {
}
}
+ /// Returns the skip (offset) of this plan node, if it has one.
+ ///
+ /// Only [`LogicalPlan::Limit`] carries a skip value; all other variants
+ /// return `Ok(None)`. Returns `Ok(None)` for a zero skip.
+ pub fn skip(&self) -> Result<Option<usize>> {
Review Comment:
I think it might be confusing if limit returns an `Option<usize>`, but I could make it a boolean function like `fn has_internal_limit(&self) -> Result<bool>` instead if that's clearer than having two methods. Although I think these methods might be reusable in other contexts as they are 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] Don't push down filter if sort has fetch, same as limit [datafusion]
Posted by "xanderbailey (via GitHub)" <gi...@apache.org>.
xanderbailey commented on code in PR #21057:
URL: https://github.com/apache/datafusion/pull/21057#discussion_r2961133589
##########
datafusion/optimizer/src/push_down_filter.rs:
##########
@@ -832,6 +832,16 @@ impl OptimizerRule for PushDownFilter {
insert_below(LogicalPlan::Distinct(distinct), new_filter)
}
LogicalPlan::Sort(sort) => {
+ // If the sort has a fetch (limit), pushing a filter below
+ // it would change semantics: the limit should apply before
+ // the filter, not after.
+ if sort.fetch.is_some() {
Review Comment:
`ExecutionPlan` has fetch on it so I wonder if we need to do this more generically?
--
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] Fix push_down_filter for children with non-empty fetch fields [datafusion]
Posted by "shivbhatia10 (via GitHub)" <gi...@apache.org>.
shivbhatia10 commented on code in PR #21057:
URL: https://github.com/apache/datafusion/pull/21057#discussion_r2969475128
##########
datafusion/optimizer/src/push_down_filter.rs:
##########
@@ -796,6 +796,12 @@ impl OptimizerRule for PushDownFilter {
filter.predicate = new_predicate;
}
+ // If the child has a fetch (limit), pushing a filter below it would
+ // change semantics: the limit should apply before the filter, not after.
+ if filter.input.fetch().is_some() {
Review Comment:
I think this makes a lot of sense, the reason I initially skipped `Limit` was because we already handle it correctly in `PushDownFilter::rewrite`, although very indirectly, because `Limit` is not covered in the match statement and therefore it falls back to leaving the filter unchanged. However this is a fragile assumption - I think the right thing to do here is to add another method to `LogicalPlan` called `skip`, since `Limit` can have both fetch and skip variants, and if either of them is non-empty we should block pushdown. This is true in general for any logical plan node I believe. So I've added that method, used the `get_skip_type` and `get_fetch_type` methods on `Limit` to extract these `Optional<usize>` types, and now we check for the presence of both skip and fetch in push down filter: [9d6e79a](https://github.com/apache/datafusion/pull/21057/commits/9d6e79a7041415aa33761995c2076859aac1b1cb)
--
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] Fix push_down_filter for children with non-empty fetch fields [datafusion]
Posted by "xanderbailey (via GitHub)" <gi...@apache.org>.
xanderbailey commented on code in PR #21057:
URL: https://github.com/apache/datafusion/pull/21057#discussion_r2967329676
##########
datafusion/optimizer/src/push_down_filter.rs:
##########
@@ -832,6 +832,16 @@ impl OptimizerRule for PushDownFilter {
insert_below(LogicalPlan::Distinct(distinct), new_filter)
}
LogicalPlan::Sort(sort) => {
+ // If the sort has a fetch (limit), pushing a filter below
+ // it would change semantics: the limit should apply before
+ // the filter, not after.
+ if sort.fetch.is_some() {
Review Comment:
I think that would be nice!
--
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] Don't push down filter if sort has fetch, same as limit [datafusion]
Posted by "xanderbailey (via GitHub)" <gi...@apache.org>.
xanderbailey commented on code in PR #21057:
URL: https://github.com/apache/datafusion/pull/21057#discussion_r2961184723
##########
datafusion/optimizer/src/push_down_filter.rs:
##########
@@ -832,6 +832,16 @@ impl OptimizerRule for PushDownFilter {
insert_below(LogicalPlan::Distinct(distinct), new_filter)
}
LogicalPlan::Sort(sort) => {
+ // If the sort has a fetch (limit), pushing a filter below
+ // it would change semantics: the limit should apply before
+ // the filter, not after.
+ if sort.fetch.is_some() {
Review Comment:
Was just trying to think about how we can prevent this kind of bug happening in the fututre.
--
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] Don't push down filter if sort has fetch, same as limit [datafusion]
Posted by "xanderbailey (via GitHub)" <gi...@apache.org>.
xanderbailey commented on code in PR #21057:
URL: https://github.com/apache/datafusion/pull/21057#discussion_r2961275380
##########
datafusion/optimizer/src/push_down_filter.rs:
##########
@@ -832,6 +832,16 @@ impl OptimizerRule for PushDownFilter {
insert_below(LogicalPlan::Distinct(distinct), new_filter)
}
LogicalPlan::Sort(sort) => {
+ // If the sort has a fetch (limit), pushing a filter below
+ // it would change semantics: the limit should apply before
+ // the filter, not after.
+ if sort.fetch.is_some() {
Review Comment:
Do we need to modify `gather_filters_for_pushdown` in `SortExec`
--
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] Fix push_down_filter for children with non-empty fetch fields [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #21057:
URL: https://github.com/apache/datafusion/pull/21057#issuecomment-4103194742
THank you also @xanderbailey for the reviews
--
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] Fix push_down_filter for children with non-empty fetch fields [datafusion]
Posted by "shivbhatia10 (via GitHub)" <gi...@apache.org>.
shivbhatia10 commented on code in PR #21057:
URL: https://github.com/apache/datafusion/pull/21057#discussion_r2969475128
##########
datafusion/optimizer/src/push_down_filter.rs:
##########
@@ -796,6 +796,12 @@ impl OptimizerRule for PushDownFilter {
filter.predicate = new_predicate;
}
+ // If the child has a fetch (limit), pushing a filter below it would
+ // change semantics: the limit should apply before the filter, not after.
+ if filter.input.fetch().is_some() {
Review Comment:
I think this makes a lot of sense, the reason I initially skipped `Limit` was because we already handle it correctly in `PushDownFilter::rewrite`, although very indirectly, because `Limit` is not covered in the match statement and therefore it falls back to leaving the filter unchanged. However this is a fragile assumption - I think the right thing to do here is to add another method to `LogicalPlan` called `skip`, since `Limit` can have both fetch and skip variants, and if either of them is non-empty we should block pushdown. This is true in general for any logical plan node I believe. So I've added that method, used the `get_skip_type` and `get_fetch_type` methods on `Limit` to extract these `Optional<usize>` types, and now we check for the presence of both skip and fetch in push down filter.
--
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] Fix push_down_filter for children with non-empty fetch fields [datafusion]
Posted by "hareshkh (via GitHub)" <gi...@apache.org>.
hareshkh commented on code in PR #21057:
URL: https://github.com/apache/datafusion/pull/21057#discussion_r2972140259
##########
datafusion/optimizer/src/push_down_filter.rs:
##########
@@ -1130,6 +1137,13 @@ impl OptimizerRule for PushDownFilter {
}
LogicalPlan::Join(join) => push_down_join(join, Some(&filter.predicate)),
LogicalPlan::TableScan(scan) => {
+ // If the scan has a fetch (limit), pushing filters into it
+ // would change semantics: the limit should apply before the
+ // filter, not after.
+ if scan.fetch.is_some() {
+ filter.input = Arc::new(LogicalPlan::TableScan(scan));
+ return Ok(Transformed::no(LogicalPlan::Filter(filter)));
+ }
Review Comment:
Thankyou @alamb - I think keeping this check for TableScan makes 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@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] Don't push down filter if sort has fetch, same as limit [datafusion]
Posted by "shivbhatia10 (via GitHub)" <gi...@apache.org>.
shivbhatia10 commented on code in PR #21057:
URL: https://github.com/apache/datafusion/pull/21057#discussion_r2961162644
##########
datafusion/optimizer/src/push_down_filter.rs:
##########
@@ -832,6 +832,16 @@ impl OptimizerRule for PushDownFilter {
insert_below(LogicalPlan::Distinct(distinct), new_filter)
}
LogicalPlan::Sort(sort) => {
+ // If the sort has a fetch (limit), pushing a filter below
+ // it would change semantics: the limit should apply before
+ // the filter, not after.
+ if sort.fetch.is_some() {
Review Comment:
That's physical nodes, this is the logical plan optimizer
--
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] Fix push_down_filter for children with non-empty fetch fields [datafusion]
Posted by "shivbhatia10 (via GitHub)" <gi...@apache.org>.
shivbhatia10 commented on PR #21057:
URL: https://github.com/apache/datafusion/pull/21057#issuecomment-4092999483
Hey @alamb and @adriangb, would you like to take a look at this one?
--
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] Fix push_down_filter for children with non-empty fetch fields [datafusion]
Posted by "shivbhatia10 (via GitHub)" <gi...@apache.org>.
shivbhatia10 commented on code in PR #21057:
URL: https://github.com/apache/datafusion/pull/21057#discussion_r2967478399
##########
datafusion/optimizer/src/push_down_filter.rs:
##########
@@ -832,6 +832,16 @@ impl OptimizerRule for PushDownFilter {
insert_below(LogicalPlan::Distinct(distinct), new_filter)
}
LogicalPlan::Sort(sort) => {
+ // If the sort has a fetch (limit), pushing a filter below
+ // it would change semantics: the limit should apply before
+ // the filter, not after.
+ if sort.fetch.is_some() {
Review Comment:
That makes sense @adriangb, I've tried adding that: [0e83891](https://github.com/apache/datafusion/pull/21057/commits/0e83891753d055f62dea152c9cacad2a9f4a642c)
--
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] Fix push_down_filter for children with non-empty fetch fields [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #21057:
URL: https://github.com/apache/datafusion/pull/21057#discussion_r2969441662
##########
datafusion/expr/src/logical_plan/plan.rs:
##########
@@ -1392,6 +1392,40 @@ impl LogicalPlan {
}
}
+ /// Returns the fetch (limit) of this plan node, if it has one.
+ ///
+ /// Only [`LogicalPlan::Sort`] and [`LogicalPlan::TableScan`] carry a fetch
+ /// value directly; all other variants return `None`.
+ pub fn fetch(&self) -> Option<usize> {
+ match self {
+ LogicalPlan::Sort(Sort { fetch, .. }) => *fetch,
+ LogicalPlan::TableScan(TableScan { fetch, .. }) => *fetch,
+ LogicalPlan::Projection(_) => None,
+ LogicalPlan::Filter(_) => None,
+ LogicalPlan::Window(_) => None,
+ LogicalPlan::Aggregate(_) => None,
+ LogicalPlan::Join(_) => None,
+ LogicalPlan::Repartition(_) => None,
+ LogicalPlan::Union(_) => None,
+ LogicalPlan::EmptyRelation(_) => None,
+ LogicalPlan::Subquery(_) => None,
+ LogicalPlan::SubqueryAlias(_) => None,
+ LogicalPlan::Limit(_) => None,
Review Comment:
I am surprised LogicalPlan::Limit does not return a value here as well;
I looked into the code a bit to try and understand why Limits were needed. Initially I thought it was because the following code claims that we only push down Filters *after* Limits
https://github.com/apache/datafusion/blob/ee24f3c3cd320b88c5ea6a985cbc17d3a5b6b37b/datafusion/optimizer/src/optimizer.rs#L297-L299
However, at some point we started running multiple optimizer passes so `PushdownFilter` can (effectively) run after `PushDownLimit`
https://github.com/apache/datafusion/blob/ee24f3c3cd320b88c5ea6a985cbc17d3a5b6b37b/datafusion/optimizer/src/optimizer.rs#L386-L402
So this bug was probably introduced when we started running multiple optimizer passes
##########
datafusion/optimizer/src/push_down_filter.rs:
##########
@@ -796,6 +796,12 @@ impl OptimizerRule for PushDownFilter {
filter.predicate = new_predicate;
}
+ // If the child has a fetch (limit), pushing a filter below it would
Review Comment:
👍
##########
datafusion/sqllogictest/test_files/push_down_filter_sort_fetch.slt:
##########
@@ -0,0 +1,55 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# Tests for filter pushdown behavior with Sort + LIMIT (fetch).
Review Comment:
I recommend adding this to an existing test rather than an entirely new .slt test to make it easier to discover int he future
Perhaps `datafusion/sqllogictest/test_files/limit.slt`
##########
datafusion/optimizer/src/push_down_filter.rs:
##########
@@ -796,6 +796,12 @@ impl OptimizerRule for PushDownFilter {
filter.predicate = new_predicate;
}
+ // If the child has a fetch (limit), pushing a filter below it would
+ // change semantics: the limit should apply before the filter, not after.
+ if filter.input.fetch().is_some() {
Review Comment:
I think we should also either explicitly check here for Limit or (better yet) change `fetch()` to also return Some for LogicalPlan::Limit for the reasons I explain above
##########
datafusion/physical-plan/src/sorts/sort.rs:
##########
@@ -2863,4 +2875,111 @@ mod tests {
drop(contender);
Ok(())
}
+
+ #[test]
+ fn test_sort_with_fetch_blocks_filter_pushdown() -> Result<()> {
+ let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Int32, false)]));
Review Comment:
these tests are currently quite repeititive and mostly boilerplate -- not only does this expand the diff unecessarily I think it also makes it harder to read and understand what is different between the different tests (and thus validate that they testing the correct thing)
I suggest making a helper funtion with the common setup so it is clear what the different tests are
##########
datafusion/sqllogictest/test_files/window.slt:
##########
@@ -3198,16 +3198,17 @@ EXPLAIN SELECT * FROM (SELECT *, ROW_NUMBER() OVER(ORDER BY a ASC) as rn1
----
logical_plan
01)Sort: rn1 ASC NULLS LAST
-02)--Sort: rn1 ASC NULLS LAST, fetch=5
-03)----Projection: annotated_data_infinite2.a0, annotated_data_infinite2.a, annotated_data_infinite2.b, annotated_data_infinite2.c, annotated_data_infinite2.d, row_number() ORDER BY [annotated_data_infinite2.a ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW AS rn1
-04)------Filter: row_number() ORDER BY [annotated_data_infinite2.a ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW < UInt64(50)
Review Comment:
it definitely had the filter too low 👍
--
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] Fix push_down_filter for children with non-empty fetch fields [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #21057:
URL: https://github.com/apache/datafusion/pull/21057#discussion_r2969507426
##########
datafusion/optimizer/src/push_down_filter.rs:
##########
@@ -1130,6 +1137,13 @@ impl OptimizerRule for PushDownFilter {
}
LogicalPlan::Join(join) => push_down_join(join, Some(&filter.predicate)),
LogicalPlan::TableScan(scan) => {
+ // If the scan has a fetch (limit), pushing filters into it
+ // would change semantics: the limit should apply before the
+ // filter, not after.
+ if scan.fetch.is_some() {
+ filter.input = Arc::new(LogicalPlan::TableScan(scan));
+ return Ok(Transformed::no(LogicalPlan::Filter(filter)));
+ }
Review Comment:
PR to improve docs
- https://github.com/apache/datafusion/pull/21091
--
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] Fix push_down_filter for children with non-empty fetch fields [datafusion]
Posted by "shivbhatia10 (via GitHub)" <gi...@apache.org>.
shivbhatia10 commented on code in PR #21057:
URL: https://github.com/apache/datafusion/pull/21057#discussion_r2962410146
##########
datafusion/sqllogictest/test_files/push_down_filter_sort_fetch.slt:
##########
@@ -0,0 +1,64 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# Regression tests: filters must NOT be pushed below Sort with fetch (LIMIT).
+#
+# When a Sort node has a fetch (e.g. ORDER BY ... LIMIT N), pushing a filter
+# below it changes query semantics: the limit should apply before the filter.
+
+statement ok
+CREATE TABLE t(id INT, value INT) AS VALUES
+(1, 100),
+(2, 200),
+(3, 300),
+(4, 400),
+(5, 500);
+
+# Correct result: first take the 3 smallest values (100, 200, 300),
+# then filter value > 200 → only (3, 300).
+#
+# Without the fix the filter is pushed below the sort+limit, producing
+# the 3 smallest values *after* filtering: (300, 400, 500) — wrong.
+query II
+SELECT * FROM (SELECT * FROM t ORDER BY value LIMIT 3) sub WHERE sub.value > 200;
+----
+3 300
+
+# Same test with DESC ordering.
+# First take the 3 largest values (500, 400, 300), then filter value < 400.
+# Correct: (3, 300). Buggy: (1, 100), (2, 200), (3, 300).
+query II
+SELECT * FROM (SELECT * FROM t ORDER BY value DESC LIMIT 3) sub WHERE sub.value < 400;
+----
+3 300
+
+# Verify the EXPLAIN plan keeps the filter above the sort+fetch.
+query TT
+EXPLAIN SELECT * FROM (SELECT * FROM t ORDER BY value LIMIT 3) sub WHERE sub.value > 200;
+----
+logical_plan
+01)SubqueryAlias: sub
+02)--Filter: t.value > Int32(200)
+03)----Sort: t.value ASC NULLS LAST, fetch=3
+04)------TableScan: t projection=[id, value]
+physical_plan
+01)FilterExec: value@1 > 200
+02)--SortExec: TopK(fetch=3), expr=[value@1 ASC NULLS LAST], preserve_partitioning=[false]
+03)----DataSourceExec: partitions=1, partition_sizes=[1]
+
+statement ok
+DROP TABLE t;
Review Comment:
<img width="955" height="841" alt="Image" src="https://github.com/user-attachments/assets/21aa5e34-d0bd-42d9-a4ac-769b7f6782dd" />
##########
datafusion/sqllogictest/test_files/push_down_filter_sort_fetch.slt:
##########
@@ -0,0 +1,64 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# Regression tests: filters must NOT be pushed below Sort with fetch (LIMIT).
+#
+# When a Sort node has a fetch (e.g. ORDER BY ... LIMIT N), pushing a filter
+# below it changes query semantics: the limit should apply before the filter.
+
+statement ok
+CREATE TABLE t(id INT, value INT) AS VALUES
+(1, 100),
+(2, 200),
+(3, 300),
+(4, 400),
+(5, 500);
+
+# Correct result: first take the 3 smallest values (100, 200, 300),
+# then filter value > 200 → only (3, 300).
+#
+# Without the fix the filter is pushed below the sort+limit, producing
+# the 3 smallest values *after* filtering: (300, 400, 500) — wrong.
+query II
+SELECT * FROM (SELECT * FROM t ORDER BY value LIMIT 3) sub WHERE sub.value > 200;
+----
+3 300
+
+# Same test with DESC ordering.
+# First take the 3 largest values (500, 400, 300), then filter value < 400.
+# Correct: (3, 300). Buggy: (1, 100), (2, 200), (3, 300).
+query II
+SELECT * FROM (SELECT * FROM t ORDER BY value DESC LIMIT 3) sub WHERE sub.value < 400;
+----
+3 300
+
+# Verify the EXPLAIN plan keeps the filter above the sort+fetch.
+query TT
+EXPLAIN SELECT * FROM (SELECT * FROM t ORDER BY value LIMIT 3) sub WHERE sub.value > 200;
+----
+logical_plan
+01)SubqueryAlias: sub
+02)--Filter: t.value > Int32(200)
+03)----Sort: t.value ASC NULLS LAST, fetch=3
+04)------TableScan: t projection=[id, value]
+physical_plan
+01)FilterExec: value@1 > 200
+02)--SortExec: TopK(fetch=3), expr=[value@1 ASC NULLS LAST], preserve_partitioning=[false]
+03)----DataSourceExec: partitions=1, partition_sizes=[1]
+
+statement ok
+DROP TABLE t;
Review Comment:
This fails without our fixes
--
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] Don't push down filter if sort has fetch, same as limit [datafusion]
Posted by "hareshkh (via GitHub)" <gi...@apache.org>.
hareshkh commented on code in PR #21057:
URL: https://github.com/apache/datafusion/pull/21057#discussion_r2961674326
##########
datafusion/optimizer/src/push_down_filter.rs:
##########
@@ -832,6 +832,16 @@ impl OptimizerRule for PushDownFilter {
insert_below(LogicalPlan::Distinct(distinct), new_filter)
}
LogicalPlan::Sort(sort) => {
+ // If the sort has a fetch (limit), pushing a filter below
+ // it would change semantics: the limit should apply before
+ // the filter, not after.
+ if sort.fetch.is_some() {
Review Comment:
This could be:
```rust
if sort.fetch.is_some() {
filter.input = Arc::new(LogicalPlan::Sort(sort));
return Ok(Transformed::no(LogicalPlan::Filter(filter)));
}
```
--
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] Fix push_down_filter for children with non-empty fetch fields [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #21057:
URL: https://github.com/apache/datafusion/pull/21057#discussion_r2974381516
##########
datafusion/expr/src/logical_plan/plan.rs:
##########
@@ -1392,6 +1392,82 @@ impl LogicalPlan {
}
}
+ /// Returns the skip (offset) of this plan node, if it has one.
+ ///
+ /// Only [`LogicalPlan::Limit`] carries a skip value; all other variants
+ /// return `Ok(None)`. Returns `Ok(None)` for a zero skip.
+ pub fn skip(&self) -> Result<Option<usize>> {
Review Comment:
I guess I was thinking that if you know you have a LogicalPlan::Limit you can call `get_skip_type` directly
Given the size of the DataFusion API already, I would prefer to avoid adding new APIs when possible
--
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] Fix push_down_filter for children with non-empty fetch fields [datafusion]
Posted by "hareshkh (via GitHub)" <gi...@apache.org>.
hareshkh commented on code in PR #21057:
URL: https://github.com/apache/datafusion/pull/21057#discussion_r2962424403
##########
datafusion/sqllogictest/test_files/push_down_filter_sort_fetch.slt:
##########
@@ -0,0 +1,64 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# Regression tests: filters must NOT be pushed below Sort with fetch (LIMIT).
+#
+# When a Sort node has a fetch (e.g. ORDER BY ... LIMIT N), pushing a filter
+# below it changes query semantics: the limit should apply before the filter.
+
+statement ok
+CREATE TABLE t(id INT, value INT) AS VALUES
+(1, 100),
+(2, 200),
+(3, 300),
+(4, 400),
+(5, 500);
+
+# Correct result: first take the 3 smallest values (100, 200, 300),
+# then filter value > 200 → only (3, 300).
+#
+# Without the fix the filter is pushed below the sort+limit, producing
+# the 3 smallest values *after* filtering: (300, 400, 500) — wrong.
Review Comment:
We can remove this from the comment as it is confusing to a future reader
--
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] Fix push_down_filter for children with non-empty fetch fields [datafusion]
Posted by "shivbhatia10 (via GitHub)" <gi...@apache.org>.
shivbhatia10 commented on PR #21057:
URL: https://github.com/apache/datafusion/pull/21057#issuecomment-4103199323
Thank you for the review @alamb - I've added pushdown filter handling for `Limit`, checking both `fetch` and `skip` variants, and I've made some changes to the tests
--
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] Fix push_down_filter for children with non-empty fetch fields [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #21057:
URL: https://github.com/apache/datafusion/pull/21057#discussion_r2969534002
##########
datafusion/expr/src/logical_plan/plan.rs:
##########
@@ -1392,6 +1392,82 @@ impl LogicalPlan {
}
}
+ /// Returns the skip (offset) of this plan node, if it has one.
+ ///
+ /// Only [`LogicalPlan::Limit`] carries a skip value; all other variants
+ /// return `Ok(None)`. Returns `Ok(None)` for a zero skip.
+ pub fn skip(&self) -> Result<Option<usize>> {
Review Comment:
Why not fold this into `fn limit()`? It seems like (almost) the same thing to me
--
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] Fix push_down_filter for children with non-empty fetch fields [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #21057:
URL: https://github.com/apache/datafusion/pull/21057#issuecomment-4097409318
I will revie this later today
--
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] Fix push_down_filter for children with non-empty fetch fields [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #21057:
URL: https://github.com/apache/datafusion/pull/21057#discussion_r2984049451
##########
datafusion/expr/src/logical_plan/plan.rs:
##########
@@ -1392,6 +1392,82 @@ impl LogicalPlan {
}
}
+ /// Returns the skip (offset) of this plan node, if it has one.
+ ///
+ /// Only [`LogicalPlan::Limit`] carries a skip value; all other variants
+ /// return `Ok(None)`. Returns `Ok(None)` for a zero skip.
+ pub fn skip(&self) -> Result<Option<usize>> {
Review Comment:
I see -- I forgot that the LogicalPlan has both skip/fetch -- this API makes sense. Thank you
--
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] Fix push_down_filter for children with non-empty fetch fields [datafusion]
Posted by "shivbhatia10 (via GitHub)" <gi...@apache.org>.
shivbhatia10 commented on code in PR #21057:
URL: https://github.com/apache/datafusion/pull/21057#discussion_r2974508559
##########
datafusion/expr/src/logical_plan/plan.rs:
##########
@@ -1392,6 +1392,82 @@ impl LogicalPlan {
}
}
+ /// Returns the skip (offset) of this plan node, if it has one.
+ ///
+ /// Only [`LogicalPlan::Limit`] carries a skip value; all other variants
+ /// return `Ok(None)`. Returns `Ok(None)` for a zero skip.
+ pub fn skip(&self) -> Result<Option<usize>> {
Review Comment:
Just to clarify, would that mean not adding the `fetch` and `skip` methods to `LogicalPlan` at all, and instead handling the logic for each node type individually in the branches of the `match` statement in `rewrite`, similar to what we had before?
--
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