arrow 60.2.0

Rust implementation of Apache Arrow
Documentation
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements

#[macro_use]
extern crate criterion;
use criterion::Criterion;

use std::sync::Arc;

use arrow::compute::{SortColumn, lexsort, sort, sort_to_indices};
use arrow::datatypes::{Int16Type, Int32Type};
use arrow::util::bench_util::*;
use arrow::{array::*, datatypes::Float32Type};
use arrow_ord::rank::rank;
use std::hint;

fn bench_sort(array: &dyn Array) {
    hint::black_box(sort(array, None).unwrap());
}

fn bench_lexsort(array_a: &ArrayRef, array_b: &ArrayRef, limit: Option<usize>) {
    let columns = vec![
        SortColumn {
            values: array_a.clone(),
            options: None,
        },
        SortColumn {
            values: array_b.clone(),
            options: None,
        },
    ];

    hint::black_box(lexsort(&columns, limit).unwrap());
}

fn bench_sort_to_indices(array: &dyn Array, limit: Option<usize>) {
    hint::black_box(sort_to_indices(array, None, limit).unwrap());
}

fn add_benchmark(c: &mut Criterion) {
    let arr = create_primitive_array::<Int32Type>(2usize.pow(10), 0.0);
    c.bench_function("sort i32 2^10", |b| b.iter(|| bench_sort(&arr)));
    c.bench_function("sort i32 to indices 2^10", |b| {
        b.iter(|| bench_sort_to_indices(&arr, None))
    });

    let arr = create_primitive_array::<Int32Type>(2usize.pow(12), 0.0);
    c.bench_function("sort i32 2^12", |b| b.iter(|| bench_sort(&arr)));
    c.bench_function("sort i32 to indices 2^12", |b| {
        b.iter(|| bench_sort_to_indices(&arr, None))
    });

    let arr = create_primitive_array::<Int32Type>(2usize.pow(10), 0.5);
    c.bench_function("sort i32 nulls 2^10", |b| b.iter(|| bench_sort(&arr)));
    c.bench_function("sort i32 nulls to indices 2^10", |b| {
        b.iter(|| bench_sort_to_indices(&arr, None))
    });

    let arr = create_primitive_array::<Int32Type>(2usize.pow(12), 0.5);
    c.bench_function("sort i32 nulls 2^12", |b| b.iter(|| bench_sort(&arr)));
    c.bench_function("sort i32 nulls to indices 2^12", |b| {
        b.iter(|| bench_sort_to_indices(&arr, None))
    });

    let arr: ArrayRef = Arc::new(create_primitive_array::<Float32Type>(2_usize.pow(12), 0.0));
    c.bench_function("sort f32 2^12", |b| b.iter(|| bench_sort(&arr)));
    c.bench_function("sort f32 to indices 2^12", |b| {
        b.iter(|| bench_sort_to_indices(&arr, None))
    });

    let arr: ArrayRef = Arc::new(create_primitive_array::<Float32Type>(2usize.pow(12), 0.5));
    c.bench_function("sort f32 nulls 2^12", |b| b.iter(|| bench_sort(&arr)));
    c.bench_function("sort f32 nulls to indices 2^12", |b| {
        b.iter(|| bench_sort_to_indices(&arr, None))
    });

    let arr = create_string_array_with_max_len::<i32>(2usize.pow(12), 0.0, 10);
    c.bench_function("sort string[0-10] to indices 2^12", |b| {
        b.iter(|| bench_sort_to_indices(&arr, None))
    });

    let arr = create_string_array_with_max_len::<i32>(2usize.pow(12), 0.5, 10);
    c.bench_function("sort string[0-10] nulls to indices 2^12", |b| {
        b.iter(|| bench_sort_to_indices(&arr, None))
    });

    let arr = create_string_array_with_max_len::<i32>(2usize.pow(12), 0.0, 100);
    c.bench_function("sort string[0-100] to indices 2^12", |b| {
        b.iter(|| bench_sort_to_indices(&arr, None))
    });

    let arr = create_string_array_with_max_len::<i32>(2usize.pow(12), 0.5, 100);
    c.bench_function("sort string[0-100] nulls to indices 2^12", |b| {
        b.iter(|| bench_sort_to_indices(&arr, None))
    });

    let arr = create_string_array::<i32>(2usize.pow(12), 0.0);
    c.bench_function("sort string[0-400] to indices 2^12", |b| {
        b.iter(|| bench_sort_to_indices(&arr, None))
    });

    let arr = create_string_array::<i32>(2usize.pow(12), 0.5);
    c.bench_function("sort string[0-400] nulls to indices 2^12", |b| {
        b.iter(|| bench_sort_to_indices(&arr, None))
    });

    let arr = create_string_array_with_len::<i32>(2usize.pow(12), 0.0, 10);
    c.bench_function("sort string[10] to indices 2^12", |b| {
        b.iter(|| bench_sort_to_indices(&arr, None))
    });

    let arr = create_string_array_with_len::<i32>(2usize.pow(12), 0.5, 10);
    c.bench_function("sort string[10] nulls to indices 2^12", |b| {
        b.iter(|| bench_sort_to_indices(&arr, None))
    });

    let arr = create_string_array_with_len::<i32>(2usize.pow(12), 0.0, 100);
    c.bench_function("sort string[100] to indices 2^12", |b| {
        b.iter(|| bench_sort_to_indices(&arr, None))
    });

    let arr = create_string_array_with_len::<i32>(2usize.pow(12), 0.5, 100);
    c.bench_function("sort string[100] nulls to indices 2^12", |b| {
        b.iter(|| bench_sort_to_indices(&arr, None))
    });

    let arr = create_string_array_with_len::<i32>(2usize.pow(12), 0.0, 1000);
    c.bench_function("sort string[1000] to indices 2^12", |b| {
        b.iter(|| bench_sort_to_indices(&arr, None))
    });

    let arr = create_string_array_with_len::<i32>(2usize.pow(12), 0.5, 1000);
    c.bench_function("sort string[1000] nulls to indices 2^12", |b| {
        b.iter(|| bench_sort_to_indices(&arr, None))
    });

    // This will generate string view arrays with 2^12 elements, each with a length fixed 10, and without nulls.
    let arr = create_string_view_array_with_fixed_len(2usize.pow(12), 0.0, 10);
    c.bench_function("sort string_view[10] to indices 2^12", |b| {
        b.iter(|| bench_sort_to_indices(&arr, None))
    });

    // This will generate string view arrays with 2^12 elements, each with a length fixed 10, and with 50% nulls.
    let arr = create_string_view_array_with_fixed_len(2usize.pow(12), 0.5, 10);
    c.bench_function("sort string_view[10] nulls to indices 2^12", |b| {
        b.iter(|| bench_sort_to_indices(&arr, None))
    });

    // This will generate string view arrays with 2^12 elements, each with a length randomly chosen from 0 to max 400, and without nulls.
    let arr = create_string_view_array(2usize.pow(12), 0.0);
    c.bench_function("sort string_view[0-400] to indices 2^12", |b| {
        b.iter(|| bench_sort_to_indices(&arr, None))
    });

    // This will generate string view arrays with 2^12 elements, each with a length randomly chosen from 0 to max 400, and with 50% nulls.
    let arr = create_string_view_array(2usize.pow(12), 0.5);
    c.bench_function("sort string_view[0-400] nulls to indices 2^12", |b| {
        b.iter(|| bench_sort_to_indices(&arr, None))
    });

    // This will generate string view arrays with 2^12 elements, each with a length < 12 bytes which is inlined data, and without nulls.
    let arr = create_string_view_array_with_max_len(2usize.pow(12), 0.0, 12);
    c.bench_function("sort string_view_inlined[0-12] to indices 2^12", |b| {
        b.iter(|| bench_sort_to_indices(&arr, None))
    });

    // This will generate string view arrays with 2^12 elements, each with a length < 12 bytes which is inlined data, and with 50% nulls.
    let arr = create_string_view_array_with_max_len(2usize.pow(12), 0.5, 12);
    c.bench_function(
        "sort string_view_inlined[0-12] nulls to indices 2^12",
        |b| b.iter(|| bench_sort_to_indices(&arr, None)),
    );

    let arr = create_string_dict_array::<Int32Type>(2usize.pow(12), 0.0, 10);
    c.bench_function("sort string[10] dict to indices 2^12", |b| {
        b.iter(|| bench_sort_to_indices(&arr, None))
    });

    let arr = create_string_dict_array::<Int32Type>(2usize.pow(12), 0.5, 10);
    c.bench_function("sort string[10] dict nulls to indices 2^12", |b| {
        b.iter(|| bench_sort_to_indices(&arr, None))
    });

    let run_encoded_array =
        create_primitive_run_array::<Int16Type, Int32Type>(2usize.pow(12), 2usize.pow(10));

    c.bench_function("sort primitive run 2^12", |b| {
        b.iter(|| bench_sort(&run_encoded_array))
    });

    c.bench_function("sort primitive run to indices 2^12", |b| {
        b.iter(|| bench_sort_to_indices(&run_encoded_array, None))
    });

    let arr_a: ArrayRef = Arc::new(create_primitive_array::<Float32Type>(2usize.pow(10), 0.0));
    let arr_b: ArrayRef = Arc::new(create_primitive_array::<Float32Type>(2usize.pow(10), 0.0));

    c.bench_function("lexsort (f32, f32) 2^10", |b| {
        b.iter(|| bench_lexsort(&arr_a, &arr_b, None))
    });

    let arr_a: ArrayRef = Arc::new(create_primitive_array::<Float32Type>(2usize.pow(12), 0.0));
    let arr_b: ArrayRef = Arc::new(create_primitive_array::<Float32Type>(2usize.pow(12), 0.0));

    c.bench_function("lexsort (f32, f32) 2^12", |b| {
        b.iter(|| bench_lexsort(&arr_a, &arr_b, None))
    });

    let arr_a: ArrayRef = Arc::new(create_primitive_array::<Float32Type>(2usize.pow(10), 0.5));
    let arr_b: ArrayRef = Arc::new(create_primitive_array::<Float32Type>(2usize.pow(10), 0.5));

    c.bench_function("lexsort (f32, f32) nulls 2^10", |b| {
        b.iter(|| bench_lexsort(&arr_a, &arr_b, None))
    });

    let arr_a: ArrayRef = Arc::new(create_primitive_array::<Float32Type>(2usize.pow(12), 0.5));
    let arr_b: ArrayRef = Arc::new(create_primitive_array::<Float32Type>(2usize.pow(12), 0.5));

    c.bench_function("lexsort (f32, f32) nulls 2^12", |b| {
        b.iter(|| bench_lexsort(&arr_a, &arr_b, None))
    });

    let arr_a: ArrayRef = Arc::new(create_boolean_array(2usize.pow(12), 0.0, 0.5));
    let arr_b: ArrayRef = Arc::new(create_boolean_array(2usize.pow(12), 0.0, 0.5));
    c.bench_function("lexsort (bool, bool) 2^12", |b| {
        b.iter(|| bench_lexsort(&arr_a, &arr_b, None))
    });

    let arr_a: ArrayRef = Arc::new(create_boolean_array(2usize.pow(12), 0.5, 0.5));
    let arr_b: ArrayRef = Arc::new(create_boolean_array(2usize.pow(12), 0.5, 0.5));
    c.bench_function("lexsort (bool, bool) nulls 2^12", |b| {
        b.iter(|| bench_lexsort(&arr_a, &arr_b, None))
    });

    let arr_a: ArrayRef = Arc::new(create_primitive_array::<Float32Type>(2usize.pow(12), 0.0));
    let arr_b: ArrayRef = Arc::new(create_primitive_array::<Float32Type>(2usize.pow(12), 0.0));
    c.bench_function("lexsort (f32, f32) 2^12 limit 10", |b| {
        b.iter(|| bench_lexsort(&arr_a, &arr_b, Some(10)))
    });

    let arr_a: ArrayRef = Arc::new(create_primitive_array::<Float32Type>(2usize.pow(12), 0.0));
    let arr_b: ArrayRef = Arc::new(create_primitive_array::<Float32Type>(2usize.pow(12), 0.0));
    c.bench_function("lexsort (f32, f32) 2^12 limit 100", |b| {
        b.iter(|| bench_lexsort(&arr_a, &arr_b, Some(100)))
    });

    let arr_a: ArrayRef = Arc::new(create_primitive_array::<Float32Type>(2usize.pow(12), 0.0));
    let arr_b: ArrayRef = Arc::new(create_primitive_array::<Float32Type>(2usize.pow(12), 0.0));
    c.bench_function("lexsort (f32, f32) 2^12 limit 1000", |b| {
        b.iter(|| bench_lexsort(&arr_a, &arr_b, Some(1000)))
    });

    let arr_a: ArrayRef = Arc::new(create_primitive_array::<Float32Type>(2usize.pow(12), 0.0));
    let arr_b: ArrayRef = Arc::new(create_primitive_array::<Float32Type>(2usize.pow(12), 0.0));
    c.bench_function("lexsort (f32, f32) 2^12 limit 2^12", |b| {
        b.iter(|| bench_lexsort(&arr_a, &arr_b, Some(2usize.pow(12))))
    });

    let arr_a: ArrayRef = Arc::new(create_primitive_array::<Float32Type>(2usize.pow(12), 0.5));
    let arr_b: ArrayRef = Arc::new(create_primitive_array::<Float32Type>(2usize.pow(12), 0.5));

    c.bench_function("lexsort (f32, f32) nulls 2^12 limit 10", |b| {
        b.iter(|| bench_lexsort(&arr_a, &arr_b, Some(10)))
    });
    c.bench_function("lexsort (f32, f32) nulls 2^12 limit 100", |b| {
        b.iter(|| bench_lexsort(&arr_a, &arr_b, Some(100)))
    });
    c.bench_function("lexsort (f32, f32) nulls 2^12 limit 1000", |b| {
        b.iter(|| bench_lexsort(&arr_a, &arr_b, Some(1000)))
    });
    c.bench_function("lexsort (f32, f32) nulls 2^12 limit 2^12", |b| {
        b.iter(|| bench_lexsort(&arr_a, &arr_b, Some(2usize.pow(12))))
    });

    let arr: ArrayRef = Arc::new(create_primitive_array::<Float32Type>(2usize.pow(12), 0.0));
    c.bench_function("rank f32 2^12", |b| {
        b.iter(|| hint::black_box(rank(&arr, None).unwrap()))
    });

    let arr: ArrayRef = Arc::new(create_primitive_array::<Float32Type>(2usize.pow(12), 0.5));
    c.bench_function("rank f32 nulls 2^12", |b| {
        b.iter(|| hint::black_box(rank(&arr, None).unwrap()))
    });

    let arr = create_string_array_with_len::<i32>(2usize.pow(12), 0.0, 10);
    c.bench_function("rank string[10] 2^12", |b| {
        b.iter(|| hint::black_box(rank(&arr, None).unwrap()))
    });

    let arr = create_string_array_with_len::<i32>(2usize.pow(12), 0.5, 10);
    c.bench_function("rank string[10] nulls 2^12", |b| {
        b.iter(|| hint::black_box(rank(&arr, None).unwrap()))
    });

    let arr = create_string_view_array_with_fixed_len(2usize.pow(12), 0.0, 10);
    c.bench_function("rank string_view[10] 2^12", |b| {
        b.iter(|| hint::black_box(rank(&arr, None).unwrap()))
    });

    let arr = create_string_view_array_with_fixed_len(2usize.pow(12), 0.5, 10);
    c.bench_function("rank string_view[10] nulls 2^12", |b| {
        b.iter(|| hint::black_box(rank(&arr, None).unwrap()))
    });

    // Mostly-inline values with enough 13-byte values to retain a backing
    // buffer and therefore exercise the mixed view path.
    let arr = create_string_view_array_with_len_range_and_seed(2usize.pow(12), 0.0, 0..14, 42);
    c.bench_function("rank string_view[0-13] sparse long 2^12", |b| {
        b.iter(|| hint::black_box(rank(&arr, None).unwrap()))
    });

    let arr = create_string_view_array(2usize.pow(12), 0.0);
    c.bench_function("rank string_view[0-400] 2^12", |b| {
        b.iter(|| hint::black_box(rank(&arr, None).unwrap()))
    });

    let arr = create_string_view_array(2usize.pow(12), 0.5);
    c.bench_function("rank string_view[0-400] nulls 2^12", |b| {
        b.iter(|| hint::black_box(rank(&arr, None).unwrap()))
    });

    // All values are longer than the inline capacity and share a 7-byte
    // prefix, exercising collisions beyond the 4 bytes stored in the view.
    let arr = create_longer_string_view_array_with_same_prefix(2usize.pow(12), 0.0);
    c.bench_function("rank string_view[13-100] same prefix 2^12", |b| {
        b.iter(|| hint::black_box(rank(&arr, None).unwrap()))
    });

    let arr = create_longer_string_view_array_with_same_prefix(2usize.pow(12), 0.5);
    c.bench_function("rank string_view[13-100] same prefix nulls 2^12", |b| {
        b.iter(|| hint::black_box(rank(&arr, None).unwrap()))
    });

    // All values share the same first 16 bytes, providing a worst-case
    // collision input for prefix-key rank implementations.
    let arr: StringViewArray = (0..2_u32.pow(12))
        .map(|i| {
            let suffix = i.wrapping_mul(2_654_435_761);
            Some(format!("abcdefghijklmnop{suffix:08x}"))
        })
        .collect();
    c.bench_function("rank string_view[24] same 16-byte prefix 2^12", |b| {
        b.iter(|| hint::black_box(rank(&arr, None).unwrap()))
    });

    let arr: StringViewArray = (0..2_u32.pow(12))
        .map(|i| {
            (i % 2 == 0).then(|| {
                let suffix = i.wrapping_mul(2_654_435_761);
                format!("abcdefghijklmnop{suffix:08x}")
            })
        })
        .collect();
    c.bench_function("rank string_view[24] same 16-byte prefix nulls 2^12", |b| {
        b.iter(|| hint::black_box(rank(&arr, None).unwrap()))
    });
}

criterion_group!(benches, add_benchmark);
criterion_main!(benches);