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 @@ -840,6 +840,7 @@ impl AggregateUDFImpl for ApproxDistinct {
| DataType::Map(_, _)
| DataType::Struct(_)
| DataType::Union(_, _)
| DataType::Dictionary(_, _)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would it work to match on the value type instead, so a dictionary is supported exactly when its values would be? Something like DataType::Dictionary(_, value_type)

@mkleen mkleen Aug 25, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point. Floats will be unsupported see #23084, So we should exclude them in all container types.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

i still feel we can support floats here if we already support it in regular distinct count 🤔

also for reference seems duckdb supports:

memory D select approx_count_distinct(a) from values (1.5::double), (1.5), ('nan'::double), (null), (0) t(a);
┌──────────────────────────┐
│ approx_count_distinct(a) │
│          int64           │
├──────────────────────────┤
│                        3 │
└──────────────────────────┘

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok makes sense. I will look into this.

| DataType::LargeBinary => Box::new(HLLAccumulator::new()),
DataType::Null => {
Box::new(NoopAccumulator::new(ScalarValue::UInt64(Some(0))))
Expand Down Expand Up @@ -919,6 +920,7 @@ fn is_hll_groups_type(data_type: &DataType) -> bool {
| DataType::Map(_, _)
| DataType::Struct(_)
| DataType::Union(_, _)
| DataType::Dictionary(_, _)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

would it make sense to have both dictionary arms on the value type, so Dictionary(_, value_type) is accepted only when is_hll_groups_type(value_type) is true?

)
}

Expand Down
34 changes: 34 additions & 0 deletions datafusion/sqllogictest/test_files/aggregate.slt
Original file line number Diff line number Diff line change
Expand Up @@ -1793,6 +1793,40 @@ SELECT g, approx_distinct(arrow_cast(arrow_cast(s, 'Binary'), 'FixedSizeBinary(1
4 1


# Dictionary: dictionary-encoded values must hash identically to the plain
# (non-dictionary) values, so the counts below match the Utf8 case above.

# Dictionary non-grouped
query I
SELECT approx_distinct(arrow_cast(s, 'Dictionary(Int32, Utf8)')) FROM approx_distinct_group_test WHERE g = 2;
----
2

# Dictionary grouped
query II
SELECT g, approx_distinct(arrow_cast(s, 'Dictionary(Int32, Utf8)')) FROM approx_distinct_group_test GROUP BY g ORDER BY g;
----
1 2
2 2
3 0
4 1

# Dictionary with a non-string value type (Int32), also exercising a
# larger (Int64) key type
query I
SELECT approx_distinct(arrow_cast(i, 'Dictionary(Int64, Int32)')) FROM approx_distinct_group_test WHERE g = 2;
----
2

query II
SELECT g, approx_distinct(arrow_cast(i, 'Dictionary(Int64, Int32)')) FROM approx_distinct_group_test GROUP BY g ORDER BY g;
----
1 2
2 2
3 0
4 1


# List
statement ok
CREATE TABLE approx_distinct_list_test (g INT, l INT[]) AS VALUES
Expand Down