Skip to content

Commit 146a949

Browse files
authored
feat: extract (epoch from col) (#5555)
* feat: extract (epoch from col) * add more tests and refine error info
1 parent 1f8ede5 commit 146a949

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

‎datafusion/core/tests/sql/expr.rs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,6 +1313,24 @@ async fn test_extract_date_part() -> Result<()> {
13131313
Ok(())
13141314
}
13151315

1316+
#[tokio::test]
1317+
async fn test_extract_epoch() -> Result<()> {
1318+
test_expression!(
1319+
"extract(epoch from '1870-01-01T07:29:10.256'::timestamp)",
1320+
"-3155646649.744"
1321+
);
1322+
test_expression!(
1323+
"extract(epoch from '2000-01-01T00:00:00.000'::timestamp)",
1324+
"946684800.0"
1325+
);
1326+
test_expression!(
1327+
"extract(epoch from to_timestamp('2000-01-01T00:00:00+00:00'))",
1328+
"946684800.0"
1329+
);
1330+
test_expression!("extract(epoch from NULL::timestamp)", "NULL");
1331+
Ok(())
1332+
}
1333+
13161334
#[tokio::test]
13171335
async fn test_extract_date_part_func() -> Result<()> {
13181336
test_expression!(

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
//! DateTime expressions
1919
20+
use arrow::array::Float64Builder;
2021
use arrow::compute::cast;
2122
use arrow::{
2223
array::TimestampNanosecondArray, compute::kernels::temporal, datatypes::TimeUnit,
@@ -478,6 +479,7 @@ pub fn date_part(args: &[ColumnarValue]) -> Result<ColumnarValue> {
478479
"millisecond" => extract_date_part!(&array, millis),
479480
"microsecond" => extract_date_part!(&array, micros),
480481
"nanosecond" => extract_date_part!(&array, nanos),
482+
"epoch" => extract_date_part!(&array, epoch),
481483
_ => Err(DataFusionError::Execution(format!(
482484
"Date part '{date_part}' not supported"
483485
))),
@@ -537,6 +539,40 @@ where
537539
to_ticks(array, 1_000_000_000)
538540
}
539541

542+
fn epoch<T>(array: &PrimitiveArray<T>) -> Result<Float64Array>
543+
where
544+
T: ArrowTemporalType + ArrowNumericType,
545+
i64: From<T::Native>,
546+
{
547+
let mut b = Float64Builder::with_capacity(array.len());
548+
match array.data_type() {
549+
DataType::Timestamp(tu, _) => {
550+
for i in 0..array.len() {
551+
if array.is_null(i) {
552+
b.append_null();
553+
} else {
554+
let scale = match tu {
555+
TimeUnit::Second => 1,
556+
TimeUnit::Millisecond => 1_000,
557+
TimeUnit::Microsecond => 1_000_000,
558+
TimeUnit::Nanosecond => 1_000_000_000,
559+
};
560+
561+
let n: i64 = array.value(i).into();
562+
b.append_value(n as f64 / scale as f64);
563+
}
564+
}
565+
}
566+
_ => {
567+
return Err(DataFusionError::Internal(format!(
568+
"Can not convert {:?} to epoch",
569+
array.data_type()
570+
)))
571+
}
572+
}
573+
Ok(b.finish())
574+
}
575+
540576
#[cfg(test)]
541577
mod tests {
542578
use std::sync::Arc;

0 commit comments

Comments
 (0)