Skip to content

Commit 799bf43

Browse files
author
B Vadlamani
committed
spark_expr_floor
1 parent 8e02b8e commit 799bf43

5 files changed

Lines changed: 723 additions & 25 deletions

File tree

datafusion/spark/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,7 @@ name = "unhex"
9797
[[bench]]
9898
harness = false
9999
name = "sha2"
100+
101+
[[bench]]
102+
harness = false
103+
name = "floor"

datafusion/spark/benches/floor.rs

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
use arrow::array::*;
19+
use arrow::datatypes::*;
20+
use criterion::{Criterion, criterion_group, criterion_main};
21+
use datafusion_common::config::ConfigOptions;
22+
use datafusion_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl};
23+
use datafusion_spark::function::math::floor::SparkFloor;
24+
use rand::rngs::StdRng;
25+
use rand::{Rng, SeedableRng};
26+
use std::hint::black_box;
27+
use std::sync::Arc;
28+
29+
fn seedable_rng() -> StdRng {
30+
StdRng::seed_from_u64(42)
31+
}
32+
33+
fn generate_float64_data(size: usize, null_density: f32) -> Float64Array {
34+
let mut rng = seedable_rng();
35+
(0..size)
36+
.map(|_| {
37+
if rng.random::<f32>() < null_density {
38+
None
39+
} else {
40+
Some(rng.random_range::<f64, _>(-1_000_000.0..1_000_000.0))
41+
}
42+
})
43+
.collect()
44+
}
45+
46+
fn generate_decimal128_data(size: usize, null_density: f32) -> Decimal128Array {
47+
let mut rng = seedable_rng();
48+
let array: Decimal128Array = (0..size)
49+
.map(|_| {
50+
if rng.random::<f32>() < null_density {
51+
None
52+
} else {
53+
Some(rng.random_range::<i128, _>(-999_999_999..999_999_999))
54+
}
55+
})
56+
.collect();
57+
array.with_precision_and_scale(18, 2).unwrap()
58+
}
59+
60+
fn run_benchmark(
61+
c: &mut Criterion,
62+
name: &str,
63+
size: usize,
64+
array: Arc<dyn Array>,
65+
return_type: &DataType,
66+
) {
67+
let floor_func = SparkFloor::new();
68+
let args = vec![ColumnarValue::Array(array)];
69+
let arg_fields: Vec<_> = args
70+
.iter()
71+
.enumerate()
72+
.map(|(idx, arg)| Field::new(format!("arg_{idx}"), arg.data_type(), true).into())
73+
.collect();
74+
let config_options = Arc::new(ConfigOptions::default());
75+
76+
c.bench_function(&format!("{name}/size={size}"), |b| {
77+
b.iter(|| {
78+
black_box(
79+
floor_func
80+
.invoke_with_args(ScalarFunctionArgs {
81+
args: args.clone(),
82+
arg_fields: arg_fields.clone(),
83+
number_rows: size,
84+
return_field: Arc::new(Field::new(
85+
"f",
86+
return_type.clone(),
87+
true,
88+
)),
89+
config_options: Arc::clone(&config_options),
90+
})
91+
.unwrap(),
92+
)
93+
})
94+
});
95+
}
96+
97+
fn criterion_benchmark(c: &mut Criterion) {
98+
let sizes = vec![1024, 4096, 8192];
99+
let null_density = 0.1;
100+
101+
for &size in &sizes {
102+
let data = generate_float64_data(size, null_density);
103+
run_benchmark(c, "floor_float64", size, Arc::new(data), &DataType::Int64);
104+
}
105+
106+
for &size in &sizes {
107+
let data = generate_decimal128_data(size, null_density);
108+
run_benchmark(
109+
c,
110+
"floor_decimal128",
111+
size,
112+
Arc::new(data),
113+
&DataType::Decimal128(17, 0),
114+
);
115+
}
116+
}
117+
118+
criterion_group!(benches, criterion_benchmark);
119+
criterion_main!(benches);

0 commit comments

Comments
 (0)