From bec8956402fc3ca735a1cec22994454a877939c7 Mon Sep 17 00:00:00 2001 From: Marko Mikulicic Date: Fri, 24 Jun 2022 18:26:37 +0200 Subject: [PATCH] wip: Implement extract epoch --- .../physical-expr/src/datetime_expressions.rs | 47 +++++++++++++++++-- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/datafusion/physical-expr/src/datetime_expressions.rs b/datafusion/physical-expr/src/datetime_expressions.rs index 1e1cfd07f3b2d..ed50fcaea9828 100644 --- a/datafusion/physical-expr/src/datetime_expressions.rs +++ b/datafusion/physical-expr/src/datetime_expressions.rs @@ -18,11 +18,15 @@ //! DateTime expressions use arrow::{ - array::{Array, ArrayRef, GenericStringArray, OffsetSizeTrait, PrimitiveArray}, + array::{ + Array, ArrayRef, GenericStringArray, Int32Array, Int32Builder, OffsetSizeTrait, + PrimitiveArray, + }, compute::kernels::cast_utils::string_to_timestamp_nanos, datatypes::{ - ArrowPrimitiveType, DataType, TimestampMicrosecondType, TimestampMillisecondType, - TimestampNanosecondType, TimestampSecondType, + ArrowNumericType, ArrowPrimitiveType, ArrowTemporalType, DataType, + TimestampMicrosecondType, TimestampMillisecondType, TimestampNanosecondType, + TimestampSecondType, }, }; use arrow::{ @@ -356,8 +360,9 @@ pub fn date_part(args: &[ColumnarValue]) -> Result { "hour" => extract_date_part!(array, temporal::hour), "minute" => extract_date_part!(array, temporal::minute), "second" => extract_date_part!(array, temporal::second), + "epoch" => extract_date_part!(array, extract_epoch), _ => Err(DataFusionError::Execution(format!( - "Date part '{}' not supported", + "Date partx '{}' not supported", date_part ))), }?; @@ -372,6 +377,40 @@ pub fn date_part(args: &[ColumnarValue]) -> Result { }) } +fn extract_epoch(array: &PrimitiveArray) -> Result +where + T: ArrowTemporalType + ArrowNumericType, + i64: std::convert::From, +{ + let mut b = Int32Builder::new(array.len()); + match array.data_type() { + &DataType::Timestamp(ref unit, _) => { + for i in 0..array.len() { + if array.is_null(i) { + b.append_null()?; + } else { + let scale = match unit { + TimeUnit::Second => 1, + TimeUnit::Millisecond => 1_000, + TimeUnit::Microsecond => 1_000_000, + TimeUnit::Nanosecond => 1_000_000_000, + }; + let n: i64 = array.value(i).into(); + b.append_value((n / scale) as i32)?; + } + } + } + dt => { + return Ok(Err(arrow::error::ArrowError::ComputeError(format!( + "epoch does not support: {:?}", + dt + )))?); + } + } + + Ok(b.finish()) +} + #[cfg(test)] mod tests { use std::sync::Arc;