datafusion_spark/function/datetime/
date_add.rs

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
18use std::any::Any;
19use std::sync::Arc;
20
21use arrow::array::ArrayRef;
22use arrow::compute;
23use arrow::datatypes::{DataType, Date32Type, Field, FieldRef};
24use datafusion_common::cast::{
25    as_date32_array, as_int8_array, as_int16_array, as_int32_array,
26};
27use datafusion_common::utils::take_function_args;
28use datafusion_common::{Result, internal_err};
29use datafusion_expr::{
30    ColumnarValue, ReturnFieldArgs, ScalarFunctionArgs, ScalarUDFImpl, Signature,
31    TypeSignature, Volatility,
32};
33use datafusion_functions::utils::make_scalar_function;
34
35#[derive(Debug, PartialEq, Eq, Hash)]
36pub struct SparkDateAdd {
37    signature: Signature,
38    aliases: Vec<String>,
39}
40
41impl Default for SparkDateAdd {
42    fn default() -> Self {
43        Self::new()
44    }
45}
46
47impl SparkDateAdd {
48    pub fn new() -> Self {
49        Self {
50            signature: Signature::one_of(
51                vec![
52                    TypeSignature::Exact(vec![DataType::Date32, DataType::Int8]),
53                    TypeSignature::Exact(vec![DataType::Date32, DataType::Int16]),
54                    TypeSignature::Exact(vec![DataType::Date32, DataType::Int32]),
55                ],
56                Volatility::Immutable,
57            ),
58            aliases: vec!["dateadd".to_string()],
59        }
60    }
61}
62
63impl ScalarUDFImpl for SparkDateAdd {
64    fn as_any(&self) -> &dyn Any {
65        self
66    }
67
68    fn name(&self) -> &str {
69        "date_add"
70    }
71
72    fn aliases(&self) -> &[String] {
73        &self.aliases
74    }
75
76    fn signature(&self) -> &Signature {
77        &self.signature
78    }
79
80    fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
81        internal_err!("Use return_field_from_args in this case instead.")
82    }
83
84    fn return_field_from_args(&self, args: ReturnFieldArgs) -> Result<FieldRef> {
85        let nullable = args.arg_fields.iter().any(|f| f.is_nullable())
86            || args
87                .scalar_arguments
88                .iter()
89                .any(|arg| matches!(arg, Some(sv) if sv.is_null()));
90
91        Ok(Arc::new(Field::new(
92            self.name(),
93            DataType::Date32,
94            nullable,
95        )))
96    }
97
98    fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
99        make_scalar_function(spark_date_add, vec![])(&args.args)
100    }
101}
102
103fn spark_date_add(args: &[ArrayRef]) -> Result<ArrayRef> {
104    let [date_arg, days_arg] = take_function_args("date_add", args)?;
105    let date_array = as_date32_array(date_arg)?;
106    let result = match days_arg.data_type() {
107        DataType::Int8 => {
108            let days_array = as_int8_array(days_arg)?;
109            compute::binary::<_, _, _, Date32Type>(
110                date_array,
111                days_array,
112                |date, days| date.wrapping_add(days as i32),
113            )?
114        }
115        DataType::Int16 => {
116            let days_array = as_int16_array(days_arg)?;
117            compute::binary::<_, _, _, Date32Type>(
118                date_array,
119                days_array,
120                |date, days| date.wrapping_add(days as i32),
121            )?
122        }
123        DataType::Int32 => {
124            let days_array = as_int32_array(days_arg)?;
125            compute::binary::<_, _, _, Date32Type>(
126                date_array,
127                days_array,
128                |date, days| date.wrapping_add(days),
129            )?
130        }
131        _ => {
132            return internal_err!(
133                "Spark `date_add` function: argument must be int8, int16, int32, got {:?}",
134                days_arg.data_type()
135            );
136        }
137    };
138    Ok(Arc::new(result))
139}
140
141#[cfg(test)]
142mod tests {
143    use super::*;
144    use arrow::datatypes::Field;
145    use datafusion_common::ScalarValue;
146
147    #[test]
148    fn test_date_add_non_nullable_inputs() {
149        let func = SparkDateAdd::new();
150        let args = &[
151            Arc::new(Field::new("date", DataType::Date32, false)),
152            Arc::new(Field::new("num", DataType::Int8, false)),
153        ];
154
155        let ret_field = func
156            .return_field_from_args(ReturnFieldArgs {
157                arg_fields: args,
158                scalar_arguments: &[None, None],
159            })
160            .unwrap();
161
162        assert_eq!(ret_field.data_type(), &DataType::Date32);
163        assert!(!ret_field.is_nullable());
164    }
165
166    #[test]
167    fn test_date_add_nullable_inputs() {
168        let func = SparkDateAdd::new();
169        let args = &[
170            Arc::new(Field::new("date", DataType::Date32, false)),
171            Arc::new(Field::new("num", DataType::Int16, true)),
172        ];
173
174        let ret_field = func
175            .return_field_from_args(ReturnFieldArgs {
176                arg_fields: args,
177                scalar_arguments: &[None, None],
178            })
179            .unwrap();
180
181        assert_eq!(ret_field.data_type(), &DataType::Date32);
182        assert!(ret_field.is_nullable());
183    }
184
185    #[test]
186    fn test_date_add_null_scalar() {
187        let func = SparkDateAdd::new();
188        let args = &[
189            Arc::new(Field::new("date", DataType::Date32, false)),
190            Arc::new(Field::new("num", DataType::Int32, false)),
191        ];
192
193        let null_scalar = ScalarValue::Int32(None);
194
195        let ret_field = func
196            .return_field_from_args(ReturnFieldArgs {
197                arg_fields: args,
198                scalar_arguments: &[None, Some(&null_scalar)],
199            })
200            .unwrap();
201
202        assert_eq!(ret_field.data_type(), &DataType::Date32);
203        assert!(ret_field.is_nullable());
204    }
205}