Skip to content

Commit adab085

Browse files
committed
test: add issue reproducers and return field nullability test for date_part
1 parent a6e86f0 commit adab085

3 files changed

Lines changed: 75 additions & 20 deletions

File tree

‎datafusion/functions/src/datetime/date_part.rs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,8 @@ where
575575
let secs = as_int32_array(secs.as_ref())?;
576576
let fraction = date_part(array, part)?;
577577
let fraction = as_int32_array(fraction.as_ref())?;
578+
// At most 60 seconds, which fits in Int32 for milliseconds and
579+
// microseconds, so the narrowing `as_` never truncates
578580
let r: PrimitiveArray<O> = binary(secs, fraction, |secs, fraction| {
579581
(secs as i64 * PER_SECOND + fraction as i64).as_()
580582
})?;
@@ -864,6 +866,38 @@ mod tests {
864866
}
865867
}
866868

869+
/// `epoch` is NULL only for a NULL input, but the integer parts can be NULL
870+
/// for a non-null input, so they are always nullable.
871+
#[test]
872+
fn return_field_nullability() {
873+
for input_nullable in [false, true] {
874+
let arg_fields: Vec<FieldRef> = vec![
875+
Field::new("a", DataType::Utf8, false).into(),
876+
Field::new(
877+
"b",
878+
DataType::Timestamp(TimeUnit::Second, None),
879+
input_nullable,
880+
)
881+
.into(),
882+
];
883+
for (part, nullable) in [
884+
("epoch", input_nullable),
885+
("second", true),
886+
("millisecond", true),
887+
("nanosecond", true),
888+
] {
889+
let part = ScalarValue::from(part);
890+
let field = DatePartFunc::new()
891+
.return_field_from_args(ReturnFieldArgs {
892+
arg_fields: &arg_fields,
893+
scalar_arguments: &[Some(&part), None],
894+
})
895+
.unwrap();
896+
assert_eq!(field.is_nullable(), nullable, "{part}, {input_nullable}");
897+
}
898+
}
899+
}
900+
867901
#[test]
868902
fn timezone_parts_return_int32() {
869903
let arg_fields: Vec<FieldRef> = vec![

‎datafusion/spark/src/function/datetime/date_part.rs‎

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -132,34 +132,39 @@ impl ScalarUDFImpl for SparkDatePart {
132132
mod tests {
133133
use super::*;
134134
use arrow::datatypes::TimeUnit;
135+
use datafusion_functions::datetime::date_part;
135136

136-
/// The result field matches the field of the `date_part` it simplifies to.
137-
/// That `date_part` can return NULL for a non-null input, so both are
138-
/// nullable.
139137
#[test]
140-
fn test_date_part_nullability_matches_simplified() -> Result<()> {
141-
let arg_fields: Vec<FieldRef> = vec![
138+
fn test_date_part_return_field_nullability_matches_date_part() {
139+
let func = SparkDatePart::new();
140+
let part = ScalarValue::from("second");
141+
142+
let non_nullable_args: Vec<FieldRef> = vec![
142143
Arc::new(Field::new("part", DataType::Utf8, false)),
143144
Arc::new(Field::new(
144145
"ts",
145146
DataType::Timestamp(TimeUnit::Second, None),
146147
false,
147148
)),
148149
];
149-
let part = ScalarValue::from("second");
150-
let scalar_arguments = [Some(&part), None];
151-
let args = || ReturnFieldArgs {
152-
arg_fields: &arg_fields,
153-
scalar_arguments: &scalar_arguments,
154-
};
155-
156-
let spark = SparkDatePart::new().return_field_from_args(args())?;
157-
let simplified =
158-
datafusion_functions::datetime::date_part().return_field_from_args(args())?;
159-
assert!(spark.is_nullable());
160-
assert_eq!(spark.is_nullable(), simplified.is_nullable());
161-
assert_eq!(spark.data_type(), simplified.data_type());
162150

163-
Ok(())
151+
let out = func
152+
.return_field_from_args(ReturnFieldArgs {
153+
arg_fields: &non_nullable_args,
154+
scalar_arguments: &[Some(&part), None],
155+
})
156+
.expect("non-nullable arg should succeed");
157+
assert_eq!(out.data_type(), &DataType::Int32);
158+
assert!(out.is_nullable());
159+
160+
// Matches the `date_part` this simplifies to
161+
let simplified = date_part()
162+
.return_field_from_args(ReturnFieldArgs {
163+
arg_fields: &non_nullable_args,
164+
scalar_arguments: &[Some(&part), None],
165+
})
166+
.expect("non-nullable arg should succeed");
167+
assert_eq!(out.data_type(), simplified.data_type());
168+
assert_eq!(out.is_nullable(), simplified.is_nullable());
164169
}
165170
}

‎datafusion/sqllogictest/test_files/datetime/date_part.slt‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1262,6 +1262,21 @@ SELECT extract(millisecond from arrow_cast('62.5 seconds', 'Interval(DayTime)'))
12621262
----
12631263
2500 2500000
12641264

1265+
# Reproducer from https://github.com/apache/datafusion/issues/26678
1266+
query III
1267+
SELECT extract(nanosecond from arrow_cast(1500000000, 'Duration(Nanosecond)')),
1268+
extract(nanosecond from interval '1.5 seconds'),
1269+
extract(microsecond from interval '1.5 seconds')
1270+
----
1271+
1500000000 1500000000 1500000
1272+
1273+
# Reproducer from https://github.com/apache/datafusion/issues/14738#issuecomment-6372885638
1274+
query II
1275+
SELECT extract(millisecond from interval '62.5 seconds'),
1276+
extract(millisecond from arrow_cast(2500000000, 'Duration(Nanosecond)'))
1277+
----
1278+
2500 2500
1279+
12651280
# Reproducer from https://github.com/apache/datafusion/issues/14738
12661281
query III
12671282
SELECT date_part('microseconds', interval '1 hour'),
@@ -1500,7 +1515,8 @@ SELECT date_part('microsecond', timestamp '1970-01-01T00:40:00' - timestamp '197
15001515
----
15011516
NULL
15021517

1503-
# Non-null inputs can give NULL results, so the result is nullable
1518+
# Non-null inputs can give NULL results, so the result is nullable.
1519+
# Reproducers from https://github.com/apache/datafusion/issues/26685
15041520
query I
15051521
SELECT extract(second from ts) FROM (VALUES (arrow_cast(9223372036854775807, 'Timestamp(Second, None)'))) AS t(ts)
15061522
----

0 commit comments

Comments
 (0)