Skip to content

Commit bec8956

Browse files
author
Marko Mikulicic
committed
wip: Implement extract epoch
1 parent 11ae737 commit bec8956

1 file changed

Lines changed: 43 additions & 4 deletions

File tree

‎datafusion/physical-expr/src/datetime_expressions.rs‎

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@
1818
//! DateTime expressions
1919
2020
use arrow::{
21-
array::{Array, ArrayRef, GenericStringArray, OffsetSizeTrait, PrimitiveArray},
21+
array::{
22+
Array, ArrayRef, GenericStringArray, Int32Array, Int32Builder, OffsetSizeTrait,
23+
PrimitiveArray,
24+
},
2225
compute::kernels::cast_utils::string_to_timestamp_nanos,
2326
datatypes::{
24-
ArrowPrimitiveType, DataType, TimestampMicrosecondType, TimestampMillisecondType,
25-
TimestampNanosecondType, TimestampSecondType,
27+
ArrowNumericType, ArrowPrimitiveType, ArrowTemporalType, DataType,
28+
TimestampMicrosecondType, TimestampMillisecondType, TimestampNanosecondType,
29+
TimestampSecondType,
2630
},
2731
};
2832
use arrow::{
@@ -356,8 +360,9 @@ pub fn date_part(args: &[ColumnarValue]) -> Result<ColumnarValue> {
356360
"hour" => extract_date_part!(array, temporal::hour),
357361
"minute" => extract_date_part!(array, temporal::minute),
358362
"second" => extract_date_part!(array, temporal::second),
363+
"epoch" => extract_date_part!(array, extract_epoch),
359364
_ => Err(DataFusionError::Execution(format!(
360-
"Date part '{}' not supported",
365+
"Date partx '{}' not supported",
361366
date_part
362367
))),
363368
}?;
@@ -372,6 +377,40 @@ pub fn date_part(args: &[ColumnarValue]) -> Result<ColumnarValue> {
372377
})
373378
}
374379

380+
fn extract_epoch<T>(array: &PrimitiveArray<T>) -> Result<Int32Array>
381+
where
382+
T: ArrowTemporalType + ArrowNumericType,
383+
i64: std::convert::From<T::Native>,
384+
{
385+
let mut b = Int32Builder::new(array.len());
386+
match array.data_type() {
387+
&DataType::Timestamp(ref unit, _) => {
388+
for i in 0..array.len() {
389+
if array.is_null(i) {
390+
b.append_null()?;
391+
} else {
392+
let scale = match unit {
393+
TimeUnit::Second => 1,
394+
TimeUnit::Millisecond => 1_000,
395+
TimeUnit::Microsecond => 1_000_000,
396+
TimeUnit::Nanosecond => 1_000_000_000,
397+
};
398+
let n: i64 = array.value(i).into();
399+
b.append_value((n / scale) as i32)?;
400+
}
401+
}
402+
}
403+
dt => {
404+
return Ok(Err(arrow::error::ArrowError::ComputeError(format!(
405+
"epoch does not support: {:?}",
406+
dt
407+
)))?);
408+
}
409+
}
410+
411+
Ok(b.finish())
412+
}
413+
375414
#[cfg(test)]
376415
mod tests {
377416
use std::sync::Arc;

0 commit comments

Comments
 (0)