Skip to main content

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        Ok(Arc::new(Field::new(
87            self.name(),
88            DataType::Date32,
89            nullable,
90        )))
91    }
92
93    fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
94        make_scalar_function(spark_date_add, vec![])(&args.args)
95    }
96}
97
98fn spark_date_add(args: &[ArrayRef]) -> Result<ArrayRef> {
99    let [date_arg, days_arg] = take_function_args("date_add", args)?;
100    let date_array = as_date32_array(date_arg)?;
101    let result = match days_arg.data_type() {
102        DataType::Int8 => {
103            let days_array = as_int8_array(days_arg)?;
104            compute::binary::<_, _, _, Date32Type>(
105                date_array,
106                days_array,
107                |date, days| date.wrapping_add(days as i32),
108            )?
109        }
110        DataType::Int16 => {
111            let days_array = as_int16_array(days_arg)?;
112            compute::binary::<_, _, _, Date32Type>(
113                date_array,
114                days_array,
115                |date, days| date.wrapping_add(days as i32),
116            )?
117        }
118        DataType::Int32 => {
119            let days_array = as_int32_array(days_arg)?;
120            compute::binary::<_, _, _, Date32Type>(
121                date_array,
122                days_array,
123                |date, days| date.wrapping_add(days),
124            )?
125        }
126        _ => {
127            return internal_err!(
128                "Spark `date_add` function: argument must be int8, int16, int32, got {:?}",
129                days_arg.data_type()
130            );
131        }
132    };
133    Ok(Arc::new(result))
134}
135
136#[cfg(test)]
137mod tests {
138    use super::*;
139    use arrow::datatypes::Field;
140
141    #[test]
142    fn test_date_add_non_nullable_inputs() {
143        let func = SparkDateAdd::new();
144        let args = &[
145            Arc::new(Field::new("date", DataType::Date32, false)),
146            Arc::new(Field::new("num", DataType::Int8, false)),
147        ];
148
149        let ret_field = func
150            .return_field_from_args(ReturnFieldArgs {
151                arg_fields: args,
152                scalar_arguments: &[None, None],
153            })
154            .unwrap();
155
156        assert_eq!(ret_field.data_type(), &DataType::Date32);
157        assert!(!ret_field.is_nullable());
158    }
159
160    #[test]
161    fn test_date_add_nullable_inputs() {
162        let func = SparkDateAdd::new();
163        let args = &[
164            Arc::new(Field::new("date", DataType::Date32, false)),
165            Arc::new(Field::new("num", DataType::Int16, true)),
166        ];
167
168        let ret_field = func
169            .return_field_from_args(ReturnFieldArgs {
170                arg_fields: args,
171                scalar_arguments: &[None, None],
172            })
173            .unwrap();
174
175        assert_eq!(ret_field.data_type(), &DataType::Date32);
176        assert!(ret_field.is_nullable());
177    }
178}