Apache DataFusion SQL Query Engine
feat: Support Struct type in approx_distinct (#23663)
Michael Kleen78be510e9082
diff --git a/datafusion/functions-aggregate/src/approx_distinct.rs b/datafusion/functions-aggregate/src/approx_distinct.rsindex 19bb807ffa..6d3ea3a8dd 100644--- a/datafusion/functions-aggregate/src/approx_distinct.rs+++ b/datafusion/functions-aggregate/src/approx_distinct.rs@@ -843,6 +843,7 @@ impl AggregateUDFImpl for ApproxDistinct {| DataType::ListView(_)| DataType::LargeListView(_)| DataType::Map(_, _)+ | DataType::Struct(_)| DataType::LargeBinary => Box::new(HLLAccumulator::new()),DataType::Null => {Box::new(NoopAccumulator::new(ScalarValue::UInt64(Some(0))))@@ -920,6 +921,7 @@ fn is_hll_groups_type(data_type: &DataType) -> bool {| DataType::ListView(_)| DataType::LargeListView(_)| DataType::Map(_, _)+ | DataType::Struct(_))}diff --git a/datafusion/sqllogictest/test_files/aggregate.slt b/datafusion/sqllogictest/test_files/aggregate.sltindex fa12f055a6..cbb9c5d031 100644--- a/datafusion/sqllogictest/test_files/aggregate.slt+++ b/datafusion/sqllogictest/test_files/aggregate.slt@@ -2133,6 +2133,44 @@ SELECT approx_distinct(m) FROM approx_distinct_map_test;statement okDROP TABLE approx_distinct_map_test;+# Struct+statement ok+CREATE TABLE approx_distinct_struct_test AS SELECT * FROM (VALUES+ (1, named_struct('a', 1, 'b', 2)), (1, named_struct('a', 1, 'b', 2)), (1, named_struct('a', 3, 'b', 3)),+ (2, named_struct('a', 4, 'b', 4)), (2, NULL),+ (3, NULL), (3, NULL),+ (4, named_struct('a', 5, 'b', 5))+) AS t(g, s);++# Struct non-grouped+query I+SELECT approx_distinct(s) FROM approx_distinct_struct_test WHERE g = 1;+----+2++# Struct grouped+# Group 1 -> {{a:1,b:2},{a:3,b:3}}=2,+# Group 2 -> {{a:4,b:4}}=1 (NULL excluded),+# Group 3 -> all null=0,+# Group 4 -> {{a:5,b:5}}=1+query II+SELECT g, approx_distinct(s) FROM approx_distinct_struct_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 structs across groups are still counted overall.+query I+SELECT approx_distinct(s) FROM approx_distinct_struct_test;+----+4++statement ok+DROP TABLE approx_distinct_struct_test;+# Integers (Int32): group 1 -> {10,20}=2, group 2 -> {30,40}=2, group 3 -> 0, group 4 -> {50}=1query IISELECT g, approx_distinct(i) FROM approx_distinct_group_test GROUP BY g ORDER BY g;