@@ -21,49 +21,102 @@ use databend_common_expression::types::DateType;
21
21
use databend_common_expression:: types:: Decimal128Type ;
22
22
use databend_common_expression:: types:: Decimal256Type ;
23
23
use databend_common_expression:: types:: Decimal64Type ;
24
+ use databend_common_expression:: types:: Float32Type ;
25
+ use databend_common_expression:: types:: Float64Type ;
26
+ use databend_common_expression:: types:: Int16Type ;
27
+ use databend_common_expression:: types:: Int32Type ;
28
+ use databend_common_expression:: types:: Int64Type ;
29
+ use databend_common_expression:: types:: Int8Type ;
24
30
use databend_common_expression:: types:: NumberDataType ;
25
- use databend_common_expression:: types:: NumberType ;
26
31
use databend_common_expression:: types:: StringType ;
27
32
use databend_common_expression:: types:: TimestampType ;
33
+ use databend_common_expression:: types:: UInt16Type ;
34
+ use databend_common_expression:: types:: UInt32Type ;
35
+ use databend_common_expression:: types:: UInt64Type ;
36
+ use databend_common_expression:: types:: UInt8Type ;
28
37
use databend_common_expression:: types:: ValueType ;
29
- use databend_common_expression:: with_number_mapped_type;
30
38
use databend_common_expression:: Column ;
31
39
use databend_common_expression:: ScalarRef ;
32
40
use databend_common_expression:: SELECTIVITY_THRESHOLD ;
33
41
use databend_storages_common_table_meta:: meta:: ColumnDistinctHLL ;
42
+ use enum_dispatch:: enum_dispatch;
34
43
35
- pub trait ColumnNDVEstimator : Send + Sync {
44
+ #[ enum_dispatch]
45
+ pub trait ColumnNDVEstimatorOps : Send + Sync {
36
46
fn update_column ( & mut self , column : & Column ) ;
37
47
fn update_scalar ( & mut self , scalar : & ScalarRef ) ;
38
48
fn finalize ( & self ) -> u64 ;
39
49
}
40
50
41
- pub fn create_column_ndv_estimator ( data_type : & DataType ) -> Box < dyn ColumnNDVEstimator > {
51
+ #[ enum_dispatch( ColumnNDVEstimatorOps ) ]
52
+ pub enum ColumnNDVEstimator {
53
+ Int8 ( ColumnNDVEstimatorImpl < Int8Type > ) ,
54
+ Int16 ( ColumnNDVEstimatorImpl < Int16Type > ) ,
55
+ Int32 ( ColumnNDVEstimatorImpl < Int32Type > ) ,
56
+ Int64 ( ColumnNDVEstimatorImpl < Int64Type > ) ,
57
+ UInt8 ( ColumnNDVEstimatorImpl < UInt8Type > ) ,
58
+ UInt16 ( ColumnNDVEstimatorImpl < UInt16Type > ) ,
59
+ UInt32 ( ColumnNDVEstimatorImpl < UInt32Type > ) ,
60
+ UInt64 ( ColumnNDVEstimatorImpl < UInt64Type > ) ,
61
+ Float32 ( ColumnNDVEstimatorImpl < Float32Type > ) ,
62
+ Float64 ( ColumnNDVEstimatorImpl < Float64Type > ) ,
63
+ String ( ColumnNDVEstimatorImpl < StringType > ) ,
64
+ Date ( ColumnNDVEstimatorImpl < DateType > ) ,
65
+ Timestamp ( ColumnNDVEstimatorImpl < TimestampType > ) ,
66
+ Decimal64 ( ColumnNDVEstimatorImpl < Decimal64Type > ) ,
67
+ Decimal128 ( ColumnNDVEstimatorImpl < Decimal128Type > ) ,
68
+ Decimal256 ( ColumnNDVEstimatorImpl < Decimal256Type > ) ,
69
+ }
70
+
71
+ pub fn create_column_ndv_estimator ( data_type : & DataType ) -> ColumnNDVEstimator {
42
72
let inner_type = data_type. remove_nullable ( ) ;
43
- with_number_mapped_type ! ( |NUM_TYPE | match inner_type {
44
- DataType :: Number ( NumberDataType :: NUM_TYPE ) => {
45
- ColumnNDVEstimatorImpl :: <NumberType <NUM_TYPE >>:: create( )
73
+ match inner_type {
74
+ DataType :: Number ( NumberDataType :: Int8 ) => {
75
+ ColumnNDVEstimator :: Int8 ( ColumnNDVEstimatorImpl :: < Int8Type > :: new ( ) )
76
+ }
77
+ DataType :: Number ( NumberDataType :: Int16 ) => {
78
+ ColumnNDVEstimator :: Int16 ( ColumnNDVEstimatorImpl :: < Int16Type > :: new ( ) )
79
+ }
80
+ DataType :: Number ( NumberDataType :: Int32 ) => {
81
+ ColumnNDVEstimator :: Int32 ( ColumnNDVEstimatorImpl :: < Int32Type > :: new ( ) )
82
+ }
83
+ DataType :: Number ( NumberDataType :: Int64 ) => {
84
+ ColumnNDVEstimator :: Int64 ( ColumnNDVEstimatorImpl :: < Int64Type > :: new ( ) )
85
+ }
86
+ DataType :: Number ( NumberDataType :: UInt8 ) => {
87
+ ColumnNDVEstimator :: UInt8 ( ColumnNDVEstimatorImpl :: < UInt8Type > :: new ( ) )
46
88
}
47
- DataType :: String => {
48
- ColumnNDVEstimatorImpl :: <StringType >:: create ( )
89
+ DataType :: Number ( NumberDataType :: UInt16 ) => {
90
+ ColumnNDVEstimator :: UInt16 ( ColumnNDVEstimatorImpl :: < UInt16Type > :: new ( ) )
49
91
}
50
- DataType :: Date => {
51
- ColumnNDVEstimatorImpl :: <DateType >:: create ( )
92
+ DataType :: Number ( NumberDataType :: UInt32 ) => {
93
+ ColumnNDVEstimator :: UInt32 ( ColumnNDVEstimatorImpl :: < UInt32Type > :: new ( ) )
52
94
}
95
+ DataType :: Number ( NumberDataType :: UInt64 ) => {
96
+ ColumnNDVEstimator :: UInt64 ( ColumnNDVEstimatorImpl :: < UInt64Type > :: new ( ) )
97
+ }
98
+ DataType :: Number ( NumberDataType :: Float32 ) => {
99
+ ColumnNDVEstimator :: Float32 ( ColumnNDVEstimatorImpl :: < Float32Type > :: new ( ) )
100
+ }
101
+ DataType :: Number ( NumberDataType :: Float64 ) => {
102
+ ColumnNDVEstimator :: Float64 ( ColumnNDVEstimatorImpl :: < Float64Type > :: new ( ) )
103
+ }
104
+ DataType :: String => ColumnNDVEstimator :: String ( ColumnNDVEstimatorImpl :: < StringType > :: new ( ) ) ,
105
+ DataType :: Date => ColumnNDVEstimator :: Date ( ColumnNDVEstimatorImpl :: < DateType > :: new ( ) ) ,
53
106
DataType :: Timestamp => {
54
- ColumnNDVEstimatorImpl :: <TimestampType >:: create ( )
107
+ ColumnNDVEstimator :: Timestamp ( ColumnNDVEstimatorImpl :: < TimestampType > :: new ( ) )
55
108
}
56
109
DataType :: Decimal ( size) => {
57
110
if size. can_carried_by_64 ( ) {
58
- ColumnNDVEstimatorImpl :: <Decimal64Type >:: create ( )
111
+ ColumnNDVEstimator :: Decimal64 ( ColumnNDVEstimatorImpl :: < Decimal64Type > :: new ( ) )
59
112
} else if size. can_carried_by_128 ( ) {
60
- ColumnNDVEstimatorImpl :: <Decimal128Type >:: create ( )
113
+ ColumnNDVEstimator :: Decimal128 ( ColumnNDVEstimatorImpl :: < Decimal128Type > :: new ( ) )
61
114
} else {
62
- ColumnNDVEstimatorImpl :: <Decimal256Type >:: create ( )
115
+ ColumnNDVEstimator :: Decimal256 ( ColumnNDVEstimatorImpl :: < Decimal256Type > :: new ( ) )
63
116
}
64
117
}
65
118
_ => unreachable ! ( "Unsupported data type: {:?}" , data_type) ,
66
- } )
119
+ }
67
120
}
68
121
69
122
pub struct ColumnNDVEstimatorImpl < T >
@@ -80,15 +133,15 @@ where
80
133
T : ValueType + Send + Sync ,
81
134
for < ' a > T :: ScalarRef < ' a > : Hash ,
82
135
{
83
- pub fn create ( ) -> Box < dyn ColumnNDVEstimator > {
84
- Box :: new ( Self {
136
+ pub fn new ( ) -> Self {
137
+ Self {
85
138
hll : ColumnDistinctHLL :: new ( ) ,
86
139
_phantom : Default :: default ( ) ,
87
- } )
140
+ }
88
141
}
89
142
}
90
143
91
- impl < T > ColumnNDVEstimator for ColumnNDVEstimatorImpl < T >
144
+ impl < T > ColumnNDVEstimatorOps for ColumnNDVEstimatorImpl < T >
92
145
where
93
146
T : ValueType + Send + Sync ,
94
147
for < ' a > T :: ScalarRef < ' a > : Hash ,
0 commit comments