algeon-datafusion 0.1.2

DataFusion physical-plan adapter for native GPU execution.
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
use std::{fs, sync::Arc};

use algeon_datafusion::{
    backend::{
        AlgeonGpuBackend, AlgeonGpuDeviceConfig, AlgeonGpuDeviceProfile, AlgeonGpuQueuePolicy, GpuDeviceMemoryLimit,
        GpuMemoryOwnership,
    },
    native_exec::{AlgeonNativeExec, AlgeonNativeExecutionConfig, SourceChunking, below_native_admission},
    planner::{AlgeonGpuExecutionMode, AlgeonNativeOptimizerConfig, AlgeonNativePhysicalOptimizerRule},
    report::PlanningReport,
};
use arrow::{
    array::{ArrayRef, Decimal128Array},
    compute::SortOptions as ArrowSortOptions,
    datatypes::{DataType, Field, Schema},
    record_batch::RecordBatch,
    util::display::array_value_to_string,
};
use cudf::{
    testing,
    testing::arrow::{
        arrow_host_export_call_count_for_test, arrow_host_import_call_count_for_test,
        reset_arrow_host_interop_observation_for_test,
    },
};
use datafusion::{
    common::ScalarValue,
    config::ConfigOptions,
    datasource::{
        physical_plan::{
            FileGroup, FileScanConfig, FileScanConfigBuilder, ParquetSource,
            parquet::{ParquetAccessPlan, RowGroupAccess},
        },
        source::DataSourceExec,
    },
    execution::{SessionStateBuilder, context::TaskContext},
    functions_aggregate::{count::count_udaf, sum::sum_udaf},
    logical_expr::Operator,
    parquet::file::reader::{FileReader, SerializedFileReader},
    physical_expr::{
        LexOrdering, PhysicalExpr, PhysicalSortExpr,
        aggregate::AggregateExprBuilder,
        expressions::{CastExpr, NotExpr, binary, col, lit},
    },
    physical_plan::{
        ExecutionPlan,
        aggregates::{AggregateExec, AggregateMode, PhysicalGroupBy},
        collect, displayable,
        filter::FilterExec,
        limit::LocalLimitExec,
        metrics::MetricsSet,
        projection::{ProjectionExec, ProjectionExpr},
        sorts::sort::SortExec,
    },
    prelude::{ParquetReadOptions, SessionConfig, SessionContext},
};
use fixtures::*;
use query_planning::{optimizer::NativeOptimizerDiagnostics, source::SourceDiagnostics};
use sql::*;
use support::*;
use tokio::{runtime::Builder as TestRuntimeBuilder, sync::Mutex};
use tpch_session::tpch_report_session_config;
use workloads::tpch::{TPCH_QUERIES, TPCH_TABLES, write_tiny_tpch_parquet_dataset};

static ARROW_HOST_OBSERVATION_LOCK: Mutex<()> = Mutex::const_new(());

#[path = "../support/tpch_session.rs"]
mod tpch_session;

mod datafusion_exec;
mod fixtures;
mod scalar_expressions;
mod sql;
mod support;
mod tpch;

async fn native_runtime_default_sql_min_max_count_matches_cpu() {
    let dir = unique_temp_dir("algeon-datafusion-native-runtime-default-min-max-count");
    let path = dir.join("input.parquet");
    write_row_group_pruning_parquet_file(&path);

    assert_default_sql_native_parity(&path, SQL_MIN_MAX_COUNT_QUERY, None).await;

    fs::remove_dir_all(dir).unwrap();
}

async fn native_runtime_default_sql_count_star_matches_cpu() {
    let dir = unique_temp_dir("algeon-datafusion-native-runtime-default-count-star");
    let path = dir.join("input.parquet");
    write_row_group_pruning_parquet_file(&path);

    assert_default_sql_native_parity(&path, SQL_COUNT_STAR_QUERY, None).await;

    fs::remove_dir_all(dir).unwrap();
}

#[test]
fn native_runtime_pruning_cases_1() {
    testing::run_named_cases(&[
        ("native_runtime_default_sql_distributed_avg_matches_cpu", || {
            TestRuntimeBuilder::new_current_thread()
                .enable_all()
                .build()
                .expect("case runtime")
                .block_on(native_runtime_default_sql_distributed_avg_matches_cpu())
        }),
        ("native_runtime_default_sql_distributed_filtered_aggregate_matches_cpu", || {
            TestRuntimeBuilder::new_current_thread()
                .enable_all()
                .build()
                .expect("case runtime")
                .block_on(native_runtime_default_sql_distributed_filtered_aggregate_matches_cpu())
        }),
        ("native_runtime_mixed_filtered_and_unfiltered_aggregates_match_cpu", || {
            TestRuntimeBuilder::new_current_thread()
                .enable_all()
                .build()
                .expect("case runtime")
                .block_on(native_runtime_mixed_filtered_and_unfiltered_aggregates_match_cpu())
        }),
        ("native_runtime_median_aggregate_matches_cpu_single_stage_bounded_without_partials", || {
            TestRuntimeBuilder::new_current_thread()
                .enable_all()
                .build()
                .expect("case runtime")
                .block_on(native_runtime_median_aggregate_matches_cpu_single_stage_bounded_without_partials())
        }),
        ("native_runtime_reader_pushdown_reduces_source_rows_and_retained_bytes", || {
            TestRuntimeBuilder::new_current_thread()
                .enable_all()
                .build()
                .expect("case runtime")
                .block_on(native_runtime_reader_pushdown_reduces_source_rows_and_retained_bytes())
        }),
        ("native_runtime_decimal32_reader_pushdown_matches_post_scan", || {
            TestRuntimeBuilder::new_current_thread()
                .enable_all()
                .build()
                .expect("case runtime")
                .block_on(native_runtime_decimal32_reader_pushdown_matches_post_scan())
        }),
    ]);
}

#[test]
fn native_runtime_pruning_cases_2() {
    testing::run_named_cases(&[
        ("native_runtime_decimal32_equality_reader_pushdown_matches_post_scan", || {
            TestRuntimeBuilder::new_current_thread()
                .enable_all()
                .build()
                .expect("case runtime")
                .block_on(native_runtime_decimal32_equality_reader_pushdown_matches_post_scan())
        }),
        ("native_runtime_pushed_predicate_source_limit_matches_cpu", || {
            TestRuntimeBuilder::new_current_thread()
                .enable_all()
                .build()
                .expect("case runtime")
                .block_on(native_runtime_pushed_predicate_source_limit_matches_cpu())
        }),
        ("native_runtime_empty_local_parquet_aggregates_match_cpu", || {
            TestRuntimeBuilder::new_current_thread()
                .enable_all()
                .build()
                .expect("case runtime")
                .block_on(native_runtime_empty_local_parquet_aggregates_match_cpu())
        }),
        ("native_runtime_physically_empty_parquet_files_match_cpu_without_replay", || {
            TestRuntimeBuilder::new_current_thread()
                .enable_all()
                .build()
                .expect("case runtime")
                .block_on(native_runtime_physically_empty_parquet_files_match_cpu_without_replay())
        }),
        ("native_runtime_filtered_empty_rollup_grand_total_matches_cpu", || {
            TestRuntimeBuilder::new_current_thread()
                .enable_all()
                .build()
                .expect("case runtime")
                .block_on(native_runtime_filtered_empty_rollup_grand_total_matches_cpu())
        }),
        ("native_runtime_bare_boolean_filter_stays_post_decode", || {
            TestRuntimeBuilder::new_current_thread()
                .enable_all()
                .build()
                .expect("case runtime")
                .block_on(native_runtime_bare_boolean_filter_stays_post_decode())
        }),
    ]);
}

#[test]
fn native_runtime_pruning_cases_3() {
    testing::run_named_cases(&[
        ("native_runtime_nullable_distributed_avg_matches_cpu", || {
            TestRuntimeBuilder::new_current_thread()
                .enable_all()
                .build()
                .expect("case runtime")
                .block_on(native_runtime_nullable_distributed_avg_matches_cpu())
        }),
        ("native_runtime_grouped_single_distinct_split_matches_cpu_with_null_values", || {
            TestRuntimeBuilder::new_current_thread()
                .enable_all()
                .build()
                .expect("case runtime")
                .block_on(native_runtime_grouped_single_distinct_split_matches_cpu_with_null_values())
        }),
        ("native_runtime_global_single_distinct_split_matches_cpu_with_null_values", || {
            TestRuntimeBuilder::new_current_thread()
                .enable_all()
                .build()
                .expect("case runtime")
                .block_on(native_runtime_global_single_distinct_split_matches_cpu_with_null_values())
        }),
        ("native_runtime_global_single_distinct_split_over_empty_input_matches_cpu", || {
            TestRuntimeBuilder::new_current_thread()
                .enable_all()
                .build()
                .expect("case runtime")
                .block_on(native_runtime_global_single_distinct_split_over_empty_input_matches_cpu())
        }),
        ("native_runtime_global_count_join_pushdown_over_empty_join_matches_cpu", || {
            TestRuntimeBuilder::new_current_thread()
                .enable_all()
                .build()
                .expect("case runtime")
                .block_on(native_runtime_global_count_join_pushdown_over_empty_join_matches_cpu())
        }),
        ("native_runtime_uneven_row_group_distributed_avg_matches_cpu", || {
            TestRuntimeBuilder::new_current_thread()
                .enable_all()
                .build()
                .expect("case runtime")
                .block_on(native_runtime_uneven_row_group_distributed_avg_matches_cpu())
        }),
    ]);
}

#[test]
fn native_runtime_pruning_cases_4() {
    testing::run_named_cases(&[
        ("native_runtime_sort_null_ordering_matches_cpu", || {
            TestRuntimeBuilder::new_current_thread()
                .enable_all()
                .build()
                .expect("case runtime")
                .block_on(native_runtime_sort_null_ordering_matches_cpu())
        }),
        ("native_runtime_bounded_topk_null_ordering_matches_cpu", || {
            TestRuntimeBuilder::new_current_thread()
                .enable_all()
                .build()
                .expect("case runtime")
                .block_on(native_runtime_bounded_topk_null_ordering_matches_cpu())
        }),
        ("native_runtime_nullable_avg_output_ordering_matches_cpu", || {
            TestRuntimeBuilder::new_current_thread()
                .enable_all()
                .build()
                .expect("case runtime")
                .block_on(native_runtime_nullable_avg_output_ordering_matches_cpu())
        }),
        ("native_runtime_discounted_revenue_global_decimal_sum_matches_cpu", || {
            TestRuntimeBuilder::new_current_thread()
                .enable_all()
                .build()
                .expect("case runtime")
                .block_on(native_runtime_discounted_revenue_global_decimal_sum_matches_cpu())
        }),
        ("native_runtime_order_sensitive_source_scan_limit_matches_cpu", || {
            TestRuntimeBuilder::new_current_thread()
                .enable_all()
                .build()
                .expect("case runtime")
                .block_on(native_runtime_order_sensitive_source_scan_limit_matches_cpu())
        }),
    ]);
}

#[test]
fn native_runtime_pruning_cases_5() {
    testing::run_named_cases(&[
        ("native_runtime_parquet_sorting_columns_scan_matches_cpu", || {
            TestRuntimeBuilder::new_current_thread()
                .enable_all()
                .build()
                .expect("case runtime")
                .block_on(native_runtime_parquet_sorting_columns_scan_matches_cpu())
        }),
        ("native_runtime_sorted_file_split_into_byte_ranges_matches_cpu", || {
            TestRuntimeBuilder::new_current_thread()
                .enable_all()
                .build()
                .expect("case runtime")
                .block_on(native_runtime_sorted_file_split_into_byte_ranges_matches_cpu())
        }),
        ("native_runtime_global_decimal_avg_matches_cpu", || {
            TestRuntimeBuilder::new_current_thread()
                .enable_all()
                .build()
                .expect("case runtime")
                .block_on(native_runtime_global_decimal_avg_matches_cpu())
        }),
        ("native_runtime_zero_column_count_matches_cpu_without_parquet_data_columns", || {
            TestRuntimeBuilder::new_current_thread()
                .enable_all()
                .build()
                .expect("case runtime")
                .block_on(native_runtime_zero_column_count_matches_cpu_without_parquet_data_columns())
        }),
        ("native_runtime_default_sql_not_filter_matches_cpu", || {
            TestRuntimeBuilder::new_current_thread()
                .enable_all()
                .build()
                .expect("case runtime")
                .block_on(native_runtime_default_sql_not_filter_matches_cpu())
        }),
    ]);
}

#[test]
fn native_runtime_pruning_cases_6() {
    testing::run_named_cases(&[
        ("native_runtime_widening_numeric_cast_matches_cpu", || {
            TestRuntimeBuilder::new_current_thread()
                .enable_all()
                .build()
                .expect("case runtime")
                .block_on(native_runtime_widening_numeric_cast_matches_cpu())
        }),
        ("native_runtime_default_sql_prunes_metadata_row_groups_for_pushed_parquet_predicate", || {
            TestRuntimeBuilder::new_current_thread()
                .enable_all()
                .build()
                .expect("case runtime")
                .block_on(native_runtime_default_sql_prunes_metadata_row_groups_for_pushed_parquet_predicate())
        }),
        ("native_runtime_partial_byte_range_access_plan_intersection_matches_cpu", || {
            TestRuntimeBuilder::new_current_thread()
                .enable_all()
                .build()
                .expect("case runtime")
                .block_on(native_runtime_partial_byte_range_access_plan_intersection_matches_cpu())
        }),
        ("native_runtime_default_sql_min_max_count_matches_cpu", || {
            TestRuntimeBuilder::new_current_thread()
                .enable_all()
                .build()
                .expect("case runtime")
                .block_on(native_runtime_default_sql_min_max_count_matches_cpu())
        }),
        ("native_runtime_default_sql_count_star_matches_cpu", || {
            TestRuntimeBuilder::new_current_thread()
                .enable_all()
                .build()
                .expect("case runtime")
                .block_on(native_runtime_default_sql_count_star_matches_cpu())
        }),
    ]);
}

async fn native_runtime_default_sql_distributed_avg_matches_cpu() {
    let dir = unique_temp_dir("algeon-datafusion-native-runtime-default-avg");
    let path = dir.join("input.parquet");
    write_row_group_pruning_parquet_file(&path);
    let total_row_groups = parquet_row_group_count(&path);

    assert_default_sql_native_parity(&path, SQL_AVG_AGGREGATE_QUERY, Some((2, total_row_groups))).await;

    fs::remove_dir_all(dir).unwrap();
}

async fn native_runtime_default_sql_distributed_filtered_aggregate_matches_cpu() {
    let dir = unique_temp_dir("algeon-datafusion-native-runtime-default-filtered-aggregate");
    let path = dir.join("input.parquet");
    write_row_group_pruning_parquet_file(&path);
    let total_row_groups = parquet_row_group_count(&path);

    assert_default_sql_native_parity(&path, SQL_FILTERED_AGGREGATE_QUERY, Some((2, total_row_groups))).await;

    fs::remove_dir_all(dir).unwrap();
}

async fn native_runtime_mixed_filtered_and_unfiltered_aggregates_match_cpu() {
    let dir = unique_temp_dir("algeon-datafusion-native-runtime-filtered-aggregate");
    let path = dir.join("input.parquet");
    write_row_group_pruning_parquet_file(&path);

    assert_sql_native_parity(&path, direct_friendly_sql_session_config(), SQL_MIXED_FILTERED_AGGREGATES_QUERY, None)
        .await;

    fs::remove_dir_all(dir).unwrap();
}

async fn native_runtime_median_aggregate_matches_cpu_single_stage_bounded_without_partials() {
    let dir = unique_temp_dir("algeon-datafusion-native-runtime-median-aggregate-bounded");
    let path = dir.join("input.parquet");
    write_row_group_pruning_parquet_file(&path);

    let cpu_batches = cpu_sql_output(&path, SQL_MEDIAN_AGGREGATE_QUERY).await;
    let native_physical_plan = sql_plan(&path, direct_friendly_sql_session_config(), SQL_MEDIAN_AGGREGATE_QUERY).await;
    let optimization = optimize_with_pruning_template(native_physical_plan);
    assert_eq!(optimization.report.selected_count(), 1, "outcomes={:?}", optimization.report.outcomes());
    assert_eq!(optimization.report.not_selected_count(), 0, "outcomes={:?}", optimization.report.outcomes());
    assert_algeon_native_exec(&optimization.plan, "median aggregate must rewrite to AlgeonNativeExec");

    let _observation_guard = ARROW_HOST_OBSERVATION_LOCK.lock().await;
    reset_arrow_host_interop_observation_for_test();
    let native_batches = collect(Arc::clone(&optimization.plan), Arc::new(TaskContext::default())).await.unwrap();
    let metrics = below_native_admission(&optimization.plan)
        .metrics()
        .expect("AlgeonNativeExec should expose DataFusion metrics");
    assert_eq!(metric_usize(&metrics, "partial_aggregate_count"), 0, "median must stay single-stage");
    assert_eq!(metric_usize(&metrics, "host_to_device_uploads"), 0);
    assert_eq!(metric_usize(&metrics, "device_to_host_materializations"), 1);

    assert_batches_equal(&cpu_batches, &native_batches);

    fs::remove_dir_all(dir).unwrap();
}

async fn native_runtime_reader_pushdown_reduces_source_rows_and_retained_bytes() {
    let dir = unique_temp_dir("algeon-datafusion-native-runtime-reader-pushdown-selective");
    let path = dir.join("input.parquet");
    write_reader_pushdown_selective_parquet_file(&path);

    let mut post_scan_config = SessionConfig::new();
    post_scan_config.options_mut().execution.parquet.pushdown_filters = false;
    let post_scan = execute_sql_native_query(&path, post_scan_config, SQL_SELECTIVE_READER_PUSHDOWN_QUERY).await;
    let mut reader_pushdown_config = SessionConfig::new();
    reader_pushdown_config.options_mut().execution.parquet.pushdown_filters = true;
    let reader_pushdown =
        execute_sql_native_query(&path, reader_pushdown_config, SQL_SELECTIVE_READER_PUSHDOWN_QUERY).await;

    assert_batches_equal(&post_scan.output, &reader_pushdown.output);
    assert_eq!(post_scan.predicate_disposition, "pruning_only");
    assert_eq!(reader_pushdown.predicate_disposition, "reader_pushdown");
    assert!(
        reader_pushdown.input_rows < post_scan.input_rows,
        "reader pushdown should reduce source rows: reader={} post_scan={}",
        reader_pushdown.input_rows,
        post_scan.input_rows
    );
    assert!(
        reader_pushdown.max_task_retained_bytes < post_scan.max_task_retained_bytes,
        "reader pushdown should reduce retained source bytes: reader={} post_scan={}",
        reader_pushdown.max_task_retained_bytes,
        post_scan.max_task_retained_bytes
    );

    fs::remove_dir_all(dir).unwrap();
}

async fn native_runtime_decimal32_reader_pushdown_matches_post_scan() {
    let dir = unique_temp_dir("algeon-datafusion-native-runtime-decimal32-reader-pushdown");
    let path = dir.join("input.parquet");
    write_decimal32_reader_pushdown_parquet_file(&path);

    let mut post_scan_config = SessionConfig::new();
    post_scan_config.options_mut().execution.parquet.pushdown_filters = false;
    let post_scan = execute_sql_native_query(&path, post_scan_config, SQL_DECIMAL32_READER_PUSHDOWN_QUERY).await;
    let mut reader_pushdown_config = SessionConfig::new();
    reader_pushdown_config.options_mut().execution.parquet.pushdown_filters = true;
    let reader_pushdown =
        execute_sql_native_query(&path, reader_pushdown_config, SQL_DECIMAL32_READER_PUSHDOWN_QUERY).await;

    assert_batches_equal(&post_scan.output, &reader_pushdown.output);
    assert_eq!(post_scan.predicate_disposition, "pruning_only");
    assert_eq!(reader_pushdown.predicate_disposition, "reader_pushdown");
    assert!(
        reader_pushdown.input_rows < post_scan.input_rows,
        "Decimal32-backed reader pushdown should reduce source rows: reader={} post_scan={}",
        reader_pushdown.input_rows,
        post_scan.input_rows
    );

    fs::remove_dir_all(dir).unwrap();
}

async fn native_runtime_decimal32_equality_reader_pushdown_matches_post_scan() {
    let dir = unique_temp_dir("algeon-datafusion-native-runtime-decimal32-equality-reader-pushdown");
    let path = dir.join("input.parquet");
    write_decimal32_reader_pushdown_parquet_file(&path);

    let mut post_scan_config = SessionConfig::new();
    post_scan_config.options_mut().execution.parquet.pushdown_filters = false;
    let post_scan =
        execute_sql_native_query(&path, post_scan_config, SQL_DECIMAL32_EQUALITY_READER_PUSHDOWN_QUERY).await;
    let mut reader_pushdown_config = SessionConfig::new();
    reader_pushdown_config.options_mut().execution.parquet.pushdown_filters = true;
    let reader_pushdown =
        execute_sql_native_query(&path, reader_pushdown_config, SQL_DECIMAL32_EQUALITY_READER_PUSHDOWN_QUERY).await;

    assert_batches_equal(&post_scan.output, &reader_pushdown.output);
    assert_eq!(post_scan.predicate_disposition, "pruning_only");
    assert_eq!(reader_pushdown.predicate_disposition, "reader_pushdown");
    assert!(
        reader_pushdown.input_rows < post_scan.input_rows,
        "Decimal32-backed equality reader pushdown should reduce source rows: reader={} post_scan={}",
        reader_pushdown.input_rows,
        post_scan.input_rows
    );

    fs::remove_dir_all(dir).unwrap();
}

async fn native_runtime_pushed_predicate_source_limit_matches_cpu() {
    let dir = unique_temp_dir("algeon-datafusion-native-runtime-pushed-predicate-source-limit");
    let path = dir.join("input.parquet");
    write_row_group_pruning_parquet_file(&path);

    let scan = parquet_scan_with_source_limit(
        parquet_scan_with_pushed_down_payload_predicate(parquet_scan(&path).await, 10),
        3,
    );
    let cpu_batches = collect(Arc::clone(&scan), Arc::new(TaskContext::default())).await.unwrap();
    let optimization = optimize_with_pruning_template(scan);
    assert_eq!(optimization.report.selected_count(), 1, "outcomes={:?}", optimization.report.outcomes());
    assert_eq!(optimization.report.not_selected_count(), 0, "outcomes={:?}", optimization.report.outcomes());
    assert_eq!(optimization.source_diagnostics[0].predicate_disposition.as_str(), "reader_pushdown");
    assert!(optimization.source_diagnostics[0].rejected_features.is_empty());
    assert_algeon_native_exec(&optimization.plan, "pushed-predicate plan must rewrite to AlgeonNativeExec");

    let _observation_guard = ARROW_HOST_OBSERVATION_LOCK.lock().await;
    reset_arrow_host_interop_observation_for_test();
    let native_batches = collect(Arc::clone(&optimization.plan), Arc::new(TaskContext::default())).await.unwrap();
    assert_batches_equal(&cpu_batches, &native_batches);

    fs::remove_dir_all(dir).unwrap();
}

async fn native_runtime_empty_local_parquet_aggregates_match_cpu() {
    let dir = unique_temp_dir("algeon-datafusion-native-runtime-empty-local-aggregates");
    let path = dir.join("input.parquet");
    write_row_group_pruning_parquet_file(&path);

    let empty_scan = parquet_scan_without_files(parquet_scan(&path).await);
    assert_empty_local_parquet_aggregate_parity(grouped_sum_aggregate_plan(Arc::clone(&empty_scan))).await;
    assert_empty_local_parquet_aggregate_parity(global_sum_aggregate_plan(empty_scan)).await;

    fs::remove_dir_all(dir).unwrap();
}

async fn native_runtime_physically_empty_parquet_files_match_cpu_without_replay() {
    let dir = unique_temp_dir("algeon-datafusion-native-runtime-physical-empty-parquet");
    let zero_row_file = dir.join("zero-rows.parquet");
    let zero_row_group_file = dir.join("zero-row-group.parquet");
    write_physically_empty_parquet_file(&zero_row_file);
    write_zero_row_group_parquet_file(&zero_row_group_file);

    assert_eq!(parquet_row_group_count(&zero_row_file), 0, "empty file fixture must have no row groups");
    assert_eq!(parquet_row_group_count(&zero_row_group_file), 1, "zero-row-group fixture must retain one row group");

    for path in [&zero_row_file, &zero_row_group_file] {
        for sql in [SQL_COUNT_ONLY_QUERY, SQL_COUNT_STAR_QUERY] {
            let cpu_batches = cpu_sql_output(path, sql).await;
            let native_plan = native_datafusion_sql_plan(path, sql).await;
            assert_algeon_native_exec(
                &native_plan,
                "physical empty parquet aggregate should lower to AlgeonNativeExec",
            );
            let native_batches = collect(native_plan, Arc::new(TaskContext::default())).await.unwrap();
            assert_batches_equal_allow_empty(&cpu_batches, &native_batches);
        }
    }

    fs::remove_dir_all(dir).unwrap();
}

async fn native_runtime_filtered_empty_rollup_grand_total_matches_cpu() {
    let dir = unique_temp_dir("algeon-datafusion-native-runtime-filtered-empty-rollup");
    let path = dir.join("bench.parquet");
    write_filtered_empty_rollup_parquet_file(&path);

    let cpu_batches = cpu_sql_output(&path, SQL_FILTERED_EMPTY_ROLLUP_SUM_QUERY).await;
    assert_eq!(
        cpu_batches.iter().map(|batch| batch.num_rows()).sum::<usize>(),
        1,
        "CPU DataFusion must emit the ROLLUP grand-total row over filtered-empty input"
    );

    let native_physical_plan = native_datafusion_sql_plan(&path, SQL_FILTERED_EMPTY_ROLLUP_SUM_QUERY).await;
    assert_algeon_native_exec(&native_physical_plan, "runtime-filtered empty ROLLUP should lower to AlgeonNativeExec");

    let task_ctx = Arc::new(TaskContext::default());
    let native_batches = collect(native_physical_plan, task_ctx).await.unwrap();
    assert_batches_equal(&cpu_batches, &native_batches);

    fs::remove_dir_all(dir).unwrap();
}

async fn native_runtime_bare_boolean_filter_stays_post_decode() {
    let dir = unique_temp_dir("algeon-datafusion-native-runtime-bare-boolean-filter");
    let path = dir.join("input.parquet");
    write_bare_boolean_filter_parquet_file(&path);

    let mut config = SessionConfig::new();
    config.options_mut().execution.parquet.pushdown_filters = true;
    let native_physical_plan = sql_plan(&path, config, SQL_BARE_BOOLEAN_FILTER_QUERY).await;
    let optimization = optimize_with_pruning_template(native_physical_plan);
    assert_eq!(optimization.report.selected_count(), 1, "outcomes={:?}", optimization.report.outcomes());
    assert_eq!(optimization.report.not_selected_count(), 0, "outcomes={:?}", optimization.report.outcomes());
    assert_eq!(optimization.source_diagnostics[0].predicate_disposition.as_str(), "native_post_scan_only");
    assert_algeon_native_exec(&optimization.plan, "bare-boolean plan must rewrite to AlgeonNativeExec");

    let native_batches = collect(Arc::clone(&optimization.plan), Arc::new(TaskContext::default())).await.unwrap();
    let metrics = below_native_admission(&optimization.plan)
        .metrics()
        .expect("AlgeonNativeExec should expose DataFusion metrics");
    assert_eq!(batch_rows(&native_batches), vec![vec!["3".to_string()]]);
    assert_eq!(metric_usize(&metrics, "input_rows"), 6);

    fs::remove_dir_all(dir).unwrap();
}

struct NativeQueryObservation {
    output: Vec<RecordBatch>,
    predicate_disposition: String,
    input_rows: u64,
    max_task_retained_bytes: u64,
}

async fn execute_sql_native_query(path: &std::path::Path, config: SessionConfig, sql: &str) -> NativeQueryObservation {
    let native_physical_plan = sql_plan(path, config, sql).await;
    let optimization = optimize_with_pruning_template(native_physical_plan);
    assert_eq!(optimization.report.selected_count(), 1, "outcomes={:?}", optimization.report.outcomes());
    assert_eq!(optimization.report.not_selected_count(), 0, "outcomes={:?}", optimization.report.outcomes());
    assert_eq!(optimization.source_diagnostics.len(), 1);
    assert_algeon_native_exec(&optimization.plan, "reader-pushdown query must rewrite to AlgeonNativeExec");

    let output = collect(Arc::clone(&optimization.plan), Arc::new(TaskContext::default())).await.unwrap();
    let metrics = below_native_admission(&optimization.plan)
        .metrics()
        .expect("AlgeonNativeExec should expose DataFusion metrics");

    NativeQueryObservation {
        output,
        predicate_disposition: optimization.source_diagnostics[0].predicate_disposition.as_str().to_string(),
        input_rows: metric_usize(&metrics, "input_rows") as u64,
        max_task_retained_bytes: metric_usize(&metrics, "max_task_retained_bytes") as u64,
    }
}

async fn native_runtime_nullable_distributed_avg_matches_cpu() {
    let dir = unique_temp_dir("algeon-datafusion-native-runtime-nullable-avg");
    let path = dir.join("input.parquet");
    write_nullable_avg_input_parquet(&path);

    assert_default_sql_native_parity(&path, SQL_AVG_BY_KEY_NO_FILTER_QUERY, None).await;

    fs::remove_dir_all(dir).unwrap();
}

async fn native_runtime_grouped_single_distinct_split_matches_cpu_with_null_values() {
    let dir = unique_temp_dir("algeon-datafusion-native-runtime-grouped-single-distinct");
    let path = dir.join("input.parquet");
    write_nullable_single_distinct_input_parquet(&path);

    assert_single_distinct_split_parity(&path, SQL_SINGLE_DISTINCT_GROUPED_QUERY).await;

    fs::remove_dir_all(dir).unwrap();
}

async fn native_runtime_global_single_distinct_split_matches_cpu_with_null_values() {
    let dir = unique_temp_dir("algeon-datafusion-native-runtime-global-single-distinct");
    let path = dir.join("input.parquet");
    write_nullable_single_distinct_input_parquet(&path);

    assert_single_distinct_split_parity(&path, SQL_SINGLE_DISTINCT_GLOBAL_QUERY).await;

    fs::remove_dir_all(dir).unwrap();
}

async fn native_runtime_global_single_distinct_split_over_empty_input_matches_cpu() {
    let dir = unique_temp_dir("algeon-datafusion-native-runtime-empty-single-distinct");
    let path = dir.join("input.parquet");
    write_nullable_single_distinct_input_parquet(&path);

    assert_single_distinct_split_parity(&path, SQL_SINGLE_DISTINCT_EMPTY_GLOBAL_QUERY).await;

    fs::remove_dir_all(dir).unwrap();
}

async fn native_runtime_global_count_join_pushdown_over_empty_join_matches_cpu() {
    let dir = unique_temp_dir("algeon-datafusion-native-runtime-empty-join-global-count");
    write_tiny_tpch_parquet_dataset(&dir).unwrap();

    assert_aggregate_join_pushdown_parity(&dir, SQL_EMPTY_JOIN_GLOBAL_COUNT_QUERY).await;

    fs::remove_dir_all(dir).unwrap();
}

async fn native_runtime_uneven_row_group_distributed_avg_matches_cpu() {
    let dir = unique_temp_dir("algeon-datafusion-native-runtime-uneven-avg");
    let path = dir.join("input.parquet");
    write_uneven_avg_input_parquet(&path);

    assert_default_sql_native_parity(&path, SQL_AVG_BY_KEY_NO_FILTER_QUERY, None).await;

    fs::remove_dir_all(dir).unwrap();
}

async fn native_runtime_sort_null_ordering_matches_cpu() {
    let dir = unique_temp_dir("algeon-datafusion-native-runtime-sort-nulls");
    let path = dir.join("input.parquet");
    write_nullable_sort_input_parquet(&path);

    for sql in [
        SQL_SORT_NULLS_ASC_FIRST_QUERY,
        SQL_SORT_NULLS_ASC_LAST_QUERY,
        SQL_SORT_NULLS_DESC_FIRST_QUERY,
        SQL_SORT_NULLS_DESC_LAST_QUERY,
    ] {
        assert_default_sql_native_parity(&path, sql, None).await;
    }

    fs::remove_dir_all(dir).unwrap();
}

async fn native_runtime_bounded_topk_null_ordering_matches_cpu() {
    let dir = unique_temp_dir("algeon-datafusion-native-runtime-topk-nulls");
    let path = dir.join("input.parquet");
    write_nullable_sort_input_parquet(&path);

    assert_default_sql_native_parity(&path, SQL_SORT_NULLS_TOPK_QUERY, None).await;

    fs::remove_dir_all(dir).unwrap();
}

async fn native_runtime_nullable_avg_output_ordering_matches_cpu() {
    let dir = unique_temp_dir("algeon-datafusion-native-runtime-nullable-avg-sort");
    let path = dir.join("input.parquet");
    write_nullable_avg_input_parquet(&path);

    assert_default_sql_native_parity(&path, SQL_AVG_ORDER_BY_VALUE_QUERY, None).await;

    fs::remove_dir_all(dir).unwrap();
}

async fn native_runtime_discounted_revenue_global_decimal_sum_matches_cpu() {
    let dir = unique_temp_dir("algeon-datafusion-native-runtime-discounted-revenue");
    let path = dir.join("lineitem.parquet");
    write_discounted_revenue_lineitem_parquet(&path);

    let cpu_batches = cpu_sql_output(&path, SQL_Q6_SHAPED_QUERY).await;
    let native_physical_plan = sql_plan(&path, direct_friendly_sql_session_config(), SQL_Q6_SHAPED_QUERY).await;
    let optimization = optimize_with_pruning_template(native_physical_plan);
    assert_eq!(optimization.report.selected_count(), 1, "outcomes={:?}", optimization.report.outcomes());
    assert_eq!(optimization.report.not_selected_count(), 0, "outcomes={:?}", optimization.report.outcomes());
    assert_eq!(optimization.source_diagnostics[0].read_column_count, 4);
    assert_algeon_native_exec(&optimization.plan, "discounted-revenue plan must rewrite to AlgeonNativeExec");

    let _observation_guard = ARROW_HOST_OBSERVATION_LOCK.lock().await;
    reset_arrow_host_interop_observation_for_test();
    let native_batches = collect(Arc::clone(&optimization.plan), Arc::new(TaskContext::default())).await.unwrap();
    let metrics = below_native_admission(&optimization.plan)
        .metrics()
        .expect("AlgeonNativeExec should expose DataFusion metrics");
    assert_eq!(metric_usize(&metrics, "host_to_device_uploads"), 0);
    assert_eq!(metric_usize(&metrics, "device_to_host_materializations"), 1);
    assert_eq!(arrow_host_import_call_count_for_test(), 0);
    assert_eq!(arrow_host_export_call_count_for_test(), 1);

    assert_batches_equal(&cpu_batches, &native_batches);
    let revenue = decimal128_array(&native_batches[0], 0);
    assert_eq!(revenue.value(0), 260_000);
    assert_eq!(revenue.scale(), 4);

    fs::remove_dir_all(dir).unwrap();
}

async fn native_runtime_order_sensitive_source_scan_limit_matches_cpu() {
    let dir = unique_temp_dir("algeon-datafusion-native-runtime-order-sensitive-scan-limit");
    let path = dir.join("input.parquet");
    write_row_group_pruning_parquet_file(&path);

    let scan = parquet_scan_with_order_sensitive_limit(parquet_scan(&path).await, 3);
    let scan_schema = scan.schema();
    let predicate =
        binary(col("payload", &scan_schema).unwrap(), Operator::GtEq, lit(ScalarValue::Int32(Some(0))), &scan_schema)
            .unwrap();
    let plan = aggregate_sort_limit_plan_with_filter(scan, predicate, Operator::Plus);
    let cpu_batches = collect(Arc::clone(&plan), Arc::new(TaskContext::default())).await.unwrap();
    let optimization = optimize_with_pruning_template(plan);
    assert_eq!(optimization.report.selected_count(), 1, "outcomes={:?}", optimization.report.outcomes());
    assert_eq!(optimization.report.not_selected_count(), 0, "outcomes={:?}", optimization.report.outcomes());
    assert_eq!(optimization.source_diagnostics[0].scan_limit_disposition.as_str(), "represented");
    assert!(
        optimization.source_diagnostics[0].rejected_features.iter().all(|feature| feature.as_str() != "preserve_order")
    );
    assert_algeon_native_exec(&optimization.plan, "order-sensitive plan must rewrite to AlgeonNativeExec");

    let _observation_guard = ARROW_HOST_OBSERVATION_LOCK.lock().await;
    reset_arrow_host_interop_observation_for_test();
    let native_batches = collect(Arc::clone(&optimization.plan), Arc::new(TaskContext::default())).await.unwrap();
    let metrics = below_native_admission(&optimization.plan)
        .metrics()
        .expect("AlgeonNativeExec should expose DataFusion metrics");
    assert_eq!(metric_usize(&metrics, "host_to_device_uploads"), 0);
    assert_eq!(metric_usize(&metrics, "device_to_host_materializations"), 1);
    assert_eq!(arrow_host_import_call_count_for_test(), 0);
    assert_eq!(arrow_host_export_call_count_for_test(), 1);

    assert_batches_equal(&cpu_batches, &native_batches);

    fs::remove_dir_all(dir).unwrap();
}

async fn native_runtime_parquet_sorting_columns_scan_matches_cpu() {
    let dir = unique_temp_dir("algeon-datafusion-native-runtime-parquet-sorting-columns");
    let path = dir.join("input.parquet");
    write_sorting_columns_parquet_file(&path);

    let cpu_plan = sql_plan(&path, SessionConfig::new(), "SELECT key, payload FROM bench LIMIT 3").await;
    assert!(
        cpu_plan.properties().output_ordering().is_some(),
        "Parquet sorting_columns metadata should infer scan output ordering"
    );
    let cpu_batches = collect(Arc::clone(&cpu_plan), Arc::new(TaskContext::default())).await.unwrap();
    let native_plan = native_datafusion_sql_plan(&path, "SELECT key, payload FROM bench LIMIT 3").await;
    assert_algeon_native_exec(&native_plan, "sorting_columns scan should optimize to native exec");
    assert_eq!(native_plan.properties().output_ordering(), cpu_plan.properties().output_ordering());

    let native_batches = collect(native_plan, Arc::new(TaskContext::default())).await.unwrap();

    assert_batches_equal(&cpu_batches, &native_batches);

    fs::remove_dir_all(dir).unwrap();
}

/// DataFusion splits one sorted file into byte-range groups under the default
/// `target_partitions`. The native reader coalesces the ranges back into that
/// file and reads its row groups in order, so the declared ordering holds.
async fn native_runtime_sorted_file_split_into_byte_ranges_matches_cpu() {
    let dir = unique_temp_dir("algeon-datafusion-native-runtime-sorted-byte-ranges");
    let path = dir.join("input.parquet");
    write_multi_row_group_sorting_columns_parquet_file(&path);
    // Lowers the 10 MiB repartition floor so this small file splits like a
    // large one; `target_partitions` keeps its default.
    let config = SessionConfig::new().with_repartition_file_min_size(1);
    let sql = "SELECT key, payload FROM bench WHERE payload >= 500 ORDER BY key LIMIT 7";

    let cpu_plan = sql_plan(&path, config.clone(), sql).await;
    let scans = collect_file_scans(&cpu_plan);
    assert_eq!(scans.len(), 1, "one Parquet scan");
    assert!(scans[0].file_groups.len() > 1, "the scan should split into byte-range groups: {:?}", scans[0].file_groups);
    assert!(!scans[0].output_ordering.is_empty(), "sorting_columns should declare the scan ordering");
    let cpu_batches = collect(Arc::clone(&cpu_plan), Arc::new(TaskContext::default())).await.unwrap();

    let native_plan = native_datafusion_sql_plan_with_config(&path, config, sql).await;
    assert_algeon_native_exec(&native_plan, "a single sorted file split into byte ranges should run natively");
    let native_batches = collect(native_plan, Arc::new(TaskContext::default())).await.unwrap();

    assert_batches_equal(&cpu_batches, &native_batches);

    fs::remove_dir_all(dir).unwrap();
}

fn collect_file_scans(plan: &Arc<dyn ExecutionPlan>) -> Vec<FileScanConfig> {
    let mut scans = plan.children().into_iter().flat_map(collect_file_scans).collect::<Vec<_>>();
    if let Some((file_scan, _parquet_source)) = plan
        .downcast_ref::<DataSourceExec>()
        .and_then(|data_source| data_source.downcast_to_file_source::<ParquetSource>())
    {
        scans.push(file_scan.clone());
    }
    scans
}

async fn native_runtime_global_decimal_avg_matches_cpu() {
    let dir = unique_temp_dir("algeon-datafusion-native-runtime-global-decimal-avg");
    let path = dir.join("lineitem.parquet");
    write_discounted_revenue_lineitem_parquet(&path);

    let cpu_batches = cpu_sql_output(&path, SQL_GLOBAL_DECIMAL_AVG_QUERY).await;
    let native_physical_plan =
        sql_plan(&path, direct_friendly_sql_session_config(), SQL_GLOBAL_DECIMAL_AVG_QUERY).await;
    let optimization = optimize_with_pruning_template(native_physical_plan);
    assert_eq!(optimization.report.selected_count(), 1, "outcomes={:?}", optimization.report.outcomes());
    assert_eq!(optimization.report.not_selected_count(), 0, "outcomes={:?}", optimization.report.outcomes());
    assert_algeon_native_exec(&optimization.plan, "decimal-avg plan must rewrite to AlgeonNativeExec");

    let _observation_guard = ARROW_HOST_OBSERVATION_LOCK.lock().await;
    reset_arrow_host_interop_observation_for_test();
    let native_batches = collect(Arc::clone(&optimization.plan), Arc::new(TaskContext::default())).await.unwrap();
    let metrics = below_native_admission(&optimization.plan)
        .metrics()
        .expect("AlgeonNativeExec should expose DataFusion metrics");
    assert_eq!(metric_usize(&metrics, "host_to_device_uploads"), 0);
    assert_eq!(metric_usize(&metrics, "device_to_host_materializations"), 1);

    assert_batches_equal(&cpu_batches, &native_batches);

    fs::remove_dir_all(dir).unwrap();
}

async fn native_runtime_zero_column_count_matches_cpu_without_parquet_data_columns() {
    let dir = unique_temp_dir("algeon-datafusion-native-runtime-zero-column-count");
    let path = dir.join("input.parquet");
    write_row_group_pruning_parquet_file(&path);
    let native_physical_plan = count_only_sort_limit_plan(parquet_scan(&path).await);
    let cpu_batches = collect(Arc::clone(&native_physical_plan), Arc::new(TaskContext::default())).await.unwrap();
    let optimization = optimize_with_pruning_template(native_physical_plan);
    assert_eq!(optimization.report.selected_count(), 1, "outcomes={:?}", optimization.report.outcomes());
    assert_eq!(optimization.report.not_selected_count(), 0, "outcomes={:?}", optimization.report.outcomes());
    assert_eq!(optimization.source_diagnostics[0].read_column_count, 0);
    assert_eq!(optimization.source_diagnostics[0].zero_column_count_disposition.as_str(), "represented");
    assert_algeon_native_exec(&optimization.plan, "zero-column plan must rewrite to AlgeonNativeExec");

    let _observation_guard = ARROW_HOST_OBSERVATION_LOCK.lock().await;
    reset_arrow_host_interop_observation_for_test();
    let native_batches = collect(Arc::clone(&optimization.plan), Arc::new(TaskContext::default())).await.unwrap();
    let metrics = below_native_admission(&optimization.plan)
        .metrics()
        .expect("AlgeonNativeExec should expose DataFusion metrics");
    assert_eq!(metric_usize(&metrics, "read_columns"), 0);
    assert_eq!(metric_usize(&metrics, "input_rows"), 8);
    assert_eq!(metric_usize(&metrics, "host_to_device_uploads"), 0);
    assert_eq!(metric_usize(&metrics, "device_to_host_materializations"), 1);
    assert_eq!(arrow_host_import_call_count_for_test(), 0);
    assert_eq!(arrow_host_export_call_count_for_test(), 1);

    assert_batches_equal(&cpu_batches, &native_batches);

    fs::remove_dir_all(dir).unwrap();
}

async fn native_runtime_default_sql_not_filter_matches_cpu() {
    let dir = unique_temp_dir("algeon-datafusion-native-runtime-not-filter");
    let path = dir.join("input.parquet");
    write_row_group_pruning_parquet_file(&path);

    let scan = parquet_scan(&path).await;
    let scan_schema = scan.schema();
    let payload_lt_10 =
        binary(col("payload", &scan_schema).unwrap(), Operator::Lt, lit(ScalarValue::Int32(Some(10))), &scan_schema)
            .unwrap();
    let predicate = Arc::new(NotExpr::new(payload_lt_10));
    let plan = aggregate_sort_limit_plan_with_filter(scan, predicate, Operator::Plus);
    assert_native_physical_plan_parity(plan, None).await;

    fs::remove_dir_all(dir).unwrap();
}

async fn native_runtime_widening_numeric_cast_matches_cpu() {
    let dir = unique_temp_dir("algeon-datafusion-native-runtime-cast");
    let path = dir.join("input.parquet");
    write_row_group_pruning_parquet_file(&path);

    let scan = parquet_scan(&path).await;
    let scan_schema = scan.schema();
    let predicate =
        binary(col("payload", &scan_schema).unwrap(), Operator::GtEq, lit(ScalarValue::Int32(Some(10))), &scan_schema)
            .unwrap();
    let cast_payload = Arc::new(CastExpr::new(col("payload", &scan_schema).unwrap(), DataType::Int64, None));
    let x00 = binary(cast_payload, Operator::Plus, col("m00", &scan_schema).unwrap(), &scan_schema).unwrap();
    let plan = aggregate_sort_limit_plan_with_projected_x00(scan, predicate, x00);
    assert_native_physical_plan_parity(plan, None).await;

    fs::remove_dir_all(dir).unwrap();
}

async fn native_runtime_default_sql_prunes_metadata_row_groups_for_pushed_parquet_predicate() {
    let dir = unique_temp_dir("algeon-datafusion-native-runtime-row-group-pruning");
    let path = dir.join("input.parquet");
    write_row_group_pruning_parquet_file(&path);
    let total_row_groups = parquet_row_group_count(&path);

    assert_default_sql_native_parity(&path, SQL_SUM_EXPRESSION_QUERY, Some((2, total_row_groups))).await;

    fs::remove_dir_all(dir).unwrap();
}

async fn native_runtime_partial_byte_range_access_plan_intersection_matches_cpu() {
    let dir = unique_temp_dir("algeon-datafusion-native-runtime-byte-range-access-plan");
    let path = dir.join("input.parquet");
    write_row_group_pruning_parquet_file(&path);
    let row_group_offsets = row_group_range_pruning_offsets(&path);
    assert!(row_group_offsets.len() >= 4, "fixture must have at least four row groups");
    let access_plan = ParquetAccessPlan::new(vec![
        RowGroupAccess::Skip,
        RowGroupAccess::Scan,
        RowGroupAccess::Skip,
        RowGroupAccess::Scan,
    ]);
    let scan = parquet_scan_with_explicit_byte_ranges_and_access_plan(
        parquet_scan(&path).await,
        vec![(row_group_offsets[1], row_group_offsets[3])],
        access_plan,
    );
    let scan_schema = scan.schema();
    let predicate =
        binary(col("payload", &scan_schema).unwrap(), Operator::GtEq, lit(ScalarValue::Int32(Some(0))), &scan_schema)
            .unwrap();
    let plan = aggregate_sort_limit_plan_with_filter(scan, predicate, Operator::Plus);
    assert_native_physical_plan_parity(plan, Some((1, 4))).await;

    fs::remove_dir_all(dir).unwrap();
}

async fn assert_default_sql_native_parity(
    path: &std::path::Path,
    sql: &str,
    expected_selected_row_groups: Option<(usize, usize)>,
) {
    assert_sql_native_parity(path, SessionConfig::new(), sql, expected_selected_row_groups).await;
}

async fn assert_sql_native_parity(
    path: &std::path::Path,
    config: SessionConfig,
    sql: &str,
    expected_selected_row_groups: Option<(usize, usize)>,
) {
    let cpu_batches = cpu_sql_output(path, sql).await;
    let native_physical_plan = sql_plan(path, config, sql).await;
    let optimization = optimize_with_pruning_template(native_physical_plan);
    assert_eq!(optimization.report.selected_count(), 1, "outcomes={:?}", optimization.report.outcomes());
    assert_eq!(optimization.report.not_selected_count(), 0, "outcomes={:?}", optimization.report.outcomes());
    assert_algeon_native_exec(&optimization.plan, "pruning template must rewrite the plan to AlgeonNativeExec");
    if let Some((selected_row_groups, total_row_groups)) = expected_selected_row_groups {
        assert_eq!(optimization.source_diagnostics[0].selected_row_groups, Some(selected_row_groups));
        assert!(selected_row_groups < total_row_groups);
    }

    let _observation_guard = ARROW_HOST_OBSERVATION_LOCK.lock().await;
    reset_arrow_host_interop_observation_for_test();
    let native_batches = collect(Arc::clone(&optimization.plan), Arc::new(TaskContext::default())).await.unwrap();
    let metrics = below_native_admission(&optimization.plan)
        .metrics()
        .expect("AlgeonNativeExec should expose DataFusion metrics");

    if let Some((selected_row_groups, total_row_groups)) = expected_selected_row_groups {
        assert_eq!(total_row_groups, 4);
        assert_eq!(metric_usize(&metrics, "row_groups_selected"), selected_row_groups);
        assert!(selected_row_groups < total_row_groups);
    }
    assert_eq!(metric_usize(&metrics, "host_to_device_uploads"), 0);
    assert_eq!(metric_usize(&metrics, "host_to_device_uploaded_rows"), 0);
    assert_eq!(metric_usize(&metrics, "device_to_host_materializations"), 1);
    assert!(metric_usize(&metrics, "source_chunks_committed") > 0);
    assert!(metric_usize(&metrics, "source_chunks_committed") <= metric_usize(&metrics, "source_chunks_planned"));
    assert_eq!(metric_usize(&metrics, "row_groups_planned"), metric_usize(&metrics, "row_groups_committed"));
    assert_eq!(arrow_host_import_call_count_for_test(), 0);
    assert_eq!(arrow_host_export_call_count_for_test(), 1);

    assert_batches_equal(&cpu_batches, &native_batches);
}

/// DataFusion keeps one aggregate for `COUNT(DISTINCT x)` beside `COUNT`, so
/// the native optimizer must split it for the comparison to cover the split.
async fn assert_single_distinct_split_parity(path: &std::path::Path, sql: &str) {
    let cpu_batches = cpu_sql_output(path, sql).await;
    let optimization = optimize_with_pruning_template_requiring_native(sql_plan(path, SessionConfig::new(), sql).await);
    assert_eq!(optimization.report.selected_count(), 1, "outcomes={:?}", optimization.report.outcomes());
    assert_algeon_native_exec(&optimization.plan, "single-distinct aggregate must rewrite to AlgeonNativeExec");
    assert_eq!(
        optimization.native_optimizer.single_distinct_aggregate_split.applied_count, 1,
        "{:?}",
        optimization.native_optimizer.single_distinct_aggregate_split
    );

    let native_batches = collect(Arc::clone(&optimization.plan), Arc::new(TaskContext::default())).await.unwrap();
    assert_batches_equal(&cpu_batches, &native_batches);
}

/// A global aggregate merges the pushed-down partial state over no rows when
/// the join is empty, which DataFusion answers with zero counts and null sums.
async fn assert_aggregate_join_pushdown_parity(root: &std::path::Path, sql: &str) {
    let cpu_batches = cpu_tpch_sql_output(root, sql).await;
    let optimization =
        optimize_with_pruning_template_requiring_native(tpch_sql_plan(root, tpch_report_session_config(), sql).await);
    assert_eq!(optimization.report.selected_count(), 1, "sql={sql} outcomes={:?}", optimization.report.outcomes());
    assert_algeon_native_exec(&optimization.plan, "aggregate over a join must rewrite to AlgeonNativeExec");
    assert_eq!(
        optimization.native_optimizer.aggregate_join_pushdown.applied_count, 1,
        "sql={sql} {:?}",
        optimization.native_optimizer.aggregate_join_pushdown
    );

    let native_batches = collect(Arc::clone(&optimization.plan), Arc::new(TaskContext::default())).await.unwrap();
    assert_batches_equal(&cpu_batches, &native_batches);
}

async fn assert_empty_local_parquet_aggregate_parity(plan: Arc<dyn ExecutionPlan>) {
    let cpu_batches = collect(Arc::clone(&plan), Arc::new(TaskContext::default())).await.unwrap();
    // A scan without files lowers to a zero-row literal, which a plan that
    // allows DataFusion CPU execution keeps there by cost.
    let optimization = optimize_with_pruning_template_requiring_native(plan);
    assert_eq!(optimization.report.selected_count(), 1, "outcomes={:?}", optimization.report.outcomes());
    assert_eq!(optimization.report.not_selected_count(), 0, "outcomes={:?}", optimization.report.outcomes());
    assert!(optimization.source_diagnostics.is_empty());
    assert_algeon_native_exec(&optimization.plan, "empty scan aggregate must rewrite to AlgeonNativeExec");

    let native_batches = collect(Arc::clone(&optimization.plan), Arc::new(TaskContext::default())).await.unwrap();
    assert_batches_equal_allow_empty(&cpu_batches, &native_batches);
}

async fn assert_native_physical_plan_parity(
    plan: Arc<dyn ExecutionPlan>,
    expected_selected_row_groups: Option<(usize, usize)>,
) {
    let cpu_batches = collect(Arc::clone(&plan), Arc::new(TaskContext::default())).await.unwrap();
    let optimization = optimize_with_pruning_template(plan);
    assert_eq!(optimization.report.selected_count(), 1, "outcomes={:?}", optimization.report.outcomes());
    assert_eq!(optimization.report.not_selected_count(), 0, "outcomes={:?}", optimization.report.outcomes());
    assert_algeon_native_exec(&optimization.plan, "pruning template must rewrite the plan to AlgeonNativeExec");
    if let Some((selected_row_groups, total_row_groups)) = expected_selected_row_groups {
        assert_eq!(optimization.source_diagnostics[0].selected_row_groups, Some(selected_row_groups));
        assert!(selected_row_groups < total_row_groups);
    }

    let _observation_guard = ARROW_HOST_OBSERVATION_LOCK.lock().await;
    reset_arrow_host_interop_observation_for_test();
    let native_batches = collect(Arc::clone(&optimization.plan), Arc::new(TaskContext::default())).await.unwrap();
    let metrics = below_native_admission(&optimization.plan)
        .metrics()
        .expect("AlgeonNativeExec should expose DataFusion metrics");

    assert_eq!(metric_usize(&metrics, "host_to_device_uploads"), 0);
    assert_eq!(metric_usize(&metrics, "host_to_device_uploaded_rows"), 0);
    assert_eq!(metric_usize(&metrics, "device_to_host_materializations"), 1);
    assert_eq!(arrow_host_import_call_count_for_test(), 0);
    assert_eq!(arrow_host_export_call_count_for_test(), 1);

    assert_batches_equal(&cpu_batches, &native_batches);
}

async fn cpu_sql_output(path: &std::path::Path, sql: &str) -> Vec<RecordBatch> {
    let plan = sql_plan(path, SessionConfig::new(), sql).await;
    collect(plan, Arc::new(TaskContext::default())).await.unwrap()
}

async fn cpu_lineitem_sql_output(path: &std::path::Path, sql: &str) -> Vec<RecordBatch> {
    let plan = lineitem_sql_plan(path, direct_friendly_sql_session_config(), sql).await;
    collect(plan, Arc::new(TaskContext::default())).await.unwrap()
}

async fn cpu_tpch_sql_output(root: &std::path::Path, sql: &str) -> Vec<RecordBatch> {
    let plan = tpch_sql_plan(root, tpch_report_session_config(), sql).await;
    collect(plan, Arc::new(TaskContext::default())).await.unwrap()
}

async fn parquet_scan(path: &std::path::Path) -> Arc<dyn ExecutionPlan> {
    let ctx = SessionContext::new_with_config(
        SessionConfig::new().with_repartition_file_scans(false).with_target_partitions(1),
    );
    let df = ctx.read_parquet(path.to_str().unwrap(), ParquetReadOptions::default()).await.unwrap();
    df.create_physical_plan().await.unwrap()
}

fn parquet_scan_with_order_sensitive_limit(scan: Arc<dyn ExecutionPlan>, limit: usize) -> Arc<dyn ExecutionPlan> {
    let data_source = scan.downcast_ref::<DataSourceExec>().expect("parquet scan should be DataSourceExec");
    let (file_scan, _parquet_source) =
        data_source.downcast_to_file_source::<ParquetSource>().expect("parquet scan should use ParquetSource");
    let file_scan =
        FileScanConfigBuilder::from(file_scan.clone()).with_limit(Some(limit)).with_preserve_order(true).build();
    DataSourceExec::from_data_source(file_scan)
}

fn parquet_scan_with_source_limit(scan: Arc<dyn ExecutionPlan>, limit: usize) -> Arc<dyn ExecutionPlan> {
    let data_source = scan.downcast_ref::<DataSourceExec>().expect("parquet scan should be DataSourceExec");
    let (file_scan, _parquet_source) =
        data_source.downcast_to_file_source::<ParquetSource>().expect("parquet scan should use ParquetSource");
    let file_scan = FileScanConfigBuilder::from(file_scan.clone()).with_limit(Some(limit)).build();
    DataSourceExec::from_data_source(file_scan)
}

fn parquet_scan_without_files(scan: Arc<dyn ExecutionPlan>) -> Arc<dyn ExecutionPlan> {
    let data_source = scan.downcast_ref::<DataSourceExec>().expect("parquet scan should be DataSourceExec");
    let (file_scan, _parquet_source) =
        data_source.downcast_to_file_source::<ParquetSource>().expect("parquet scan should use ParquetSource");
    let file_scan =
        FileScanConfigBuilder::from(file_scan.clone()).with_file_groups(vec![FileGroup::new(vec![])]).build();
    DataSourceExec::from_data_source(file_scan)
}

fn parquet_scan_with_pushed_down_payload_predicate(scan: Arc<dyn ExecutionPlan>, value: i32) -> Arc<dyn ExecutionPlan> {
    let data_source = scan.downcast_ref::<DataSourceExec>().expect("parquet scan should be DataSourceExec");
    let (file_scan, parquet_source) =
        data_source.downcast_to_file_source::<ParquetSource>().expect("parquet scan should use ParquetSource");
    let scan_schema = scan.schema();
    let predicate = binary(
        col("payload", &scan_schema).unwrap(),
        Operator::GtEq,
        lit(ScalarValue::Int32(Some(value))),
        &scan_schema,
    )
    .unwrap();
    let parquet_source = parquet_source.with_predicate(predicate).with_pushdown_filters(true);
    let file_scan = FileScanConfigBuilder::from(file_scan.clone()).with_source(Arc::new(parquet_source)).build();
    DataSourceExec::from_data_source(file_scan)
}

fn grouped_sum_aggregate_plan(scan: Arc<dyn ExecutionPlan>) -> Arc<dyn ExecutionPlan> {
    let scan_schema = scan.schema();
    let group_by = PhysicalGroupBy::new_single(vec![(col("key", &scan_schema).unwrap(), "key".to_string())]);
    let sum = AggregateExprBuilder::new(sum_udaf(), vec![col("m00", &scan_schema).unwrap()])
        .schema(Arc::clone(&scan_schema))
        .alias("sum_m00")
        .build()
        .unwrap();
    Arc::new(
        AggregateExec::try_new(AggregateMode::Single, group_by, vec![Arc::new(sum)], vec![None], scan, scan_schema)
            .unwrap(),
    )
}

fn global_sum_aggregate_plan(scan: Arc<dyn ExecutionPlan>) -> Arc<dyn ExecutionPlan> {
    let scan_schema = scan.schema();
    let sum = AggregateExprBuilder::new(sum_udaf(), vec![col("m00", &scan_schema).unwrap()])
        .schema(Arc::clone(&scan_schema))
        .alias("sum_m00")
        .build()
        .unwrap();
    Arc::new(
        AggregateExec::try_new(
            AggregateMode::Single,
            PhysicalGroupBy::new_single(Vec::new()),
            vec![Arc::new(sum)],
            vec![None],
            scan,
            scan_schema,
        )
        .unwrap(),
    )
}

fn parquet_scan_with_explicit_byte_ranges_and_access_plan(
    scan: Arc<dyn ExecutionPlan>,
    ranges: Vec<(i64, i64)>,
    access_plan: ParquetAccessPlan,
) -> Arc<dyn ExecutionPlan> {
    let data_source = scan.downcast_ref::<DataSourceExec>().expect("parquet scan should be DataSourceExec");
    let (file_scan, _parquet_source) =
        data_source.downcast_to_file_source::<ParquetSource>().expect("parquet scan should use ParquetSource");
    let file_groups = file_scan
        .file_groups
        .iter()
        .map(|group| {
            FileGroup::new(
                group
                    .files()
                    .iter()
                    .flat_map(|file| {
                        ranges
                            .iter()
                            .map(|(start, end)| {
                                file.clone().with_range(*start, *end).with_extension(access_plan.clone())
                            })
                            .collect::<Vec<_>>()
                    })
                    .collect(),
            )
        })
        .collect();
    let file_scan = FileScanConfigBuilder::from(file_scan.clone()).with_file_groups(file_groups).build();
    DataSourceExec::from_data_source(file_scan)
}

fn row_group_range_pruning_offsets(path: &std::path::Path) -> Vec<i64> {
    let reader = SerializedFileReader::try_from(path).unwrap();
    reader
        .metadata()
        .row_groups()
        .iter()
        .map(|row_group| {
            let column = row_group.column(0);
            column.dictionary_page_offset().unwrap_or_else(|| column.data_page_offset())
        })
        .collect()
}

async fn sql_plan(path: &std::path::Path, config: SessionConfig, sql: &str) -> Arc<dyn ExecutionPlan> {
    let ctx = SessionContext::new_with_config(config);
    ctx.register_parquet("bench", path.to_str().unwrap(), ParquetReadOptions::default()).await.unwrap();
    let df = ctx.sql(sql).await.unwrap();
    df.create_physical_plan().await.unwrap()
}

async fn lineitem_sql_plan(path: &std::path::Path, config: SessionConfig, sql: &str) -> Arc<dyn ExecutionPlan> {
    let ctx = SessionContext::new_with_config(config);
    ctx.register_parquet("lineitem", path.to_str().unwrap(), ParquetReadOptions::default()).await.unwrap();
    let df = ctx.sql(sql).await.unwrap();
    df.create_physical_plan().await.unwrap()
}

async fn tpch_sql_plan(root: &std::path::Path, config: SessionConfig, sql: &str) -> Arc<dyn ExecutionPlan> {
    let ctx = SessionContext::new_with_config(config);
    register_tiny_tpch_tables(&ctx, root).await;
    let df = ctx.sql(sql).await.unwrap();
    df.create_physical_plan().await.unwrap()
}