|
17 | 17 |
|
18 | 18 | //! DateTime expressions |
19 | 19 |
|
| 20 | +use arrow::array::Float64Builder; |
20 | 21 | use arrow::compute::cast; |
21 | 22 | use arrow::{ |
22 | 23 | array::TimestampNanosecondArray, compute::kernels::temporal, datatypes::TimeUnit, |
@@ -478,6 +479,7 @@ pub fn date_part(args: &[ColumnarValue]) -> Result<ColumnarValue> { |
478 | 479 | "millisecond" => extract_date_part!(&array, millis), |
479 | 480 | "microsecond" => extract_date_part!(&array, micros), |
480 | 481 | "nanosecond" => extract_date_part!(&array, nanos), |
| 482 | + "epoch" => extract_date_part!(&array, epoch), |
481 | 483 | _ => Err(DataFusionError::Execution(format!( |
482 | 484 | "Date part '{date_part}' not supported" |
483 | 485 | ))), |
@@ -537,6 +539,40 @@ where |
537 | 539 | to_ticks(array, 1_000_000_000) |
538 | 540 | } |
539 | 541 |
|
| 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 | + |
540 | 576 | #[cfg(test)] |
541 | 577 | mod tests { |
542 | 578 | use std::sync::Arc; |
|
0 commit comments