Skip to content

Apache DataFusion SQL Query Engine

Add dicts to aggregation fuzz testing (#16232)

Dmitrii Blagininf513e2c0c22a
diff --git a/datafusion/core/tests/fuzz_cases/aggregate_fuzz.rs b/datafusion/core/tests/fuzz_cases/aggregate_fuzz.rs
index 7e5ad011b5..940b85bb96 100644
--- a/datafusion/core/tests/fuzz_cases/aggregate_fuzz.rs
+++ b/datafusion/core/tests/fuzz_cases/aggregate_fuzz.rs
@@ -254,6 +254,12 @@ fn baseline_config() -> DatasetGeneratorConfig {
// low cardinality to try and get many repeated runs
vec![String::from("u8_low")],
vec![String::from("utf8_low"), String::from("u8_low")],
+ vec![String::from("dictionary_utf8_low")],
+ vec![
+ String::from("dictionary_utf8_low"),
+ String::from("utf8_low"),
+ String::from("u8_low"),
+ ],
],
}
}
diff --git a/datafusion/core/tests/fuzz_cases/record_batch_generator.rs b/datafusion/core/tests/fuzz_cases/record_batch_generator.rs
index 7b48eadf77..4eac1482ad 100644
--- a/datafusion/core/tests/fuzz_cases/record_batch_generator.rs
+++ b/datafusion/core/tests/fuzz_cases/record_batch_generator.rs
@@ -17,16 +17,17 @@
use std::sync::Arc;
-use arrow::array::{ArrayRef, RecordBatch};
+use arrow::array::{ArrayRef, DictionaryArray, PrimitiveArray, RecordBatch};
use arrow::datatypes::{
- BooleanType, DataType, Date32Type, Date64Type, Decimal128Type, Decimal256Type,
- DurationMicrosecondType, DurationMillisecondType, DurationNanosecondType,
- DurationSecondType, Field, Float32Type, Float64Type, Int16Type, Int32Type, Int64Type,
- Int8Type, IntervalDayTimeType, IntervalMonthDayNanoType, IntervalUnit,
- IntervalYearMonthType, Schema, Time32MillisecondType, Time32SecondType,
- Time64MicrosecondType, Time64NanosecondType, TimeUnit, TimestampMicrosecondType,
- TimestampMillisecondType, TimestampNanosecondType, TimestampSecondType, UInt16Type,
- UInt32Type, UInt64Type, UInt8Type,
+ ArrowPrimitiveType, BooleanType, DataType, Date32Type, Date64Type, Decimal128Type,
+ Decimal256Type, DurationMicrosecondType, DurationMillisecondType,
+ DurationNanosecondType, DurationSecondType, Field, Float32Type, Float64Type,
+ Int16Type, Int32Type, Int64Type, Int8Type, IntervalDayTimeType,
+ IntervalMonthDayNanoType, IntervalUnit, IntervalYearMonthType, Schema,
+ Time32MillisecondType, Time32SecondType, Time64MicrosecondType, Time64NanosecondType,
+ TimeUnit, TimestampMicrosecondType, TimestampMillisecondType,
+ TimestampNanosecondType, TimestampSecondType, UInt16Type, UInt32Type, UInt64Type,
+ UInt8Type,
};
use arrow_schema::{
DECIMAL128_MAX_PRECISION, DECIMAL128_MAX_SCALE, DECIMAL256_MAX_PRECISION,
@@ -126,6 +127,11 @@ pub fn get_supported_types_columns(rng_seed: u64) -> Vec<ColumnDescr> {
ColumnDescr::new("binary", DataType::Binary),
ColumnDescr::new("large_binary", DataType::LargeBinary),
ColumnDescr::new("binaryview", DataType::BinaryView),
+ ColumnDescr::new(
+ "dictionary_utf8_low",
+ DataType::Dictionary(Box::new(DataType::UInt64), Box::new(DataType::Utf8)),
+ )
+ .with_max_num_distinct(10),
]
}
@@ -185,17 +191,13 @@ pub struct RecordBatchGenerator {
}
macro_rules! generate_decimal_array {
- ($SELF:ident, $NUM_ROWS:ident, $MAX_NUM_DISTINCT: expr, $BATCH_GEN_RNG:ident, $ARRAY_GEN_RNG:ident, $PRECISION: ident, $SCALE: ident, $ARROW_TYPE: ident) => {{
- let null_pct_idx =
- $BATCH_GEN_RNG.random_range(0..$SELF.candidate_null_pcts.len());
- let null_pct = $SELF.candidate_null_pcts[null_pct_idx];
-
+ ($SELF:ident, $NUM_ROWS:ident, $MAX_NUM_DISTINCT: expr, $NULL_PCT:ident, $BATCH_GEN_RNG:ident, $ARRAY_GEN_RNG:ident, $PRECISION: ident, $SCALE: ident, $ARROW_TYPE: ident) => {{
let mut generator = DecimalArrayGenerator {
precision: $PRECISION,
scale: $SCALE,
num_decimals: $NUM_ROWS,
num_distinct_decimals: $MAX_NUM_DISTINCT,
- null_pct,
+ null_pct: $NULL_PCT,
rng: $ARRAY_GEN_RNG,
};
@@ -205,18 +207,13 @@ macro_rules! generate_decimal_array {
// Generating `BooleanArray` due to it being a special type in Arrow (bit-packed)
macro_rules! generate_boolean_array {
- ($SELF:ident, $NUM_ROWS:ident, $MAX_NUM_DISTINCT:expr, $BATCH_GEN_RNG:ident, $ARRAY_GEN_RNG:ident, $ARROW_TYPE: ident) => {{
- // Select a null percentage from the candidate percentages
- let null_pct_idx =
- $BATCH_GEN_RNG.random_range(0..$SELF.candidate_null_pcts.len());
- let null_pct = $SELF.candidate_null_pcts[null_pct_idx];
-
+ ($SELF:ident, $NUM_ROWS:ident, $MAX_NUM_DISTINCT:expr, $NULL_PCT:ident, $BATCH_GEN_RNG:ident, $ARRAY_GEN_RNG:ident, $ARROW_TYPE: ident) => {{
let num_distinct_booleans = if $MAX_NUM_DISTINCT >= 2 { 2 } else { 1 };
let mut generator = BooleanArrayGenerator {
num_booleans: $NUM_ROWS,
num_distinct_booleans,
- null_pct,
+ null_pct: $NULL_PCT,
rng: $ARRAY_GEN_RNG,
};
@@ -225,15 +222,11 @@ macro_rules! generate_boolean_array {
}
macro_rules! generate_primitive_array {
- ($SELF:ident, $NUM_ROWS:ident, $MAX_NUM_DISTINCT:expr, $BATCH_GEN_RNG:ident, $ARRAY_GEN_RNG:ident, $ARROW_TYPE:ident) => {{
- let null_pct_idx =
- $BATCH_GEN_RNG.random_range(0..$SELF.candidate_null_pcts.len());
- let null_pct = $SELF.candidate_null_pcts[null_pct_idx];
-
+ ($SELF:ident, $NUM_ROWS:ident, $MAX_NUM_DISTINCT:expr, $NULL_PCT:ident, $BATCH_GEN_RNG:ident, $ARRAY_GEN_RNG:ident, $ARROW_TYPE:ident) => {{
let mut generator = PrimitiveArrayGenerator {
num_primitives: $NUM_ROWS,
num_distinct_primitives: $MAX_NUM_DISTINCT,
- null_pct,
+ null_pct: $NULL_PCT,
rng: $ARRAY_GEN_RNG,
};
@@ -241,6 +234,28 @@ macro_rules! generate_primitive_array {
}};
}
+macro_rules! generate_dict {
+ ($SELF:ident, $NUM_ROWS:ident, $MAX_NUM_DISTINCT:expr, $NULL_PCT:ident, $BATCH_GEN_RNG:ident, $ARRAY_GEN_RNG:ident, $ARROW_TYPE:ident, $VALUES: ident) => {{
+ debug_assert_eq!($VALUES.len(), $MAX_NUM_DISTINCT);
+ let keys: PrimitiveArray<$ARROW_TYPE> = (0..$NUM_ROWS)
+ .map(|_| {
+ if $BATCH_GEN_RNG.random::<f64>() < $NULL_PCT {
+ None
+ } else if $MAX_NUM_DISTINCT > 1 {
+ let range = 0..($MAX_NUM_DISTINCT
+ as <$ARROW_TYPE as ArrowPrimitiveType>::Native);
+ Some($ARRAY_GEN_RNG.random_range(range))
+ } else {
+ Some(0)
+ }
+ })
+ .collect();
+
+ let dict = DictionaryArray::new(keys, $VALUES);
+ Arc::new(dict) as ArrayRef
+ }};
+}
+
impl RecordBatchGenerator {
/// Create a new `RecordBatchGenerator` with a random seed. The generated
/// batches will be different each time.
@@ -302,6 +317,25 @@ impl RecordBatchGenerator {
num_rows: usize,
batch_gen_rng: &mut StdRng,
array_gen_rng: StdRng,
+ ) -> ArrayRef {
+ let null_pct_idx = batch_gen_rng.random_range(0..self.candidate_null_pcts.len());
+ let null_pct = self.candidate_null_pcts[null_pct_idx];
+
+ Self::generate_array_of_type_inner(
+ col,
+ num_rows,
+ batch_gen_rng,
+ array_gen_rng,
+ null_pct,
+ )
+ }
+
+ fn generate_array_of_type_inner(
+ col: &ColumnDescr,
+ num_rows: usize,
+ batch_gen_rng: &mut StdRng,
+ array_gen_rng: StdRng,
+ null_pct: f64,
) -> ArrayRef {
let num_distinct = if num_rows > 1 {
batch_gen_rng.random_range(1..num_rows)
@@ -320,6 +354,7 @@ impl RecordBatchGenerator {
self,
num_rows,
max_num_distinct,
+ null_pct,
batch_gen_rng,
array_gen_rng,
Int8Type
@@ -330,6 +365,7 @@ impl RecordBatchGenerator {
self,
num_rows,
max_num_distinct,
+ null_pct,
batch_gen_rng,
array_gen_rng,
Int16Type
@@ -340,6 +376,7 @@ impl RecordBatchGenerator {
self,
num_rows,
max_num_distinct,
+ null_pct,
batch_gen_rng,
array_gen_rng,
Int32Type
@@ -350,6 +387,7 @@ impl RecordBatchGenerator {
self,
num_rows,
max_num_distinct,
+ null_pct,
batch_gen_rng,
array_gen_rng,
Int64Type
@@ -360,6 +398,7 @@ impl RecordBatchGenerator {
self,
num_rows,
max_num_distinct,
+ null_pct,
batch_gen_rng,
array_gen_rng,
UInt8Type
@@ -370,6 +409,7 @@ impl RecordBatchGenerator {
self,
num_rows,
max_num_distinct,
+ null_pct,
batch_gen_rng,
array_gen_rng,
UInt16Type
@@ -380,6 +420,7 @@ impl RecordBatchGenerator {
self,
num_rows,
max_num_distinct,
+ null_pct,
batch_gen_rng,
array_gen_rng,
UInt32Type
@@ -390,6 +431,7 @@ impl RecordBatchGenerator {
self,
num_rows,
max_num_distinct,
+ null_pct,
batch_gen_rng,
array_gen_rng,
UInt64Type
@@ -400,6 +442,7 @@ impl RecordBatchGenerator {
self,
num_rows,
max_num_distinct,
+ null_pct,
batch_gen_rng,
array_gen_rng,
Float32Type
@@ -410,6 +453,7 @@ impl RecordBatchGenerator {
self,
num_rows,
max_num_distinct,
+ null_pct,
batch_gen_rng,
array_gen_rng,
Float64Type
@@ -420,6 +464,7 @@ impl RecordBatchGenerator {
self,
num_rows,
max_num_distinct,
+ null_pct,
batch_gen_rng,
array_gen_rng,
Date32Type
@@ -430,6 +475,7 @@ impl RecordBatchGenerator {
self,
num_rows,
max_num_distinct,
+ null_pct,
batch_gen_rng,
array_gen_rng,
Date64Type
@@ -440,6 +486,7 @@ impl RecordBatchGenerator {
self,
num_rows,
max_num_distinct,
+ null_pct,
batch_gen_rng,
array_gen_rng,
Time32SecondType
@@ -450,6 +497,7 @@ impl RecordBatchGenerator {
self,
num_rows,
max_num_distinct,
+ null_pct,
batch_gen_rng,
array_gen_rng,
Time32MillisecondType
@@ -460,6 +508,7 @@ impl RecordBatchGenerator {
self,
num_rows,
max_num_distinct,
+ null_pct,
batch_gen_rng,
array_gen_rng,
Time64MicrosecondType
@@ -470,6 +519,7 @@ impl RecordBatchGenerator {
self,
num_rows,
max_num_distinct,
+ null_pct,
batch_gen_rng,
array_gen_rng,
Time64NanosecondType
@@ -480,6 +530,7 @@ impl RecordBatchGenerator {
self,
num_rows,
max_num_distinct,
+ null_pct,
batch_gen_rng,
array_gen_rng,
IntervalYearMonthType
@@ -490,6 +541,7 @@ impl RecordBatchGenerator {
self,
num_rows,
max_num_distinct,
+ null_pct,
batch_gen_rng,
array_gen_rng,
IntervalDayTimeType
@@ -500,6 +552,7 @@ impl RecordBatchGenerator {
self,
num_rows,
max_num_distinct,
+ null_pct,
batch_gen_rng,
array_gen_rng,
IntervalMonthDayNanoType
@@ -510,6 +563,7 @@ impl RecordBatchGenerator {
self,
num_rows,
max_num_distinct,
+ null_pct,
batch_gen_rng,
array_gen_rng,
DurationSecondType
@@ -520,6 +574,7 @@ impl RecordBatchGenerator {
self,
num_rows,
max_num_distinct,
+ null_pct,
batch_gen_rng,
array_gen_rng,
DurationMillisecondType
@@ -530,6 +585,7 @@ impl RecordBatchGenerator {
self,
num_rows,
max_num_distinct,
+ null_pct,
batch_gen_rng,
array_gen_rng,
DurationMicrosecondType
@@ -540,6 +596,7 @@ impl RecordBatchGenerator {
self,
num_rows,
max_num_distinct,
+ null_pct,
batch_gen_rng,
array_gen_rng,
DurationNanosecondType
@@ -550,6 +607,7 @@ impl RecordBatchGenerator {
self,
num_rows,
max_num_distinct,
+ null_pct,
batch_gen_rng,
array_gen_rng,
TimestampSecondType
@@ -560,6 +618,7 @@ impl RecordBatchGenerator {
self,
num_rows,
max_num_distinct,
+ null_pct,
batch_gen_rng,
array_gen_rng,
TimestampMillisecondType
@@ -570,6 +629,7 @@ impl RecordBatchGenerator {
self,
num_rows,
max_num_distinct,
+ null_pct,
batch_gen_rng,
array_gen_rng,
TimestampMicrosecondType
@@ -580,15 +640,13 @@ impl RecordBatchGenerator {
self,
num_rows,
max_num_distinct,
+ null_pct,
batch_gen_rng,
array_gen_rng,
TimestampNanosecondType
)
}
DataType::Utf8 | DataType::LargeUtf8 | DataType::Utf8View => {
- let null_pct_idx =
- batch_gen_rng.random_range(0..self.candidate_null_pcts.len());
- let null_pct = self.candidate_null_pcts[null_pct_idx];
let max_len = batch_gen_rng.random_range(1..50);
let mut generator = StringArrayGenerator {
@@ -607,9 +665,6 @@ impl RecordBatchGenerator {
}
}
DataType::Binary | DataType::LargeBinary | DataType::BinaryView => {
- let null_pct_idx =
- batch_gen_rng.random_range(0..self.candidate_null_pcts.len());
- let null_pct = self.candidate_null_pcts[null_pct_idx];
let max_len = batch_gen_rng.random_range(1..100);
let mut generator = BinaryArrayGenerator {
@@ -632,6 +687,7 @@ impl RecordBatchGenerator {
self,
num_rows,
max_num_distinct,
+ null_pct,
batch_gen_rng,
array_gen_rng,
precision,
@@ -644,6 +700,7 @@ impl RecordBatchGenerator {
self,
num_rows,
max_num_distinct,
+ null_pct,
batch_gen_rng,
array_gen_rng,
precision,
@@ -656,11 +713,43 @@ impl RecordBatchGenerator {
self,
num_rows,
max_num_distinct,
+ null_pct,
batch_gen_rng,
array_gen_rng,
BooleanType
}
}
+ DataType::Dictionary(ref key_type, ref value_type)
+ if key_type.is_dictionary_key_type() =>
+ {
+ // We generate just num_distinct values because they will be reused by different keys
+ let mut array_gen_rng = array_gen_rng;
+
+ let values = Self::generate_array_of_type_inner(
+ &ColumnDescr::new("values", *value_type.clone()),
+ num_distinct,
+ batch_gen_rng,
+ array_gen_rng.clone(),
+ // Once https://github.com/apache/datafusion/issues/16228 is fixed
+ // we can also generate nulls in values
+ 0.0, // null values are generated on the key level
+ );
+
+ match key_type.as_ref() {
+ // new key types can be added here
+ DataType::UInt64 => generate_dict!(
+ self,
+ num_rows,
+ num_distinct,
+ null_pct,
+ batch_gen_rng,
+ array_gen_rng,
+ UInt64Type,
+ values
+ ),
+ _ => panic!("Invalid dictionary keys type: {key_type}"),
+ }
+ }
_ => {
panic!("Unsupported data generator type: {}", col.column_type)
}