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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to