Skip to content

feat(spark): implement Spark datetime function last_day #16828

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions datafusion/spark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ name = "datafusion_spark"

[dependencies]
arrow = { workspace = true }
chrono = { workspace = true }
datafusion-catalog = { workspace = true }
datafusion-common = { workspace = true }
datafusion-execution = { workspace = true }
Expand Down
125 changes: 125 additions & 0 deletions datafusion/spark/src/function/datetime/last_day.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// 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::array::{ArrayRef, AsArray, Date32Array};
use arrow::datatypes::{DataType, Date32Type};
use chrono::{Datelike, Duration, NaiveDate};
use datafusion_common::{exec_datafusion_err, internal_err, Result, ScalarValue};
use datafusion_expr::{
ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility,
};

#[derive(Debug)]
pub struct SparkLastDay {
signature: Signature,
}

impl Default for SparkLastDay {
fn default() -> Self {
Self::new()
}
}

impl SparkLastDay {
pub fn new() -> Self {
Self {
signature: Signature::exact(vec![DataType::Date32], Volatility::Immutable),
}
}
}

impl ScalarUDFImpl for SparkLastDay {
fn as_any(&self) -> &dyn Any {
self
}

fn name(&self) -> &str {
"last_day"
}

fn signature(&self) -> &Signature {
&self.signature
}

fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
Ok(DataType::Date32)
}

fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
let ScalarFunctionArgs { args, .. } = args;
let [arg] = args.as_slice() else {
return internal_err!(
"Spark `last_day` function requires 1 argument, got {}",
args.len()
);
};
match arg {
ColumnarValue::Scalar(ScalarValue::Date32(days)) => {
if let Some(days) = days {
Ok(ColumnarValue::Scalar(ScalarValue::Date32(Some(
spark_last_day(*days)?,
))))
} else {
Ok(ColumnarValue::Scalar(ScalarValue::Date32(None)))
}
}
ColumnarValue::Array(array) => {
let result = match array.data_type() {
DataType::Date32 => {
let result: Date32Array = array
.as_primitive::<Date32Type>()
.try_unary(spark_last_day)?
.with_data_type(DataType::Date32);
Ok(Arc::new(result) as ArrayRef)
}
other => {
internal_err!("Unsupported data type {other:?} for Spark function `last_day`")
}
}?;
Ok(ColumnarValue::Array(result))
}
other => {
internal_err!("Unsupported arg {other:?} for Spark function `last_day")
}
}
}
}

fn spark_last_day(days: i32) -> Result<i32> {
let date = Date32Type::to_naive_date(days);

let (year, month) = (date.year(), date.month());
let (next_year, next_month) = if month == 12 {
(year + 1, 1)
} else {
(year, month + 1)
};

let first_day_next_month = NaiveDate::from_ymd_opt(next_year, next_month, 1)
.ok_or_else(|| {
exec_datafusion_err!(
"Spark `last_day`: Unable to parse date from {next_year}, {next_month}, 1"
)
})?;

Ok(Date32Type::from_naive_date(
first_day_next_month - Duration::days(1),
))
}
17 changes: 15 additions & 2 deletions datafusion/spark/src/function/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,24 @@
// specific language governing permissions and limitations
// under the License.

pub mod last_day;

use datafusion_expr::ScalarUDF;
use datafusion_functions::make_udf_function;
use std::sync::Arc;

pub mod expr_fn {}
make_udf_function!(last_day::SparkLastDay, last_day);

pub mod expr_fn {
use datafusion_functions::export_functions;

export_functions!((
last_day,
"Returns the last day of the month which the date belongs to.",
arg1
));
}

pub fn functions() -> Vec<Arc<ScalarUDF>> {
vec![]
vec![last_day()]
}
100 changes: 96 additions & 4 deletions datafusion/sqllogictest/test_files/spark/datetime/last_day.slt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,99 @@
# For more information, please see:
# https://github.com/apache/datafusion/issues/15914

## Original Query: SELECT last_day('2009-01-12');
## PySpark 3.5.5 Result: {'last_day(2009-01-12)': datetime.date(2009, 1, 31), 'typeof(last_day(2009-01-12))': 'date', 'typeof(2009-01-12)': 'string'}
#query
#SELECT last_day('2009-01-12'::string);
query D
SELECT last_day('2009-01-12'::DATE);
----
2009-01-31


query D
SELECT last_day('2015-02-28'::DATE);
----
2015-02-28

query D
SELECT last_day('2015-03-27'::DATE);
----
2015-03-31

query D
SELECT last_day('2015-04-26'::DATE);
----
2015-04-30

query D
SELECT last_day('2015-05-25'::DATE);
----
2015-05-31

query D
SELECT last_day('2015-06-24'::DATE);
----
2015-06-30

query D
SELECT last_day('2015-07-23'::DATE);
----
2015-07-31

query D
SELECT last_day('2015-08-01'::DATE);
----
2015-08-31

query D
SELECT last_day('2015-09-02'::DATE);
----
2015-09-30

query D
SELECT last_day('2015-10-03'::DATE);
----
2015-10-31

query D
SELECT last_day('2015-11-04'::DATE);
----
2015-11-30

query D
SELECT last_day('2015-12-05'::DATE);
----
2015-12-31


query D
SELECT last_day('2016-01-06'::DATE);
----
2016-01-31

query D
SELECT last_day('2016-02-07'::DATE);
----
2016-02-29


query D
SELECT last_day(null::DATE);
----
NULL


statement error Failed to coerce arguments to satisfy a call to 'last_day' function
select last_day('foo');


statement error Failed to coerce arguments to satisfy a call to 'last_day' function
select last_day(123);


statement error 'last_day' does not support zero arguments
select last_day();

statement error Failed to coerce arguments to satisfy a call to 'last_day' function
select last_day(last_day('2016-02-07'::string, 'foo'));

statement error Failed to coerce arguments to satisfy a call to 'last_day' function
select last_day(last_day('2016-02-31'::string));