-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Support IGNORE NULLS for FIRST/LAST window function #9470
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
acf5d9d
Support IGNORE NULLS for FIRST/LAST window function
656aec4
fix error
720a0a7
fix style error
60ff3fa
fix clippy error
ced9583
add tests for all NULL values
30ef3d2
address comments
cc9f3b4
fix format
7a15263
address comments
49abb8a
fix format
73a9924
Fix commented test case
mustafasrepo 7c7aa8b
resolve conflicts
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,7 @@ pub struct NthValue { | |
/// Output data type | ||
data_type: DataType, | ||
kind: NthValueKind, | ||
ignore_nulls: bool, | ||
} | ||
|
||
impl NthValue { | ||
|
@@ -50,12 +51,14 @@ impl NthValue { | |
name: impl Into<String>, | ||
expr: Arc<dyn PhysicalExpr>, | ||
data_type: DataType, | ||
ignore_nulls: bool, | ||
) -> Self { | ||
Self { | ||
name: name.into(), | ||
expr, | ||
data_type, | ||
kind: NthValueKind::First, | ||
ignore_nulls, | ||
} | ||
} | ||
|
||
|
@@ -64,12 +67,14 @@ impl NthValue { | |
name: impl Into<String>, | ||
expr: Arc<dyn PhysicalExpr>, | ||
data_type: DataType, | ||
ignore_nulls: bool, | ||
) -> Self { | ||
Self { | ||
name: name.into(), | ||
expr, | ||
data_type, | ||
kind: NthValueKind::Last, | ||
ignore_nulls, | ||
} | ||
} | ||
|
||
|
@@ -79,14 +84,19 @@ impl NthValue { | |
expr: Arc<dyn PhysicalExpr>, | ||
data_type: DataType, | ||
n: u32, | ||
ignore_nulls: bool, | ||
) -> Result<Self> { | ||
if ignore_nulls { | ||
return exec_err!("NTH_VALUE ignore_nulls is not supported yet"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will have a separate PR for this soon. |
||
} | ||
match n { | ||
0 => exec_err!("NTH_VALUE expects n to be non-zero"), | ||
_ => Ok(Self { | ||
name: name.into(), | ||
expr, | ||
data_type, | ||
kind: NthValueKind::Nth(n as i64), | ||
ignore_nulls, | ||
}), | ||
} | ||
} | ||
|
@@ -122,7 +132,10 @@ impl BuiltInWindowFunctionExpr for NthValue { | |
finalized_result: None, | ||
kind: self.kind, | ||
}; | ||
Ok(Box::new(NthValueEvaluator { state })) | ||
Ok(Box::new(NthValueEvaluator { | ||
state, | ||
ignore_nulls: self.ignore_nulls, | ||
})) | ||
} | ||
|
||
fn reverse_expr(&self) -> Option<Arc<dyn BuiltInWindowFunctionExpr>> { | ||
|
@@ -136,6 +149,7 @@ impl BuiltInWindowFunctionExpr for NthValue { | |
expr: self.expr.clone(), | ||
data_type: self.data_type.clone(), | ||
kind: reversed_kind, | ||
ignore_nulls: self.ignore_nulls, | ||
})) | ||
} | ||
} | ||
|
@@ -144,6 +158,7 @@ impl BuiltInWindowFunctionExpr for NthValue { | |
#[derive(Debug)] | ||
pub(crate) struct NthValueEvaluator { | ||
state: NthValueState, | ||
ignore_nulls: bool, | ||
} | ||
|
||
impl PartitionEvaluator for NthValueEvaluator { | ||
|
@@ -184,7 +199,8 @@ impl PartitionEvaluator for NthValueEvaluator { | |
} | ||
} | ||
}; | ||
if is_prunable { | ||
// Do not memoize results when nulls are ignored. | ||
if is_prunable && !self.ignore_nulls { | ||
if self.state.finalized_result.is_none() && !is_reverse_direction { | ||
let result = ScalarValue::try_from_array(out, size - 1)?; | ||
self.state.finalized_result = Some(result); | ||
|
@@ -210,9 +226,39 @@ impl PartitionEvaluator for NthValueEvaluator { | |
// We produce None if the window is empty. | ||
return ScalarValue::try_from(arr.data_type()); | ||
} | ||
|
||
// Extract valid indices if ignoring nulls. | ||
let (slice, valid_indices) = if self.ignore_nulls { | ||
let slice = arr.slice(range.start, n_range); | ||
let valid_indices = | ||
slice.nulls().unwrap().valid_indices().collect::<Vec<_>>(); | ||
if valid_indices.is_empty() { | ||
return ScalarValue::try_from(arr.data_type()); | ||
} | ||
(Some(slice), Some(valid_indices)) | ||
} else { | ||
(None, None) | ||
}; | ||
match self.state.kind { | ||
NthValueKind::First => ScalarValue::try_from_array(arr, range.start), | ||
NthValueKind::Last => ScalarValue::try_from_array(arr, range.end - 1), | ||
NthValueKind::First => { | ||
if let Some(slice) = &slice { | ||
let valid_indices = valid_indices.unwrap(); | ||
ScalarValue::try_from_array(slice, valid_indices[0]) | ||
} else { | ||
ScalarValue::try_from_array(arr, range.start) | ||
} | ||
} | ||
NthValueKind::Last => { | ||
if let Some(slice) = &slice { | ||
let valid_indices = valid_indices.unwrap(); | ||
ScalarValue::try_from_array( | ||
slice, | ||
valid_indices[valid_indices.len() - 1], | ||
) | ||
} else { | ||
ScalarValue::try_from_array(arr, range.end - 1) | ||
} | ||
} | ||
NthValueKind::Nth(n) => { | ||
match n.cmp(&0) { | ||
Ordering::Greater => { | ||
|
@@ -295,6 +341,7 @@ mod tests { | |
"first_value".to_owned(), | ||
Arc::new(Column::new("arr", 0)), | ||
DataType::Int32, | ||
false, | ||
); | ||
test_i32_result(first_value, Int32Array::from(vec![1; 8]))?; | ||
Ok(()) | ||
|
@@ -306,6 +353,7 @@ mod tests { | |
"last_value".to_owned(), | ||
Arc::new(Column::new("arr", 0)), | ||
DataType::Int32, | ||
false, | ||
); | ||
test_i32_result( | ||
last_value, | ||
|
@@ -330,6 +378,7 @@ mod tests { | |
Arc::new(Column::new("arr", 0)), | ||
DataType::Int32, | ||
1, | ||
false, | ||
)?; | ||
test_i32_result(nth_value, Int32Array::from(vec![1; 8]))?; | ||
Ok(()) | ||
|
@@ -342,6 +391,7 @@ mod tests { | |
Arc::new(Column::new("arr", 0)), | ||
DataType::Int32, | ||
2, | ||
false, | ||
)?; | ||
test_i32_result( | ||
nth_value, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is also for
NTH_VALUE
right? Maybe add it to the title?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, nvm, saw your comment below.