datafusion 55.0.0

DataFusion is an in-memory query engine that uses Apache Arrow as the memory model
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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

//! Benchmarks for Merge and sort performance
//!
//! Each benchmark:
//! 1. Creates a list of tuples (sorted if necessary)
//!
//! 2. Divides those tuples across some number of streams of [`RecordBatch`]
//!    preserving any ordering
//!
//! 3. Times how long it takes for a given sort plan to process the input
//!
//! Pictorially:
//!
//! ```
//!                           Rows are randomly
//!                          divided into separate
//!                         RecordBatch "streams",
//! ┌────┐ ┌────┐ ┌────┐     preserving the order        ┌────┐ ┌────┐ ┌────┐
//! │    │ │    │ │    │                                 │    │ │    │ │    │
//! │    │ │    │ │    │ ──────────────┐                 │    │ │    │ │    │
//! │    │ │    │ │    │               └─────────────▶   │ C1 │ │... │ │ CN │
//! │    │ │    │ │    │ ───────────────┐                │    │ │    │ │    │
//! │    │ │    │ │    │               ┌┼─────────────▶  │    │ │    │ │    │
//! │    │ │    │ │    │               ││                │    │ │    │ │    │
//! │    │ │    │ │    │               ││                └────┘ └────┘ └────┘
//! │    │ │    │ │    │               ││                ┌────┐ ┌────┐ ┌────┐
//! │    │ │    │ │    │               │└───────────────▶│    │ │    │ │    │
//! │    │ │    │ │    │               │                 │    │ │    │ │    │
//! │    │ │    │ │    │         ...   │                 │ C1 │ │... │ │ CN │
//! │    │ │    │ │    │ ──────────────┘                 │    │ │    │ │    │
//! │    │ │    │ │    │                ┌──────────────▶ │    │ │    │ │    │
//! │ C1 │ │... │ │ CN │                │                │    │ │    │ │    │
//! │    │ │    │ │    │───────────────┐│                └────┘ └────┘ └────┘
//! │    │ │    │ │    │               ││
//! │    │ │    │ │    │               ││
//! │    │ │    │ │    │               ││                         ...
//! │    │ │    │ │    │   ────────────┼┼┐
//! │    │ │    │ │    │               │││
//! │    │ │    │ │    │               │││               ┌────┐ ┌────┐ ┌────┐
//! │    │ │    │ │    │ ──────────────┼┘│               │    │ │    │ │    │
//! │    │ │    │ │    │               │ │               │    │ │    │ │    │
//! │    │ │    │ │    │               │ │               │ C1 │ │... │ │ CN │
//! │    │ │    │ │    │               └─┼────────────▶  │    │ │    │ │    │
//! │    │ │    │ │    │                 │               │    │ │    │ │    │
//! │    │ │    │ │    │                 └─────────────▶ │    │ │    │ │    │
//! └────┘ └────┘ └────┘                                 └────┘ └────┘ └────┘
//!    Input RecordBatch                                  NUM_STREAMS input
//!      Columns 1..N                                       RecordBatches
//! INPUT_SIZE sorted rows                                (still INPUT_SIZE total
//!     ~10% duplicates                                          rows)
//! ```

use arrow::array::{ArrayRef, StringViewArray, StringViewBuilder};
use arrow::{
    array::{Array, DictionaryArray, Float64Array, Int64Array, StringArray},
    datatypes::{Field, Int32Type, Schema},
    record_batch::RecordBatch,
};
use datafusion::physical_plan::sorts::sort::SortExec;
use datafusion::{
    execution::context::TaskContext,
    physical_plan::{
        ExecutionPlan, ExecutionPlanProperties,
        coalesce_partitions::CoalescePartitionsExec,
        sorts::sort_preserving_merge::SortPreservingMergeExec,
    },
    prelude::SessionContext,
};
use datafusion_datasource::memory::MemorySourceConfig;
use datafusion_physical_expr::{PhysicalSortExpr, expressions::col};
use datafusion_physical_expr_common::sort_expr::LexOrdering;
use std::sync::Arc;
use std::time::Duration;

/// Benchmarks for SortPreservingMerge stream
use criterion::{Criterion, criterion_group, criterion_main};
use datafusion_execution::config::SessionConfig;
use futures::StreamExt;
use itertools::Itertools;
use rand::rngs::StdRng;
use rand::seq::SliceRandom;
use rand::{Rng, SeedableRng};
use tokio::runtime::Runtime;

/// Total number of streams to divide each input into
/// models 8 partition plan (should it be 16??)
const NUM_STREAMS: usize = 8;

/// The size of each batch within each stream
const BATCH_SIZE: usize = 1024;

/// Input sizes to benchmark. The small size (100K) exercises the
/// in-memory concat-and-sort path; the large size (1M) exercises
/// the sort-then-merge path with high fan-in.
const INPUT_SIZES: &[(u64, &str)] = &[(100_000, "100k"), (1_000_000, "1M")];

/// Number of extra (non-sort-key) payload columns to carry alongside the sort
/// keys in the axis benchmarks. Measures the cost of reordering wide batches.
const EXTRA_COLUMN_COUNTS: &[usize] = &[0, 5, 20, 100];

/// Input ordering profiles for the SortExec axis benchmarks.
#[derive(Clone, Copy, Debug)]
enum DataProfile {
    Sorted,
    Unsorted,
    /// Fully sorted, then 10% of rows swapped to random positions.
    NearlySorted,
}

impl DataProfile {
    /// Arrange `v` (whose initial order is irrelevant) into this profile.
    fn apply<T: Ord>(self, mut v: Vec<T>) -> Vec<T> {
        let mut rng = StdRng::seed_from_u64(99);
        match self {
            DataProfile::Sorted => v.sort_unstable(),
            DataProfile::Unsorted => v.shuffle(&mut rng),
            DataProfile::NearlySorted => {
                v.sort_unstable();
                let n = v.len();

                // 10% is globally misplaced
                for _ in 0..n / 10 {
                    v.swap(rng.random_range(0..n), rng.random_range(0..n));
                }
            }
        }
        v
    }
}

/// Sort-key cardinality, i.e. how much the key values overlap across rows and
/// partitions. Only affects the sort keys, not the extra payload columns.
#[derive(Clone, Copy, Debug)]
enum Cardinality {
    /// Heavy overlap: i64 in `0..input_size` (~1/3 duplicates), 100 distinct
    /// strings repeated across all rows.
    Low,
    /// Minimal overlap: full-range i64 and random strings (~no duplicates).
    High,
}

type PartitionedBatches = Vec<Vec<RecordBatch>>;
type StreamGenerator = Box<dyn Fn(bool) -> PartitionedBatches>;

fn criterion_benchmark(c: &mut Criterion) {
    for &(input_size, size_label) in INPUT_SIZES {
        let cases: Vec<(&str, StreamGenerator)> = vec![
            (
                "i64",
                Box::new(move |sorted| i64_streams(sorted, input_size)),
            ),
            (
                "f64",
                Box::new(move |sorted| f64_streams(sorted, input_size)),
            ),
            (
                "utf8 low cardinality",
                Box::new(move |sorted| utf8_low_cardinality_streams(sorted, input_size)),
            ),
            (
                "utf8 high cardinality",
                Box::new(move |sorted| utf8_high_cardinality_streams(sorted, input_size)),
            ),
            (
                "utf8 view low cardinality",
                Box::new(move |sorted| {
                    utf8_view_low_cardinality_streams(sorted, input_size)
                }),
            ),
            (
                "utf8 view high cardinality",
                Box::new(move |sorted| {
                    utf8_view_high_cardinality_streams(sorted, input_size)
                }),
            ),
            (
                "utf8 tuple",
                Box::new(move |sorted| utf8_tuple_streams(sorted, input_size)),
            ),
            (
                "utf8 view tuple",
                Box::new(move |sorted| utf8_view_tuple_streams(sorted, input_size)),
            ),
            (
                "utf8 dictionary",
                Box::new(move |sorted| dictionary_streams(sorted, input_size)),
            ),
            (
                "utf8 dictionary tuple",
                Box::new(move |sorted| dictionary_tuple_streams(sorted, input_size)),
            ),
            (
                "mixed dictionary tuple",
                Box::new(move |sorted| {
                    mixed_dictionary_tuple_streams(sorted, input_size)
                }),
            ),
            (
                "mixed tuple",
                Box::new(move |sorted| mixed_tuple_streams(sorted, input_size)),
            ),
            (
                "mixed tuple with utf8 view",
                Box::new(move |sorted| {
                    mixed_tuple_with_utf8_view_streams(sorted, input_size)
                }),
            ),
        ];

        for (name, f) in &cases {
            c.bench_function(&format!("merge sorted {name} {size_label}"), |b| {
                let data = f(true);
                let case = BenchCase::merge_sorted(BATCH_SIZE, &data);
                b.iter(move || case.run())
            });

            c.bench_function(&format!("sort merge {name} {size_label}"), |b| {
                let data = f(false);
                let case = BenchCase::sort_merge(BATCH_SIZE, &data);
                b.iter(move || case.run())
            });

            c.bench_function(&format!("sort {name} {size_label}"), |b| {
                let data = f(false);
                let case = BenchCase::sort(BATCH_SIZE, &data);
                b.iter(move || case.run())
            });

            c.bench_function(&format!("sort partitioned {name} {size_label}"), |b| {
                let data = f(false);
                let case = BenchCase::sort_partitioned(BATCH_SIZE, &data);
                b.iter(move || case.run())
            });
        }
    }
}

/// Encapsulates running each test case
struct BenchCase {
    runtime: Runtime,
    task_ctx: Arc<TaskContext>,

    // The plan to run
    plan: Arc<dyn ExecutionPlan>,
}

impl BenchCase {
    /// Prepare to run a benchmark that merges the specified
    /// pre-sorted partitions (streams) together using all keys
    fn merge_sorted(batch_size: usize, partitions: &[Vec<RecordBatch>]) -> Self {
        let runtime = tokio::runtime::Builder::new_multi_thread().build().unwrap();
        let session_ctx = SessionContext::new_with_config(
            SessionConfig::new().with_batch_size(batch_size),
        );
        let task_ctx = session_ctx.task_ctx();

        let schema = partitions[0][0].schema();
        let sort = make_sort_exprs(schema.as_ref());

        let exec = MemorySourceConfig::try_new_exec(partitions, schema, None).unwrap();
        let plan = Arc::new(SortPreservingMergeExec::new(sort, exec));

        Self {
            runtime,
            task_ctx,
            plan,
        }
    }

    /// Test SortExec in  "partitioned" mode followed by a SortPreservingMerge
    fn sort_merge(batch_size: usize, partitions: &[Vec<RecordBatch>]) -> Self {
        let runtime = tokio::runtime::Builder::new_multi_thread().build().unwrap();
        let session_ctx = SessionContext::new_with_config(
            SessionConfig::new().with_batch_size(batch_size),
        );
        let task_ctx = session_ctx.task_ctx();

        let schema = partitions[0][0].schema();
        let sort = make_sort_exprs(schema.as_ref());

        let source = MemorySourceConfig::try_new_exec(partitions, schema, None).unwrap();
        let exec = SortExec::new(sort.clone(), source).with_preserve_partitioning(true);
        let plan = Arc::new(SortPreservingMergeExec::new(sort, Arc::new(exec)));

        Self {
            runtime,
            task_ctx,
            plan,
        }
    }

    /// Test SortExec in "partitioned" mode which sorts the input streams
    /// individually into some number of output streams
    fn sort(batch_size: usize, partitions: &[Vec<RecordBatch>]) -> Self {
        let runtime = tokio::runtime::Builder::new_multi_thread().build().unwrap();
        let session_ctx = SessionContext::new_with_config(
            SessionConfig::new().with_batch_size(batch_size),
        );
        let task_ctx = session_ctx.task_ctx();

        let schema = partitions[0][0].schema();
        let sort = make_sort_exprs(schema.as_ref());

        let exec = MemorySourceConfig::try_new_exec(partitions, schema, None).unwrap();
        let exec = Arc::new(CoalescePartitionsExec::new(exec));
        let plan = Arc::new(SortExec::new(sort, exec));

        Self {
            runtime,
            task_ctx,
            plan,
        }
    }

    /// Test SortExec in "partitioned" mode which sorts the input streams
    /// individually into some number of output streams
    fn sort_partitioned(batch_size: usize, partitions: &[Vec<RecordBatch>]) -> Self {
        let runtime = tokio::runtime::Builder::new_multi_thread().build().unwrap();
        let session_ctx = SessionContext::new_with_config(
            SessionConfig::new().with_batch_size(batch_size),
        );
        let task_ctx = session_ctx.task_ctx();

        let schema = partitions[0][0].schema();
        let sort = make_sort_exprs(schema.as_ref());

        let source = MemorySourceConfig::try_new_exec(partitions, schema, None).unwrap();
        let exec = SortExec::new(sort, source).with_preserve_partitioning(true);
        let plan = Arc::new(CoalescePartitionsExec::new(Arc::new(exec)));

        Self {
            runtime,
            task_ctx,
            plan,
        }
    }

    /// runs the specified plan to completion, draining all input and
    /// panic'ing on error
    fn run(&self) {
        let plan = Arc::clone(&self.plan);
        let task_ctx = Arc::clone(&self.task_ctx);

        assert_eq!(plan.output_partitioning().partition_count(), 1);

        self.runtime.block_on(async move {
            let mut stream = plan.execute(0, task_ctx).unwrap();
            while let Some(b) = stream.next().await {
                b.expect("unexpected execution error");
            }
        })
    }
}

const EXTRA_COLUMN_NAME_PREFIX: &str = "extra_";

/// Make sort exprs for each column in `schema`, skipping non-sort payload
/// columns added by [`with_extra_columns`].
fn make_sort_exprs(schema: &Schema) -> LexOrdering {
    let sort_exprs = schema
        .fields()
        .iter()
        .filter(|f| !f.name().starts_with(EXTRA_COLUMN_NAME_PREFIX))
        .map(|f| PhysicalSortExpr::new_default(col(f.name(), schema).unwrap()));
    LexOrdering::new(sort_exprs).unwrap()
}

/// Create streams of int64 (where approximately 1/3 values is repeated)
fn i64_streams(sorted: bool, input_size: u64) -> PartitionedBatches {
    let mut values = DataGenerator::new(input_size).i64_values();
    if sorted {
        values.sort_unstable();
    }

    split_tuples(values, build_i64_batch)
}

/// Build a single-column i64 [`RecordBatch`].
fn build_i64_batch(v: Vec<i64>) -> RecordBatch {
    let array = Int64Array::from(v);
    RecordBatch::try_from_iter(vec![("i64", Arc::new(array) as _)]).unwrap()
}

/// Build a single-column utf8 view [`RecordBatch`] under the given column name.
fn build_utf8_view_batch(name: &str, v: Vec<Option<Arc<str>>>) -> RecordBatch {
    let array: StringViewArray = v.into_iter().collect();
    RecordBatch::try_from_iter(vec![(name, Arc::new(array) as _)]).unwrap()
}

/// Create streams of f64 (where approximately 1/3 values are repeated)
/// with the same distribution as i64_streams
fn f64_streams(sorted: bool, input_size: u64) -> PartitionedBatches {
    let mut values = DataGenerator::new(input_size).f64_values();
    if sorted {
        values.sort_unstable_by(|a, b| a.total_cmp(b));
    }

    split_tuples(values, |v| {
        let array = Float64Array::from(v);
        RecordBatch::try_from_iter(vec![("f64", Arc::new(array) as _)]).unwrap()
    })
}

/// Create streams of random low cardinality utf8 values
fn utf8_low_cardinality_streams(sorted: bool, input_size: u64) -> PartitionedBatches {
    let mut values = DataGenerator::new(input_size).utf8_low_cardinality_values();
    if sorted {
        values.sort_unstable();
    }
    split_tuples(values, |v| {
        let array: StringArray = v.into_iter().collect();
        RecordBatch::try_from_iter(vec![("utf_low", Arc::new(array) as _)]).unwrap()
    })
}

/// Create streams of random low cardinality utf8_view values
fn utf8_view_low_cardinality_streams(
    sorted: bool,
    input_size: u64,
) -> PartitionedBatches {
    let mut values = DataGenerator::new(input_size).utf8_low_cardinality_values();
    if sorted {
        values.sort_unstable();
    }
    split_tuples(values, |v| build_utf8_view_batch("utf_view_low", v))
}

/// Create streams of high  cardinality (~ no duplicates) utf8_view values
fn utf8_view_high_cardinality_streams(
    sorted: bool,
    input_size: u64,
) -> PartitionedBatches {
    let mut values = DataGenerator::new(input_size).utf8_high_cardinality_values();
    if sorted {
        values.sort_unstable();
    }
    split_tuples(values, |v| build_utf8_view_batch("utf_view_high", v))
}

/// Create streams of high  cardinality (~ no duplicates) utf8 values
fn utf8_high_cardinality_streams(sorted: bool, input_size: u64) -> PartitionedBatches {
    let mut values = DataGenerator::new(input_size).utf8_high_cardinality_values();
    if sorted {
        values.sort_unstable();
    }
    split_tuples(values, |v| {
        let array: StringArray = v.into_iter().collect();
        RecordBatch::try_from_iter(vec![("utf_high", Arc::new(array) as _)]).unwrap()
    })
}

/// Create a batch of (utf8_low, utf8_low, utf8_high)
fn utf8_tuple_streams(sorted: bool, input_size: u64) -> PartitionedBatches {
    let mut data_gen = DataGenerator::new(input_size);

    // need to sort by the combined key, so combine them together
    let mut tuples: Vec<_> = data_gen
        .utf8_low_cardinality_values()
        .into_iter()
        .zip(data_gen.utf8_low_cardinality_values())
        .zip(data_gen.utf8_high_cardinality_values())
        .collect();

    if sorted {
        tuples.sort_unstable();
    }

    split_tuples(tuples, |tuples| {
        let (tuples, utf8_high): (Vec<_>, Vec<_>) = tuples.into_iter().unzip();
        let (utf8_low1, utf8_low2): (Vec<_>, Vec<_>) = tuples.into_iter().unzip();

        let utf8_high: StringArray = utf8_high.into_iter().collect();
        let utf8_low1: StringArray = utf8_low1.into_iter().collect();
        let utf8_low2: StringArray = utf8_low2.into_iter().collect();

        RecordBatch::try_from_iter(vec![
            ("utf_low1", Arc::new(utf8_low1) as _),
            ("utf_low2", Arc::new(utf8_low2) as _),
            ("utf_high", Arc::new(utf8_high) as _),
        ])
        .unwrap()
    })
}

/// Create a batch of (utf8_view_low, utf8_view_low, utf8_view_high)
fn utf8_view_tuple_streams(sorted: bool, input_size: u64) -> PartitionedBatches {
    let mut data_gen = DataGenerator::new(input_size);

    // need to sort by the combined key, so combine them together
    let mut tuples: Vec<_> = data_gen
        .utf8_low_cardinality_values()
        .into_iter()
        .zip(data_gen.utf8_low_cardinality_values())
        .zip(data_gen.utf8_high_cardinality_values())
        .collect();

    if sorted {
        tuples.sort_unstable();
    }

    split_tuples(tuples, |tuples| {
        let (tuples, utf8_high): (Vec<_>, Vec<_>) = tuples.into_iter().unzip();
        let (utf8_low1, utf8_low2): (Vec<_>, Vec<_>) = tuples.into_iter().unzip();

        let utf8_view_high: StringViewArray = utf8_high.into_iter().collect();
        let utf8_view_low1: StringViewArray = utf8_low1.into_iter().collect();
        let utf8_view_low2: StringViewArray = utf8_low2.into_iter().collect();

        RecordBatch::try_from_iter(vec![
            ("utf_view_low1", Arc::new(utf8_view_low1) as _),
            ("utf_view_low2", Arc::new(utf8_view_low2) as _),
            ("utf_view_high", Arc::new(utf8_view_high) as _),
        ])
        .unwrap()
    })
}

/// Create a batch of (f64, utf8_low, utf8_low, i64)
fn mixed_tuple_streams(sorted: bool, input_size: u64) -> PartitionedBatches {
    let mut data_gen = DataGenerator::new(input_size);

    // need to sort by the combined key, so combine them together
    let mut tuples: Vec<_> = data_gen
        .i64_values()
        .into_iter()
        .zip(data_gen.utf8_low_cardinality_values())
        .zip(data_gen.utf8_low_cardinality_values())
        .zip(data_gen.i64_values())
        .collect();

    if sorted {
        tuples.sort_unstable();
    }

    split_tuples(tuples, build_mixed_tuple_batch)
}

/// The tuple shape used by the `mixed tuple` case: (i64, utf8_low, utf8_low, i64)
type MixedTuple = (((i64, Option<Arc<str>>), Option<Arc<str>>), i64);

/// Build a (f64, utf8_low, utf8_low, i64) batch from [`MixedTuple`]s
/// (the leading i64 becomes the f64 column).
fn build_mixed_tuple_batch(tuples: Vec<MixedTuple>) -> RecordBatch {
    let (tuples, i64_values): (Vec<_>, Vec<_>) = tuples.into_iter().unzip();
    let (tuples, utf8_low2): (Vec<_>, Vec<_>) = tuples.into_iter().unzip();
    let (f64_values, utf8_low1): (Vec<_>, Vec<_>) = tuples.into_iter().unzip();

    let f64_values: Float64Array = f64_values.into_iter().map(|v| v as f64).collect();

    let utf8_low1: StringArray = utf8_low1.into_iter().collect();
    let utf8_low2: StringArray = utf8_low2.into_iter().collect();
    let i64_values: Int64Array = i64_values.into_iter().collect();

    RecordBatch::try_from_iter(vec![
        ("f64", Arc::new(f64_values) as _),
        ("utf_low1", Arc::new(utf8_low1) as _),
        ("utf_low2", Arc::new(utf8_low2) as _),
        ("i64", Arc::new(i64_values) as _),
    ])
    .unwrap()
}

/// Create a batch of (f64, utf8_view_low, utf8_view_low, i64)
fn mixed_tuple_with_utf8_view_streams(
    sorted: bool,
    input_size: u64,
) -> PartitionedBatches {
    let mut data_gen = DataGenerator::new(input_size);

    // need to sort by the combined key, so combine them together
    let mut tuples: Vec<_> = data_gen
        .i64_values()
        .into_iter()
        .zip(data_gen.utf8_low_cardinality_values())
        .zip(data_gen.utf8_low_cardinality_values())
        .zip(data_gen.i64_values())
        .collect();

    if sorted {
        tuples.sort_unstable();
    }

    split_tuples(tuples, |tuples| {
        let (tuples, i64_values): (Vec<_>, Vec<_>) = tuples.into_iter().unzip();
        let (tuples, utf8_low2): (Vec<_>, Vec<_>) = tuples.into_iter().unzip();
        let (f64_values, utf8_low1): (Vec<_>, Vec<_>) = tuples.into_iter().unzip();

        let f64_values: Float64Array = f64_values.into_iter().map(|v| v as f64).collect();

        let utf8_view_low1: StringViewArray = utf8_low1.into_iter().collect();
        let utf8_view_low2: StringViewArray = utf8_low2.into_iter().collect();
        let i64_values: Int64Array = i64_values.into_iter().collect();

        RecordBatch::try_from_iter(vec![
            ("f64", Arc::new(f64_values) as _),
            ("utf_view_low1", Arc::new(utf8_view_low1) as _),
            ("utf_view_low2", Arc::new(utf8_view_low2) as _),
            ("i64", Arc::new(i64_values) as _),
        ])
        .unwrap()
    })
}

/// Create a batch of (utf8_dict)
fn dictionary_streams(sorted: bool, input_size: u64) -> PartitionedBatches {
    let mut data_gen = DataGenerator::new(input_size);
    let mut values = data_gen.utf8_low_cardinality_values();
    if sorted {
        values.sort_unstable();
    }

    split_tuples(values, |v| {
        let dictionary: DictionaryArray<Int32Type> =
            v.iter().map(Option::as_deref).collect();
        RecordBatch::try_from_iter(vec![("dict", Arc::new(dictionary) as _)]).unwrap()
    })
}

/// Create a batch of (utf8_dict, utf8_dict, utf8_dict)
fn dictionary_tuple_streams(sorted: bool, input_size: u64) -> PartitionedBatches {
    let mut data_gen = DataGenerator::new(input_size);
    let mut tuples: Vec<_> = data_gen
        .utf8_low_cardinality_values()
        .into_iter()
        .zip(data_gen.utf8_low_cardinality_values())
        .zip(data_gen.utf8_low_cardinality_values())
        .collect();

    if sorted {
        tuples.sort_unstable();
    }

    split_tuples(tuples, |tuples| {
        let (tuples, c): (Vec<_>, Vec<_>) = tuples.into_iter().unzip();
        let (a, b): (Vec<_>, Vec<_>) = tuples.into_iter().unzip();

        let a: DictionaryArray<Int32Type> = a.iter().map(Option::as_deref).collect();
        let b: DictionaryArray<Int32Type> = b.iter().map(Option::as_deref).collect();
        let c: DictionaryArray<Int32Type> = c.iter().map(Option::as_deref).collect();

        RecordBatch::try_from_iter(vec![
            ("a", Arc::new(a) as _),
            ("b", Arc::new(b) as _),
            ("c", Arc::new(c) as _),
        ])
        .unwrap()
    })
}

/// Create a batch of (utf8_dict, utf8_dict, utf8_dict, i64)
fn mixed_dictionary_tuple_streams(sorted: bool, input_size: u64) -> PartitionedBatches {
    let mut data_gen = DataGenerator::new(input_size);
    let mut tuples: Vec<_> = data_gen
        .utf8_low_cardinality_values()
        .into_iter()
        .zip(data_gen.utf8_low_cardinality_values())
        .zip(data_gen.utf8_low_cardinality_values())
        .zip(data_gen.i64_values())
        .collect();

    if sorted {
        tuples.sort_unstable();
    }

    split_tuples(tuples, |tuples| {
        let (tuples, d): (Vec<_>, Vec<_>) = tuples.into_iter().unzip();
        let (tuples, c): (Vec<_>, Vec<_>) = tuples.into_iter().unzip();
        let (a, b): (Vec<_>, Vec<_>) = tuples.into_iter().unzip();

        let a: DictionaryArray<Int32Type> = a.iter().map(Option::as_deref).collect();
        let b: DictionaryArray<Int32Type> = b.iter().map(Option::as_deref).collect();
        let c: DictionaryArray<Int32Type> = c.iter().map(Option::as_deref).collect();
        let d: Int64Array = d.into_iter().collect();

        RecordBatch::try_from_iter(vec![
            ("a", Arc::new(a) as _),
            ("b", Arc::new(b) as _),
            ("c", Arc::new(c) as _),
            ("d", Arc::new(d) as _),
        ])
        .unwrap()
    })
}

/// Encapsulates creating data for this test
struct DataGenerator {
    rng: StdRng,
    input_size: u64,
}

impl DataGenerator {
    fn new(input_size: u64) -> Self {
        Self {
            rng: StdRng::seed_from_u64(42),
            input_size,
        }
    }

    /// Create an array of i64 sorted values (where approximately 1/3 values is repeated)
    fn i64_values(&mut self) -> Vec<i64> {
        let mut vec: Vec<_> = (0..self.input_size)
            .map(|_| self.rng.random_range(0..self.input_size as i64))
            .collect();

        vec.sort_unstable();

        // 6287 distinct / 10000 total
        //let num_distinct = vec.iter().collect::<HashSet<_>>().len();
        //println!("{} distinct / {} total", num_distinct, vec.len());
        vec
    }

    /// Create an array of f64 sorted values (with same distribution of `i64_values`)
    fn f64_values(&mut self) -> Vec<f64> {
        self.i64_values().into_iter().map(|v| v as f64).collect()
    }

    /// array of low cardinality (100 distinct) values
    fn utf8_low_cardinality_values(&mut self) -> Vec<Option<Arc<str>>> {
        let strings = (0..100)
            .map(|s| format!("value{s}").into())
            .collect::<Vec<_>>();

        // pick from the 100 strings randomly
        let mut input = (0..self.input_size)
            .map(|_| {
                let idx = self.rng.random_range(0..strings.len());
                let s = Arc::clone(&strings[idx]);
                Some(s)
            })
            .collect::<Vec<_>>();

        input.sort_unstable();
        input
    }

    /// Create sorted values of high  cardinality (~ no duplicates) utf8 values
    fn utf8_high_cardinality_values(&mut self) -> Vec<Option<Arc<str>>> {
        // make random strings
        let mut input = (0..self.input_size)
            .map(|_| Some(self.random_string().into()))
            .collect::<Vec<_>>();

        input.sort_unstable();
        input
    }

    fn random_string(&mut self) -> String {
        let rng = &mut self.rng;
        rng.sample_iter(rand::distr::Alphanumeric)
            .filter(|c| c.is_ascii_alphabetic())
            .take(20)
            .map(char::from)
            .collect::<String>()
    }

    /// i64 values with the given cardinality (initial order is irrelevant since
    /// callers reorder via [`DataProfile::apply`]).
    fn i64_values_by(&mut self, card: Cardinality) -> Vec<i64> {
        match card {
            Cardinality::Low => self.i64_values(),
            // Full i64 range -> effectively unique (minimal overlap)
            Cardinality::High => {
                (0..self.input_size).map(|_| self.rng.random()).collect()
            }
        }
    }

    /// utf8 values with the given cardinality.
    fn utf8_values_by(&mut self, card: Cardinality) -> Vec<Option<Arc<str>>> {
        match card {
            Cardinality::Low => self.utf8_low_cardinality_values(),
            Cardinality::High => self.utf8_high_cardinality_values(),
        }
    }
}

/// Splits the `input` tuples randomly into batches of `BATCH_SIZE` distributed across
/// `NUM_STREAMS` partitions, preserving any ordering
///
/// `f` is function that takes a list of tuples and produces a [`RecordBatch`]
fn split_tuples<T, F>(input: Vec<T>, f: F) -> PartitionedBatches
where
    F: Fn(Vec<T>) -> RecordBatch,
{
    // figure out which inputs go where
    let mut rng = StdRng::seed_from_u64(1337);

    let mut outputs: Vec<Vec<Vec<T>>> = (0..NUM_STREAMS).map(|_| Vec::new()).collect();

    for i in input {
        let stream_idx = rng.random_range(0..NUM_STREAMS);
        let stream = &mut outputs[stream_idx];
        match stream.last_mut() {
            Some(x) if x.len() < BATCH_SIZE => x.push(i),
            _ => {
                let mut v = Vec::with_capacity(BATCH_SIZE);
                v.push(i);
                stream.push(v)
            }
        }
    }

    outputs
        .into_iter()
        .map(|stream| stream.into_iter().map(&f).collect())
        .collect()
}

fn create_single_partition<T, F>(
    input: Vec<T>,
    f: F,
    batch_size: usize,
) -> Vec<RecordBatch>
where
    F: Fn(Vec<T>) -> RecordBatch,
{
    input
        .into_iter()
        .chunks(batch_size)
        .into_iter()
        .map(|x| f(x.collect_vec()))
        .collect()
}

/// Read a duration (seconds, may be fractional) from `var`. panics if set to a value that isn't a number.
fn env_duration(var: &str) -> Option<Duration> {
    let s = std::env::var(var).ok()?;

    let secs = s
        .parse::<f64>()
        .unwrap_or_else(|e| panic!("invalid {var}={s:?}: {e}"));

    Some(Duration::from_secs_f64(secs))
}

/// Read a `usize` from `var`. panics if set to a value that isn't an integer.
fn env_usize(var: &str) -> Option<usize> {
    let s = std::env::var(var).ok()?;

    Some(
        s.parse::<usize>()
            .unwrap_or_else(|e| panic!("invalid {var}={s:?}: {e}")),
    )
}

type AxisGenerator = Box<dyn Fn(DataProfile, Cardinality, usize) -> Vec<RecordBatch>>;

/// Benchmarks `SortExec` (at the 1M input size) on single partition across the following axes:
/// 1. Sort columns
///     - single column with a specialized impl (primitive or byte(view))
///     - multiple columns, which will use fallback impl
/// 2. Number of columns in the record batch - more columns mean more data to
///    copy while reordering and more memory to hold
/// 3. Value cardinality - whether the sort-key values overlap or not
/// 4. Input ordering - already sorted / unsorted / nearly sorted
fn sort_axis_benchmark(c: &mut Criterion) {
    let input_size = 1_000_000u64;
    let size_label = "1M";

    const AXIS_BATCH_SIZE: usize = 8192;

    let cases: Vec<(&str, AxisGenerator)> = vec![
        (
            "i64",
            Box::new(move |p, card, extra| {
                i64_axis(p, card, extra, input_size, AXIS_BATCH_SIZE)
            }),
        ),
        (
            "utf8 view",
            Box::new(move |p, card, extra| {
                utf8_view_axis(p, card, extra, input_size, AXIS_BATCH_SIZE)
            }),
        ),
        (
            "mixed tuple",
            Box::new(move |p, card, extra| {
                mixed_tuple_axis(p, card, extra, input_size, AXIS_BATCH_SIZE)
            }),
        ),
    ];

    let mut group = c.benchmark_group("sort_axis");

    if let Some(sample_size) = env_usize("SORT_AXIS_SAMPLE_SIZE") {
        group.sample_size(sample_size);
    }

    if let Some(warm_up_time) = env_duration("SORT_AXIS_WARMUP_SECS") {
        group.warm_up_time(warm_up_time);
    }

    if let Some(measurement_time) = env_duration("SORT_AXIS_MEASUREMENT_SECS") {
        group.measurement_time(measurement_time);
    }

    for (name, f) in &cases {
        for card in [Cardinality::Low, Cardinality::High] {
            for &extra in EXTRA_COLUMN_COUNTS {
                for profile in [
                    DataProfile::Sorted,
                    DataProfile::Unsorted,
                    DataProfile::NearlySorted,
                ] {
                    group.bench_function(
                        format!(
                            "sort {name} {size_label} {card:?} cardinality {profile:?} +{extra}cols",
                        ),
                        |b| {
                            let data = f(profile, card, extra);
                            let case = BenchCase::sort_partitioned(AXIS_BATCH_SIZE, &[data]);
                            b.iter(move || case.run())
                        },
                    );
                }
            }
        }
    }

    group.finish();
}

/// Single-column i64 batches
fn i64_axis(
    profile: DataProfile,
    card: Cardinality,
    extra: usize,
    input_size: u64,
    batch_size: usize,
) -> Vec<RecordBatch> {
    let values = profile.apply(DataGenerator::new(input_size).i64_values_by(card));
    let batches = create_single_partition(values, build_i64_batch, batch_size);
    with_extra_columns(batches, extra)
}

/// Single-column utf8 view batches
fn utf8_view_axis(
    profile: DataProfile,
    card: Cardinality,
    extra: usize,
    input_size: u64,
    batch_size: usize,
) -> Vec<RecordBatch> {
    let values = profile.apply(DataGenerator::new(input_size).utf8_values_by(card));
    let batches = create_single_partition(
        values,
        |v| build_utf8_view_batch("utf_view", v),
        batch_size,
    );
    with_extra_columns(batches, extra)
}

/// Multi-column (f64, utf8, utf8, i64) batches.
fn mixed_tuple_axis(
    profile: DataProfile,
    card: Cardinality,
    extra: usize,
    input_size: u64,
    batch_size: usize,
) -> Vec<RecordBatch> {
    let mut data_gen = DataGenerator::new(input_size);
    let tuples: Vec<MixedTuple> = data_gen
        .i64_values_by(card)
        .into_iter()
        .zip(data_gen.utf8_values_by(card))
        .zip(data_gen.utf8_values_by(card))
        .zip(data_gen.i64_values_by(card))
        .collect();
    let batches = create_single_partition(
        profile.apply(tuples),
        build_mixed_tuple_batch,
        batch_size,
    );
    with_extra_columns(batches, extra)
}

/// Append `n` extra non-sort-key payload columns to every batch, split across i64, string, string view and dictionary
fn with_extra_columns(batches: Vec<RecordBatch>, n: usize) -> Vec<RecordBatch> {
    if n == 0 {
        return batches;
    }
    let mut rng = StdRng::seed_from_u64(7);

    type Generator = Box<dyn Fn(&mut DataGenerator) -> ArrayRef>;

    let generators: Vec<Generator> = vec![
        Box::new(|data_gen: &mut DataGenerator| {
            let arr = Int64Array::from_iter_values(data_gen.i64_values());

            Arc::new(arr)
        }),
        Box::new(|data_gen: &mut DataGenerator| {
            let values = data_gen.utf8_low_cardinality_values();
            let arr: StringArray = values.iter().map(|item| item.as_deref()).collect();

            Arc::new(arr)
        }),
        Box::new(|data_gen: &mut DataGenerator| {
            let values = data_gen.utf8_low_cardinality_values();
            let mut builder =
                StringViewBuilder::with_capacity(values.len()).with_deduplicate_strings();
            for v in values {
                builder.append_option(v.as_deref());
            }

            let arr = builder.finish();

            Arc::new(arr)
        }),
        Box::new(|data_gen: &mut DataGenerator| {
            let values = data_gen.utf8_low_cardinality_values();

            let arr: DictionaryArray<Int32Type> =
                values.iter().map(|item| item.as_deref()).collect();

            Arc::new(arr)
        }),
    ];

    let generator_index = (0..n)
        .map(|_| rng.random_range(0..generators.len()))
        .collect::<Vec<_>>();

    let mut generator = DataGenerator { input_size: 1, rng };

    batches
        .into_iter()
        .map(|batch| {
            let num_rows = batch.num_rows();
            let mut fields = batch.schema().fields().iter().cloned().collect::<Vec<_>>();
            let mut columns = batch.columns().to_vec();
            generator.input_size = num_rows as u64;

            for (col_index, gen_index) in generator_index.iter().enumerate() {
                let gen_fn = &generators[*gen_index];

                let array = gen_fn(&mut generator);
                fields.push(Arc::new(Field::new(
                    format!("{EXTRA_COLUMN_NAME_PREFIX}{col_index}"),
                    array.data_type().clone(),
                    array.logical_null_count() > 0,
                )));
                columns.push(array);
            }

            RecordBatch::try_new(Arc::new(Schema::new(fields)), columns).unwrap()
        })
        .collect()
}

criterion_group!(benches, criterion_benchmark, sort_axis_benchmark);
criterion_main!(benches);