-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat(spark): implement add_months function #19711
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
base: main
Are you sure you want to change the base?
Changes from all commits
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,103 @@ | ||
| // 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::ops::Add; | ||
| use std::sync::Arc; | ||
|
|
||
| use arrow::datatypes::{DataType, Field, FieldRef, IntervalUnit}; | ||
| use datafusion_common::utils::take_function_args; | ||
| use datafusion_common::{Result, internal_err}; | ||
| use datafusion_expr::simplify::{ExprSimplifyResult, SimplifyContext}; | ||
| use datafusion_expr::{ | ||
| Cast, ColumnarValue, Expr, ReturnFieldArgs, ScalarFunctionArgs, ScalarUDFImpl, | ||
| Signature, Volatility, | ||
| }; | ||
|
|
||
| /// <https://spark.apache.org/docs/latest/api/sql/index.html#add_months> | ||
| #[derive(Debug, PartialEq, Eq, Hash)] | ||
| pub struct SparkAddMonths { | ||
| signature: Signature, | ||
| } | ||
|
|
||
| impl Default for SparkAddMonths { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl SparkAddMonths { | ||
| pub fn new() -> Self { | ||
| Self { | ||
| signature: Signature::exact( | ||
| vec![DataType::Date32, DataType::Int32], | ||
| Volatility::Immutable, | ||
| ), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl ScalarUDFImpl for SparkAddMonths { | ||
| fn as_any(&self) -> &dyn Any { | ||
| self | ||
| } | ||
|
|
||
| fn name(&self) -> &str { | ||
| "add_months" | ||
| } | ||
|
|
||
| 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()) | ||
| || args | ||
| .scalar_arguments | ||
| .iter() | ||
| .any(|arg| matches!(arg, Some(sv) if sv.is_null())); | ||
|
|
||
| Ok(Arc::new(Field::new( | ||
| self.name(), | ||
| DataType::Date32, | ||
| nullable, | ||
| ))) | ||
| } | ||
|
|
||
| fn simplify( | ||
| &self, | ||
| args: Vec<Expr>, | ||
| _info: &SimplifyContext, | ||
| ) -> Result<ExprSimplifyResult> { | ||
| let [date_arg, months_arg] = take_function_args("add_months", args)?; | ||
|
|
||
| Ok(ExprSimplifyResult::Simplified(date_arg.add(Expr::Cast( | ||
| Cast::new( | ||
| Box::new(months_arg), | ||
| DataType::Interval(IntervalUnit::YearMonth), | ||
| ), | ||
| )))) | ||
| } | ||
|
|
||
| fn invoke_with_args(&self, _args: ScalarFunctionArgs) -> Result<ColumnarValue> { | ||
| internal_err!("invoke should not be called on a simplified add_months() function") | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,13 +15,38 @@ | |
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
|
Contributor
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. Does spark have a behaviour for overflow/result date too large that we need to consider?
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. saprk will error with
Contributor
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 this is interesting; panicking is definitely not good, and I can reproduce that on main too: DataFusion CLI v51.0.0
> select '2016-07-30'::date + arrow_cast(2147483647::int, 'Interval(YearMonth)');
thread 'main' (15797910) panicked at /Users/jeffrey/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/chrono-0.4.42/src/naive/date/mod.rs:2005:41:
`NaiveDate + Months` out of range
note: run with `RUST_BACKTRACE=1` environment variable to display a backtraceWe should raise this as a separate issue as this should be erroring not panicking
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. traced it back to https://github.com/chronotope/chrono/blob/1c0b8f011ab2f2e53c195df1866a1fb4c7fd193a/src/naive/date/mod.rs#L2034 |
||
| # This file was originally created by a porting script from: | ||
| # https://github.com/lakehq/sail/tree/43b6ed8221de5c4c4adbedbb267ae1351158b43c/crates/sail-spark-connect/tests/gold_data/function | ||
| # This file is part of the implementation of the datafusion-spark function library. | ||
| # For more information, please see: | ||
| # https://github.com/apache/datafusion/issues/15914 | ||
|
|
||
| ## Original Query: SELECT add_months('2016-08-31', 1); | ||
| ## PySpark 3.5.5 Result: {'add_months(2016-08-31, 1)': datetime.date(2016, 9, 30), 'typeof(add_months(2016-08-31, 1))': 'date', 'typeof(2016-08-31)': 'string', 'typeof(1)': 'int'} | ||
| #query | ||
| #SELECT add_months('2016-08-31'::string, 1::int); | ||
| query D | ||
| SELECT add_months('2016-07-30'::date, 1::int); | ||
| ---- | ||
| 2016-08-30 | ||
|
|
||
| query D | ||
| SELECT add_months('2016-07-30'::date, 0::int); | ||
| ---- | ||
| 2016-07-30 | ||
|
|
||
| query D | ||
| SELECT add_months('2016-07-30'::date, 10000::int); | ||
| ---- | ||
| 2849-11-30 | ||
|
|
||
| query D | ||
| SELECT add_months('2016-07-30'::date, -5::int); | ||
| ---- | ||
| 2016-02-29 | ||
|
|
||
| # Test with NULL values | ||
| query D | ||
| SELECT add_months(NULL::date, 1::int); | ||
| ---- | ||
| NULL | ||
|
|
||
| query D | ||
| SELECT add_months('2016-07-30'::date, NULL::int); | ||
| ---- | ||
| NULL | ||
|
|
||
| query D | ||
| SELECT add_months(NULL::date, NULL::int); | ||
| ---- | ||
| NULL | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,7 +41,7 @@ SELECT date_add('2016-07-30'::date, arrow_cast(1, 'Int8')); | |
| 2016-07-31 | ||
|
|
||
| query D | ||
| SELECT date_sub('2016-07-30'::date, 0::int); | ||
| SELECT date_add('2016-07-30'::date, 0::int); | ||
|
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. caught this issue |
||
| ---- | ||
| 2016-07-30 | ||
|
|
||
|
|
@@ -51,20 +51,15 @@ SELECT date_add('2016-07-30'::date, 2147483647::int)::int; | |
| -2147466637 | ||
|
|
||
| query I | ||
| SELECT date_sub('1969-01-01'::date, 2147483647::int)::int; | ||
| SELECT date_add('1969-01-01'::date, 2147483647::int)::int; | ||
| ---- | ||
| 2147483284 | ||
| 2147483282 | ||
|
|
||
| query D | ||
| SELECT date_add('2016-07-30'::date, 100000::int); | ||
| ---- | ||
| 2290-05-15 | ||
|
|
||
| query D | ||
| SELECT date_sub('2016-07-30'::date, 100000::int); | ||
| ---- | ||
| 1742-10-15 | ||
|
Comment on lines
-54
to
-66
Contributor
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. Maybe we can copy the date_sub ones into date_sub.slt?
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. updated |
||
|
|
||
| # Test with negative day values (should subtract days) | ||
| query D | ||
| SELECT date_add('2016-07-30'::date, -5::int); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If an argument is a scalar null then its corresponding field should already be nullable