@@ -1498,13 +1498,77 @@ mod tests {
14981498 use crate :: ExecutionPlan ;
14991499 use crate :: InputOrderMode ;
15001500 use crate :: test:: TestMemoryExec ;
1501- use arrow:: array:: { Int32Array , Int64Array } ;
1501+ use arrow:: array:: { Int32Array , Int64Array , UInt32Array } ;
15021502 use arrow:: datatypes:: { DataType , Field , Schema } ;
15031503 use datafusion_execution:: runtime_env:: RuntimeEnvBuilder ;
1504- use datafusion_functions_aggregate:: count:: count_udaf;
1504+ use datafusion_functions_aggregate:: { array_agg :: array_agg_udaf , count:: count_udaf} ;
15051505 use datafusion_physical_expr:: aggregate:: AggregateExprBuilder ;
15061506 use datafusion_physical_expr:: expressions:: col;
15071507
1508+ #[ tokio:: test]
1509+ async fn grouped_hash_stream_reports_distinct_array_agg_submetric ( ) -> Result < ( ) > {
1510+ let schema = Arc :: new ( Schema :: new ( vec ! [
1511+ Field :: new( "group" , DataType :: Int32 , false ) ,
1512+ Field :: new( "value" , DataType :: UInt32 , false ) ,
1513+ ] ) ) ;
1514+ let batch = RecordBatch :: try_new (
1515+ Arc :: clone ( & schema) ,
1516+ vec ! [
1517+ Arc :: new( Int32Array :: from( vec![ 1 , 1 , 2 , 2 ] ) ) ,
1518+ Arc :: new( UInt32Array :: from( vec![ 3 , 3 , 4 , 4 ] ) ) ,
1519+ ] ,
1520+ ) ?;
1521+ let input = Arc :: new ( TestMemoryExec :: try_new (
1522+ & [ vec ! [ batch] ] ,
1523+ Arc :: clone ( & schema) ,
1524+ None ,
1525+ ) ?) ;
1526+ let group_by = PhysicalGroupBy :: new_single ( vec ! [ (
1527+ col( "group" , & schema) ?,
1528+ "group" . to_string( ) ,
1529+ ) ] ) ;
1530+ let aggregate_expr = Arc :: new (
1531+ AggregateExprBuilder :: new ( array_agg_udaf ( ) , vec ! [ col( "value" , & schema) ?] )
1532+ . schema ( Arc :: clone ( & schema) )
1533+ . distinct ( )
1534+ . alias ( "distinct_values" )
1535+ . build ( ) ?,
1536+ ) ;
1537+ let output_schema = Arc :: new ( create_schema (
1538+ & schema,
1539+ & group_by,
1540+ std:: slice:: from_ref ( & aggregate_expr) ,
1541+ AggregateMode :: Single ,
1542+ ) ?) ;
1543+ let aggregate_exec = AggregateExec :: try_new (
1544+ AggregateMode :: Single ,
1545+ group_by,
1546+ vec ! [ aggregate_expr] ,
1547+ vec ! [ None ] ,
1548+ input,
1549+ output_schema,
1550+ ) ?;
1551+
1552+ let mut stream = GroupedHashAggregateStream :: new (
1553+ & aggregate_exec,
1554+ & Arc :: new ( TaskContext :: default ( ) ) ,
1555+ 0 ,
1556+ ) ?;
1557+ while let Some ( batch) = stream. next ( ) . await {
1558+ batch?;
1559+ }
1560+
1561+ assert ! (
1562+ aggregate_exec
1563+ . metrics( )
1564+ . unwrap( )
1565+ . sum_by_name( "agg_expr_0_internal_distinct_time" )
1566+ . is_some_and( |metric| metric. as_usize( ) > 0 )
1567+ ) ;
1568+
1569+ Ok ( ( ) )
1570+ }
1571+
15081572 // Migrated to PartialHashAggregateStream coverage in hash_stream.rs;
15091573 // kept here for the legacy GroupedHashAggregateStream implementation.
15101574 #[ tokio:: test]
0 commit comments