Skip to content

Commit 8bae266

Browse files
committed
.
1 parent d3d3e16 commit 8bae266

4 files changed

Lines changed: 25 additions & 39 deletions

File tree

datafusion/functions/benches/to_time.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ fn random_time_string(rng: &mut ThreadRng) -> String {
3838
}
3939

4040
fn time_strings(rng: &mut ThreadRng) -> StringArray {
41-
let strings: Vec<String> = (0..100_000)
42-
.map(|_| random_time_string(rng))
43-
.collect();
41+
let strings: Vec<String> = (0..100_000).map(|_| random_time_string(rng)).collect();
4442
StringArray::from(strings)
4543
}
4644

@@ -84,11 +82,7 @@ fn bench_to_time(c: &mut Criterion, name: &str, array: ArrayRef) {
8482

8583
fn criterion_benchmark(c: &mut Criterion) {
8684
let mut rng = rand::rng();
87-
bench_to_time(
88-
c,
89-
"to_time_no_nulls_100k",
90-
Arc::new(time_strings(&mut rng)),
91-
);
85+
bench_to_time(c, "to_time_no_nulls_100k", Arc::new(time_strings(&mut rng)));
9286
bench_to_time(
9387
c,
9488
"to_time_10pct_nulls_100k",

datafusion/functions/src/datetime/make_time.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717

1818
use std::sync::Arc;
1919

20-
use arrow::array::builder::PrimitiveBuilder;
2120
use arrow::array::cast::AsArray;
2221
use arrow::array::types::Int32Type;
2322
use arrow::array::{Array, PrimitiveArray};
23+
use arrow::buffer::NullBuffer;
2424
use arrow::datatypes::DataType::Time32;
2525
use arrow::datatypes::{DataType, Time32SecondType, TimeUnit};
2626
use chrono::prelude::*;
@@ -142,24 +142,31 @@ impl ScalarUDFImpl for MakeTimeFunc {
142142
let minutes = minutes.as_primitive::<Int32Type>();
143143
let seconds = seconds.as_primitive::<Int32Type>();
144144

145-
let mut builder: PrimitiveBuilder<Time32SecondType> =
146-
PrimitiveArray::builder(len);
145+
let nulls = NullBuffer::union(
146+
NullBuffer::union(hours.nulls(), minutes.nulls()).as_ref(),
147+
seconds.nulls(),
148+
);
147149

150+
let mut values = Vec::with_capacity(len);
148151
for i in 0..len {
149-
// match postgresql behaviour which returns null for any null input
150-
if hours.is_null(i) || minutes.is_null(i) || seconds.is_null(i) {
151-
builder.append_null();
152+
// Match Postgres behaviour which returns null for any null input
153+
if nulls.as_ref().is_some_and(|n| n.is_null(i)) {
154+
values.push(0);
152155
} else {
153156
make_time_inner(
154157
hours.value(i),
155158
minutes.value(i),
156159
seconds.value(i),
157-
|seconds: i32| builder.append_value(seconds),
160+
|seconds: i32| values.push(seconds),
158161
)?;
159162
}
160163
}
161164

162-
Ok(ColumnarValue::Array(Arc::new(builder.finish())))
165+
Ok(ColumnarValue::Array(Arc::new(PrimitiveArray::<
166+
Time32SecondType,
167+
>::new(
168+
values.into(), nulls
169+
))))
163170
}
164171
}
165172
}

datafusion/functions/src/datetime/to_local_time.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::ops::Add;
1919
use std::sync::Arc;
2020

2121
use arrow::array::timezone::Tz;
22-
use arrow::array::{ArrayRef, PrimitiveBuilder};
22+
use arrow::array::{ArrayRef, PrimitiveArray};
2323
use arrow::datatypes::DataType::Timestamp;
2424
use arrow::datatypes::TimeUnit::{Microsecond, Millisecond, Nanosecond, Second};
2525
use arrow::datatypes::{
@@ -153,18 +153,9 @@ fn transform_array<T: ArrowTimestampType>(
153153
tz: Tz,
154154
) -> Result<ColumnarValue> {
155155
let primitive_array = as_primitive_array::<T>(array)?;
156-
let mut builder = PrimitiveBuilder::<T>::with_capacity(primitive_array.len());
157-
for ts_opt in primitive_array.iter() {
158-
match ts_opt {
159-
None => builder.append_null(),
160-
Some(ts) => {
161-
let adjusted_ts: i64 = adjust_to_local_time::<T>(ts, tz)?;
162-
builder.append_value(adjusted_ts)
163-
}
164-
}
165-
}
166-
167-
Ok(ColumnarValue::Array(Arc::new(builder.finish())))
156+
let result: PrimitiveArray<T> =
157+
primitive_array.try_unary(|ts| adjust_to_local_time::<T>(ts, tz))?;
158+
Ok(ColumnarValue::Array(Arc::new(result)))
168159
}
169160

170161
fn to_local_time(time_value: &ColumnarValue) -> Result<ColumnarValue> {

datafusion/functions/src/datetime/to_time.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
// under the License.
1717

1818
use crate::datetime::common::*;
19-
use arrow::array::builder::PrimitiveBuilder;
2019
use arrow::array::cast::AsArray;
2120
use arrow::array::temporal_conversions::time_to_time64ns;
2221
use arrow::array::types::Time64NanosecondType;
@@ -213,20 +212,15 @@ fn parse_time_array<'a, A: StringArrayType<'a>>(
213212
array: &A,
214213
formats: &[&str],
215214
) -> Result<PrimitiveArray<Time64NanosecondType>> {
216-
let mut builder: PrimitiveBuilder<Time64NanosecondType> =
217-
PrimitiveArray::builder(array.len());
218-
215+
let mut values = Vec::with_capacity(array.len());
219216
for i in 0..array.len() {
220217
if array.is_null(i) {
221-
builder.append_null();
218+
values.push(0);
222219
} else {
223-
let s = array.value(i);
224-
let nanos = parse_time_with_formats(s, formats)?;
225-
builder.append_value(nanos);
220+
values.push(parse_time_with_formats(array.value(i), formats)?);
226221
}
227222
}
228-
229-
Ok(builder.finish())
223+
Ok(PrimitiveArray::new(values.into(), array.nulls().cloned()))
230224
}
231225

232226
/// Parse time string using provided formats

0 commit comments

Comments
 (0)