|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use arrow::array::{Array, Date32Array, Int32Array}; |
| 19 | +use arrow::datatypes::DataType; |
| 20 | +use datafusion::common::{utils::take_function_args, DataFusionError, Result, ScalarValue}; |
| 21 | +use datafusion::logical_expr::{ |
| 22 | + ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility, |
| 23 | +}; |
| 24 | +use std::any::Any; |
| 25 | +use std::sync::Arc; |
| 26 | + |
| 27 | +/// Spark-compatible date_from_unix_date function. |
| 28 | +/// Converts an integer representing days since Unix epoch (1970-01-01) to a Date32 value. |
| 29 | +#[derive(Debug, PartialEq, Eq, Hash)] |
| 30 | +pub struct SparkDateFromUnixDate { |
| 31 | + signature: Signature, |
| 32 | + aliases: Vec<String>, |
| 33 | +} |
| 34 | + |
| 35 | +impl SparkDateFromUnixDate { |
| 36 | + pub fn new() -> Self { |
| 37 | + Self { |
| 38 | + signature: Signature::exact(vec![DataType::Int32], Volatility::Immutable), |
| 39 | + aliases: vec![], |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl Default for SparkDateFromUnixDate { |
| 45 | + fn default() -> Self { |
| 46 | + Self::new() |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl ScalarUDFImpl for SparkDateFromUnixDate { |
| 51 | + fn as_any(&self) -> &dyn Any { |
| 52 | + self |
| 53 | + } |
| 54 | + |
| 55 | + fn name(&self) -> &str { |
| 56 | + "date_from_unix_date" |
| 57 | + } |
| 58 | + |
| 59 | + fn signature(&self) -> &Signature { |
| 60 | + &self.signature |
| 61 | + } |
| 62 | + |
| 63 | + fn return_type(&self, _: &[DataType]) -> Result<DataType> { |
| 64 | + Ok(DataType::Date32) |
| 65 | + } |
| 66 | + |
| 67 | + fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> { |
| 68 | + let [unix_date] = take_function_args(self.name(), args.args)?; |
| 69 | + match unix_date { |
| 70 | + ColumnarValue::Array(arr) => { |
| 71 | + let int_array = arr.as_any().downcast_ref::<Int32Array>().ok_or_else(|| { |
| 72 | + DataFusionError::Execution( |
| 73 | + "date_from_unix_date expects Int32Array input".to_string(), |
| 74 | + ) |
| 75 | + })?; |
| 76 | + |
| 77 | + // Date32 and Int32 both represent days since epoch, so we can directly |
| 78 | + // reinterpret the values. The only operation needed is creating a Date32Array |
| 79 | + // from the same underlying i32 values. |
| 80 | + let date_array = |
| 81 | + Date32Array::new(int_array.values().clone(), int_array.nulls().cloned()); |
| 82 | + |
| 83 | + Ok(ColumnarValue::Array(Arc::new(date_array))) |
| 84 | + } |
| 85 | + ColumnarValue::Scalar(scalar) => match scalar { |
| 86 | + ScalarValue::Int32(Some(days)) => { |
| 87 | + Ok(ColumnarValue::Scalar(ScalarValue::Date32(Some(days)))) |
| 88 | + } |
| 89 | + ScalarValue::Int32(None) | ScalarValue::Null => { |
| 90 | + Ok(ColumnarValue::Scalar(ScalarValue::Date32(None))) |
| 91 | + } |
| 92 | + _ => Err(DataFusionError::Execution( |
| 93 | + "date_from_unix_date expects Int32 scalar input".to_string(), |
| 94 | + )), |
| 95 | + }, |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + fn aliases(&self) -> &[String] { |
| 100 | + &self.aliases |
| 101 | + } |
| 102 | +} |
0 commit comments