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
68 changes: 66 additions & 2 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ pub enum FuzzerDataType {
IntervalMonthDayNano,
// String type for text data
String,
// List of Int32 values, the first (simplest) array type
Int32Array,
}

impl FuzzerDataType {
Expand Down Expand Up @@ -86,6 +88,7 @@ impl FuzzerDataType {
DataType::Interval(datafusion::arrow::datatypes::IntervalUnit::MonthDayNano)
}
FuzzerDataType::String => DataType::Utf8,
FuzzerDataType::Int32Array => DataType::new_list(DataType::Int32, true),
}
}

Expand Down Expand Up @@ -115,6 +118,9 @@ impl FuzzerDataType {
Some(FuzzerDataType::IntervalMonthDayNano)
}
DataType::Utf8 => Some(FuzzerDataType::String),
DataType::List(field) if *field.data_type() == DataType::Int32 => {
Some(FuzzerDataType::Int32Array)
}
_ => None,
}
}
Expand All @@ -136,6 +142,7 @@ impl FuzzerDataType {
FuzzerDataType::Timestamp => "timestamp",
FuzzerDataType::IntervalMonthDayNano => "interval_month_day_nano",
FuzzerDataType::String => "string",
FuzzerDataType::Int32Array => "int32_array",
}
}

Expand All @@ -154,7 +161,8 @@ impl FuzzerDataType {
| FuzzerDataType::Time64Nanosecond
| FuzzerDataType::Timestamp
| FuzzerDataType::IntervalMonthDayNano
| FuzzerDataType::String => false,
| FuzzerDataType::String
| FuzzerDataType::Int32Array => false,
}
}

Expand All @@ -173,7 +181,8 @@ impl FuzzerDataType {
| FuzzerDataType::Float64
| FuzzerDataType::Boolean
| FuzzerDataType::Decimal
| FuzzerDataType::String => false,
| FuzzerDataType::String
| FuzzerDataType::Int32Array => false,
}
}

Expand Down Expand Up @@ -206,6 +215,7 @@ impl FuzzerDataType {
FuzzerDataType::Timestamp => "TIMESTAMP",
FuzzerDataType::IntervalMonthDayNano => "INTERVAL",
FuzzerDataType::String => "VARCHAR",
FuzzerDataType::Int32Array => "INT[]",
}
}
}
Expand Down Expand Up @@ -240,6 +250,7 @@ pub fn init_available_data_types() {
FuzzerDataType::Timestamp,
FuzzerDataType::IntervalMonthDayNano,
FuzzerDataType::String,
FuzzerDataType::Int32Array,
]
});
}
Expand Down Expand Up @@ -400,6 +411,59 @@ mod tests {
assert!(!int8_type.is_time());
}

#[test]
fn test_int32_array_type_properties() {
let array_type = FuzzerDataType::Int32Array;

assert_eq!(
array_type.to_datafusion_type(),
DataType::new_list(DataType::Int32, true)
);
assert_eq!(
FuzzerDataType::from_datafusion_type(&DataType::new_list(DataType::Int32, true)),
Some(FuzzerDataType::Int32Array)
);
assert_eq!(array_type.display_name(), "int32_array");
assert_eq!(array_type.to_sql_type(), "INT[]");
assert!(!array_type.is_numeric());
assert!(!array_type.is_time());
}

/// End-to-end example: the exact SQL shapes the fuzzer generates for
/// Int32Array must be accepted by DataFusion (DDL, INSERT and SELECT).
#[tokio::test]
async fn test_int32_array_end_to_end() {
use datafusion::prelude::SessionContext;

let ctx = SessionContext::new();
ctx.sql("CREATE TABLE arr_example (id INT, vals INT[])")
.await
.unwrap()
.collect()
.await
.unwrap();
ctx.sql("INSERT INTO arr_example VALUES (1, [1, 2, 3]), (2, [-5]), (3, NULL)")
.await
.unwrap()
.collect()
.await
.unwrap();

let batches = ctx
.sql("SELECT vals FROM arr_example")
.await
.unwrap()
.collect()
.await
.unwrap();
let total_rows: usize = batches.iter().map(|b| b.num_rows()).sum();
assert_eq!(total_rows, 3);
assert_eq!(
batches[0].schema().field(0).data_type(),
&DataType::new_list(DataType::Int32, true)
);
}

#[test]
fn test_simplified_decimal_type() {
// Test that the simplified Decimal type works correctly
Expand Down
59 changes: 59 additions & 0 deletions src/common/value_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub enum GeneratedValue {
Timestamp(i64, Option<String>), // Nanoseconds since Unix epoch (1970-01-01 00:00:00 UTC) with optional timezone
IntervalMonthDayNano(i128), // MonthDayNano interval as i128 (months, days, nanoseconds)
String(String), // String value
Int32Array(Vec<i32>), // List of Int32 values
Null,
}

Expand Down Expand Up @@ -112,6 +113,15 @@ pub fn generate_value(
scale,
}
}
FuzzerDataType::Int32Array => {
// Short non-empty arrays: empty literals like `[]` have an untyped
// element type and fail to cast, so always generate 1..=5 elements
let length = rng.random_range(1..=5);
let values = (0..length)
.map(|_| rng.random_range(config.int_range.0..=config.int_range.1))
.collect();
GeneratedValue::Int32Array(values)
}
FuzzerDataType::Date32 => {
// Generate a reasonable range of dates:
// - Days 0-36500 covers approximately 100 years from 1970-01-01
Expand Down Expand Up @@ -339,6 +349,10 @@ impl GeneratedValue {
let escaped = s.replace("'", "''");
format!("'{}'", escaped)
}
GeneratedValue::Int32Array(values) => {
let elements: Vec<String> = values.iter().map(|v| v.to_string()).collect();
format!("[{}]", elements.join(", "))
}
GeneratedValue::Null => "NULL".to_string(),
}
}
Expand Down Expand Up @@ -386,6 +400,14 @@ impl GeneratedValue {
ScalarValue::IntervalMonthDayNano(Some(interval_value))
}
GeneratedValue::String(s) => ScalarValue::Utf8(Some(s.clone())),
GeneratedValue::Int32Array(values) => {
use datafusion::arrow::array::ListArray;
use datafusion::arrow::datatypes::Int32Type;
let list = ListArray::from_iter_primitive::<Int32Type, _, _>(std::iter::once(
Some(values.iter().map(|v| Some(*v)).collect::<Vec<_>>()),
));
ScalarValue::List(Arc::new(list))
}
GeneratedValue::Null => ScalarValue::Null,
}
}
Expand Down Expand Up @@ -543,6 +565,43 @@ mod tests {
}
}

#[test]
fn test_int32_array_value_generation_and_conversions() {
let mut rng = rng_from_seed(42);
let config = ValueGenerationConfig {
nullable: false,
..ValueGenerationConfig::default()
};

for _ in 0..100 {
let value = generate_value(&mut rng, &FuzzerDataType::Int32Array, &config);

match &value {
GeneratedValue::Int32Array(values) => {
assert!((1..=5).contains(&values.len()));
for v in values {
assert!((config.int_range.0..=config.int_range.1).contains(v));
}

// SQL literal like `[1, 2, 3]`
let sql = value.to_sql_string();
assert!(sql.starts_with('[') && sql.ends_with(']'));

// ScalarValue::List with Int32 element type
match value.to_scalar_value() {
datafusion::scalar::ScalarValue::List(list) => {
use datafusion::arrow::array::Array;
assert_eq!(list.len(), 1);
assert_eq!(list.value(0).len(), values.len());
}
other => panic!("Expected List scalar, got: {other:?}"),
}
}
other => panic!("Expected Int32Array value, got: {other:?}"),
}
}
}

#[test]
fn test_cached_config_in_runtime_context() {
// Test that RuntimeContext has the expected cached config
Expand Down
Loading
Loading