|
| 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::compute::kernels::arity::binary; |
| 20 | +use arrow::datatypes::DataType; |
| 21 | +use datafusion::common::{utils::take_function_args, DataFusionError, Result}; |
| 22 | +use datafusion::logical_expr::{ |
| 23 | + ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility, |
| 24 | +}; |
| 25 | +use std::any::Any; |
| 26 | +use std::sync::Arc; |
| 27 | + |
| 28 | +/// Spark-compatible date_diff function. |
| 29 | +/// Returns the number of days from startDate to endDate (endDate - startDate). |
| 30 | +#[derive(Debug, PartialEq, Eq, Hash)] |
| 31 | +pub struct SparkDateDiff { |
| 32 | + signature: Signature, |
| 33 | + aliases: Vec<String>, |
| 34 | +} |
| 35 | + |
| 36 | +impl SparkDateDiff { |
| 37 | + pub fn new() -> Self { |
| 38 | + Self { |
| 39 | + signature: Signature::exact( |
| 40 | + vec![DataType::Date32, DataType::Date32], |
| 41 | + Volatility::Immutable, |
| 42 | + ), |
| 43 | + aliases: vec!["datediff".to_string()], |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +impl Default for SparkDateDiff { |
| 49 | + fn default() -> Self { |
| 50 | + Self::new() |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl ScalarUDFImpl for SparkDateDiff { |
| 55 | + fn as_any(&self) -> &dyn Any { |
| 56 | + self |
| 57 | + } |
| 58 | + |
| 59 | + fn name(&self) -> &str { |
| 60 | + "date_diff" |
| 61 | + } |
| 62 | + |
| 63 | + fn signature(&self) -> &Signature { |
| 64 | + &self.signature |
| 65 | + } |
| 66 | + |
| 67 | + fn return_type(&self, _: &[DataType]) -> Result<DataType> { |
| 68 | + Ok(DataType::Int32) |
| 69 | + } |
| 70 | + |
| 71 | + fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> { |
| 72 | + let [end_date, start_date] = take_function_args(self.name(), args.args)?; |
| 73 | + |
| 74 | + // Convert scalars to arrays for uniform processing |
| 75 | + let end_arr = end_date.into_array(1)?; |
| 76 | + let start_arr = start_date.into_array(1)?; |
| 77 | + |
| 78 | + let end_date_array = end_arr |
| 79 | + .as_any() |
| 80 | + .downcast_ref::<Date32Array>() |
| 81 | + .ok_or_else(|| { |
| 82 | + DataFusionError::Execution("date_diff expects Date32Array for end_date".to_string()) |
| 83 | + })?; |
| 84 | + |
| 85 | + let start_date_array = start_arr |
| 86 | + .as_any() |
| 87 | + .downcast_ref::<Date32Array>() |
| 88 | + .ok_or_else(|| { |
| 89 | + DataFusionError::Execution( |
| 90 | + "date_diff expects Date32Array for start_date".to_string(), |
| 91 | + ) |
| 92 | + })?; |
| 93 | + |
| 94 | + // Date32 stores days since epoch, so difference is just subtraction |
| 95 | + let result: Int32Array = |
| 96 | + binary(end_date_array, start_date_array, |end, start| end - start)?; |
| 97 | + |
| 98 | + Ok(ColumnarValue::Array(Arc::new(result))) |
| 99 | + } |
| 100 | + |
| 101 | + fn aliases(&self) -> &[String] { |
| 102 | + &self.aliases |
| 103 | + } |
| 104 | +} |
0 commit comments