Skip to content

Commit 274e8be

Browse files
author
Shiv Bhatia
committed
Add slt test which fails without fix
1 parent 5e8fc77 commit 274e8be

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
# Regression tests: filters must NOT be pushed below Sort with fetch (LIMIT).
19+
#
20+
# When a Sort node has a fetch (e.g. ORDER BY ... LIMIT N), pushing a filter
21+
# below it changes query semantics: the limit should apply before the filter.
22+
23+
statement ok
24+
CREATE TABLE t(id INT, value INT) AS VALUES
25+
(1, 100),
26+
(2, 200),
27+
(3, 300),
28+
(4, 400),
29+
(5, 500);
30+
31+
# Correct result: first take the 3 smallest values (100, 200, 300),
32+
# then filter value > 200 → only (3, 300).
33+
#
34+
# Without the fix the filter is pushed below the sort+limit, producing
35+
# the 3 smallest values *after* filtering: (300, 400, 500) — wrong.
36+
query II
37+
SELECT * FROM (SELECT * FROM t ORDER BY value LIMIT 3) sub WHERE sub.value > 200;
38+
----
39+
3 300
40+
41+
# Same test with DESC ordering.
42+
# First take the 3 largest values (500, 400, 300), then filter value < 400.
43+
# Correct: (3, 300). Buggy: (1, 100), (2, 200), (3, 300).
44+
query II
45+
SELECT * FROM (SELECT * FROM t ORDER BY value DESC LIMIT 3) sub WHERE sub.value < 400;
46+
----
47+
3 300
48+
49+
# Verify the EXPLAIN plan keeps the filter above the sort+fetch.
50+
query TT
51+
EXPLAIN SELECT * FROM (SELECT * FROM t ORDER BY value LIMIT 3) sub WHERE sub.value > 200;
52+
----
53+
logical_plan
54+
01)SubqueryAlias: sub
55+
02)--Filter: t.value > Int32(200)
56+
03)----Sort: t.value ASC NULLS LAST, fetch=3
57+
04)------TableScan: t projection=[id, value]
58+
physical_plan
59+
01)FilterExec: value@1 > 200
60+
02)--SortExec: TopK(fetch=3), expr=[value@1 ASC NULLS LAST], preserve_partitioning=[false]
61+
03)----DataSourceExec: partitions=1, partition_sizes=[1]
62+
63+
statement ok
64+
DROP TABLE t;

0 commit comments

Comments
 (0)