Skip to content

Commit cb3fa1c

Browse files
authored
fix: derive custom nullability for the spark next_day (#19253)
## 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 #19156 - 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 `next_day` uses `return_field_from_args` <!-- 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? - Added new unit tests, previous all tests pass <!-- 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 d68b629 commit cb3fa1c

File tree

1 file changed

+48
-4
lines changed

1 file changed

+48
-4
lines changed

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

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

2121
use arrow::array::{ArrayRef, AsArray, Date32Array, StringArrayType, new_null_array};
22-
use arrow::datatypes::{DataType, Date32Type};
22+
use arrow::datatypes::{DataType, Date32Type, Field, FieldRef};
2323
use chrono::{Datelike, Duration, Weekday};
24-
use datafusion_common::{Result, ScalarValue, exec_err};
24+
use datafusion_common::{Result, ScalarValue, exec_err, internal_err};
2525
use datafusion_expr::{
26-
ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility,
26+
ColumnarValue, ReturnFieldArgs, ScalarFunctionArgs, ScalarUDFImpl, Signature,
27+
Volatility,
2728
};
2829

2930
/// <https://spark.apache.org/docs/latest/api/sql/index.html#next_day>
@@ -63,7 +64,13 @@ impl ScalarUDFImpl for SparkNextDay {
6364
}
6465

6566
fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
66-
Ok(DataType::Date32)
67+
internal_err!("return_field_from_args should be used instead")
68+
}
69+
70+
fn return_field_from_args(&self, _args: ReturnFieldArgs) -> Result<FieldRef> {
71+
// Spark marks next_day as always nullable because invalid day_of_week values
72+
// can yield NULL even when inputs are non-null.
73+
Ok(Arc::new(Field::new(self.name(), DataType::Date32, true)))
6774
}
6875

6976
fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
@@ -245,3 +252,40 @@ fn spark_next_day(days: i32, day_of_week: &str) -> Option<i32> {
245252
None
246253
}
247254
}
255+
256+
#[cfg(test)]
257+
mod tests {
258+
use super::*;
259+
use datafusion_expr::ReturnFieldArgs;
260+
261+
#[test]
262+
fn return_type_is_not_used() {
263+
let func = SparkNextDay::new();
264+
let err = func
265+
.return_type(&[DataType::Date32, DataType::Utf8])
266+
.unwrap_err();
267+
assert!(
268+
err.to_string()
269+
.contains("return_field_from_args should be used instead")
270+
);
271+
}
272+
273+
#[test]
274+
fn next_day_is_always_nullable() {
275+
let func = SparkNextDay::new();
276+
let date_field: FieldRef =
277+
Arc::new(Field::new("start_date", DataType::Date32, false));
278+
let day_field: FieldRef =
279+
Arc::new(Field::new("day_of_week", DataType::Utf8, false));
280+
281+
let field = func
282+
.return_field_from_args(ReturnFieldArgs {
283+
arg_fields: &[Arc::clone(&date_field), Arc::clone(&day_field)],
284+
scalar_arguments: &[None, None],
285+
})
286+
.unwrap();
287+
288+
assert_eq!(field.data_type(), &DataType::Date32);
289+
assert!(field.is_nullable());
290+
}
291+
}

0 commit comments

Comments
 (0)