forked from apache/datafusion
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_pushdown.slt
More file actions
2282 lines (1932 loc) · 74.6 KB
/
sort_pushdown.slt
File metadata and controls
2282 lines (1932 loc) · 74.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#Sort Pushdown for ordered Parquet files
statement ok
SET datafusion.execution.parquet.pushdown_filters = true;
statement ok
SET datafusion.optimizer.enable_sort_pushdown = true;
# Test 1: Sort Pushdown for ordered Parquet files
# Create a sorted dataset
statement ok
CREATE TABLE sorted_data(id INT, value INT, name VARCHAR) AS VALUES
(1, 100, 'a'),
(2, 200, 'b'),
(3, 300, 'c'),
(4, 400, 'd'),
(5, 500, 'e'),
(6, 600, 'f'),
(7, 700, 'g'),
(8, 800, 'h'),
(9, 900, 'i'),
(10, 1000, 'j');
# Copy to parquet with sorting
query I
COPY (SELECT * FROM sorted_data ORDER BY id ASC)
TO 'test_files/scratch/sort_pushdown/sorted_data.parquet';
----
10
statement ok
CREATE EXTERNAL TABLE sorted_parquet(id INT, value INT, name VARCHAR)
STORED AS PARQUET
LOCATION 'test_files/scratch/sort_pushdown/sorted_data.parquet'
WITH ORDER (id ASC);
# Test 1.1: Sort pushdown with DESC (opposite of ASC)
# Should show reverse_row_groups=true
query TT
EXPLAIN SELECT * FROM sorted_parquet ORDER BY id DESC LIMIT 3;
----
logical_plan
01)Sort: sorted_parquet.id DESC NULLS FIRST, fetch=3
02)--TableScan: sorted_parquet projection=[id, value, name]
physical_plan
01)SortExec: TopK(fetch=3), expr=[id@0 DESC], preserve_partitioning=[false]
02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/sort_pushdown/sorted_data.parquet]]}, projection=[id, value, name], file_type=parquet, predicate=DynamicFilter [ empty ], reverse_row_groups=true
# Test 1.2: Verify results are correct
query IIT
SELECT * FROM sorted_parquet ORDER BY id DESC LIMIT 3;
----
10 1000 j
9 900 i
8 800 h
# Test 1.3: Should NOT apply for ASC (same direction)
query TT
EXPLAIN SELECT * FROM sorted_parquet ORDER BY id ASC LIMIT 3;
----
logical_plan
01)Sort: sorted_parquet.id ASC NULLS LAST, fetch=3
02)--TableScan: sorted_parquet projection=[id, value, name]
physical_plan DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/sort_pushdown/sorted_data.parquet]]}, projection=[id, value, name], limit=3, output_ordering=[id@0 ASC NULLS LAST], file_type=parquet
# Test 1.4: Disable sort pushdown
statement ok
SET datafusion.optimizer.enable_sort_pushdown = false;
query TT
EXPLAIN SELECT * FROM sorted_parquet ORDER BY id DESC LIMIT 3;
----
logical_plan
01)Sort: sorted_parquet.id DESC NULLS FIRST, fetch=3
02)--TableScan: sorted_parquet projection=[id, value, name]
physical_plan
01)SortExec: TopK(fetch=3), expr=[id@0 DESC], preserve_partitioning=[false]
02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/sort_pushdown/sorted_data.parquet]]}, projection=[id, value, name], output_ordering=[id@0 ASC NULLS LAST], file_type=parquet, predicate=DynamicFilter [ empty ]
# Re-enable
statement ok
SET datafusion.optimizer.enable_sort_pushdown = true;
# Test 1.5: With OFFSET
query TT
EXPLAIN SELECT * FROM sorted_parquet ORDER BY id DESC LIMIT 3 OFFSET 2;
----
logical_plan
01)Limit: skip=2, fetch=3
02)--Sort: sorted_parquet.id DESC NULLS FIRST, fetch=5
03)----TableScan: sorted_parquet projection=[id, value, name]
physical_plan
01)GlobalLimitExec: skip=2, fetch=3
02)--SortExec: TopK(fetch=5), expr=[id@0 DESC], preserve_partitioning=[false]
03)----DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/sort_pushdown/sorted_data.parquet]]}, projection=[id, value, name], file_type=parquet, predicate=DynamicFilter [ empty ], reverse_row_groups=true
query IIT
SELECT * FROM sorted_parquet ORDER BY id DESC LIMIT 3 OFFSET 2;
----
8 800 h
7 700 g
6 600 f
# Test 1.6: Reverse scan with row selection (page index pruning)
# This tests that when reverse_row_groups=true, the RowSelection is also properly reversed
# Create a dataset with multiple row groups and enable page index
statement ok
CREATE TABLE multi_rg_data(id INT, category VARCHAR, value INT) AS VALUES
(1, 'alpha', 10),
(2, 'alpha', 20),
(3, 'beta', 30),
(4, 'beta', 40),
(5, 'gamma', 50),
(6, 'gamma', 60),
(7, 'delta', 70),
(8, 'delta', 80);
# Write with small row groups (2 rows each = 4 row groups)
statement ok
SET datafusion.execution.parquet.max_row_group_size = 2;
query I
COPY (SELECT * FROM multi_rg_data ORDER BY id ASC)
TO 'test_files/scratch/sort_pushdown/multi_rg_sorted.parquet';
----
8
# Reset row group size
statement ok
SET datafusion.execution.parquet.max_row_group_size = 1048576;
statement ok
CREATE EXTERNAL TABLE multi_rg_sorted(id INT, category VARCHAR, value INT)
STORED AS PARQUET
LOCATION 'test_files/scratch/sort_pushdown/multi_rg_sorted.parquet'
WITH ORDER (id ASC);
# Enable page index for better pruning
statement ok
SET datafusion.execution.parquet.enable_page_index = true;
statement ok
SET datafusion.execution.parquet.pushdown_filters = true;
# Test with reverse scan and filter that prunes some row groups
# This will create a RowSelection with partial row group scans
query TT
EXPLAIN SELECT * FROM multi_rg_sorted
WHERE category IN ('alpha', 'gamma')
ORDER BY id DESC LIMIT 5;
----
logical_plan
01)Sort: multi_rg_sorted.id DESC NULLS FIRST, fetch=5
02)--Filter: multi_rg_sorted.category = Utf8View("alpha") OR multi_rg_sorted.category = Utf8View("gamma")
03)----TableScan: multi_rg_sorted projection=[id, category, value], partial_filters=[multi_rg_sorted.category = Utf8View("alpha") OR multi_rg_sorted.category = Utf8View("gamma")]
physical_plan
01)SortExec: TopK(fetch=5), expr=[id@0 DESC], preserve_partitioning=[false]
02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/sort_pushdown/multi_rg_sorted.parquet]]}, projection=[id, category, value], file_type=parquet, predicate=(category@1 = alpha OR category@1 = gamma) AND DynamicFilter [ empty ], reverse_row_groups=true, pruning_predicate=category_null_count@2 != row_count@3 AND category_min@0 <= alpha AND alpha <= category_max@1 OR category_null_count@2 != row_count@3 AND category_min@0 <= gamma AND gamma <= category_max@1, required_guarantees=[category in (alpha, gamma)]
# Verify the results are correct despite reverse scanning with row selection
# Expected: gamma values (6, 5) then alpha values (2, 1), in DESC order by id
query ITI
SELECT * FROM multi_rg_sorted
WHERE category IN ('alpha', 'gamma')
ORDER BY id DESC LIMIT 5;
----
6 gamma 60
5 gamma 50
2 alpha 20
1 alpha 10
# Test with more complex selection pattern
query ITI
SELECT * FROM multi_rg_sorted
WHERE category IN ('beta', 'delta')
ORDER BY id DESC;
----
8 delta 80
7 delta 70
4 beta 40
3 beta 30
# Test forward scan for comparison (should give same logical results in ASC order)
query ITI
SELECT * FROM multi_rg_sorted
WHERE category IN ('alpha', 'gamma')
ORDER BY id ASC;
----
1 alpha 10
2 alpha 20
5 gamma 50
6 gamma 60
# Disable reverse scan and verify it still works
statement ok
SET datafusion.optimizer.enable_sort_pushdown = false;
query ITI
SELECT * FROM multi_rg_sorted
WHERE category IN ('alpha', 'gamma')
ORDER BY id DESC LIMIT 5;
----
6 gamma 60
5 gamma 50
2 alpha 20
1 alpha 10
# Re-enable
statement ok
SET datafusion.optimizer.enable_sort_pushdown = true;
# Test 1.7: Sort pushdown with more than one partition
# Create multiple parquet files to trigger it
# Split data into multiple files
statement ok
CREATE TABLE sorted_data_part1(id INT, value INT, name VARCHAR) AS VALUES
(1, 100, 'a'),
(2, 200, 'b'),
(3, 300, 'c');
statement ok
CREATE TABLE sorted_data_part2(id INT, value INT, name VARCHAR) AS VALUES
(4, 400, 'd'),
(5, 500, 'e'),
(6, 600, 'f');
statement ok
CREATE TABLE sorted_data_part3(id INT, value INT, name VARCHAR) AS VALUES
(7, 700, 'g'),
(8, 800, 'h'),
(9, 900, 'i'),
(10, 1000, 'j');
# Create directory for multi-file parquet
query I
COPY (SELECT * FROM sorted_data_part1 ORDER BY id ASC)
TO 'test_files/scratch/sort_pushdown/sorted_multi/part1.parquet';
----
3
query I
COPY (SELECT * FROM sorted_data_part2 ORDER BY id ASC)
TO 'test_files/scratch/sort_pushdown/sorted_multi/part2.parquet';
----
3
query I
COPY (SELECT * FROM sorted_data_part3 ORDER BY id ASC)
TO 'test_files/scratch/sort_pushdown/sorted_multi/part3.parquet';
----
4
# Create external table pointing to directory with multiple files
statement ok
CREATE EXTERNAL TABLE sorted_parquet_multi(id INT, value INT, name VARCHAR)
STORED AS PARQUET
LOCATION 'test_files/scratch/sort_pushdown/sorted_multi/'
WITH ORDER (id ASC);
# Enable multiple partitions
statement ok
SET datafusion.execution.target_partitions = 4;
# Now we should see RepartitionExec because we have 3 input partitions (3 files)
query TT
EXPLAIN SELECT * FROM sorted_parquet_multi ORDER BY id DESC LIMIT 3;
----
logical_plan
01)Sort: sorted_parquet_multi.id DESC NULLS FIRST, fetch=3
02)--TableScan: sorted_parquet_multi projection=[id, value, name]
physical_plan
01)SortPreservingMergeExec: [id@0 DESC], fetch=3
02)--SortExec: TopK(fetch=3), expr=[id@0 DESC], preserve_partitioning=[true]
03)----DataSourceExec: file_groups={3 groups: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/sort_pushdown/sorted_multi/part1.parquet], [WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/sort_pushdown/sorted_multi/part2.parquet], [WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/sort_pushdown/sorted_multi/part3.parquet]]}, projection=[id, value, name], file_type=parquet, predicate=DynamicFilter [ empty ], reverse_row_groups=true
# Verify correctness with repartitioning and multiple files
query IIT
SELECT * FROM sorted_parquet_multi ORDER BY id DESC LIMIT 3;
----
10 1000 j
9 900 i
8 800 h
# Test ASC order (should not trigger reverse scan)
query IIT
SELECT * FROM sorted_parquet_multi ORDER BY id ASC LIMIT 3;
----
1 100 a
2 200 b
3 300 c
# Cleanup
statement ok
DROP TABLE sorted_data_part1;
statement ok
DROP TABLE sorted_data_part2;
statement ok
DROP TABLE sorted_data_part3;
statement ok
DROP TABLE sorted_parquet_multi;
# Reset to default
statement ok
SET datafusion.execution.target_partitions = 4;
# Cleanup
statement ok
DROP TABLE multi_rg_data;
statement ok
DROP TABLE multi_rg_sorted;
statement ok
SET datafusion.execution.parquet.enable_page_index = false;
statement ok
SET datafusion.execution.parquet.pushdown_filters = true;
# Cleanup
statement ok
DROP TABLE sorted_data;
statement ok
DROP TABLE sorted_parquet;
statement ok
SET datafusion.optimizer.enable_sort_pushdown = true;
# Test 2: Sort pushdown with constant column filtering
# This tests the case where a leading sort column becomes constant through WHERE filtering
# Create a multi-column sorted dataset (like time-series data)
statement ok
CREATE TABLE timeseries_data(timeframe VARCHAR, period_end INT, value DOUBLE) AS VALUES
('daily', 1, 100.0),
('daily', 2, 150.0),
('daily', 3, 200.0),
('weekly', 1, 500.0),
('weekly', 2, 600.0),
('weekly', 3, 700.0),
('monthly', 1, 1000.0),
('monthly', 2, 1100.0),
('monthly', 3, 1200.0),
('quarterly', 1, 5000.0),
('quarterly', 2, 5500.0),
('quarterly', 3, 6000.0);
# Copy to parquet with multi-column sorting (timeframe ASC, period_end ASC)
query I
COPY (SELECT * FROM timeseries_data ORDER BY timeframe ASC, period_end ASC)
TO 'test_files/scratch/sort_pushdown/timeseries_sorted.parquet';
----
12
statement ok
CREATE EXTERNAL TABLE timeseries_parquet(timeframe VARCHAR, period_end INT, value DOUBLE)
STORED AS PARQUET
LOCATION 'test_files/scratch/sort_pushdown/timeseries_sorted.parquet'
WITH ORDER (timeframe ASC, period_end ASC);
# Test 2.1: Query with constant prefix filter and DESC on remaining column
# WHERE timeframe='quarterly' makes the first sort column constant
# ORDER BY period_end DESC should trigger reverse scan because:
# File ordering: [timeframe ASC, period_end ASC]
# After filtering timeframe='quarterly': effectively [period_end ASC]
# Request: [period_end DESC] -> exact reverse!
query TT
EXPLAIN SELECT * FROM timeseries_parquet
WHERE timeframe = 'quarterly'
ORDER BY period_end DESC
LIMIT 2;
----
logical_plan
01)Sort: timeseries_parquet.period_end DESC NULLS FIRST, fetch=2
02)--Filter: timeseries_parquet.timeframe = Utf8View("quarterly")
03)----TableScan: timeseries_parquet projection=[timeframe, period_end, value], partial_filters=[timeseries_parquet.timeframe = Utf8View("quarterly")]
physical_plan
01)SortExec: TopK(fetch=2), expr=[period_end@1 DESC], preserve_partitioning=[false]
02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/sort_pushdown/timeseries_sorted.parquet]]}, projection=[timeframe, period_end, value], file_type=parquet, predicate=timeframe@0 = quarterly AND DynamicFilter [ empty ], reverse_row_groups=true, pruning_predicate=timeframe_null_count@2 != row_count@3 AND timeframe_min@0 <= quarterly AND quarterly <= timeframe_max@1, required_guarantees=[timeframe in (quarterly)]
# Test 2.2: Verify the results are correct
query TIR
SELECT * FROM timeseries_parquet
WHERE timeframe = 'quarterly'
ORDER BY period_end DESC
LIMIT 2;
----
quarterly 3 6000
quarterly 2 5500
# Test 2.3: Same filter but ASC order (should not trigger reverse scan, ordering already satisfied)
query TT
EXPLAIN SELECT * FROM timeseries_parquet
WHERE timeframe = 'quarterly'
ORDER BY period_end ASC
LIMIT 2;
----
logical_plan
01)Sort: timeseries_parquet.period_end ASC NULLS LAST, fetch=2
02)--Filter: timeseries_parquet.timeframe = Utf8View("quarterly")
03)----TableScan: timeseries_parquet projection=[timeframe, period_end, value], partial_filters=[timeseries_parquet.timeframe = Utf8View("quarterly")]
physical_plan DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/sort_pushdown/timeseries_sorted.parquet]]}, projection=[timeframe, period_end, value], limit=2, output_ordering=[timeframe@0 ASC NULLS LAST, period_end@1 ASC NULLS LAST], file_type=parquet, predicate=timeframe@0 = quarterly, pruning_predicate=timeframe_null_count@2 != row_count@3 AND timeframe_min@0 <= quarterly AND quarterly <= timeframe_max@1, required_guarantees=[timeframe in (quarterly)]
# Test 2.4: Verify ASC results
query TIR
SELECT * FROM timeseries_parquet
WHERE timeframe = 'quarterly'
ORDER BY period_end ASC
LIMIT 2;
----
quarterly 1 5000
quarterly 2 5500
# Test 2.5: Test with different constant value
query TIR
SELECT * FROM timeseries_parquet
WHERE timeframe = 'weekly'
ORDER BY period_end DESC;
----
weekly 3 700
weekly 2 600
weekly 1 500
# Test 2.6: Test without constant filter (no reverse scan because need both columns)
# Request: [timeframe ASC, period_end DESC]
# File has: [timeframe ASC, period_end ASC]
# These are NOT reverse of each other - only second column is reversed
query TT
EXPLAIN SELECT * FROM timeseries_parquet
ORDER BY timeframe ASC, period_end DESC
LIMIT 3;
----
logical_plan
01)Sort: timeseries_parquet.timeframe ASC NULLS LAST, timeseries_parquet.period_end DESC NULLS FIRST, fetch=3
02)--TableScan: timeseries_parquet projection=[timeframe, period_end, value]
physical_plan
01)SortExec: TopK(fetch=3), expr=[timeframe@0 ASC NULLS LAST, period_end@1 DESC], preserve_partitioning=[false], sort_prefix=[timeframe@0 ASC NULLS LAST]
02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/sort_pushdown/timeseries_sorted.parquet]]}, projection=[timeframe, period_end, value], output_ordering=[timeframe@0 ASC NULLS LAST, period_end@1 ASC NULLS LAST], file_type=parquet, predicate=DynamicFilter [ empty ]
# Test 2.7: Disable sort pushdown and verify filter still works
statement ok
SET datafusion.optimizer.enable_sort_pushdown = false;
query TT
EXPLAIN SELECT * FROM timeseries_parquet
WHERE timeframe = 'quarterly'
ORDER BY period_end DESC
LIMIT 2;
----
logical_plan
01)Sort: timeseries_parquet.period_end DESC NULLS FIRST, fetch=2
02)--Filter: timeseries_parquet.timeframe = Utf8View("quarterly")
03)----TableScan: timeseries_parquet projection=[timeframe, period_end, value], partial_filters=[timeseries_parquet.timeframe = Utf8View("quarterly")]
physical_plan
01)SortExec: TopK(fetch=2), expr=[period_end@1 DESC], preserve_partitioning=[false]
02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/sort_pushdown/timeseries_sorted.parquet]]}, projection=[timeframe, period_end, value], output_ordering=[timeframe@0 ASC NULLS LAST, period_end@1 ASC NULLS LAST], file_type=parquet, predicate=timeframe@0 = quarterly AND DynamicFilter [ empty ], pruning_predicate=timeframe_null_count@2 != row_count@3 AND timeframe_min@0 <= quarterly AND quarterly <= timeframe_max@1, required_guarantees=[timeframe in (quarterly)]
# Results should still be correct
query TIR
SELECT * FROM timeseries_parquet
WHERE timeframe = 'quarterly'
ORDER BY period_end DESC
LIMIT 2;
----
quarterly 3 6000
quarterly 2 5500
# Re-enable
statement ok
SET datafusion.optimizer.enable_sort_pushdown = true;
# Test 2.8: Test with IN clause (multiple constant values)
# Note: IN clause with multiple values means timeframe is NOT constant
# (could be 'daily' or 'weekly'), so the first sort column cannot be eliminated.
# Without a constant first column, we cannot reverse scan based on just period_end DESC.
# The physical plan should NOT show reverse_row_groups=true
query TT
EXPLAIN SELECT * FROM timeseries_parquet
WHERE timeframe IN ('daily', 'weekly')
ORDER BY period_end DESC
LIMIT 3;
----
logical_plan
01)Sort: timeseries_parquet.period_end DESC NULLS FIRST, fetch=3
02)--Filter: timeseries_parquet.timeframe = Utf8View("daily") OR timeseries_parquet.timeframe = Utf8View("weekly")
03)----TableScan: timeseries_parquet projection=[timeframe, period_end, value], partial_filters=[timeseries_parquet.timeframe = Utf8View("daily") OR timeseries_parquet.timeframe = Utf8View("weekly")]
physical_plan
01)SortExec: TopK(fetch=3), expr=[period_end@1 DESC], preserve_partitioning=[false]
02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/sort_pushdown/timeseries_sorted.parquet]]}, projection=[timeframe, period_end, value], output_ordering=[timeframe@0 ASC NULLS LAST, period_end@1 ASC NULLS LAST], file_type=parquet, predicate=(timeframe@0 = daily OR timeframe@0 = weekly) AND DynamicFilter [ empty ], pruning_predicate=timeframe_null_count@2 != row_count@3 AND timeframe_min@0 <= daily AND daily <= timeframe_max@1 OR timeframe_null_count@2 != row_count@3 AND timeframe_min@0 <= weekly AND weekly <= timeframe_max@1, required_guarantees=[timeframe in (daily, weekly)]
# Test 2.9: Complex case - literal constant in sort expression itself
# The literal 'constant' is ignored in sort analysis
# After stripping: ORDER BY period_end DESC
# With WHERE timeframe='monthly' making first column constant
# File: [period_end ASC] (after constant column removal)
# Request: [period_end DESC] -> exact reverse, triggers reverse scan
query TT
EXPLAIN SELECT * FROM timeseries_parquet
WHERE timeframe = 'monthly'
ORDER BY 'constant', period_end DESC
LIMIT 2;
----
logical_plan
01)Sort: Utf8("constant") ASC NULLS LAST, timeseries_parquet.period_end DESC NULLS FIRST, fetch=2
02)--Filter: timeseries_parquet.timeframe = Utf8View("monthly")
03)----TableScan: timeseries_parquet projection=[timeframe, period_end, value], partial_filters=[timeseries_parquet.timeframe = Utf8View("monthly")]
physical_plan
01)SortExec: TopK(fetch=2), expr=[period_end@1 DESC], preserve_partitioning=[false]
02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/sort_pushdown/timeseries_sorted.parquet]]}, projection=[timeframe, period_end, value], file_type=parquet, predicate=timeframe@0 = monthly AND DynamicFilter [ empty ], reverse_row_groups=true, pruning_predicate=timeframe_null_count@2 != row_count@3 AND timeframe_min@0 <= monthly AND monthly <= timeframe_max@1, required_guarantees=[timeframe in (monthly)]
# Verify results
query TIR
SELECT * FROM timeseries_parquet
WHERE timeframe = 'monthly'
ORDER BY period_end DESC
LIMIT 2;
----
monthly 3 1200
monthly 2 1100
# Test 2.10: Filter on non-leading sort column
# File order: [timeframe ASC, period_end ASC]
# Filter: period_end = 2 (makes second column constant)
# Request: [timeframe DESC]
# After constant column removal: File has [timeframe ASC], Request wants [timeframe DESC]
# This is exact reverse -> triggers reverse scan
query TT
EXPLAIN SELECT * FROM timeseries_parquet
WHERE period_end = 2
ORDER BY timeframe DESC;
----
logical_plan
01)Sort: timeseries_parquet.timeframe DESC NULLS FIRST
02)--Filter: timeseries_parquet.period_end = Int32(2)
03)----TableScan: timeseries_parquet projection=[timeframe, period_end, value], partial_filters=[timeseries_parquet.period_end = Int32(2)]
physical_plan
01)SortExec: expr=[timeframe@0 DESC], preserve_partitioning=[false]
02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/sort_pushdown/timeseries_sorted.parquet]]}, projection=[timeframe, period_end, value], file_type=parquet, predicate=period_end@1 = 2, reverse_row_groups=true, pruning_predicate=period_end_null_count@2 != row_count@3 AND period_end_min@0 <= 2 AND 2 <= period_end_max@1, required_guarantees=[period_end in (2)]
# Cleanup
statement ok
DROP TABLE timeseries_data;
statement ok
DROP TABLE timeseries_parquet;
# Reset to default
statement ok
SET datafusion.optimizer.enable_sort_pushdown = true;
# Test 3: Sort pushdown with monotonic functions
# This tests that reverse scan works when sort expressions involve monotonic functions
# Create test data with timestamp column
statement ok
CREATE TABLE timestamp_data(id INT, ts TIMESTAMP, volume BIGINT, price DOUBLE) AS VALUES
(1, TIMESTAMP '2024-01-15 10:00:00', 1000, 100.0),
(2, TIMESTAMP '2024-01-20 11:00:00', 1500, 105.0),
(3, TIMESTAMP '2024-01-25 12:00:00', 2000, 110.0),
(4, TIMESTAMP '2024-02-05 09:00:00', 1200, 108.0),
(5, TIMESTAMP '2024-02-15 14:00:00', 1800, 112.0),
(6, TIMESTAMP '2024-02-25 15:00:00', 2200, 115.0),
(7, TIMESTAMP '2024-03-10 09:00:00', 1300, 113.0),
(8, TIMESTAMP '2024-03-18 14:00:00', 1900, 118.0),
(9, TIMESTAMP '2024-03-28 15:00:00', 2300, 120.0);
# Copy to parquet with sorting by timestamp ASC
query I
COPY (SELECT * FROM timestamp_data ORDER BY ts ASC)
TO 'test_files/scratch/sort_pushdown/timestamp_sorted.parquet';
----
9
# Test 3.1: Simple monotonic function - date_trunc
# Create external table with file ordering that conceptually includes date_trunc
# File is actually sorted by [ts ASC], but conceptually [date_trunc('month', ts) ASC, ts ASC]
statement ok
CREATE EXTERNAL TABLE timestamp_parquet(id INT, ts TIMESTAMP, volume BIGINT, price DOUBLE)
STORED AS PARQUET
LOCATION 'test_files/scratch/sort_pushdown/timestamp_sorted.parquet'
WITH ORDER (ts ASC);
# Query with ORDER BY ts DESC
# File ordering: [ts ASC]
# Request: [ts DESC]
# This should trigger reverse_row_groups=true
query TT
EXPLAIN SELECT * FROM timestamp_parquet
ORDER BY ts DESC
LIMIT 3;
----
logical_plan
01)Sort: timestamp_parquet.ts DESC NULLS FIRST, fetch=3
02)--TableScan: timestamp_parquet projection=[id, ts, volume, price]
physical_plan
01)SortExec: TopK(fetch=3), expr=[ts@1 DESC], preserve_partitioning=[false]
02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/sort_pushdown/timestamp_sorted.parquet]]}, projection=[id, ts, volume, price], file_type=parquet, predicate=DynamicFilter [ empty ], reverse_row_groups=true
# Verify results
query IPIR
SELECT * FROM timestamp_parquet
ORDER BY ts DESC
LIMIT 3;
----
9 2024-03-28T15:00:00 2300 120
8 2024-03-18T14:00:00 1900 118
7 2024-03-10T09:00:00 1300 113
# Test 3.2: Monotonic function in ORDER BY - date_trunc DESC
# File ordering: [ts ASC]
# Request: [date_trunc('day', ts) DESC]
# Since date_trunc is monotonic with ts, reversed file ordering [ts DESC] satisfies [date_trunc DESC]
query TT
EXPLAIN SELECT * FROM timestamp_parquet
ORDER BY date_trunc('day', ts) DESC
LIMIT 3;
----
logical_plan
01)Sort: date_trunc(Utf8("day"), timestamp_parquet.ts) DESC NULLS FIRST, fetch=3
02)--TableScan: timestamp_parquet projection=[id, ts, volume, price]
physical_plan
01)SortExec: TopK(fetch=3), expr=[date_trunc(day, ts@1) DESC], preserve_partitioning=[false]
02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/sort_pushdown/timestamp_sorted.parquet]]}, projection=[id, ts, volume, price], file_type=parquet, predicate=DynamicFilter [ empty ], reverse_row_groups=true
# Verify results (descending day)
query IPIR
SELECT * FROM timestamp_parquet
ORDER BY date_trunc('day', ts) DESC
LIMIT 3;
----
9 2024-03-28T15:00:00 2300 120
8 2024-03-18T14:00:00 1900 118
7 2024-03-10T09:00:00 1300 113
# Test 3.3: Multi-column scenario with explicit monotonic function in file ordering
# Create a table where we explicitly declare the ordering includes the monotonic function
# This simulates files that are partitioned/sorted by [date_trunc('month', ts) ASC, ts ASC]
# Create a new parquet file sorted by [ts ASC] (which implies date_trunc ordering)
statement ok
CREATE TABLE multi_month_data(id INT, ts TIMESTAMP, volume BIGINT, price DOUBLE) AS VALUES
-- January 2024
(1, TIMESTAMP '2024-01-05 09:30:00', 1000, 100.0),
(2, TIMESTAMP '2024-01-15 14:30:00', 1500, 105.0),
(3, TIMESTAMP '2024-01-25 15:59:00', 2000, 110.0),
-- February 2024
(4, TIMESTAMP '2024-02-03 09:30:00', 1200, 108.0),
(5, TIMESTAMP '2024-02-14 12:00:00', 1800, 112.0),
(6, TIMESTAMP '2024-02-28 15:59:00', 2200, 115.0),
-- March 2024
(7, TIMESTAMP '2024-03-01 09:30:00', 1300, 113.0),
(8, TIMESTAMP '2024-03-15 14:00:00', 1900, 118.0),
(9, TIMESTAMP '2024-03-29 15:59:00', 2300, 120.0);
query I
COPY (SELECT * FROM multi_month_data ORDER BY ts ASC)
TO 'test_files/scratch/sort_pushdown/multi_month_sorted.parquet';
----
9
# Declare the file has ordering [ts ASC]
# Conceptually this means [date_trunc('month', ts) ASC, ts ASC] due to monotonicity
statement ok
CREATE EXTERNAL TABLE multi_month_parquet(id INT, ts TIMESTAMP, volume BIGINT, price DOUBLE)
STORED AS PARQUET
LOCATION 'test_files/scratch/sort_pushdown/multi_month_sorted.parquet'
WITH ORDER (ts ASC);
# Test 3.3a: Request ORDER BY ts DESC (opposite direction)
# File: [ts ASC]
# Request: [ts DESC]
# Should trigger reverse_row_groups=true
query TT
EXPLAIN SELECT * FROM multi_month_parquet
ORDER BY ts DESC
LIMIT 2;
----
logical_plan
01)Sort: multi_month_parquet.ts DESC NULLS FIRST, fetch=2
02)--TableScan: multi_month_parquet projection=[id, ts, volume, price]
physical_plan
01)SortExec: TopK(fetch=2), expr=[ts@1 DESC], preserve_partitioning=[false]
02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/sort_pushdown/multi_month_sorted.parquet]]}, projection=[id, ts, volume, price], file_type=parquet, predicate=DynamicFilter [ empty ], reverse_row_groups=true
query IPIR
SELECT * FROM multi_month_parquet
ORDER BY ts DESC
LIMIT 2;
----
9 2024-03-29T15:59:00 2300 120
8 2024-03-15T14:00:00 1900 118
# Test 3.3b: Request ORDER BY date_trunc('month', ts) DESC, ts DESC
# File: [ts ASC] (which implies [date_trunc('month', ts) ASC, ts ASC])
# Request: [date_trunc('month', ts) DESC, ts DESC]
# The reversed file ordering [ts DESC] satisfies this because:
# - date_trunc is monotonic with ts
# - So [ts DESC] implies [date_trunc('month', ts) DESC, ts DESC]
query TT
EXPLAIN SELECT * FROM multi_month_parquet
ORDER BY date_trunc('month', ts) DESC, ts DESC
LIMIT 2;
----
logical_plan
01)Sort: date_trunc(Utf8("month"), multi_month_parquet.ts) DESC NULLS FIRST, multi_month_parquet.ts DESC NULLS FIRST, fetch=2
02)--TableScan: multi_month_parquet projection=[id, ts, volume, price]
physical_plan
01)SortExec: TopK(fetch=2), expr=[date_trunc(month, ts@1) DESC, ts@1 DESC], preserve_partitioning=[false]
02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/sort_pushdown/multi_month_sorted.parquet]]}, projection=[id, ts, volume, price], file_type=parquet, predicate=DynamicFilter [ empty ], reverse_row_groups=true
query IPIR
SELECT * FROM multi_month_parquet
ORDER BY date_trunc('month', ts) DESC, ts DESC
LIMIT 2;
----
9 2024-03-29T15:59:00 2300 120
8 2024-03-15T14:00:00 1900 118
# Test 3.4: CAST as a monotonic function
statement ok
CREATE TABLE int_data(id INT, small_val SMALLINT, big_val BIGINT) AS VALUES
(1, 10, 100),
(2, 20, 200),
(3, 30, 300),
(4, 40, 400),
(5, 50, 500);
query I
COPY (SELECT * FROM int_data ORDER BY small_val ASC)
TO 'test_files/scratch/sort_pushdown/int_sorted.parquet';
----
5
statement ok
CREATE EXTERNAL TABLE int_parquet(id INT, small_val SMALLINT, big_val BIGINT)
STORED AS PARQUET
LOCATION 'test_files/scratch/sort_pushdown/int_sorted.parquet'
WITH ORDER (small_val ASC);
# CAST preserves ordering: CAST(small_val AS BIGINT) is monotonic with small_val
query TT
EXPLAIN SELECT * FROM int_parquet
ORDER BY CAST(small_val AS BIGINT) DESC
LIMIT 2;
----
logical_plan
01)Sort: CAST(int_parquet.small_val AS Int64) DESC NULLS FIRST, fetch=2
02)--TableScan: int_parquet projection=[id, small_val, big_val]
physical_plan
01)SortExec: TopK(fetch=2), expr=[CAST(small_val@1 AS Int64) DESC], preserve_partitioning=[false]
02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/sort_pushdown/int_sorted.parquet]]}, projection=[id, small_val, big_val], file_type=parquet, predicate=DynamicFilter [ empty ], reverse_row_groups=true
query III
SELECT * FROM int_parquet
ORDER BY CAST(small_val AS BIGINT) DESC
LIMIT 2;
----
5 50 500
4 40 400
# Test 3.5: CEIL as a monotonic function
statement ok
CREATE TABLE float_data(id INT, value DOUBLE) AS VALUES
(1, 1.1),
(2, 2.3),
(3, 3.5),
(4, 4.7),
(5, 5.9);
query I
COPY (SELECT * FROM float_data ORDER BY value ASC)
TO 'test_files/scratch/sort_pushdown/float_sorted.parquet';
----
5
statement ok
CREATE EXTERNAL TABLE float_parquet(id INT, value DOUBLE)
STORED AS PARQUET
LOCATION 'test_files/scratch/sort_pushdown/float_sorted.parquet'
WITH ORDER (value ASC);
# CEIL is monotonic increasing
query TT
EXPLAIN SELECT * FROM float_parquet
ORDER BY CEIL(value) DESC
LIMIT 3;
----
logical_plan
01)Sort: ceil(float_parquet.value) DESC NULLS FIRST, fetch=3
02)--TableScan: float_parquet projection=[id, value]
physical_plan
01)SortExec: TopK(fetch=3), expr=[ceil(value@1) DESC], preserve_partitioning=[false]
02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/sort_pushdown/float_sorted.parquet]]}, projection=[id, value], file_type=parquet, predicate=DynamicFilter [ empty ], reverse_row_groups=true
query IR
SELECT * FROM float_parquet
ORDER BY CEIL(value) DESC
LIMIT 3;
----
5 5.9
4 4.7
3 3.5
# Test 3.6: Negative case - ABS is NOT monotonic over mixed positive/negative range
statement ok
CREATE TABLE signed_data(id INT, value DOUBLE) AS VALUES
(1, -5.0),
(2, -3.0),
(3, -1.0),
(4, 2.0),
(5, 4.0);
query I
COPY (SELECT * FROM signed_data ORDER BY value ASC)
TO 'test_files/scratch/sort_pushdown/signed_sorted.parquet';
----
5
statement ok
CREATE EXTERNAL TABLE signed_parquet(id INT, value DOUBLE)
STORED AS PARQUET
LOCATION 'test_files/scratch/sort_pushdown/signed_sorted.parquet'
WITH ORDER (value ASC);
# ABS is NOT monotonic over the full range [-5, 4], so should NOT trigger reverse scan
query TT
EXPLAIN SELECT * FROM signed_parquet
ORDER BY ABS(value) DESC
LIMIT 3;
----
logical_plan
01)Sort: abs(signed_parquet.value) DESC NULLS FIRST, fetch=3
02)--TableScan: signed_parquet projection=[id, value]
physical_plan
01)SortExec: TopK(fetch=3), expr=[abs(value@1) DESC], preserve_partitioning=[false]
02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/sort_pushdown/signed_sorted.parquet]]}, projection=[id, value], output_ordering=[value@1 ASC NULLS LAST], file_type=parquet, predicate=DynamicFilter [ empty ]
# Results should still be correct (no optimization applied)
query IR
SELECT * FROM signed_parquet
ORDER BY ABS(value) DESC
LIMIT 3;
----
1 -5
5 4
2 -3
# Test 3.7: Aggregate ORDER BY expression should keep SortExec
# Source pattern declared on parquet scan: [x ASC, y ASC].
# Requested pattern in ORDER BY: [x ASC, CAST(y AS BIGINT) % 2 ASC].
# Example for x=1 input y order 1,2,3 gives bucket order 1,0,1, which does not
# match requested bucket ASC order. SortExec is required above AggregateExec.
statement ok
SET datafusion.execution.target_partitions = 1;
statement ok
CREATE TABLE agg_expr_data(x INT, y INT, v INT) AS VALUES
(1, 1, 10),
(1, 2, 20),
(1, 3, 30),
(2, 1, 40),
(2, 2, 50),
(2, 3, 60);
query I
COPY (SELECT * FROM agg_expr_data ORDER BY x, y)
TO 'test_files/scratch/sort_pushdown/agg_expr_sorted.parquet';
----
6
statement ok
CREATE EXTERNAL TABLE agg_expr_parquet(x INT, y INT, v INT)
STORED AS PARQUET
LOCATION 'test_files/scratch/sort_pushdown/agg_expr_sorted.parquet'
WITH ORDER (x ASC, y ASC);
query TT
EXPLAIN SELECT
x,
CAST(y AS BIGINT) % 2,
SUM(v)
FROM agg_expr_parquet
GROUP BY x, CAST(y AS BIGINT) % 2
ORDER BY x, CAST(y AS BIGINT) % 2;
----
logical_plan
01)Sort: agg_expr_parquet.x ASC NULLS LAST, agg_expr_parquet.y % Int64(2) ASC NULLS LAST
02)--Aggregate: groupBy=[[agg_expr_parquet.x, CAST(agg_expr_parquet.y AS Int64) % Int64(2)]], aggr=[[sum(CAST(agg_expr_parquet.v AS Int64))]]
03)----TableScan: agg_expr_parquet projection=[x, y, v]
physical_plan
01)SortExec: expr=[x@0 ASC NULLS LAST, agg_expr_parquet.y % Int64(2)@1 ASC NULLS LAST], preserve_partitioning=[false]
02)--AggregateExec: mode=Single, gby=[x@0 as x, CAST(y@1 AS Int64) % 2 as agg_expr_parquet.y % Int64(2)], aggr=[sum(agg_expr_parquet.v)], ordering_mode=PartiallySorted([0])
03)----DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/sort_pushdown/agg_expr_sorted.parquet]]}, projection=[x, y, v], output_ordering=[x@0 ASC NULLS LAST, y@1 ASC NULLS LAST], file_type=parquet
# Expected output pattern from ORDER BY [x, bucket]:
# rows grouped by x, and within each x bucket appears as 0 then 1.
query III
SELECT
x,
CAST(y AS BIGINT) % 2,
SUM(v)
FROM agg_expr_parquet
GROUP BY x, CAST(y AS BIGINT) % 2
ORDER BY x, CAST(y AS BIGINT) % 2;
----
1 0 20
1 1 40
2 0 50
2 1 100
# Test 3.8: Aggregate ORDER BY monotonic expression can push down (no SortExec)
query TT
EXPLAIN SELECT
x,
CAST(y AS BIGINT),
SUM(v)
FROM agg_expr_parquet
GROUP BY x, CAST(y AS BIGINT)
ORDER BY x, CAST(y AS BIGINT);
----
logical_plan
01)Sort: agg_expr_parquet.x ASC NULLS LAST, agg_expr_parquet.y ASC NULLS LAST
02)--Aggregate: groupBy=[[agg_expr_parquet.x, CAST(agg_expr_parquet.y AS Int64)]], aggr=[[sum(CAST(agg_expr_parquet.v AS Int64))]]
03)----TableScan: agg_expr_parquet projection=[x, y, v]
physical_plan
01)AggregateExec: mode=Single, gby=[x@0 as x, CAST(y@1 AS Int64) as agg_expr_parquet.y], aggr=[sum(agg_expr_parquet.v)], ordering_mode=Sorted
02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/sort_pushdown/agg_expr_sorted.parquet]]}, projection=[x, y, v], output_ordering=[x@0 ASC NULLS LAST, y@1 ASC NULLS LAST], file_type=parquet
query III
SELECT
x,
CAST(y AS BIGINT),
SUM(v)
FROM agg_expr_parquet
GROUP BY x, CAST(y AS BIGINT)
ORDER BY x, CAST(y AS BIGINT);
----
1 1 10
1 2 20
1 3 30
2 1 40
2 2 50
2 3 60
# Test 3.9: Aggregate ORDER BY aggregate output should keep SortExec
query TT
EXPLAIN SELECT x, SUM(v)
FROM agg_expr_parquet
GROUP BY x
ORDER BY SUM(v);
----
logical_plan
01)Sort: sum(agg_expr_parquet.v) ASC NULLS LAST
02)--Aggregate: groupBy=[[agg_expr_parquet.x]], aggr=[[sum(CAST(agg_expr_parquet.v AS Int64))]]
03)----TableScan: agg_expr_parquet projection=[x, v]
physical_plan
01)SortExec: expr=[sum(agg_expr_parquet.v)@1 ASC NULLS LAST], preserve_partitioning=[false]
02)--AggregateExec: mode=Single, gby=[x@0 as x], aggr=[sum(agg_expr_parquet.v)], ordering_mode=Sorted
03)----DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/sort_pushdown/agg_expr_sorted.parquet]]}, projection=[x, v], output_ordering=[x@0 ASC NULLS LAST], file_type=parquet
query II
SELECT x, SUM(v)
FROM agg_expr_parquet
GROUP BY x
ORDER BY SUM(v);
----
1 60
2 150
# Test 3.10: Aggregate with non-preserved input order should keep SortExec
# v is not part of the order by
query TT
EXPLAIN SELECT v, SUM(y)
FROM agg_expr_parquet
GROUP BY v
ORDER BY v;
----
logical_plan
01)Sort: agg_expr_parquet.v ASC NULLS LAST
02)--Aggregate: groupBy=[[agg_expr_parquet.v]], aggr=[[sum(CAST(agg_expr_parquet.y AS Int64))]]
03)----TableScan: agg_expr_parquet projection=[y, v]
physical_plan
01)SortExec: expr=[v@0 ASC NULLS LAST], preserve_partitioning=[false]
02)--AggregateExec: mode=Single, gby=[v@1 as v], aggr=[sum(agg_expr_parquet.y)]
03)----DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/sort_pushdown/agg_expr_sorted.parquet]]}, projection=[y, v], file_type=parquet
query II
SELECT v, SUM(y)
FROM agg_expr_parquet
GROUP BY v
ORDER BY v;
----
10 1
20 2