-
Notifications
You must be signed in to change notification settings - Fork 1.9k
add integration tests for rank, dense_rank, fix last_value evaluation with rank #638
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -22,9 +22,11 @@ use crate::physical_plan::window_functions::PartitionEvaluator; | |
| use crate::physical_plan::{window_functions::BuiltInWindowFunctionExpr, PhysicalExpr}; | ||
| use crate::scalar::ScalarValue; | ||
| use arrow::array::{new_null_array, ArrayRef}; | ||
| use arrow::compute::kernels::window::shift; | ||
| use arrow::datatypes::{DataType, Field}; | ||
| use arrow::record_batch::RecordBatch; | ||
| use std::any::Any; | ||
| use std::iter; | ||
| use std::ops::Range; | ||
| use std::sync::Arc; | ||
|
|
||
|
|
@@ -138,21 +140,56 @@ pub(crate) struct NthValueEvaluator { | |
| } | ||
|
|
||
| impl PartitionEvaluator for NthValueEvaluator { | ||
| fn evaluate_partition(&self, partition: Range<usize>) -> Result<ArrayRef> { | ||
| let value = &self.values[0]; | ||
| fn include_rank(&self) -> bool { | ||
| true | ||
| } | ||
|
|
||
| fn evaluate_partition(&self, _partition: Range<usize>) -> Result<ArrayRef> { | ||
| unreachable!("first, last, and nth_value evaluation must be called with evaluate_partition_with_rank") | ||
| } | ||
|
|
||
| fn evaluate_partition_with_rank( | ||
| &self, | ||
| partition: Range<usize>, | ||
| ranks_in_partition: &[Range<usize>], | ||
| ) -> Result<ArrayRef> { | ||
| let arr = &self.values[0]; | ||
| let num_rows = partition.end - partition.start; | ||
| let value = value.slice(partition.start, num_rows); | ||
| let index: usize = match self.kind { | ||
| NthValueKind::First => 0, | ||
| NthValueKind::Last => (num_rows as usize) - 1, | ||
| NthValueKind::Nth(n) => (n as usize) - 1, | ||
| }; | ||
| Ok(if index >= num_rows { | ||
| new_null_array(value.data_type(), num_rows) | ||
| } else { | ||
| let value = ScalarValue::try_from_array(&value, index)?; | ||
| value.to_array_of_size(num_rows) | ||
| }) | ||
| match self.kind { | ||
| NthValueKind::First => { | ||
| let value = ScalarValue::try_from_array(arr, partition.start)?; | ||
| Ok(value.to_array_of_size(num_rows)) | ||
| } | ||
| NthValueKind::Last => { | ||
| // because the default window frame is between unbounded preceding and current | ||
| // row with peer evaluation, hence the last rows expands until the end of the peers | ||
| let values = ranks_in_partition | ||
| .iter() | ||
| .map(|range| { | ||
| let len = range.end - range.start; | ||
| let value = ScalarValue::try_from_array(arr, range.end - 1)?; | ||
| Ok(iter::repeat(value).take(len)) | ||
| }) | ||
| .collect::<Result<Vec<_>>>()? | ||
| .into_iter() | ||
| .flatten(); | ||
| ScalarValue::iter_to_array(values) | ||
| } | ||
| NthValueKind::Nth(n) => { | ||
| let index = (n as usize) - 1; | ||
| if index >= num_rows { | ||
| Ok(new_null_array(arr.data_type(), num_rows)) | ||
| } else { | ||
| let value = | ||
| ScalarValue::try_from_array(arr, partition.start + index)?; | ||
| let arr = value.to_array_of_size(num_rows); | ||
| // because the default window frame is between unbounded preceding and current | ||
| // row, hence the shift because for values with indices < index they should be | ||
| // null. This changes when window frames other than default is implemented | ||
| shift(arr.as_ref(), index as i64).map_err(DataFusionError::ArrowError) | ||
|
Contributor
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. 👍 |
||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -164,16 +201,17 @@ mod tests { | |
| use arrow::record_batch::RecordBatch; | ||
| use arrow::{array::*, datatypes::*}; | ||
|
|
||
| fn test_i32_result(expr: NthValue, expected: Vec<i32>) -> Result<()> { | ||
| fn test_i32_result(expr: NthValue, expected: Int32Array) -> Result<()> { | ||
| let arr: ArrayRef = Arc::new(Int32Array::from(vec![1, -2, 3, -4, 5, -6, 7, 8])); | ||
| let values = vec![arr]; | ||
| let schema = Schema::new(vec![Field::new("arr", DataType::Int32, false)]); | ||
| let batch = RecordBatch::try_new(Arc::new(schema), values.clone())?; | ||
| let result = expr.create_evaluator(&batch)?.evaluate(vec![0..8])?; | ||
| let result = expr | ||
| .create_evaluator(&batch)? | ||
| .evaluate_with_rank(vec![0..8], vec![0..8])?; | ||
| assert_eq!(1, result.len()); | ||
| let result = result[0].as_any().downcast_ref::<Int32Array>().unwrap(); | ||
| let result = result.values(); | ||
| assert_eq!(expected, result); | ||
| assert_eq!(expected, *result); | ||
| Ok(()) | ||
| } | ||
|
|
||
|
|
@@ -184,7 +222,7 @@ mod tests { | |
| Arc::new(Column::new("arr", 0)), | ||
| DataType::Int32, | ||
| ); | ||
| test_i32_result(first_value, vec![1; 8])?; | ||
| test_i32_result(first_value, Int32Array::from_iter_values(vec![1; 8]))?; | ||
| Ok(()) | ||
| } | ||
|
|
||
|
|
@@ -195,7 +233,7 @@ mod tests { | |
| Arc::new(Column::new("arr", 0)), | ||
| DataType::Int32, | ||
| ); | ||
| test_i32_result(last_value, vec![8; 8])?; | ||
| test_i32_result(last_value, Int32Array::from_iter_values(vec![8; 8]))?; | ||
| Ok(()) | ||
| } | ||
|
|
||
|
|
@@ -207,7 +245,7 @@ mod tests { | |
| DataType::Int32, | ||
| 1, | ||
| )?; | ||
| test_i32_result(nth_value, vec![1; 8])?; | ||
| test_i32_result(nth_value, Int32Array::from_iter_values(vec![1; 8]))?; | ||
| Ok(()) | ||
| } | ||
|
|
||
|
|
@@ -219,7 +257,19 @@ mod tests { | |
| DataType::Int32, | ||
| 2, | ||
| )?; | ||
| test_i32_result(nth_value, vec![-2; 8])?; | ||
| test_i32_result( | ||
| nth_value, | ||
| Int32Array::from(vec![ | ||
| None, | ||
| Some(-2), | ||
| Some(-2), | ||
| Some(-2), | ||
| Some(-2), | ||
| Some(-2), | ||
| Some(-2), | ||
| Some(-2), | ||
| ]), | ||
| )?; | ||
| Ok(()) | ||
| } | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -903,7 +903,7 @@ async fn csv_query_window_with_partition_by() -> Result<()> { | |
| "-21481", | ||
| "-16974", | ||
| "-21481", | ||
| "-21481", | ||
| "NULL", | ||
| ], | ||
| vec![ | ||
| "141680161", | ||
|
|
@@ -952,15 +952,8 @@ async fn csv_query_window_with_order_by() -> Result<()> { | |
| let actual = execute(&mut ctx, sql).await; | ||
| let expected = vec![ | ||
|
Member
Author
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. |
||
| vec![ | ||
| "28774375", | ||
| "61035129", | ||
| "61035129", | ||
| "1", | ||
| "61035129", | ||
| "61035129", | ||
| "61035129", | ||
| "2025611582", | ||
| "-108973366", | ||
| "28774375", "61035129", "61035129", "1", "61035129", "61035129", "61035129", | ||
| "61035129", "NULL", | ||
| ], | ||
| vec![ | ||
| "63044568", | ||
|
|
@@ -970,7 +963,7 @@ async fn csv_query_window_with_order_by() -> Result<()> { | |
| "61035129", | ||
| "-108973366", | ||
| "61035129", | ||
| "2025611582", | ||
| "-108973366", | ||
| "-108973366", | ||
| ], | ||
| vec![ | ||
|
|
@@ -981,7 +974,7 @@ async fn csv_query_window_with_order_by() -> Result<()> { | |
| "623103518", | ||
| "-108973366", | ||
| "61035129", | ||
| "2025611582", | ||
| "623103518", | ||
| "-108973366", | ||
| ], | ||
| vec![ | ||
|
|
@@ -992,7 +985,7 @@ async fn csv_query_window_with_order_by() -> Result<()> { | |
| "623103518", | ||
| "-1927628110", | ||
| "61035129", | ||
| "2025611582", | ||
| "-1927628110", | ||
| "-108973366", | ||
| ], | ||
| vec![ | ||
|
|
@@ -1003,7 +996,7 @@ async fn csv_query_window_with_order_by() -> Result<()> { | |
| "623103518", | ||
| "-1927628110", | ||
| "61035129", | ||
| "2025611582", | ||
| "-1899175111", | ||
| "-108973366", | ||
| ], | ||
| ]; | ||
|
|
||
27 changes: 27 additions & 0 deletions
27
integration-tests/sqls/simple_window_built_in_functions.sql
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| -- Licensed to the Apache Software Foundation (ASF) under one | ||
| -- or more contributor license agreements. See the NOTICE file | ||
| -- distributed with this work for additional information | ||
| -- regarding copyright ownership. The ASF licenses this file | ||
| -- to you under the Apache License, Version 2.0 (the | ||
| -- "License"); you may not use this file except in compliance | ||
| -- with the License. You may obtain a copy of the License at | ||
|
|
||
| -- http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| -- Unless required by applicable law or agreed to in writing, software | ||
| -- distributed under the License is distributed on an "AS IS" BASIS, | ||
| -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| -- See the License for the specific language governing permissions and | ||
| -- limitations under the License. | ||
|
|
||
| SELECT | ||
| c9, | ||
| row_number() OVER (ORDER BY c9) row_num, | ||
| first_value(c9) OVER (ORDER BY c9) first_c9, | ||
| first_value(c9) OVER (ORDER BY c9 DESC) first_c9_desc, | ||
| last_value(c9) OVER (ORDER BY c9) last_c9, | ||
| last_value(c9) OVER (ORDER BY c9 DESC) last_c9_desc, | ||
| nth_value(c9, 2) OVER (ORDER BY c9) second_c9, | ||
| nth_value(c9, 2) OVER (ORDER BY c9 DESC) second_c9_desc | ||
| FROM test | ||
| ORDER BY c9; |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
22 changes: 22 additions & 0 deletions
22
integration-tests/sqls/simple_window_ranked_built_in_functions.sql
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| -- Licensed to the Apache Software Foundation (ASF) under one | ||
| -- or more contributor license agreements. See the NOTICE file | ||
| -- distributed with this work for additional information | ||
| -- regarding copyright ownership. The ASF licenses this file | ||
| -- to you under the Apache License, Version 2.0 (the | ||
| -- "License"); you may not use this file except in compliance | ||
| -- with the License. You may obtain a copy of the License at | ||
|
|
||
| -- http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| -- Unless required by applicable law or agreed to in writing, software | ||
| -- distributed under the License is distributed on an "AS IS" BASIS, | ||
| -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| -- See the License for the specific language governing permissions and | ||
| -- limitations under the License. | ||
|
|
||
| select | ||
| c9, | ||
| rank() OVER (PARTITION BY c2 ORDER BY c3) rank_by_c3, | ||
| dense_rank() OVER (PARTITION BY c2 ORDER BY c3) dense_rank_by_c3 | ||
| FROM test | ||
| ORDER BY c9; |
This file contains hidden or 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
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 very cool