Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions datafusion/physical-expr/src/datetime_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"Date partx '{}' not supported",
"Date part '{}' not supported",

date_part
))),
}?;
Expand All @@ -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());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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/value etc. and work directly on the values (check the arrow-rs kernels for examples).
Ok to leave this for a future PR if you like 👍

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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
Expand Down