Skip to content

Commit 1c84bac

Browse files
peterxcliclaude
andcommitted
perf: keep dense-null and low-cardinality dictionary inputs off the slow path
Review of #5771 asked for dense-null and dictionary shapes in the no-regression comparison. Adding them found two real regressions, both now fixed. Arrow's `unary_opt`, which the replaced `date_part` path uses, visits only valid indices via `try_for_each_valid_idx`, while `unary` evaluates every slot. An all-null batch was therefore nearly free before and was not after: dayofweek on a flat all-null array measured 0.30x, a 3.3x slowdown. `map_dates` and the clock fast path now switch on null density. The threshold is half the batch rather than the mere presence of a null, because `unary` was still ahead at 87.5% nulls and switching on any null gave up most of the win on a lightly-null column (sparse dropped from 8.97x to 3.45x before the threshold was tuned). Arrow's `date_part` maps a dictionary's values and rewraps the keys, so the old chain's calendar work was proportional to cardinality. Decoding to Date32 first made a cardinality-8 column about 2x slower (0.51x). The weekday kernels now map the dictionary values and rewrap, then unpack, matching the old cost profile with a cheaper kernel; cardinality 1024 improves from 1.19x to about 2.1x as a result. `map_dates` takes a generic `F: Fn(i32) -> i32 + Copy` rather than a `fn` pointer: a pointer forces an indirect call per element inside `unary` and cost about 2.5x (no-null dayofweek fell from 9.0x to 3.7x before this was corrected). Benchmarks: both targets gain dense-null and all-null shapes plus dictionary inputs at cardinality 8 and 1024, comparing the real UDF against the complete old chain including the unpacking cast. The old-path arm also had `Cast` and `Add` in the wrong order; the serde emitted `Add(Cast(datepart(..), Int32), 1)`, and arrow rejects `Dictionary(Int32, Int32) + Int32`, so the dictionary comparison could not have been valid before. The clock generator spanned only 1965-1966 despite its mixed-epoch description and now walks 1875 to 2064 across the epoch. Equivalence assertions compare nullability per row on every shape. Co-Authored-By: Claude Opus 5 <[email protected]>
1 parent fd4c716 commit 1c84bac

4 files changed

Lines changed: 301 additions & 157 deletions

File tree

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

Lines changed: 119 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@
2626
//! `Date32` runs `unary_opt(|d| date32_to_datetime(d).map(..))` -- a `NaiveDateTime` per row plus
2727
//! a recomputed null mask -- and the `+ 1` / `- 1` is a second pass over the result.
2828
29-
use arrow::array::{Array, ArrayRef, Date32Array, Int32Array, Scalar};
29+
use arrow::array::{Array, ArrayRef, Date32Array, DictionaryArray, Int32Array, Scalar};
3030
use arrow::compute::kernels::numeric::{add_wrapping, sub_wrapping};
31-
use arrow::compute::{date_part, DatePart};
32-
use arrow::datatypes::DataType;
33-
use arrow::datatypes::Field;
31+
use arrow::compute::{cast, date_part, DatePart};
32+
use arrow::datatypes::{DataType, Field, Int32Type};
3433
use criterion::{criterion_group, criterion_main, Criterion, Throughput};
3534
use datafusion::config::ConfigOptions;
3635
use datafusion::logical_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl};
@@ -39,13 +38,43 @@ use std::hint::black_box;
3938
use std::sync::Arc;
4039

4140
const ROWS: usize = 8_192;
42-
const NULL_STRIDE: usize = 8;
41+
42+
/// Null density of an input shape. The old path's `unary_opt` only visits valid slots while the
43+
/// kernel's `unary` visits every slot, so the dense and all-null shapes are where a replacement
44+
/// like this is most likely to backfire.
45+
#[derive(Clone, Copy)]
46+
enum Nulls {
47+
None,
48+
Sparse,
49+
Dense,
50+
All,
51+
}
52+
53+
impl Nulls {
54+
fn tag(self) -> &'static str {
55+
match self {
56+
Nulls::None => "no_nulls",
57+
Nulls::Sparse => "sparse_nulls",
58+
Nulls::Dense => "dense_nulls",
59+
Nulls::All => "all_nulls",
60+
}
61+
}
62+
63+
fn is_null(self, i: usize) -> bool {
64+
match self {
65+
Nulls::None => false,
66+
Nulls::Sparse => i.is_multiple_of(8), // 12.5%
67+
Nulls::Dense => !i.is_multiple_of(8), // 87.5%
68+
Nulls::All => true,
69+
}
70+
}
71+
}
4372

4473
/// Epoch days spread over roughly 1970..2050, the range a date column actually holds.
45-
fn dates(nulls: bool) -> Date32Array {
74+
fn dates(nulls: Nulls) -> Date32Array {
4675
(0..ROWS)
4776
.map(|i| {
48-
if nulls && i.is_multiple_of(NULL_STRIDE) {
77+
if nulls.is_null(i) {
4978
None
5079
} else {
5180
Some((i as i32).wrapping_mul(3) % 29_220)
@@ -54,19 +83,45 @@ fn dates(nulls: bool) -> Date32Array {
5483
.collect()
5584
}
5685

57-
// ---- current path -------------------------------------------------------------------------
86+
/// A dictionary-encoded date column, the shape a partition column arrives in from a Parquet
87+
/// scan. Cardinality matters: the old path ran the calendar conversion over the dictionary
88+
/// *values* only and rewrapped the keys, so a low-cardinality column did very little work.
89+
fn dict_dates(cardinality: usize, nulls: Nulls) -> DictionaryArray<Int32Type> {
90+
let values = Arc::new(Date32Array::from(
91+
(0..cardinality)
92+
.map(|i| (i as i32).wrapping_mul(37) % 29_220)
93+
.collect::<Vec<_>>(),
94+
)) as ArrayRef;
95+
let keys: Int32Array = (0..ROWS)
96+
.map(|i| {
97+
if nulls.is_null(i) {
98+
None
99+
} else {
100+
Some((i % cardinality) as i32)
101+
}
102+
})
103+
.collect();
104+
DictionaryArray::<Int32Type>::new(keys, values)
105+
}
106+
107+
// ---- the chain the serde emitted before this change ------------------------------------------
58108

109+
/// The serde emitted `Add(Cast(datepart('dow', child), Int32), 1)`. The cast came *first*: it was
110+
/// an identity no-op for a plain array, but it is what unpacked a dictionary result, and arrow's
111+
/// arithmetic kernels reject `Dictionary(Int32, Int32) + Int32` outright, so the order matters.
59112
fn current_dayofweek(array: &ArrayRef) -> ArrayRef {
60113
let part = date_part(array.as_ref(), DatePart::DayOfWeekSunday0).unwrap();
61-
add_wrapping(&part, &Scalar::new(Int32Array::from(vec![1]))).unwrap()
114+
let unpacked = cast(&part, &DataType::Int32).unwrap();
115+
add_wrapping(&unpacked, &Scalar::new(Int32Array::from(vec![1]))).unwrap()
62116
}
63117

64118
fn current_weekday(array: &ArrayRef) -> ArrayRef {
65119
let part = date_part(array.as_ref(), DatePart::DayOfWeekMonday1).unwrap();
66-
sub_wrapping(&part, &Scalar::new(Int32Array::from(vec![1]))).unwrap()
120+
let unpacked = cast(&part, &DataType::Int32).unwrap();
121+
sub_wrapping(&unpacked, &Scalar::new(Int32Array::from(vec![1]))).unwrap()
67122
}
68123

69-
// ---- native kernels, as the serde now emits them --------------------------------------------
124+
// ---- native kernels, as the serde emits them now ---------------------------------------------
70125

71126
fn invoke(udf: &dyn ScalarUDFImpl, array: &ArrayRef) -> ArrayRef {
72127
udf.invoke_with_args(ScalarFunctionArgs {
@@ -81,53 +136,67 @@ fn invoke(udf: &dyn ScalarUDFImpl, array: &ArrayRef) -> ArrayRef {
81136
.unwrap()
82137
}
83138

84-
/// The benchmark is only meaningful if both arms agree, so check before timing.
85-
fn assert_equivalent(array: &Date32Array) {
86-
let dyn_array: ArrayRef = Arc::new(array.clone());
139+
/// Both arms must agree, values *and* null buffer, before any timing is meaningful.
140+
fn assert_equivalent(array: &ArrayRef) {
87141
for (current, native) in [
88142
(
89-
current_dayofweek(&dyn_array),
90-
invoke(&SparkDayOfWeek::new(), &dyn_array),
91-
),
92-
(
93-
current_weekday(&dyn_array),
94-
invoke(&SparkWeekDay::new(), &dyn_array),
143+
current_dayofweek(array),
144+
invoke(&SparkDayOfWeek::new(), array),
95145
),
146+
(current_weekday(array), invoke(&SparkWeekDay::new(), array)),
96147
] {
97-
assert_eq!(
98-
current.as_any().downcast_ref::<Int32Array>().unwrap(),
99-
native.as_any().downcast_ref::<Int32Array>().unwrap(),
100-
);
148+
let a = current.as_any().downcast_ref::<Int32Array>().unwrap();
149+
let b = native.as_any().downcast_ref::<Int32Array>().unwrap();
150+
// Compare nullability logically: the old path materialises an all-valid null buffer
151+
// where the kernel leaves `None`, which is the same thing to every consumer.
152+
assert_eq!(a.null_count(), b.null_count(), "null counts differ");
153+
for i in 0..a.len() {
154+
assert_eq!(a.is_null(i), b.is_null(i), "null flag differs at row {i}");
155+
}
156+
assert_eq!(a, b, "values differ");
101157
}
102158
}
103159

160+
fn bench_shape(c: &mut Criterion, shape: &str, array: ArrayRef) {
161+
assert_equivalent(&array);
162+
163+
let mut group = c.benchmark_group(format!("dayofweek/{shape}"));
164+
group.throughput(Throughput::Elements(ROWS as u64));
165+
group.bench_function("datepart_dow_plus_one", |b| {
166+
b.iter(|| black_box(current_dayofweek(black_box(&array))))
167+
});
168+
let dow = SparkDayOfWeek::new();
169+
group.bench_function("native_spark_dayofweek", |b| {
170+
b.iter(|| black_box(invoke(black_box(&dow), black_box(&array))))
171+
});
172+
group.finish();
173+
174+
let mut group = c.benchmark_group(format!("weekday/{shape}"));
175+
group.throughput(Throughput::Elements(ROWS as u64));
176+
group.bench_function("datepart_isodow_minus_one", |b| {
177+
b.iter(|| black_box(current_weekday(black_box(&array))))
178+
});
179+
let wd = SparkWeekDay::new();
180+
group.bench_function("native_spark_weekday", |b| {
181+
b.iter(|| black_box(invoke(black_box(&wd), black_box(&array))))
182+
});
183+
group.finish();
184+
}
185+
104186
fn criterion_benchmark(c: &mut Criterion) {
105-
for (nulls, null_tag) in [(false, "no_nulls"), (true, "sparse_nulls")] {
106-
let array = dates(nulls);
107-
assert_equivalent(&array);
108-
let dyn_array: ArrayRef = Arc::new(array);
109-
110-
let mut group = c.benchmark_group(format!("dayofweek/{null_tag}"));
111-
group.throughput(Throughput::Elements(ROWS as u64));
112-
group.bench_function("datepart_dow_plus_one", |b| {
113-
b.iter(|| black_box(current_dayofweek(black_box(&dyn_array))))
114-
});
115-
let dow = SparkDayOfWeek::new();
116-
group.bench_function("native_spark_dayofweek", |b| {
117-
b.iter(|| black_box(invoke(black_box(&dow), black_box(&dyn_array))))
118-
});
119-
group.finish();
120-
121-
let mut group = c.benchmark_group(format!("weekday/{null_tag}"));
122-
group.throughput(Throughput::Elements(ROWS as u64));
123-
group.bench_function("datepart_isodow_minus_one", |b| {
124-
b.iter(|| black_box(current_weekday(black_box(&dyn_array))))
125-
});
126-
let wd = SparkWeekDay::new();
127-
group.bench_function("native_spark_weekday", |b| {
128-
b.iter(|| black_box(invoke(black_box(&wd), black_box(&dyn_array))))
129-
});
130-
group.finish();
187+
for nulls in [Nulls::None, Nulls::Sparse, Nulls::Dense, Nulls::All] {
188+
bench_shape(c, &format!("flat/{}", nulls.tag()), Arc::new(dates(nulls)));
189+
}
190+
// Low cardinality is the case the old path handled cheaply: it converted 8 distinct dates
191+
// and rewrapped the keys. High cardinality approaches one conversion per row.
192+
for cardinality in [8usize, 1024] {
193+
for nulls in [Nulls::None, Nulls::Sparse, Nulls::Dense] {
194+
bench_shape(
195+
c,
196+
&format!("dict{cardinality}/{}", nulls.tag()),
197+
Arc::new(dict_dates(cardinality, nulls)),
198+
);
199+
}
131200
}
132201
}
133202

0 commit comments

Comments
 (0)