-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert
rank
/ dense_rank
and percent_rank
builtin functions to…
… UDWF (#12718) * wip: converting rank builtin function to UDWF * commented BuiltInWindowFunction in datafusion.proto and fixed issue related to Datafusion window function * implemented rank.rs, percent_rank.rs and dense_rank.rs in datafusion functions-window * removed a test from built in window function test for percent_rank and updated pbson fields * removed unnecessary code * added window_functions field to the MockSessionState * updated rank, percent_rank and dense_rank udwf to use macros * wip: fix rank functionality in sql integration * fixed rank udwf not found issue in sql_integration.rs * evaluating rank, percent_rank and dense_rank udwf with evaluate_with_rank function * fixed rank projection test * wip: fixing the percent_rank() documentation * fixed the docs error issue * fixed data type of the percent_rank udwf * updated prost.rs file * updated test and documentation * Fix logical conflicts * tweak module documentation --------- Co-authored-by: jatin <[email protected]> Co-authored-by: Andrew Lamb <[email protected]>
- Loading branch information
1 parent
101e455
commit 939ef9e
Showing
27 changed files
with
687 additions
and
469 deletions.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
// 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. | ||
|
||
//! `dense_rank` window function implementation | ||
use std::any::Any; | ||
use std::fmt::Debug; | ||
use std::iter; | ||
use std::ops::Range; | ||
use std::sync::Arc; | ||
|
||
use crate::define_udwf_and_expr; | ||
use crate::rank::RankState; | ||
use datafusion_common::arrow::array::ArrayRef; | ||
use datafusion_common::arrow::array::UInt64Array; | ||
use datafusion_common::arrow::compute::SortOptions; | ||
use datafusion_common::arrow::datatypes::DataType; | ||
use datafusion_common::arrow::datatypes::Field; | ||
use datafusion_common::utils::get_row_at_idx; | ||
use datafusion_common::{Result, ScalarValue}; | ||
use datafusion_expr::{PartitionEvaluator, Signature, Volatility, WindowUDFImpl}; | ||
use datafusion_functions_window_common::field; | ||
use datafusion_functions_window_common::partition::PartitionEvaluatorArgs; | ||
use field::WindowUDFFieldArgs; | ||
|
||
define_udwf_and_expr!( | ||
DenseRank, | ||
dense_rank, | ||
"Returns rank of the current row without gaps. This function counts peer groups" | ||
); | ||
|
||
/// dense_rank expression | ||
#[derive(Debug)] | ||
pub struct DenseRank { | ||
signature: Signature, | ||
} | ||
|
||
impl DenseRank { | ||
/// Create a new `dense_rank` function | ||
pub fn new() -> Self { | ||
Self { | ||
signature: Signature::any(0, Volatility::Immutable), | ||
} | ||
} | ||
} | ||
|
||
impl Default for DenseRank { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl WindowUDFImpl for DenseRank { | ||
fn as_any(&self) -> &dyn Any { | ||
self | ||
} | ||
|
||
fn name(&self) -> &str { | ||
"dense_rank" | ||
} | ||
|
||
fn signature(&self) -> &Signature { | ||
&self.signature | ||
} | ||
|
||
fn partition_evaluator( | ||
&self, | ||
_partition_evaluator_args: PartitionEvaluatorArgs, | ||
) -> Result<Box<dyn PartitionEvaluator>> { | ||
Ok(Box::<DenseRankEvaluator>::default()) | ||
} | ||
|
||
fn field(&self, field_args: WindowUDFFieldArgs) -> Result<Field> { | ||
Ok(Field::new(field_args.name(), DataType::UInt64, false)) | ||
} | ||
|
||
fn sort_options(&self) -> Option<SortOptions> { | ||
Some(SortOptions { | ||
descending: false, | ||
nulls_first: false, | ||
}) | ||
} | ||
} | ||
|
||
/// State for the `dense_rank` built-in window function. | ||
#[derive(Debug, Default)] | ||
struct DenseRankEvaluator { | ||
state: RankState, | ||
} | ||
|
||
impl PartitionEvaluator for DenseRankEvaluator { | ||
fn is_causal(&self) -> bool { | ||
// The dense_rank function doesn't need "future" values to emit results: | ||
true | ||
} | ||
|
||
fn evaluate( | ||
&mut self, | ||
values: &[ArrayRef], | ||
range: &Range<usize>, | ||
) -> Result<ScalarValue> { | ||
let row_idx = range.start; | ||
// There is no argument, values are order by column values (where rank is calculated) | ||
let range_columns = values; | ||
let last_rank_data = get_row_at_idx(range_columns, row_idx)?; | ||
let new_rank_encountered = | ||
if let Some(state_last_rank_data) = &self.state.last_rank_data { | ||
// if rank data changes, new rank is encountered | ||
state_last_rank_data != &last_rank_data | ||
} else { | ||
// First rank seen | ||
true | ||
}; | ||
|
||
if new_rank_encountered { | ||
self.state.last_rank_data = Some(last_rank_data); | ||
self.state.last_rank_boundary += self.state.current_group_count; | ||
self.state.current_group_count = 1; | ||
self.state.n_rank += 1; | ||
} else { | ||
// data is still in the same rank | ||
self.state.current_group_count += 1; | ||
} | ||
|
||
Ok(ScalarValue::UInt64(Some(self.state.n_rank as u64))) | ||
} | ||
|
||
fn evaluate_all_with_rank( | ||
&self, | ||
_num_rows: usize, | ||
ranks_in_partition: &[Range<usize>], | ||
) -> Result<ArrayRef> { | ||
let result = Arc::new(UInt64Array::from_iter_values( | ||
ranks_in_partition | ||
.iter() | ||
.zip(1u64..) | ||
.flat_map(|(range, rank)| { | ||
let len = range.end - range.start; | ||
iter::repeat(rank).take(len) | ||
}), | ||
)); | ||
|
||
Ok(result) | ||
} | ||
|
||
fn supports_bounded_execution(&self) -> bool { | ||
true | ||
} | ||
|
||
fn include_rank(&self) -> bool { | ||
true | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use datafusion_common::cast::as_uint64_array; | ||
|
||
fn test_with_rank(expr: &DenseRank, expected: Vec<u64>) -> Result<()> { | ||
test_i32_result(expr, vec![0..2, 2..3, 3..6, 6..7, 7..8], expected) | ||
} | ||
|
||
#[allow(clippy::single_range_in_vec_init)] | ||
fn test_without_rank(expr: &DenseRank, expected: Vec<u64>) -> Result<()> { | ||
test_i32_result(expr, vec![0..8], expected) | ||
} | ||
|
||
fn test_i32_result( | ||
expr: &DenseRank, | ||
ranks: Vec<Range<usize>>, | ||
expected: Vec<u64>, | ||
) -> Result<()> { | ||
let args = PartitionEvaluatorArgs::default(); | ||
let result = expr | ||
.partition_evaluator(args)? | ||
.evaluate_all_with_rank(8, &ranks)?; | ||
let result = as_uint64_array(&result)?; | ||
let result = result.values(); | ||
assert_eq!(expected, *result); | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn test_dense_rank() -> Result<()> { | ||
let r = DenseRank::default(); | ||
test_without_rank(&r, vec![1; 8])?; | ||
test_with_rank(&r, vec![1, 1, 2, 3, 3, 3, 4, 5])?; | ||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.