From a677c967564e6864c6ef119482c354e1a9eeeab0 Mon Sep 17 00:00:00 2001 From: Michael Kleen Date: Tue, 18 Aug 2026 09:44:57 +0200 Subject: [PATCH] Add support for dictionary for approx_distinct --- .../src/approx_distinct.rs | 2 ++ .../sqllogictest/test_files/aggregate.slt | 34 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/datafusion/functions-aggregate/src/approx_distinct.rs b/datafusion/functions-aggregate/src/approx_distinct.rs index 1746edd8239f2..00a69713b79db 100644 --- a/datafusion/functions-aggregate/src/approx_distinct.rs +++ b/datafusion/functions-aggregate/src/approx_distinct.rs @@ -840,6 +840,7 @@ impl AggregateUDFImpl for ApproxDistinct { | DataType::Map(_, _) | DataType::Struct(_) | DataType::Union(_, _) + | DataType::Dictionary(_, _) | DataType::LargeBinary => Box::new(HLLAccumulator::new()), DataType::Null => { Box::new(NoopAccumulator::new(ScalarValue::UInt64(Some(0)))) @@ -919,6 +920,7 @@ fn is_hll_groups_type(data_type: &DataType) -> bool { | DataType::Map(_, _) | DataType::Struct(_) | DataType::Union(_, _) + | DataType::Dictionary(_, _) ) } diff --git a/datafusion/sqllogictest/test_files/aggregate.slt b/datafusion/sqllogictest/test_files/aggregate.slt index 11651ef64ce1a..592efc50458e3 100644 --- a/datafusion/sqllogictest/test_files/aggregate.slt +++ b/datafusion/sqllogictest/test_files/aggregate.slt @@ -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