@@ -397,6 +397,39 @@ impl ApproxDistinct {
397397 }
398398}
399399
400+ #[ cold]
401+ fn get_small_int_approx_accumulator (
402+ data_type : & DataType ,
403+ ) -> Result < Box < dyn Accumulator > > {
404+ match data_type {
405+ DataType :: UInt8 => Ok ( Box :: new ( ApproxDistinctBitmapWrapper {
406+ inner : BoolArray256DistinctCountAccumulator :: new ( ) ,
407+ } ) ) ,
408+ DataType :: Int8 => Ok ( Box :: new ( ApproxDistinctBitmapWrapper {
409+ inner : BoolArray256DistinctCountAccumulatorI8 :: new ( ) ,
410+ } ) ) ,
411+ DataType :: UInt16 => Ok ( Box :: new ( ApproxDistinctBitmapWrapper {
412+ inner : Bitmap65536DistinctCountAccumulator :: new ( ) ,
413+ } ) ) ,
414+ DataType :: Int16 => Ok ( Box :: new ( ApproxDistinctBitmapWrapper {
415+ inner : Bitmap65536DistinctCountAccumulatorI16 :: new ( ) ,
416+ } ) ) ,
417+ _ => internal_err ! ( "unsupported small int type: {}" , data_type) ,
418+ }
419+ }
420+
421+ #[ cold]
422+ fn get_small_int_state_field ( name : & str , data_type : & DataType ) -> Result < Vec < FieldRef > > {
423+ Ok ( vec ! [
424+ Field :: new_list(
425+ format_state_name( name, "approx_distinct" ) ,
426+ Field :: new_list_field( data_type. clone( ) , true ) ,
427+ false ,
428+ )
429+ . into( ) ,
430+ ] )
431+ }
432+
400433impl AggregateUDFImpl for ApproxDistinct {
401434 fn name ( & self ) -> & str {
402435 "approx_distinct"
@@ -421,38 +454,9 @@ impl AggregateUDFImpl for ApproxDistinct {
421454 )
422455 . into( ) ,
423456 ] ) ,
424- DataType :: UInt8 => Ok ( vec ! [
425- Field :: new_list(
426- format_state_name( args. name, "approx_distinct" ) ,
427- Field :: new_list_field( DataType :: UInt8 , true ) ,
428- false ,
429- )
430- . into( ) ,
431- ] ) ,
432- DataType :: Int8 => Ok ( vec ! [
433- Field :: new_list(
434- format_state_name( args. name, "approx_distinct" ) ,
435- Field :: new_list_field( DataType :: Int8 , true ) ,
436- false ,
437- )
438- . into( ) ,
439- ] ) ,
440- DataType :: UInt16 => Ok ( vec ! [
441- Field :: new_list(
442- format_state_name( args. name, "approx_distinct" ) ,
443- Field :: new_list_field( DataType :: UInt16 , true ) ,
444- false ,
445- )
446- . into( ) ,
447- ] ) ,
448- DataType :: Int16 => Ok ( vec ! [
449- Field :: new_list(
450- format_state_name( args. name, "approx_distinct" ) ,
451- Field :: new_list_field( DataType :: Int16 , true ) ,
452- false ,
453- )
454- . into( ) ,
455- ] ) ,
457+ DataType :: UInt8 | DataType :: Int8 | DataType :: UInt16 | DataType :: Int16 => {
458+ get_small_int_state_field ( args. name , data_type)
459+ }
456460 _ => Ok ( vec ! [
457461 Field :: new(
458462 format_state_name( args. name, "hll_registers" ) ,
@@ -468,21 +472,11 @@ impl AggregateUDFImpl for ApproxDistinct {
468472 let data_type = acc_args. expr_fields [ 0 ] . data_type ( ) ;
469473
470474 let accumulator: Box < dyn Accumulator > = match data_type {
471- // Use bitmap accumulators for small integer types
472- DataType :: UInt8 => Box :: new ( ApproxDistinctBitmapWrapper {
473- inner : BoolArray256DistinctCountAccumulator :: new ( ) ,
474- } ) ,
475- DataType :: UInt16 => Box :: new ( ApproxDistinctBitmapWrapper {
476- inner : Bitmap65536DistinctCountAccumulator :: new ( ) ,
477- } ) ,
475+ DataType :: UInt8 | DataType :: Int8 | DataType :: UInt16 | DataType :: Int16 => {
476+ return get_small_int_approx_accumulator ( data_type) ;
477+ }
478478 DataType :: UInt32 => Box :: new ( NumericHLLAccumulator :: < UInt32Type > :: new ( ) ) ,
479479 DataType :: UInt64 => Box :: new ( NumericHLLAccumulator :: < UInt64Type > :: new ( ) ) ,
480- DataType :: Int8 => Box :: new ( ApproxDistinctBitmapWrapper {
481- inner : BoolArray256DistinctCountAccumulatorI8 :: new ( ) ,
482- } ) ,
483- DataType :: Int16 => Box :: new ( ApproxDistinctBitmapWrapper {
484- inner : Bitmap65536DistinctCountAccumulatorI16 :: new ( ) ,
485- } ) ,
486480 DataType :: Int32 => Box :: new ( NumericHLLAccumulator :: < Int32Type > :: new ( ) ) ,
487481 DataType :: Int64 => Box :: new ( NumericHLLAccumulator :: < Int64Type > :: new ( ) ) ,
488482 DataType :: Date32 => Box :: new ( NumericHLLAccumulator :: < Date32Type > :: new ( ) ) ,
0 commit comments