Skip to content

Commit 41c7854

Browse files
committed
update
1 parent f8864ff commit 41c7854

File tree

3 files changed

+76
-21
lines changed

3 files changed

+76
-21
lines changed

src/query/storages/fuse/src/io/write/stream/column_ndv_estimator.rs

Lines changed: 73 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,49 +21,102 @@ use databend_common_expression::types::DateType;
2121
use databend_common_expression::types::Decimal128Type;
2222
use databend_common_expression::types::Decimal256Type;
2323
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;
2430
use databend_common_expression::types::NumberDataType;
25-
use databend_common_expression::types::NumberType;
2631
use databend_common_expression::types::StringType;
2732
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;
2837
use databend_common_expression::types::ValueType;
29-
use databend_common_expression::with_number_mapped_type;
3038
use databend_common_expression::Column;
3139
use databend_common_expression::ScalarRef;
3240
use databend_common_expression::SELECTIVITY_THRESHOLD;
3341
use databend_storages_common_table_meta::meta::ColumnDistinctHLL;
42+
use enum_dispatch::enum_dispatch;
3443

35-
pub trait ColumnNDVEstimator: Send + Sync {
44+
#[enum_dispatch]
45+
pub trait ColumnNDVEstimatorOps: Send + Sync {
3646
fn update_column(&mut self, column: &Column);
3747
fn update_scalar(&mut self, scalar: &ScalarRef);
3848
fn finalize(&self) -> u64;
3949
}
4050

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 {
4272
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())
4688
}
47-
DataType::String => {
48-
ColumnNDVEstimatorImpl::<StringType>::create()
89+
DataType::Number(NumberDataType::UInt16) => {
90+
ColumnNDVEstimator::UInt16(ColumnNDVEstimatorImpl::<UInt16Type>::new())
4991
}
50-
DataType::Date => {
51-
ColumnNDVEstimatorImpl::<DateType>::create()
92+
DataType::Number(NumberDataType::UInt32) => {
93+
ColumnNDVEstimator::UInt32(ColumnNDVEstimatorImpl::<UInt32Type>::new())
5294
}
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()),
53106
DataType::Timestamp => {
54-
ColumnNDVEstimatorImpl::<TimestampType>::create()
107+
ColumnNDVEstimator::Timestamp(ColumnNDVEstimatorImpl::<TimestampType>::new())
55108
}
56109
DataType::Decimal(size) => {
57110
if size.can_carried_by_64() {
58-
ColumnNDVEstimatorImpl::<Decimal64Type>::create()
111+
ColumnNDVEstimator::Decimal64(ColumnNDVEstimatorImpl::<Decimal64Type>::new())
59112
} else if size.can_carried_by_128() {
60-
ColumnNDVEstimatorImpl::<Decimal128Type>::create()
113+
ColumnNDVEstimator::Decimal128(ColumnNDVEstimatorImpl::<Decimal128Type>::new())
61114
} else {
62-
ColumnNDVEstimatorImpl::<Decimal256Type>::create()
115+
ColumnNDVEstimator::Decimal256(ColumnNDVEstimatorImpl::<Decimal256Type>::new())
63116
}
64117
}
65118
_ => unreachable!("Unsupported data type: {:?}", data_type),
66-
})
119+
}
67120
}
68121

69122
pub struct ColumnNDVEstimatorImpl<T>
@@ -80,15 +133,15 @@ where
80133
T: ValueType + Send + Sync,
81134
for<'a> T::ScalarRef<'a>: Hash,
82135
{
83-
pub fn create() -> Box<dyn ColumnNDVEstimator> {
84-
Box::new(Self {
136+
pub fn new() -> Self {
137+
Self {
85138
hll: ColumnDistinctHLL::new(),
86139
_phantom: Default::default(),
87-
})
140+
}
88141
}
89142
}
90143

91-
impl<T> ColumnNDVEstimator for ColumnNDVEstimatorImpl<T>
144+
impl<T> ColumnNDVEstimatorOps for ColumnNDVEstimatorImpl<T>
92145
where
93146
T: ValueType + Send + Sync,
94147
for<'a> T::ScalarRef<'a>: Hash,

src/query/storages/fuse/src/io/write/stream/column_statistics_state.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ use databend_storages_common_table_meta::meta::StatisticsOfColumns;
2525
use crate::io::write::stream::create_column_ndv_estimator;
2626
use crate::io::write::stream::create_column_stats_builder;
2727
use crate::io::write::stream::ColumnNDVEstimator;
28+
use crate::io::write::stream::ColumnNDVEstimatorOps;
2829
use crate::io::write::stream::ColumnStatisticsBuilder;
2930
use crate::io::write::stream::ColumnStatsOps;
3031
use crate::statistics::traverse_values_dfs;
3132

3233
pub struct ColumnStatisticsState {
3334
col_stats: HashMap<ColumnId, ColumnStatisticsBuilder>,
34-
distinct_columns: HashMap<ColumnId, Box<dyn ColumnNDVEstimator>>,
35+
distinct_columns: HashMap<ColumnId, ColumnNDVEstimator>,
3536
}
3637

3738
impl ColumnStatisticsState {

src/query/storages/fuse/src/io/write/stream/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub(crate) use block_builder::StreamBlockBuilder;
2222
pub(crate) use block_builder::StreamBlockProperties;
2323
pub(crate) use column_ndv_estimator::create_column_ndv_estimator;
2424
pub(crate) use column_ndv_estimator::ColumnNDVEstimator;
25+
pub(crate) use column_ndv_estimator::ColumnNDVEstimatorOps;
2526
pub(crate) use column_statistics_builder::create_column_stats_builder;
2627
pub(crate) use column_statistics_builder::ColumnStatisticsBuilder;
2728
pub(crate) use column_statistics_builder::ColumnStatsOps;

0 commit comments

Comments
 (0)