-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Use exact distinct_count from statistics if exists for COUNT(DISTINCT column)) calculations
#20845
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -365,31 +365,37 @@ impl AggregateUDFImpl for Count { | |
| } | ||
|
|
||
| fn value_from_stats(&self, statistics_args: &StatisticsArgs) -> Option<ScalarValue> { | ||
| let [expr] = statistics_args.exprs else { | ||
| return None; | ||
| }; | ||
| let col_stats = &statistics_args.statistics.column_statistics; | ||
|
|
||
| if statistics_args.is_distinct { | ||
| // Only column references can be resolved from statistics; | ||
| // expressions like casts or literals are not supported. | ||
| let col_expr = expr.as_any().downcast_ref::<expressions::Column>()?; | ||
| if let Precision::Exact(dc) = col_stats[col_expr.index()].distinct_count { | ||
| return Some(ScalarValue::Int64(Some(dc as i64))); | ||
|
Contributor
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. This assumes
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. Distinct count is usize, we should deal with the case where it could overflow
Contributor
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. Since other paths were casting as well I did not do it actually but let me do it for all paths |
||
| } | ||
| return None; | ||
| } | ||
| if let Precision::Exact(num_rows) = statistics_args.statistics.num_rows | ||
| && 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>() | ||
| && lit_expr.value() == &COUNT_STAR_EXPANSION | ||
| { | ||
| return Some(ScalarValue::Int64(Some(num_rows as i64))); | ||
|
|
||
| let Precision::Exact(num_rows) = statistics_args.statistics.num_rows else { | ||
| return None; | ||
| }; | ||
|
|
||
| // TODO optimize with exprs other than Column | ||
| if let Some(col_expr) = expr.as_any().downcast_ref::<expressions::Column>() { | ||
| if let Precision::Exact(val) = col_stats[col_expr.index()].null_count { | ||
| return Some(ScalarValue::Int64(Some((num_rows - val) as i64))); | ||
| } | ||
| } else if let Some(lit_expr) = | ||
| expr.as_any().downcast_ref::<expressions::Literal>() | ||
| && lit_expr.value() == &COUNT_STAR_EXPANSION | ||
| { | ||
| return Some(ScalarValue::Int64(Some(num_rows as i64))); | ||
| } | ||
|
|
||
| None | ||
| } | ||
|
|
||
|
|
||
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.
Are we able to combine these two functions
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 did not exactly understand but changed it to one table driven test