Skip to content

Commit ae755c4

Browse files
committed
refactor: simplify native array extrema and focused regressions
1 parent dc36cda commit ae755c4

14 files changed

Lines changed: 559 additions & 2012 deletions

‎native/spark-expr/benches/array_extrema.rs‎

Lines changed: 95 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,12 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
//! Compares the Spark-compatible UDF with the DataFusion version pinned by Cargo.lock.
19-
//! All timed inputs contain ordinary finite, nonzero values. Nested elements contain
20-
//! no inner nulls: mixed zero signs, NaN payloads, and nested null ordering deliberately
21-
//! differ from DataFusion and belong in the correctness tests, not parity benchmarks.
22-
//! The null percentage controls both outer-row and immediate-child validity.
23-
//!
24-
//! Run the whole bounded matrix, or filter (for example) by float64 or nested_float64:
25-
//! cargo bench -p datafusion-comet-spark-expr --bench array_extrema -- float64
18+
//! Compare ordinary-data extrema with DataFusion; special-value semantics belong in tests.
2619
27-
use arrow::array::{ArrayRef, Float32Array, Float64Array, Int32Array, ListArray};
20+
use arrow::array::{ArrayRef, Float32Array, Float64Array, ListArray};
2821
use arrow::buffer::{NullBuffer, OffsetBuffer};
2922
use arrow::datatypes::Field;
30-
use criterion::{
31-
criterion_group, criterion_main, measurement::WallTime, BenchmarkGroup, BenchmarkId, Criterion,
32-
Throughput,
33-
};
23+
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
3424
use datafusion::common::config::ConfigOptions;
3525
use datafusion::functions_nested::min_max::{array_max_udf, array_min_udf};
3626
use datafusion::logical_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDF};
@@ -39,148 +29,108 @@ use std::hint::black_box;
3929
use std::sync::Arc;
4030
use std::time::Duration;
4131

42-
fn valid(index: usize, null_percent: usize) -> bool {
43-
(index * 17 + 23) % 100 >= null_percent
44-
}
45-
46-
fn list(values: ArrayRef, rows: usize, len: usize, null_percent: usize) -> ArrayRef {
47-
let offsets: Vec<i32> = (0..=rows).map(|row| (row * len) as i32).collect();
48-
let nulls = (null_percent != 0).then(|| {
49-
NullBuffer::from(
50-
(0..rows)
51-
.map(|row| valid(row, null_percent))
52-
.collect::<Vec<_>>(),
53-
)
54-
});
32+
fn list(values: ArrayRef, len: usize, nullable: bool) -> ArrayRef {
33+
let rows = values.len() / len;
5534
Arc::new(ListArray::new(
5635
Arc::new(Field::new_list_field(values.data_type().clone(), true)),
57-
OffsetBuffer::new(offsets.into()),
36+
OffsetBuffer::from_lengths(std::iter::repeat_n(len, rows)),
5837
values,
59-
nulls,
38+
nullable.then(|| NullBuffer::from_iter((0..rows).map(|i| i % 10 != 0))),
6039
))
6140
}
6241

63-
fn finite_value(index: usize) -> i32 {
64-
((index * 104_729 + 51) % 1_000_003 + 1) as i32
65-
}
66-
67-
fn primitive_input(kind: &str, rows: usize, len: usize, null_percent: usize) -> ArrayRef {
68-
let values = (0..rows * len).map(|i| valid(i, null_percent).then(|| finite_value(i)));
69-
let values: ArrayRef = match kind {
70-
"float32" => Arc::new(Float32Array::from_iter(
71-
values.map(|v| v.map(|v| v as f32 / 8.0)),
72-
)),
73-
"float64" => Arc::new(Float64Array::from_iter(
74-
values.map(|v| v.map(|v| f64::from(v) / 8.0)),
75-
)),
76-
"int32_control" => Arc::new(Int32Array::from_iter(values)),
77-
_ => unreachable!(),
78-
};
79-
list(values, rows, len, null_percent)
80-
}
81-
82-
fn nested_input(rows: usize, len: usize, null_percent: usize) -> ArrayRef {
83-
let children = rows * len;
84-
let values: ArrayRef = Arc::new(Float64Array::from_iter_values(
85-
(0..children * 4).map(|i| f64::from(finite_value(i)) / 8.0),
86-
));
87-
// Null immediate children are skipped by both implementations; there are no
88-
// null float values inside a valid child list, so recursive ordering agrees.
89-
list(
90-
list(values, children, 4, null_percent),
91-
rows,
92-
len,
93-
null_percent,
94-
)
95-
}
96-
97-
fn args(input: &ArrayRef, udf: &ScalarUDF) -> ScalarFunctionArgs {
98-
ScalarFunctionArgs {
99-
args: vec![ColumnarValue::Array(Arc::clone(input))],
100-
arg_fields: vec![Arc::new(Field::new(
101-
"input",
102-
input.data_type().clone(),
103-
true,
104-
))],
105-
number_rows: input.len(),
106-
return_field: Arc::new(Field::new(
107-
"result",
108-
udf.return_type(&[input.data_type().clone()]).unwrap(),
109-
true,
110-
)),
111-
config_options: Arc::new(ConfigOptions::default()),
112-
}
113-
}
114-
115-
fn bench_case(
116-
group: &mut BenchmarkGroup<'_, WallTime>,
117-
input: ArrayRef,
118-
len: usize,
119-
null_percent: usize,
120-
) {
121-
let case = format!("rows={}_len={len}_null={null_percent}pct", input.len());
122-
group.throughput(Throughput::Elements((input.len() * len) as u64));
123-
for is_min in [true, false] {
124-
let operation = if is_min { "min" } else { "max" };
125-
let comet = ScalarUDF::from(SparkArrayExtrema::new(is_min));
126-
let datafusion = if is_min {
127-
array_min_udf()
128-
} else {
129-
array_max_udf()
130-
};
131-
let args = args(&input, &comet);
132-
// Validate every fixture before timing. This is a parity/control check for
133-
// ordinary data only, not an oracle for Spark's special-value semantics.
134-
let comet_result = comet
135-
.invoke_with_args(args.clone())
136-
.unwrap()
137-
.into_array(input.len())
138-
.unwrap();
139-
let datafusion_result = datafusion
140-
.invoke_with_args(args.clone())
141-
.unwrap()
142-
.into_array(input.len())
143-
.unwrap();
144-
assert_eq!(
145-
comet_result.to_data(),
146-
datafusion_result.to_data(),
147-
"{operation}/{case}"
148-
);
149-
for (name, udf) in [("comet", &comet), ("datafusion", datafusion.as_ref())] {
150-
group.bench_function(
151-
BenchmarkId::new(format!("{operation}_{name}"), &case),
152-
|b| b.iter(|| black_box(udf.invoke_with_args(black_box(args.clone())).unwrap())),
153-
);
154-
}
155-
}
156-
}
157-
15842
fn criterion_benchmark(c: &mut Criterion) {
159-
// 104 cases with one-second measurements and a short warmup: roughly two
160-
// minutes on an idle machine. CLI filters can select individual dimensions.
161-
for kind in ["float32", "float64", "int32_control", "nested_float64"] {
162-
let mut group = c.benchmark_group(format!("array_extrema/{kind}"));
163-
group.sample_size(20);
164-
group.warm_up_time(Duration::from_millis(250));
165-
group.measurement_time(Duration::from_secs(1));
166-
let (lengths, null_percentages): (&[usize], &[usize]) = match kind {
167-
"float32" | "float64" => (&[8, 32, 1024], &[0, 10, 50]),
168-
"int32_control" => (&[8, 1024], &[0, 50]),
169-
_ => (&[8, 64], &[0, 50]),
170-
};
171-
for &len in lengths {
172-
let rows = (65_536 / len).min(4096);
173-
for &null_percent in null_percentages {
174-
let input = if kind == "nested_float64" {
175-
nested_input(rows, len, null_percent)
176-
} else {
177-
primitive_input(kind, rows, len, null_percent)
178-
};
179-
bench_case(&mut group, input, len, null_percent);
43+
let mut group = c.benchmark_group("array_extrema");
44+
group.sample_size(20);
45+
group.warm_up_time(Duration::from_millis(250));
46+
group.measurement_time(Duration::from_secs(1));
47+
for len in [8, 1024] {
48+
for nullable in [false, true] {
49+
let values = (0..64 * len)
50+
.map(|i| (!nullable || i % 10 != 0).then_some(((i * 17) % 1000 + 1) as f64));
51+
let inputs = [
52+
(
53+
"float32",
54+
list(
55+
Arc::new(Float32Array::from_iter(
56+
values.clone().map(|v| v.map(|v| v as f32)),
57+
)),
58+
len,
59+
nullable,
60+
),
61+
),
62+
(
63+
"float64",
64+
list(Arc::new(Float64Array::from_iter(values)), len, nullable),
65+
),
66+
(
67+
"nested",
68+
// Null list elements are skipped by both engines. Keep inner floats non-null
69+
// so the fixture does not depend on their different nested null ordering.
70+
list(
71+
list(
72+
Arc::new(Float64Array::from_iter_values(
73+
(0..64 * len * 4).map(|i| ((i * 17) % 1000 + 1) as f64),
74+
)),
75+
4,
76+
nullable,
77+
),
78+
len,
79+
nullable,
80+
),
81+
),
82+
];
83+
for (kind, input) in inputs {
84+
for is_min in [true, false] {
85+
let comet = ScalarUDF::from(SparkArrayExtrema::new(is_min));
86+
let datafusion = if is_min {
87+
array_min_udf()
88+
} else {
89+
array_max_udf()
90+
};
91+
let args = ScalarFunctionArgs {
92+
args: vec![ColumnarValue::Array(Arc::clone(&input))],
93+
arg_fields: vec![Arc::new(Field::new(
94+
"input",
95+
input.data_type().clone(),
96+
true,
97+
))],
98+
number_rows: input.len(),
99+
return_field: Arc::new(Field::new(
100+
"result",
101+
comet.return_type(&[input.data_type().clone()]).unwrap(),
102+
true,
103+
)),
104+
config_options: Arc::new(ConfigOptions::default()),
105+
};
106+
let evaluate = |udf: &ScalarUDF| {
107+
udf.invoke_with_args(args.clone())
108+
.unwrap()
109+
.into_array(input.len())
110+
.unwrap()
111+
};
112+
assert_eq!(evaluate(&comet).to_data(), evaluate(&datafusion).to_data());
113+
let op = if is_min { "min" } else { "max" };
114+
for (engine, udf) in [("comet", &comet), ("datafusion", datafusion.as_ref())] {
115+
group.bench_function(
116+
BenchmarkId::new(
117+
format!("{kind}_{op}_{engine}"),
118+
format!("len={len}_null={nullable}"),
119+
),
120+
|b| {
121+
b.iter(|| {
122+
black_box(
123+
udf.invoke_with_args(black_box(args.clone())).unwrap(),
124+
)
125+
})
126+
},
127+
);
128+
}
129+
}
180130
}
181131
}
182-
group.finish();
183132
}
133+
group.finish();
184134
}
185135

186136
criterion_group!(benches, criterion_benchmark);

0 commit comments

Comments
 (0)