-
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
Add value_from_statisics
to AggregateUDFImpl, remove special case for min/max/count aggregate statistics
#12296
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f478344
Removes min/max/count comparison based on name in aggregate statistics
edmondop 751e35a
Abstracting away value from statistics
edmondop 016decf
Removing imports
edmondop f41dd3e
Introduced StatisticsArgs
edmondop faf6c45
Fixed docs
edmondop 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
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -25,7 +25,8 @@ use std::vec; | |||||||||||||
|
||||||||||||||
use arrow::datatypes::{DataType, Field}; | ||||||||||||||
|
||||||||||||||
use datafusion_common::{exec_err, not_impl_err, Result, ScalarValue}; | ||||||||||||||
use datafusion_common::{exec_err, not_impl_err, Result, ScalarValue, Statistics}; | ||||||||||||||
use datafusion_physical_expr_common::physical_expr::PhysicalExpr; | ||||||||||||||
|
||||||||||||||
use crate::expr::AggregateFunction; | ||||||||||||||
use crate::function::{ | ||||||||||||||
|
@@ -93,6 +94,19 @@ impl fmt::Display for AggregateUDF { | |||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
pub struct StatisticsArgs<'a> { | ||||||||||||||
pub statistics: &'a Statistics, | ||||||||||||||
pub return_type: &'a DataType, | ||||||||||||||
/// Whether the aggregate function is distinct. | ||||||||||||||
/// | ||||||||||||||
/// ```sql | ||||||||||||||
/// SELECT COUNT(DISTINCT column1) FROM t; | ||||||||||||||
/// ``` | ||||||||||||||
pub is_distinct: bool, | ||||||||||||||
/// The physical expression of arguments the aggregate function takes. | ||||||||||||||
pub exprs: &'a [Arc<dyn PhysicalExpr>], | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
impl AggregateUDF { | ||||||||||||||
/// Create a new AggregateUDF | ||||||||||||||
/// | ||||||||||||||
|
@@ -262,6 +276,13 @@ impl AggregateUDF { | |||||||||||||
self.inner.is_descending() | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
pub fn value_from_stats( | ||||||||||||||
&self, | ||||||||||||||
statistics_args: &StatisticsArgs, | ||||||||||||||
) -> Option<ScalarValue> { | ||||||||||||||
self.inner.value_from_stats(statistics_args) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/// See [`AggregateUDFImpl::default_value`] for more details. | ||||||||||||||
pub fn default_value(&self, data_type: &DataType) -> Result<ScalarValue> { | ||||||||||||||
self.inner.default_value(data_type) | ||||||||||||||
|
@@ -574,6 +595,10 @@ pub trait AggregateUDFImpl: Debug + Send + Sync { | |||||||||||||
fn is_descending(&self) -> Option<bool> { | ||||||||||||||
None | ||||||||||||||
} | ||||||||||||||
// Return the value of the current UDF from the statistics | ||||||||||||||
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.
Suggested change
|
||||||||||||||
fn value_from_stats(&self, _statistics_args: &StatisticsArgs) -> Option<ScalarValue> { | ||||||||||||||
None | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/// Returns default value of the function given the input is all `null`. | ||||||||||||||
/// | ||||||||||||||
|
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 |
---|---|---|
|
@@ -16,7 +16,9 @@ | |
// under the License. | ||
|
||
use ahash::RandomState; | ||
use datafusion_common::stats::Precision; | ||
use datafusion_functions_aggregate_common::aggregate::count_distinct::BytesViewDistinctCountAccumulator; | ||
use datafusion_physical_expr::expressions; | ||
use std::collections::HashSet; | ||
use std::ops::BitAnd; | ||
use std::{fmt::Debug, sync::Arc}; | ||
|
@@ -46,14 +48,15 @@ use datafusion_expr::{ | |
function::AccumulatorArgs, utils::format_state_name, Accumulator, AggregateUDFImpl, | ||
EmitTo, GroupsAccumulator, Signature, Volatility, | ||
}; | ||
use datafusion_expr::{Expr, ReversedUDAF, TypeSignature}; | ||
use datafusion_expr::{Expr, ReversedUDAF, StatisticsArgs, TypeSignature}; | ||
use datafusion_functions_aggregate_common::aggregate::count_distinct::{ | ||
BytesDistinctCountAccumulator, FloatDistinctCountAccumulator, | ||
PrimitiveDistinctCountAccumulator, | ||
}; | ||
use datafusion_functions_aggregate_common::aggregate::groups_accumulator::accumulate::accumulate_indices; | ||
use datafusion_physical_expr_common::binary_map::OutputType; | ||
|
||
use datafusion_common::utils::expr::COUNT_STAR_EXPANSION; | ||
make_udaf_expr_and_func!( | ||
Count, | ||
count, | ||
|
@@ -291,6 +294,36 @@ impl AggregateUDFImpl for Count { | |
fn default_value(&self, _data_type: &DataType) -> Result<ScalarValue> { | ||
Ok(ScalarValue::Int64(Some(0))) | ||
} | ||
|
||
fn value_from_stats(&self, statistics_args: &StatisticsArgs) -> Option<ScalarValue> { | ||
if statistics_args.is_distinct { | ||
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. 👍 |
||
return None; | ||
} | ||
if let Precision::Exact(num_rows) = statistics_args.statistics.num_rows { | ||
if statistics_args.exprs.len() == 1 { | ||
// TODO optimize with exprs other than Column | ||
if let Some(col_expr) = statistics_args.exprs[0] | ||
.as_any() | ||
.downcast_ref::<expressions::Column>() | ||
{ | ||
let current_val = &statistics_args.statistics.column_statistics | ||
[col_expr.index()] | ||
.null_count; | ||
if let &Precision::Exact(val) = current_val { | ||
return Some(ScalarValue::Int64(Some((num_rows - val) as i64))); | ||
} | ||
} else if let Some(lit_expr) = statistics_args.exprs[0] | ||
.as_any() | ||
.downcast_ref::<expressions::Literal>() | ||
{ | ||
if lit_expr.value() == &COUNT_STAR_EXPANSION { | ||
return Some(ScalarValue::Int64(Some(num_rows as i64))); | ||
} | ||
} | ||
} | ||
} | ||
None | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
|
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.
❤️
I think it would also be great to add some documentation. Perhaps like this: