Skip to content

Apache DataFusion SQL Query Engine

clarify logic in nth_value window function (#14104)

zjregee888df6a97a0e
diff --git a/datafusion/functions-window/src/nth_value.rs b/datafusion/functions-window/src/nth_value.rs
index e5d866940c..16caa5293e 100644
--- a/datafusion/functions-window/src/nth_value.rs
+++ b/datafusion/functions-window/src/nth_value.rs
@@ -345,24 +345,26 @@ impl PartitionEvaluator for NthValueEvaluator {
return ScalarValue::try_from(arr.data_type());
}
- // Extract valid indices if ignoring nulls.
+ // If null values exist and need to be ignored, extract the valid indices.
let valid_indices = if self.ignore_nulls {
- // Calculate valid indices, inside the window frame boundaries
+ // Calculate valid indices, inside the window frame boundaries.
let slice = arr.slice(range.start, n_range);
- let valid_indices = slice
- .nulls()
- .map(|nulls| {
- nulls
+ match slice.nulls() {
+ Some(nulls) => {
+ let valid_indices = nulls
.valid_indices()
- // Add offset `range.start` to valid indices, to point correct index in the original arr.
- .map(|idx| idx + range.start)
- .collect::<Vec<_>>()
- })
- .unwrap_or_default();
- if valid_indices.is_empty() {
- None
- } else {
- Some(valid_indices)
+ .map(|idx| {
+ // Add offset `range.start` to valid indices, to point correct index in the original arr.
+ idx + range.start
+ })
+ .collect::<Vec<_>>();
+ if valid_indices.is_empty() {
+ // If all values are null, return directly.
+ return ScalarValue::try_from(arr.data_type());
+ }
+ Some(valid_indices)
+ }
+ None => None,
}
} else {
None