Skip to content

Support Avg distinct for decimal and float type #15414

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

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

qazxcdswe123
Copy link
Contributor

Which issue does this PR close?

Rationale for this change

What changes are included in this PR?

  • All in Support Avg distinct for float64 type #15413 and
  • DecimalDistinctAvgAccumulator implematation using DistinctSumAccumulator
  • Tests (Note that as single_distinct_to_groupby rule have not been rm, sqllogictest is not accurate so I added tests in decimal.rs

Are these changes tested?

Yes, sqllogictest and rust tests

Are there any user-facing changes?

No

@github-actions github-actions bot added sqllogictest SQL Logic Tests (.slt) functions Changes to functions implementation labels Mar 25, 2025
let sum_scalar = self.sum_accumulator.evaluate()?;

match sum_scalar {
ScalarValue::Decimal128(Some(sum), _, _) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though I prefer a generic solution like

struct DecimalAvgAccumulator<T: DecimalType + ArrowNumericType + Debug> {

I couldn't find a way to convert ScalarValue to Primitive.
And I also think this solution is acceptable

)?;
// `distinct_count` returns `u64`, but `avg` expects `i256`
// first convert `u64` to `i128`, then convert `i128` to `i256` to avoid overflow
let distinct_cnt: i128 = self.sum_accumulator.distinct_count() as i128;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is awkward but can prevent overflow

@@ -6686,3 +6688,35 @@ SELECT a, median(b), arrow_typeof(median(b)) FROM group_median_all_nulls GROUP B
----
group0 NULL Int32
group1 NULL Int32

statement ok
create table t_decimal (c decimal(10, 4)) as values (100.00), (125.00), (175.00), (200.00), (200.00), (300.00), (null), (null);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tests are not reliable as single_distinct_to_groupby is still there

@github-actions github-actions bot added core Core DataFusion crate proto Related to proto crate labels Mar 25, 2025
@Omega359
Copy link
Contributor

FYI, the sqlite tests have a number of avg(distinct) queries that will need to be updated. See the README for details.

@alamb alamb marked this pull request as draft March 25, 2025 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Core DataFusion crate functions Changes to functions implementation proto Related to proto crate sqllogictest SQL Logic Tests (.slt)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

avg(distinct) support
2 participants