Skip to content

feat: support ApproxDistinct with utf8view #15200

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

Merged
merged 3 commits into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion datafusion/functions-aggregate/src/approx_distinct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//! Defines physical expressions that can evaluated at runtime during query execution

use crate::hyperloglog::HyperLogLog;
use arrow::array::BinaryArray;
use arrow::array::{BinaryArray, StringViewArray};
use arrow::array::{
GenericBinaryArray, GenericStringArray, OffsetSizeTrait, PrimitiveArray,
};
Expand Down Expand Up @@ -126,6 +126,27 @@ where
}
}

#[derive(Debug)]
struct StringViewHLLAccumulator<T>
where
T: OffsetSizeTrait,
{
hll: HyperLogLog<String>,
phantom_data: PhantomData<T>,
}

impl<T> StringViewHLLAccumulator<T>
where
T: OffsetSizeTrait,
{
pub fn new() -> Self {
Self {
hll: HyperLogLog::new(),
phantom_data: PhantomData,
}
}
}

#[derive(Debug)]
struct BinaryHLLAccumulator<T>
where
Expand Down Expand Up @@ -197,6 +218,21 @@ where
default_accumulator_impl!();
}

impl<T> Accumulator for StringViewHLLAccumulator<T>
where
T: OffsetSizeTrait,
{
fn update_batch(&mut self, values: &[ArrayRef]) -> Result<()> {
let array: &StringViewArray = downcast_value!(values[0], StringViewArray);
// flatten because we would skip nulls
self.hll
.extend(array.iter().flatten().map(|s| s.to_string()));
Ok(())
}

default_accumulator_impl!();
}

impl<T> Accumulator for StringHLLAccumulator<T>
where
T: OffsetSizeTrait,
Expand Down Expand Up @@ -311,6 +347,7 @@ impl AggregateUDFImpl for ApproxDistinct {
DataType::Int64 => Box::new(NumericHLLAccumulator::<Int64Type>::new()),
DataType::Utf8 => Box::new(StringHLLAccumulator::<i32>::new()),
DataType::LargeUtf8 => Box::new(StringHLLAccumulator::<i64>::new()),
DataType::Utf8View => Box::new(StringViewHLLAccumulator::<i32>::new()),
DataType::Binary => Box::new(BinaryHLLAccumulator::<i32>::new()),
DataType::LargeBinary => Box::new(BinaryHLLAccumulator::<i64>::new()),
other => {
Expand Down
21 changes: 21 additions & 0 deletions datafusion/sqllogictest/test_files/aggregate_skip_partial.slt
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,27 @@ SELECT c2, approx_distinct(c1), approx_distinct(c5) FROM aggregate_test_100 GROU
4 5 23
5 5 14

# Test approx_distinct for varchar(with Utf8View) / int
statement ok
CREATE TABLE aggregate_test_100_utf8view AS SELECT
arrow_cast(c1, 'Utf8View') as c1,
c2,
c5
FROM aggregate_test_100;

# Test approx_distinct for varchar(with Utf8View) / int
query III
SELECT c2, approx_distinct(c1), approx_distinct(c5) FROM aggregate_test_100_utf8view GROUP BY c2 ORDER BY c2;
----
1 5 22
Copy link
Contributor

Choose a reason for hiding this comment

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

it is the same as above 💯

2 5 22
3 5 19
4 5 23
5 5 14

statement ok
DROP TABLE aggregate_test_100_utf8view;

# Test count with nullable fields
query III
SELECT c2, count(c3), count(c11) FROM aggregate_test_100_null GROUP BY c2 ORDER BY c2;
Expand Down