-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fuzz test for spillable sort (#1706)
- Loading branch information
Showing
8 changed files
with
234 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# 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. | ||
|
||
[package] | ||
name = "fuzz-utils" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
arrow = { version = "8.0.0", features = ["prettyprint"] } | ||
rand = "0.8" | ||
env_logger = "0.9.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// 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. | ||
|
||
//! Common utils for fuzz tests | ||
use arrow::{array::Int32Array, record_batch::RecordBatch}; | ||
use rand::prelude::StdRng; | ||
use rand::Rng; | ||
|
||
pub use env_logger; | ||
|
||
/// Extracts the i32 values from the set of batches and returns them as a single Vec | ||
pub fn batches_to_vec(batches: &[RecordBatch]) -> Vec<Option<i32>> { | ||
batches | ||
.iter() | ||
.map(|batch| { | ||
assert_eq!(batch.num_columns(), 1); | ||
batch | ||
.column(0) | ||
.as_any() | ||
.downcast_ref::<Int32Array>() | ||
.unwrap() | ||
.iter() | ||
}) | ||
.flatten() | ||
.collect() | ||
} | ||
|
||
/// extract values from batches and sort them | ||
pub fn partitions_to_sorted_vec(partitions: &[Vec<RecordBatch>]) -> Vec<Option<i32>> { | ||
let mut values: Vec<_> = partitions | ||
.iter() | ||
.map(|batches| batches_to_vec(batches).into_iter()) | ||
.flatten() | ||
.collect(); | ||
|
||
values.sort_unstable(); | ||
values | ||
} | ||
|
||
/// Adds a random number of empty record batches into the stream | ||
pub fn add_empty_batches( | ||
batches: Vec<RecordBatch>, | ||
rng: &mut StdRng, | ||
) -> Vec<RecordBatch> { | ||
let schema = batches[0].schema(); | ||
|
||
batches | ||
.into_iter() | ||
.map(|batch| { | ||
// insert 0, or 1 empty batches before and after the current batch | ||
let empty_batch = RecordBatch::new_empty(schema.clone()); | ||
std::iter::repeat(empty_batch.clone()) | ||
.take(rng.gen_range(0..2)) | ||
.chain(std::iter::once(batch)) | ||
.chain(std::iter::repeat(empty_batch).take(rng.gen_range(0..2))) | ||
}) | ||
.flatten() | ||
.collect() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// 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. | ||
|
||
//! Fuzz Test for various corner cases sorting RecordBatches exceeds available memory and should spill | ||
|
||
use arrow::{ | ||
array::{ArrayRef, Int32Array}, | ||
compute::SortOptions, | ||
record_batch::RecordBatch, | ||
}; | ||
use datafusion::execution::memory_manager::MemoryManagerConfig; | ||
use datafusion::execution::runtime_env::{RuntimeConfig, RuntimeEnv}; | ||
use datafusion::physical_plan::expressions::{col, PhysicalSortExpr}; | ||
use datafusion::physical_plan::memory::MemoryExec; | ||
use datafusion::physical_plan::sorts::sort::SortExec; | ||
use datafusion::physical_plan::{collect, ExecutionPlan}; | ||
use fuzz_utils::{add_empty_batches, batches_to_vec, partitions_to_sorted_vec}; | ||
use rand::prelude::StdRng; | ||
use rand::{Rng, SeedableRng}; | ||
use std::sync::Arc; | ||
|
||
#[tokio::test] | ||
async fn test_sort_1k_mem() { | ||
run_sort(1024, vec![(5, false), (2000, true), (1000000, true)]).await | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_sort_100k_mem() { | ||
run_sort(102400, vec![(5, false), (2000, false), (1000000, true)]).await | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_sort_unlimited_mem() { | ||
run_sort( | ||
usize::MAX, | ||
vec![(5, false), (2000, false), (1000000, false)], | ||
) | ||
.await | ||
} | ||
|
||
/// Sort the input using SortExec and ensure the results are correct according to `Vec::sort` | ||
async fn run_sort(pool_size: usize, size_spill: Vec<(usize, bool)>) { | ||
for (size, spill) in size_spill { | ||
let input = vec![make_staggered_batches(size)]; | ||
let first_batch = input | ||
.iter() | ||
.map(|p| p.iter()) | ||
.flatten() | ||
.next() | ||
.expect("at least one batch"); | ||
let schema = first_batch.schema(); | ||
|
||
let sort = vec![PhysicalSortExpr { | ||
expr: col("x", &schema).unwrap(), | ||
options: SortOptions { | ||
descending: false, | ||
nulls_first: true, | ||
}, | ||
}]; | ||
|
||
let exec = MemoryExec::try_new(&input, schema, None).unwrap(); | ||
let sort = Arc::new(SortExec::try_new(sort, Arc::new(exec)).unwrap()); | ||
|
||
let runtime_config = RuntimeConfig::new().with_memory_manager( | ||
MemoryManagerConfig::try_new_limit(pool_size, 1.0).unwrap(), | ||
); | ||
let runtime = Arc::new(RuntimeEnv::new(runtime_config).unwrap()); | ||
let collected = collect(sort.clone(), runtime).await.unwrap(); | ||
|
||
let expected = partitions_to_sorted_vec(&input); | ||
let actual = batches_to_vec(&collected); | ||
|
||
if spill { | ||
assert_ne!(sort.metrics().unwrap().spill_count().unwrap(), 0); | ||
} else { | ||
assert_eq!(sort.metrics().unwrap().spill_count().unwrap(), 0); | ||
} | ||
|
||
assert_eq!(expected, actual, "failure in @ pool_size {}", pool_size); | ||
} | ||
} | ||
|
||
/// Return randomly sized record batches in a field named 'x' of type `Int32` | ||
/// with randomized i32 content | ||
fn make_staggered_batches(len: usize) -> Vec<RecordBatch> { | ||
let mut rng = rand::thread_rng(); | ||
let mut input: Vec<i32> = vec![0; len]; | ||
rng.fill(&mut input[..]); | ||
let input = Int32Array::from_iter_values(input.into_iter()); | ||
|
||
// split into several record batches | ||
let mut remainder = | ||
RecordBatch::try_from_iter(vec![("x", Arc::new(input) as ArrayRef)]).unwrap(); | ||
|
||
let mut batches = vec![]; | ||
|
||
// use a random number generator to pick a random sized output | ||
let mut rng = StdRng::seed_from_u64(42); | ||
while remainder.num_rows() > 0 { | ||
let batch_size = rng.gen_range(0..remainder.num_rows() + 1); | ||
|
||
batches.push(remainder.slice(0, batch_size)); | ||
remainder = remainder.slice(batch_size, remainder.num_rows() - batch_size); | ||
} | ||
|
||
add_empty_batches(batches, &mut rng) | ||
} |