Repository navigation
wip: Implement extract epoch #2786
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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<ColumnarValue> { | |
| "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<ColumnarValue> { | |
| }) | ||
| } | ||
|
|
||
| fn extract_epoch<T>(array: &PrimitiveArray<T>) -> Result<Int32Array> | ||
| where | ||
| T: ArrowTemporalType + ArrowNumericType, | ||
| i64: std::convert::From<T::Native>, | ||
| { | ||
| let mut b = Int32Builder::new(array.len()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: In terms of performance (and readability), it is better to avoid the builder/ |
||
| 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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would recommend using the |
||
| 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; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.