Skip to content

Commit 34b94f0

Browse files
author
wiedld
committed
chore: move LIMIT+OFFSET tests to proper sqllogic test case
1 parent 30145cf commit 34b94f0

2 files changed

Lines changed: 143 additions & 127 deletions

File tree

datafusion/sqllogictest/test_files/limit.slt

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,3 +565,146 @@ physical_plan
565565

566566
statement ok
567567
drop table data;
568+
569+
570+
####################
571+
# Test issue: limit pushdown with offsets
572+
####################
573+
574+
statement ok
575+
CREATE EXTERNAL TABLE ordered_table (
576+
a0 INT,
577+
a INT,
578+
b INT,
579+
c INT UNSIGNED,
580+
d INT
581+
)
582+
STORED AS CSV
583+
WITH ORDER (c ASC)
584+
LOCATION '../core/tests/data/window_2.csv'
585+
OPTIONS ('format.has_header' 'true');
586+
587+
# all results
588+
query II
589+
SELECT b, sum(a) FROM ordered_table GROUP BY b order by b desc;
590+
----
591+
3 25
592+
2 25
593+
1 0
594+
0 0
595+
596+
# limit only
597+
query II
598+
SELECT b, sum(a) FROM ordered_table GROUP BY b order by b desc LIMIT 3;
599+
----
600+
3 25
601+
2 25
602+
1 0
603+
604+
# offset only
605+
query II
606+
SELECT b, sum(a) FROM ordered_table GROUP BY b order by b desc OFFSET 1;
607+
----
608+
2 25
609+
1 0
610+
0 0
611+
612+
# offset + limit
613+
query II
614+
SELECT b, sum(a) FROM ordered_table GROUP BY b order by b desc OFFSET 1 LIMIT 2;
615+
----
616+
2 25
617+
1 0
618+
619+
# Applying offset & limit when multiple streams from groupby
620+
query TT
621+
EXPLAIN SELECT b, sum(a) FROM ordered_table GROUP BY b order by b desc OFFSET 1 LIMIT 2;
622+
----
623+
logical_plan
624+
01)Limit: skip=1, fetch=2
625+
02)--Sort: ordered_table.b DESC NULLS FIRST, fetch=3
626+
03)----Aggregate: groupBy=[[ordered_table.b]], aggr=[[sum(CAST(ordered_table.a AS Int64))]]
627+
04)------TableScan: ordered_table projection=[a, b]
628+
physical_plan
629+
01)GlobalLimitExec: skip=1, fetch=2
630+
02)--SortPreservingMergeExec: [b@0 DESC], fetch=3
631+
03)----SortExec: TopK(fetch=3), expr=[b@0 DESC], preserve_partitioning=[true]
632+
04)------AggregateExec: mode=FinalPartitioned, gby=[b@0 as b], aggr=[sum(ordered_table.a)]
633+
05)--------CoalesceBatchesExec: target_batch_size=8192
634+
06)----------RepartitionExec: partitioning=Hash([b@0], 4), input_partitions=4
635+
07)------------AggregateExec: mode=Partial, gby=[b@1 as b], aggr=[sum(ordered_table.a)]
636+
08)--------------RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1
637+
09)----------------CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/core/tests/data/window_2.csv]]}, projection=[a, b], has_header=true
638+
639+
# Applying offset & limit when multiple streams from union
640+
query TT
641+
explain select * FROM (
642+
select c FROM ordered_table
643+
UNION ALL
644+
select d FROM ordered_table
645+
) order by 1 desc LIMIT 10 OFFSET 4;
646+
----
647+
logical_plan
648+
01)Limit: skip=4, fetch=10
649+
02)--Sort: ordered_table.c DESC NULLS FIRST, fetch=14
650+
03)----Union
651+
04)------Projection: CAST(ordered_table.c AS Int64) AS c
652+
05)--------TableScan: ordered_table projection=[c]
653+
06)------Projection: CAST(ordered_table.d AS Int64) AS c
654+
07)--------TableScan: ordered_table projection=[d]
655+
physical_plan
656+
01)GlobalLimitExec: skip=4, fetch=10
657+
02)--SortPreservingMergeExec: [c@0 DESC], fetch=14
658+
03)----UnionExec
659+
04)------SortExec: TopK(fetch=14), expr=[c@0 DESC], preserve_partitioning=[true]
660+
05)--------ProjectionExec: expr=[CAST(c@0 AS Int64) as c]
661+
06)----------RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1
662+
07)------------CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/core/tests/data/window_2.csv]]}, projection=[c], output_ordering=[c@0 ASC NULLS LAST], has_header=true
663+
08)------SortExec: TopK(fetch=14), expr=[c@0 DESC], preserve_partitioning=[true]
664+
09)--------ProjectionExec: expr=[CAST(d@0 AS Int64) as c]
665+
10)----------RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1
666+
11)------------CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/core/tests/data/window_2.csv]]}, projection=[d], has_header=true
667+
668+
# ApplyingmLIMIT & OFFSET to subquery.
669+
query III
670+
select t1.b, c, c2 FROM (
671+
select b, c FROM ordered_table ORDER BY b desc, c desc OFFSET 1 LIMIT 4
672+
) as t1 INNER JOIN (
673+
select b, c as c2 FROM ordered_table ORDER BY b desc, d desc OFFSET 1 LIMIT 4
674+
) as t2
675+
ON t1.b = t2.b
676+
ORDER BY t1.b desc, c desc, c2 desc;
677+
----
678+
3 98 96
679+
3 98 89
680+
3 98 82
681+
3 98 79
682+
3 97 96
683+
3 97 89
684+
3 97 82
685+
3 97 79
686+
3 96 96
687+
3 96 89
688+
3 96 82
689+
3 96 79
690+
3 95 96
691+
3 95 89
692+
3 95 82
693+
3 95 79
694+
695+
# Apply OFFSET & LIMIT to both parent and child (subquery).
696+
query III
697+
select t1.b, c, c2 FROM (
698+
select b, c FROM ordered_table ORDER BY b desc, c desc OFFSET 1 LIMIT 4
699+
) as t1 INNER JOIN (
700+
select b, c as c2 FROM ordered_table ORDER BY b desc, d desc OFFSET 1 LIMIT 4
701+
) as t2
702+
ON t1.b = t2.b
703+
ORDER BY t1.b desc, c desc, c2 desc
704+
OFFSET 3 LIMIT 2;
705+
----
706+
3 99 82
707+
3 99 79
708+
709+
statement ok
710+
drop table ordered_table;

datafusion/sqllogictest/test_files/order.slt

Lines changed: 0 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,130 +1196,3 @@ physical_plan
11961196
02)--RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
11971197
03)----SortExec: TopK(fetch=1), expr=[a@0 ASC NULLS LAST], preserve_partitioning=[false]
11981198
04)------CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/core/tests/data/window_2.csv]]}, projection=[a, b], has_header=true
1199-
1200-
1201-
####################
1202-
# Test issue: TBD
1203-
####################
1204-
1205-
# all results
1206-
query II
1207-
SELECT b, sum(a) FROM ordered_table GROUP BY b order by b desc;
1208-
----
1209-
3 25
1210-
2 25
1211-
1 0
1212-
0 0
1213-
1214-
# limit only
1215-
query II
1216-
SELECT b, sum(a) FROM ordered_table GROUP BY b order by b desc LIMIT 3;
1217-
----
1218-
3 25
1219-
2 25
1220-
1 0
1221-
1222-
# offset only
1223-
query II
1224-
SELECT b, sum(a) FROM ordered_table GROUP BY b order by b desc OFFSET 1;
1225-
----
1226-
2 25
1227-
1 0
1228-
0 0
1229-
1230-
# offset + limit
1231-
query II
1232-
SELECT b, sum(a) FROM ordered_table GROUP BY b order by b desc OFFSET 1 LIMIT 2;
1233-
----
1234-
2 25
1235-
1 0
1236-
1237-
# Applying offset & limit when multiple streams from groupby
1238-
query TT
1239-
EXPLAIN SELECT b, sum(a) FROM ordered_table GROUP BY b order by b desc OFFSET 1 LIMIT 2;
1240-
----
1241-
logical_plan
1242-
01)Limit: skip=1, fetch=2
1243-
02)--Sort: ordered_table.b DESC NULLS FIRST, fetch=3
1244-
03)----Aggregate: groupBy=[[ordered_table.b]], aggr=[[sum(CAST(ordered_table.a AS Int64))]]
1245-
04)------TableScan: ordered_table projection=[a, b]
1246-
physical_plan
1247-
01)GlobalLimitExec: skip=1, fetch=2
1248-
02)--SortPreservingMergeExec: [b@0 DESC], fetch=3
1249-
03)----SortExec: TopK(fetch=3), expr=[b@0 DESC], preserve_partitioning=[true]
1250-
04)------AggregateExec: mode=FinalPartitioned, gby=[b@0 as b], aggr=[sum(ordered_table.a)]
1251-
05)--------CoalesceBatchesExec: target_batch_size=8192
1252-
06)----------RepartitionExec: partitioning=Hash([b@0], 2), input_partitions=2
1253-
07)------------AggregateExec: mode=Partial, gby=[b@1 as b], aggr=[sum(ordered_table.a)]
1254-
08)--------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
1255-
09)----------------CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/core/tests/data/window_2.csv]]}, projection=[a, b], has_header=true
1256-
1257-
# Applying offset & limit when multiple streams from union
1258-
query TT
1259-
explain select * FROM (
1260-
select c FROM ordered_table
1261-
UNION ALL
1262-
select d FROM ordered_table
1263-
) order by 1 desc LIMIT 10 OFFSET 4;
1264-
----
1265-
logical_plan
1266-
01)Limit: skip=4, fetch=10
1267-
02)--Sort: ordered_table.c DESC NULLS FIRST, fetch=14
1268-
03)----Union
1269-
04)------Projection: CAST(ordered_table.c AS Int64) AS c
1270-
05)--------TableScan: ordered_table projection=[c]
1271-
06)------Projection: CAST(ordered_table.d AS Int64) AS c
1272-
07)--------TableScan: ordered_table projection=[d]
1273-
physical_plan
1274-
01)GlobalLimitExec: skip=4, fetch=10
1275-
02)--SortPreservingMergeExec: [c@0 DESC], fetch=14
1276-
03)----UnionExec
1277-
04)------SortExec: TopK(fetch=14), expr=[c@0 DESC], preserve_partitioning=[true]
1278-
05)--------ProjectionExec: expr=[CAST(c@0 AS Int64) as c]
1279-
06)----------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
1280-
07)------------CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/core/tests/data/window_2.csv]]}, projection=[c], output_ordering=[c@0 ASC NULLS LAST], has_header=true
1281-
08)------SortExec: TopK(fetch=14), expr=[c@0 DESC], preserve_partitioning=[true]
1282-
09)--------ProjectionExec: expr=[CAST(d@0 AS Int64) as c]
1283-
10)----------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
1284-
11)------------CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/core/tests/data/window_2.csv]]}, projection=[d], has_header=true
1285-
1286-
# ApplyingmLIMIT & OFFSET to subquery.
1287-
query III
1288-
select t1.b, c, c2 FROM (
1289-
select b, c FROM ordered_table ORDER BY b desc, c desc OFFSET 1 LIMIT 4
1290-
) as t1 INNER JOIN (
1291-
select b, c as c2 FROM ordered_table ORDER BY b desc, d desc OFFSET 1 LIMIT 4
1292-
) as t2
1293-
ON t1.b = t2.b
1294-
ORDER BY t1.b desc, c desc, c2 desc;
1295-
----
1296-
3 98 96
1297-
3 98 89
1298-
3 98 82
1299-
3 98 79
1300-
3 97 96
1301-
3 97 89
1302-
3 97 82
1303-
3 97 79
1304-
3 96 96
1305-
3 96 89
1306-
3 96 82
1307-
3 96 79
1308-
3 95 96
1309-
3 95 89
1310-
3 95 82
1311-
3 95 79
1312-
1313-
# Apply OFFSET & LIMIT to both parent and child (subquery).
1314-
query III
1315-
select t1.b, c, c2 FROM (
1316-
select b, c FROM ordered_table ORDER BY b desc, c desc OFFSET 1 LIMIT 4
1317-
) as t1 INNER JOIN (
1318-
select b, c as c2 FROM ordered_table ORDER BY b desc, d desc OFFSET 1 LIMIT 4
1319-
) as t2
1320-
ON t1.b = t2.b
1321-
ORDER BY t1.b desc, c desc, c2 desc
1322-
OFFSET 3 LIMIT 2;
1323-
----
1324-
3 99 82
1325-
3 99 79

0 commit comments

Comments
 (0)