Skip to content
Open
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
2 changes: 2 additions & 0 deletions datafusion/functions-aggregate/src/approx_distinct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,7 @@ impl AggregateUDFImpl for ApproxDistinct {
| DataType::FixedSizeList(_, _)
| DataType::ListView(_)
| DataType::LargeListView(_)
| DataType::Map(_, _)
| DataType::LargeBinary => Box::new(HLLAccumulator::new()),
DataType::Null => {
Box::new(NoopAccumulator::new(ScalarValue::UInt64(Some(0))))
Expand Down Expand Up @@ -918,6 +919,7 @@ fn is_hll_groups_type(data_type: &DataType) -> bool {
| DataType::FixedSizeList(_, _)
| DataType::ListView(_)
| DataType::LargeListView(_)
| DataType::Map(_, _)
)
}

Expand Down
38 changes: 38 additions & 0 deletions datafusion/sqllogictest/test_files/aggregate.slt
Original file line number Diff line number Diff line change
Expand Up @@ -2095,6 +2095,44 @@ SELECT approx_distinct(arrow_cast(l, 'FixedSizeList(2, Int32)')) FROM approx_dis
statement ok
DROP TABLE approx_distinct_list_test;

# Map
statement ok
CREATE TABLE approx_distinct_map_test AS SELECT * FROM (VALUES
(1, MAP {'a': 1, 'b': 2}), (1, MAP {'a': 1, 'b': 2}), (1, MAP {'c': 3}),
(2, MAP {'d': 4}), (2, NULL),
(3, NULL), (3, NULL),
(4, MAP {'e': 5})
) AS t(g, m);

# Map non-grouped
query I
SELECT approx_distinct(m) FROM approx_distinct_map_test WHERE g = 1;
----
2

# Map grouped
# Group 1 -> {{a:1,b:2},{c:3}}=2,
# Group 2 -> {{d:4}}=1 (NULL excluded),
# Group 3 -> all null=0,
# Group 4 -> {{e:5}}=1
query II
SELECT g, approx_distinct(m) FROM approx_distinct_map_test GROUP BY g ORDER BY g;
----
1 2
2 1
3 0
4 1

# The non-group path must agree with the grouped path on
# the same data, and distinct maps across groups are still counted overall.
query I
SELECT approx_distinct(m) FROM approx_distinct_map_test;
----
4

statement ok
DROP TABLE approx_distinct_map_test;

# Integers (Int32): group 1 -> {10,20}=2, group 2 -> {30,40}=2, group 3 -> 0, group 4 -> {50}=1
query II
SELECT g, approx_distinct(i) FROM approx_distinct_group_test GROUP BY g ORDER BY g;
Expand Down
Loading