forked from apache/datafusion-comet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate_diff.rs
More file actions
104 lines (89 loc) · 3.21 KB
/
Copy pathdate_diff.rs
File metadata and controls
104 lines (89 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use arrow::array::{Array, Date32Array, Int32Array};
use arrow::compute::kernels::arity::binary;
use arrow::datatypes::DataType;
use datafusion::common::{utils::take_function_args, DataFusionError, Result};
use datafusion::logical_expr::{
ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility,
};
use std::any::Any;
use std::sync::Arc;
/// Spark-compatible date_diff function.
/// Returns the number of days from startDate to endDate (endDate - startDate).
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct SparkDateDiff {
signature: Signature,
aliases: Vec<String>,
}
impl SparkDateDiff {
pub fn new() -> Self {
Self {
signature: Signature::exact(
vec![DataType::Date32, DataType::Date32],
Volatility::Immutable,
),
aliases: vec!["datediff".to_string()],
}
}
}
impl Default for SparkDateDiff {
fn default() -> Self {
Self::new()
}
}
impl ScalarUDFImpl for SparkDateDiff {
fn as_any(&self) -> &dyn Any {
self
}
fn name(&self) -> &str {
"date_diff"
}
fn signature(&self) -> &Signature {
&self.signature
}
fn return_type(&self, _: &[DataType]) -> Result<DataType> {
Ok(DataType::Int32)
}
fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
let [end_date, start_date] = take_function_args(self.name(), args.args)?;
// Convert scalars to arrays for uniform processing
let end_arr = end_date.into_array(1)?;
let start_arr = start_date.into_array(1)?;
let end_date_array = end_arr
.as_any()
.downcast_ref::<Date32Array>()
.ok_or_else(|| {
DataFusionError::Execution("date_diff expects Date32Array for end_date".to_string())
})?;
let start_date_array = start_arr
.as_any()
.downcast_ref::<Date32Array>()
.ok_or_else(|| {
DataFusionError::Execution(
"date_diff expects Date32Array for start_date".to_string(),
)
})?;
// Date32 stores days since epoch, so difference is just subtraction
let result: Int32Array =
binary(end_date_array, start_date_array, |end, start| end - start)?;
Ok(ColumnarValue::Array(Arc::new(result)))
}
fn aliases(&self) -> &[String] {
&self.aliases
}
}