-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat(spark): implement Spark date_diff function
#19845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| // 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 std::any::Any; | ||
| use std::sync::Arc; | ||
|
|
||
| use arrow::datatypes::{DataType, Field, FieldRef}; | ||
| use datafusion_common::types::{NativeType, logical_date, logical_string}; | ||
| use datafusion_common::utils::take_function_args; | ||
| use datafusion_common::{Result, internal_err}; | ||
| use datafusion_expr::simplify::{ExprSimplifyResult, SimplifyContext}; | ||
| use datafusion_expr::{ | ||
| Coercion, ColumnarValue, Expr, ExprSchemable, Operator, ReturnFieldArgs, | ||
| ScalarFunctionArgs, ScalarUDFImpl, Signature, TypeSignatureClass, Volatility, | ||
| binary_expr, | ||
| }; | ||
|
|
||
| /// <https://spark.apache.org/docs/latest/api/sql/index.html#date_diff> | ||
| #[derive(Debug, PartialEq, Eq, Hash)] | ||
| pub struct SparkDateDiff { | ||
| signature: Signature, | ||
| aliases: Vec<String>, | ||
| } | ||
|
|
||
| impl Default for SparkDateDiff { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl SparkDateDiff { | ||
| pub fn new() -> Self { | ||
| Self { | ||
| signature: Signature::coercible( | ||
| vec![ | ||
| Coercion::new_implicit( | ||
| TypeSignatureClass::Native(logical_date()), | ||
| vec![ | ||
| TypeSignatureClass::Native(logical_string()), | ||
| TypeSignatureClass::Timestamp, | ||
| ], | ||
| NativeType::Date, | ||
| ), | ||
| Coercion::new_implicit( | ||
| TypeSignatureClass::Native(logical_date()), | ||
| vec![ | ||
| TypeSignatureClass::Native(logical_string()), | ||
| TypeSignatureClass::Timestamp, | ||
| ], | ||
| NativeType::Date, | ||
| ), | ||
| ], | ||
| Volatility::Immutable, | ||
| ), | ||
| aliases: vec!["datediff".to_string()], | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl ScalarUDFImpl for SparkDateDiff { | ||
| fn as_any(&self) -> &dyn Any { | ||
| self | ||
| } | ||
|
|
||
| fn name(&self) -> &str { | ||
| "date_diff" | ||
| } | ||
|
|
||
| fn aliases(&self) -> &[String] { | ||
| &self.aliases | ||
| } | ||
|
|
||
| fn signature(&self) -> &Signature { | ||
| &self.signature | ||
| } | ||
|
|
||
| fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> { | ||
| internal_err!("return_field_from_args should be used instead") | ||
| } | ||
|
|
||
| fn return_field_from_args(&self, args: ReturnFieldArgs) -> Result<FieldRef> { | ||
| let nullable = args.arg_fields.iter().any(|f| f.is_nullable()); | ||
| Ok(Arc::new(Field::new(self.name(), DataType::Int32, nullable))) | ||
| } | ||
|
|
||
| fn invoke_with_args(&self, _args: ScalarFunctionArgs) -> Result<ColumnarValue> { | ||
| internal_err!( | ||
| "spark date_diff should have been simplified to standard subtraction" | ||
| ) | ||
| } | ||
|
|
||
| fn simplify( | ||
| &self, | ||
| args: Vec<Expr>, | ||
| info: &SimplifyContext, | ||
| ) -> Result<ExprSimplifyResult> { | ||
| let [end, start] = take_function_args(self.name(), args)?; | ||
| Ok(ExprSimplifyResult::Simplified(binary_expr( | ||
| end.cast_to(&DataType::Int32, info.schema())?, | ||
| Operator::Minus, | ||
| start.cast_to(&DataType::Int32, info.schema())?, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if the argument data type is Date64 ? The signature allows it.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh good catch ! updated and added tests for it |
||
| ))) | ||
| } | ||
| } | ||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.