Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion datafusion/core/tests/fuzz_cases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ mod equivalence;
mod pruning;

mod leaf_pushdown_fuzz;
mod limit_fuzz;
#[expect(clippy::needless_pass_by_value)]
mod sort_preserving_repartition_fuzz;
mod window_fuzz;
Expand Down
2 changes: 2 additions & 0 deletions datafusion/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ extended_tests = []
[dev-dependencies]
arrow = { workspace = true }
bytes = { workspace = true }
datafusion = { workspace = true }
datafusion-common = { workspace = true }
datafusion-execution = { workspace = true }
datafusion-expr = { workspace = true }
datafusion-physical-plan = { workspace = true }
futures = { workspace = true }
parking_lot = { workspace = true }
Expand Down
14 changes: 14 additions & 0 deletions datafusion/fuzz/tests/fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ async fn key_range_bitmap_union() -> Result<()> {
.await
}

#[tokio::test]
async fn sort_topk() -> Result<()> {
run_suite(
&suites::limit::LimitFuzzer,
RunConfig {
run_seed: None,
max_cases: Some(100),
time_limit: None,
cases_per_dataset: 100,
},
)
.await
}

async fn run_suite<T: FuzzTest>(test: &T, config: RunConfig) -> Result<()> {
eprintln!("Running fuzz suite: {}", test.name());
let stats = run(test, config).await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,44 @@ use arrow::datatypes::SchemaRef;
use arrow::util::pretty::pretty_format_batches;
use datafusion::datasource::MemTable;
use datafusion::prelude::SessionContext;
use datafusion_common::Result;
use datafusion_common::assert_contains;
use rand::{Rng, rng};
use rand::rngs::StdRng;
use rand::{Rng, SeedableRng};
use std::sync::Arc;
use test_utils::fuzz::FuzzTest;
use test_utils::stagger_batch;

#[tokio::test]
async fn test_sort_topk_i32() {
run_limit_fuzz_test(SortedData::new_i32).await
}
/// Runs TopK on random data. Each case picks a data type, an input size, and
/// a limit from its case seed.
pub(crate) struct LimitFuzzer;

#[tokio::test]
async fn test_sort_topk_f64() {
run_limit_fuzz_test(SortedData::new_f64).await
}
impl FuzzTest for LimitFuzzer {
type Dataset = ();

#[tokio::test]
async fn test_sort_topk_str() {
run_limit_fuzz_test(SortedData::new_str).await
}
fn name(&self) -> &str {
"sort_topk"
}

#[tokio::test]
async fn test_sort_topk_i64str() {
run_limit_fuzz_test(SortedData::new_i64str).await
}
fn prepare_dataset(&self, _: u64) -> impl Future<Output = Result<()>> {
std::future::ready(Ok(()))
}

/// Run TopK fuzz tests the specified input data with different
/// different test functions so they can run in parallel)
async fn run_limit_fuzz_test<F>(make_data: F)
where
F: Fn(usize) -> SortedData,
{
let mut rng = rng();
for size in [10, 1_0000, 10_000, 100_000] {
let data = make_data(size);
// test various limits including some random ones
for limit in [1, 3, 7, 17, 10000, rng.random_range(1..size * 2)] {
// limit can be larger than the number of rows in the input
run_limit_test(limit, &data).await;
}
async fn run_case(&self, _: &(), case_seed: u64) -> Result<()> {
Comment thread
Nagato-Yuzuru marked this conversation as resolved.
Resolved
let mut rng = StdRng::seed_from_u64(case_seed);
let make_data = match rng.random_range(0..4) {
0 => SortedData::new_i32,
1 => SortedData::new_f64,
2 => SortedData::new_str,
_ => SortedData::new_i64str,
};
let size = [10, 1_000, 10_000, 100_000][rng.random_range(0..4)];
// The limit can be larger than the number of input rows.
let limit =
[1, 3, 7, 17, 10_000, rng.random_range(1..size * 2)][rng.random_range(0..6)];
let data = make_data(size, &mut rng);
run_limit_test(limit, &data).await;
Ok(())
}
}

Expand All @@ -57,7 +56,7 @@ where
/// Each variants stores the input batches and the expected sorted values
/// compute the expected output for a given fetch (limit) value.
#[derive(Debug)]
enum SortedData {
pub(crate) enum SortedData {
// single Int32 column
I32 {
batches: Vec<RecordBatch>,
Expand All @@ -83,8 +82,7 @@ enum SortedData {
impl SortedData {
/// Create an i32 column of random values, with the specified number of
/// rows, sorted the default
fn new_i32(size: usize) -> Self {
let mut rng = rng();
fn new_i32(size: usize, rng: &mut StdRng) -> Self {
// have some repeats (approximately 1/3 of the values are the same)
let max = size as i32 / 3;
let data: Vec<Option<i32>> = (0..size)
Expand All @@ -104,8 +102,7 @@ impl SortedData {

/// Create an f64 column of random values, with the specified number of
/// rows, sorted the default
fn new_f64(size: usize) -> Self {
let mut rng = rng();
fn new_f64(size: usize, rng: &mut StdRng) -> Self {
let mut data: Vec<Option<f64>> = (0..size / 3)
.map(|_| {
// no nulls for now
Expand All @@ -128,12 +125,11 @@ impl SortedData {

/// Create an string column of random values, with the specified number of
/// rows, sorted the default
fn new_str(size: usize) -> Self {
let mut rng = rng();
fn new_str(size: usize, rng: &mut StdRng) -> Self {
let mut data: Vec<Option<String>> = (0..size / 3)
.map(|_| {
// no nulls for now
Some(get_random_string(16))
Some(get_random_string(rng, 16))
})
.collect();

Expand All @@ -152,14 +148,12 @@ impl SortedData {

/// Create two columns of random values (int64, string), with the specified number of
/// rows, sorted the default
fn new_i64str(size: usize) -> Self {
let mut rng = rng();

fn new_i64str(size: usize, rng: &mut StdRng) -> Self {
// 100 distinct values
let strings: Vec<Option<String>> = (0..100)
.map(|_| {
// no nulls for now
Some(get_random_string(16))
Some(get_random_string(rng, 16))
})
.collect();

Expand Down Expand Up @@ -326,9 +320,8 @@ async fn run_limit_test(fetch: usize, data: &SortedData) {
}

/// Return random ASCII String with len
fn get_random_string(len: usize) -> String {
rng()
.sample_iter(rand::distr::Alphanumeric)
fn get_random_string(rng: &mut StdRng, len: usize) -> String {
rng.sample_iter(rand::distr::Alphanumeric)
.take(len)
.map(char::from)
.collect()
Expand Down
1 change: 1 addition & 0 deletions datafusion/fuzz/tests/suites/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
//! Implementation for fuzz test cases.

pub(crate) mod key_range_bitmap;
pub(crate) mod limit;
pub(crate) mod lru_queue;
pub(crate) mod spill_pool;