Skip to content

Commit 29580fa

Browse files
authored
refactor: share a single pad kernel between lpad and rpad (apache#26367)
## Which issue does this PR close? - Closes apache#26303. ## Rationale for this change lpad and rpad had four copies of the same per-row padding loop, plus two copies of the scalar fast path. The only real difference between them was whether the fill gets written before or after the string. ## What changes are included in this PR? - New `unicode/pad.rs` with a single kernel, generic over a `PadSide` trait (`LeftPad` / `RightPad`, same idea as `LeftRightSlicer` in `common.rs`). `lpad.rs` and `rpad.rs` are now thin wrappers with just the docs and tests. - The no-fill case goes through the same loop with `" "` as the fill, so the second loop is gone. - `parse_target_len` replaces the six copies of the `i32::MAX` / negative length check. Error text is unchanged. Two things came out of benchmarking: - Once the shared length check had its error branch inline, it stopped getting inlined into the hot loop and the no-fill path got noticeably slower. Moving the error into a `#[cold]` function fixed that. - The non-ASCII path now writes the fill in whole copies instead of one char at a time with a `%` per char. That's where the speedup below comes from. I also added `no fill` cases to the pad benchmarks (first commit). None of the existing cases covered a 2-arg call with a length column, which is the path this changes the most. ## What is the testing strategy for this PR? Existing lpad/rpad unit tests and sqllogictests pass unchanged. I added tests in `pad.rs` that pass array arguments to both functions, since most of the existing ones go through the scalar fast path: no fill, fill, nulls, zero length, truncation, string view input, a partial multi-char unicode fill (`rpad('josé', 6, 'ñéx')`), and the too-large length error. ## Benchmarks `cargo bench -p datafusion-functions --features unicode_expressions --bench unicode_expressions -- "pad size="`, main vs this branch, run back to back on the same machine: - non-ASCII string with a fill (8 cases): 31–48% faster - everything else (40 cases): somewhere between −16% and +14% in both directions, which is about the noise on my machine. The scalar cases, whose logic didn't change, moved by the same amount. Happy to re-run anything if someone wants different numbers. ## Are there any user-facing changes? No. --- Separately, I found that a negative length coming from a column panics on main (`pad_data_capacity` does `len as usize`). That's not part of this PR, and I'll open its own issue.
1 parent 3bc3d0b commit 29580fa

6 files changed

Lines changed: 646 additions & 859 deletions

File tree

‎datafusion/functions/benches/unicode_expressions/pad.rs‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,60 @@ fn criterion_benchmark(c: &mut Criterion) {
363363
},
364364
);
365365

366+
// Utf8 type without fill (pads with spaces)
367+
let mut args = create_pad_args::<i32>(size, 5, 20, false);
368+
args.truncate(2);
369+
let arg_fields = args
370+
.iter()
371+
.enumerate()
372+
.map(|(idx, arg)| {
373+
Field::new(format!("arg_{idx}"), arg.data_type(), true).into()
374+
})
375+
.collect::<Vec<_>>();
376+
377+
group.bench_function(
378+
format!("lpad utf8 no fill [size={size}, str_len=5, target=20]"),
379+
|b| {
380+
b.iter(|| {
381+
let args_cloned = args.clone();
382+
black_box(unicode::lpad().invoke_with_args(ScalarFunctionArgs {
383+
args: args_cloned,
384+
arg_fields: arg_fields.clone(),
385+
number_rows: size,
386+
return_field: Field::new("f", DataType::Utf8, true).into(),
387+
config_options: Arc::clone(&config_options),
388+
}))
389+
})
390+
},
391+
);
392+
393+
// Utf8 type with Unicode strings, without fill
394+
let mut args = create_unicode_pad_args(size, 20, false);
395+
args.truncate(2);
396+
let arg_fields = args
397+
.iter()
398+
.enumerate()
399+
.map(|(idx, arg)| {
400+
Field::new(format!("arg_{idx}"), arg.data_type(), true).into()
401+
})
402+
.collect::<Vec<_>>();
403+
404+
group.bench_function(
405+
format!("lpad utf8 unicode no fill [size={size}, target=20]"),
406+
|b| {
407+
b.iter(|| {
408+
let args_cloned = args.clone();
409+
black_box(unicode::lpad().invoke_with_args(ScalarFunctionArgs {
410+
args: args_cloned,
411+
arg_fields: arg_fields.clone(),
412+
number_rows: size,
413+
return_field: Field::new("f", DataType::Utf8, true).into(),
414+
config_options: Arc::clone(&config_options),
415+
}))
416+
})
417+
},
418+
);
419+
366420
// --- Scalar length + fill benchmarks ---
367421

368422
// Utf8 with scalar length and fill (3-arg)
@@ -641,6 +695,60 @@ fn criterion_benchmark(c: &mut Criterion) {
641695
},
642696
);
643697

698+
// Utf8 type without fill (pads with spaces)
699+
let mut args = create_pad_args::<i32>(size, 5, 20, false);
700+
args.truncate(2);
701+
let arg_fields = args
702+
.iter()
703+
.enumerate()
704+
.map(|(idx, arg)| {
705+
Field::new(format!("arg_{idx}"), arg.data_type(), true).into()
706+
})
707+
.collect::<Vec<_>>();
708+
709+
group.bench_function(
710+
format!("rpad utf8 no fill [size={size}, str_len=5, target=20]"),
711+
|b| {
712+
b.iter(|| {
713+
let args_cloned = args.clone();
714+
black_box(unicode::rpad().invoke_with_args(ScalarFunctionArgs {
715+
args: args_cloned,
716+
arg_fields: arg_fields.clone(),
717+
number_rows: size,
718+
return_field: Field::new("f", DataType::Utf8, true).into(),
719+
config_options: Arc::clone(&config_options),
720+
}))
721+
})
722+
},
723+
);
724+
725+
// Utf8 type with Unicode strings, without fill
726+
let mut args = create_unicode_pad_args(size, 20, false);
727+
args.truncate(2);
728+
let arg_fields = args
729+
.iter()
730+
.enumerate()
731+
.map(|(idx, arg)| {
732+
Field::new(format!("arg_{idx}"), arg.data_type(), true).into()
733+
})
734+
.collect::<Vec<_>>();
735+
736+
group.bench_function(
737+
format!("rpad utf8 unicode no fill [size={size}, target=20]"),
738+
|b| {
739+
b.iter(|| {
740+
let args_cloned = args.clone();
741+
black_box(unicode::rpad().invoke_with_args(ScalarFunctionArgs {
742+
args: args_cloned,
743+
arg_fields: arg_fields.clone(),
744+
number_rows: size,
745+
return_field: Field::new("f", DataType::Utf8, true).into(),
746+
config_options: Arc::clone(&config_options),
747+
}))
748+
})
749+
},
750+
);
751+
644752
// --- Scalar length + fill benchmarks ---
645753

646754
// Utf8 with scalar length and fill (3-arg)

0 commit comments

Comments
 (0)