Skip to content

Commit c4ca946

Browse files
authored
fix: derive custome nullable for the spark last_day (#19232)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Closes #19153. - Part of #19144 ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> ## What changes are included in this PR? - Spark `last_day` now reports schema using `return_field_from_args` - Added unit tests <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> ## Are these changes tested? - All previouse tests pass - Added new unit tests <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. -->
1 parent 2c6f6d3 commit c4ca946

File tree

1 file changed

+73
-3
lines changed

1 file changed

+73
-3
lines changed

datafusion/spark/src/function/datetime/last_day.rs

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ use std::any::Any;
1919
use std::sync::Arc;
2020

2121
use arrow::array::{ArrayRef, AsArray, Date32Array};
22-
use arrow::datatypes::{DataType, Date32Type};
22+
use arrow::datatypes::{DataType, Date32Type, Field, FieldRef};
2323
use chrono::{Datelike, Duration, NaiveDate};
2424
use datafusion_common::utils::take_function_args;
2525
use datafusion_common::{exec_datafusion_err, internal_err, Result, ScalarValue};
2626
use datafusion_expr::{
27-
ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility,
27+
ColumnarValue, ReturnFieldArgs, ScalarFunctionArgs, ScalarUDFImpl, Signature,
28+
Volatility,
2829
};
2930

3031
#[derive(Debug, PartialEq, Eq, Hash)]
@@ -60,7 +61,19 @@ impl ScalarUDFImpl for SparkLastDay {
6061
}
6162

6263
fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
63-
Ok(DataType::Date32)
64+
internal_err!("return_field_from_args should be used instead")
65+
}
66+
67+
fn return_field_from_args(&self, args: ReturnFieldArgs) -> Result<FieldRef> {
68+
let Some(field) = args.arg_fields.first() else {
69+
return internal_err!("Spark `last_day` expects exactly one argument");
70+
};
71+
72+
Ok(Arc::new(Field::new(
73+
self.name(),
74+
DataType::Date32,
75+
field.is_nullable(),
76+
)))
6477
}
6578

6679
fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
@@ -119,3 +132,60 @@ fn spark_last_day(days: i32) -> Result<i32> {
119132
first_day_next_month - Duration::days(1),
120133
))
121134
}
135+
136+
#[cfg(test)]
137+
mod tests {
138+
use super::*;
139+
use crate::function::utils::test::test_scalar_function;
140+
use arrow::array::{Array, Date32Array};
141+
use arrow::datatypes::Field;
142+
use datafusion_common::ScalarValue;
143+
use datafusion_expr::{ColumnarValue, ReturnFieldArgs};
144+
145+
#[test]
146+
fn test_last_day_nullability_matches_input() {
147+
let func = SparkLastDay::new();
148+
149+
let non_nullable_arg = Arc::new(Field::new("arg", DataType::Date32, false));
150+
let nullable_arg = Arc::new(Field::new("arg", DataType::Date32, true));
151+
152+
let non_nullable_out = func
153+
.return_field_from_args(ReturnFieldArgs {
154+
arg_fields: &[Arc::clone(&non_nullable_arg)],
155+
scalar_arguments: &[None],
156+
})
157+
.expect("non-nullable arg should succeed");
158+
assert_eq!(non_nullable_out.data_type(), &DataType::Date32);
159+
assert!(!non_nullable_out.is_nullable());
160+
161+
let nullable_out = func
162+
.return_field_from_args(ReturnFieldArgs {
163+
arg_fields: &[Arc::clone(&nullable_arg)],
164+
scalar_arguments: &[None],
165+
})
166+
.expect("nullable arg should succeed");
167+
assert_eq!(nullable_out.data_type(), &DataType::Date32);
168+
assert!(nullable_out.is_nullable());
169+
}
170+
171+
#[test]
172+
fn test_last_day_scalar_evaluation() {
173+
test_scalar_function!(
174+
SparkLastDay::new(),
175+
vec![ColumnarValue::Scalar(ScalarValue::Date32(Some(0)))],
176+
Ok(Some(30)),
177+
i32,
178+
DataType::Date32,
179+
Date32Array
180+
);
181+
182+
test_scalar_function!(
183+
SparkLastDay::new(),
184+
vec![ColumnarValue::Scalar(ScalarValue::Date32(None))],
185+
Ok(None),
186+
i32,
187+
DataType::Date32,
188+
Date32Array
189+
);
190+
}
191+
}

0 commit comments

Comments
 (0)