-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Convert rank
/ dense_rank
and percent_rank
builtin functions to UDWF
#12718
Merged
alamb
merged 21 commits into
apache:main
from
jatin510:feature/12648-udwf-rank-percentrank-denserank
Oct 10, 2024
+687
−469
Merged
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
05fd8bc
wip: converting rank builtin function to UDWF
65d213c
commented BuiltInWindowFunction in datafusion.proto and fixed issue r…
d7f38fa
implemented rank.rs, percent_rank.rs and dense_rank.rs in datafusion …
00e9044
removed a test from built in window function test for percent_rank an…
ceba9d4
Merge branch 'main' into feature/12648-udwf-rank-percentrank-denserank
a3a4589
removed unnecessary code
5a401ff
added window_functions field to the MockSessionState
04085bb
merge main branch
jatin510 07eb07f
updated rank, percent_rank and dense_rank udwf to use macros
jatin510 937c030
wip: fix rank functionality in sql integration
jatin510 f06bdc7
fixed rank udwf not found issue in sql_integration.rs
jatin510 736fd6e
evaluating rank, percent_rank and dense_rank udwf with evaluate_with_…
jatin510 ceb594b
fixed rank projection test
jatin510 98dfbbf
wip: fixing the percent_rank() documentation
jatin510 f57c5d2
fixed the docs error issue
jatin510 f1c7547
fixed data type of the percent_rank udwf
jatin510 b9613e1
updated prost.rs file
jatin510 bd852e0
updated test and documentation
jatin510 7ec1604
Merge remote-tracking branch 'apache/main' into feature/12648-udwf-ra…
alamb 0c5d5be
Fix logical conflicts
alamb 38fc8ed
tweak module documentation
alamb 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
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,183 @@ | ||
// 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. | ||
|
||
//! Defines physical expression for `dense_rank` that can evaluated at runtime during query execution | ||
|
||
use std::any::Any; | ||
use std::fmt::Debug; | ||
use std::ops::Range; | ||
|
||
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::{Result, ScalarValue}; | ||
use datafusion_expr::expr::WindowFunction; | ||
use datafusion_expr::{Expr, PartitionEvaluator, Signature, Volatility, WindowUDFImpl}; | ||
use datafusion_functions_window_common::field; | ||
use field::WindowUDFFieldArgs; | ||
|
||
/// Create a [`WindowFunction`](Expr::WindowFunction) expression for | ||
/// `dense_rank` user-defined window function. | ||
pub fn dense_rank() -> Expr { | ||
Expr::WindowFunction(WindowFunction::new(dense_rank_udwf(), vec![])) | ||
} | ||
|
||
/// Singleton instance of `dense_rank`, ensures the UDWF is only created once. | ||
#[allow(non_upper_case_globals)] | ||
static STATIC_DenseNumber: std::sync::OnceLock< | ||
std::sync::Arc<datafusion_expr::WindowUDF>, | ||
> = std::sync::OnceLock::new(); | ||
|
||
/// Returns a [`WindowUDF`](datafusion_expr::WindowUDF) for `dense_rank` | ||
/// user-defined window function. | ||
pub fn dense_rank_udwf() -> std::sync::Arc<datafusion_expr::WindowUDF> { | ||
STATIC_DenseNumber | ||
.get_or_init(|| { | ||
std::sync::Arc::new(datafusion_expr::WindowUDF::from(DenseNumber::default())) | ||
}) | ||
.clone() | ||
} | ||
|
||
/// dense_rank expression | ||
#[derive(Debug)] | ||
pub struct DenseNumber { | ||
signature: Signature, | ||
} | ||
|
||
impl DenseNumber { | ||
/// Create a new `dense_rank` function | ||
pub fn new() -> Self { | ||
Self { | ||
signature: Signature::any(0, Volatility::Immutable), | ||
} | ||
} | ||
} | ||
|
||
impl Default for DenseNumber { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl WindowUDFImpl for DenseNumber { | ||
fn as_any(&self) -> &dyn Any { | ||
self | ||
} | ||
|
||
fn name(&self) -> &str { | ||
"dense_rank" | ||
} | ||
|
||
fn signature(&self) -> &Signature { | ||
&self.signature | ||
} | ||
|
||
fn partition_evaluator(&self) -> Result<Box<dyn PartitionEvaluator>> { | ||
Ok(Box::<NumDensesEvaluator>::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 NumDensesEvaluator { | ||
n_rows: usize, | ||
} | ||
|
||
impl PartitionEvaluator for NumDensesEvaluator { | ||
fn is_causal(&self) -> bool { | ||
// The dense_rank function doesn't need "future" values to emit results: | ||
true | ||
} | ||
|
||
fn evaluate_all( | ||
&mut self, | ||
_values: &[ArrayRef], | ||
num_rows: usize, | ||
) -> Result<ArrayRef> { | ||
Ok(std::sync::Arc::new(UInt64Array::from_iter_values( | ||
1..(num_rows as u64) + 1, | ||
))) | ||
} | ||
|
||
fn evaluate( | ||
&mut self, | ||
_values: &[ArrayRef], | ||
_range: &Range<usize>, | ||
) -> Result<ScalarValue> { | ||
self.n_rows += 1; | ||
Ok(ScalarValue::UInt64(Some(self.n_rows as u64))) | ||
} | ||
|
||
fn supports_bounded_execution(&self) -> bool { | ||
true | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::sync::Arc; | ||
|
||
use datafusion_common::arrow::array::{Array, BooleanArray}; | ||
use datafusion_common::cast::as_uint64_array; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn dense_rank_all_null() -> Result<()> { | ||
let values: ArrayRef = Arc::new(BooleanArray::from(vec![ | ||
None, None, None, None, None, None, None, None, | ||
])); | ||
let num_rows = values.len(); | ||
|
||
let actual = DenseNumber::default() | ||
.partition_evaluator()? | ||
.evaluate_all(&[values], num_rows)?; | ||
let actual = as_uint64_array(&actual)?; | ||
|
||
assert_eq!(vec![1, 2, 3, 4, 5, 6, 7, 8], *actual.values()); | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn dense_rank_all_values() -> Result<()> { | ||
let values: ArrayRef = Arc::new(BooleanArray::from(vec![ | ||
true, false, true, false, false, true, false, true, | ||
])); | ||
let num_rows = values.len(); | ||
|
||
let actual = DenseNumber::default() | ||
.partition_evaluator()? | ||
.evaluate_all(&[values], num_rows)?; | ||
let actual = as_uint64_array(&actual)?; | ||
|
||
assert_eq!(vec![1, 2, 3, 4, 5, 6, 7, 8], *actual.values()); | ||
Ok(()) | ||
} | ||
} |
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.
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.
Technically speaking this module contains the
WindowUDFImpl
fordense_rank
, but I think this wording is consistent with the other code in this crate.I have this PR checked out to resolve some logical conflicts anyways, so I will pushed a commit to change it in this PR too