|
| 1 | +// Copyright 2021 Datafuse Labs |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use std::sync::Arc; |
| 16 | + |
| 17 | +use databend_common_exception::Result; |
| 18 | +use databend_common_expression::row::RowConverter as CommonConverter; |
| 19 | +use databend_common_expression::types::AccessType; |
| 20 | +use databend_common_expression::types::ArgType; |
| 21 | +use databend_common_expression::types::DataType; |
| 22 | +use databend_common_expression::types::DateType; |
| 23 | +use databend_common_expression::types::NumberDataType; |
| 24 | +use databend_common_expression::types::NumberType; |
| 25 | +use databend_common_expression::types::StringType; |
| 26 | +use databend_common_expression::types::TimestampType; |
| 27 | +use databend_common_expression::with_number_mapped_type; |
| 28 | +use databend_common_expression::DataSchemaRef; |
| 29 | +use databend_common_expression::SortColumnDescription; |
| 30 | +use databend_common_pipeline_core::processors::InputPort; |
| 31 | +use databend_common_pipeline_core::processors::OutputPort; |
| 32 | +use databend_common_pipeline_core::processors::Processor; |
| 33 | +use databend_common_pipeline_transforms::sort::CommonRows; |
| 34 | +use databend_common_pipeline_transforms::sort::RowConverter; |
| 35 | +use databend_common_pipeline_transforms::sort::Rows; |
| 36 | +use databend_common_pipeline_transforms::sort::SimpleRowConverter; |
| 37 | +use databend_common_pipeline_transforms::sort::SimpleRowsAsc; |
| 38 | +use databend_common_pipeline_transforms::AccumulatingTransformer; |
| 39 | +use databend_common_pipeline_transforms::Transformer; |
| 40 | +use match_template::match_template; |
| 41 | + |
| 42 | +use crate::pipelines::processors::transforms::recluster::transform_add_order_column::TransformAddOrderColumn; |
| 43 | +use crate::pipelines::processors::transforms::recluster::TransformRangePartitionIndexer; |
| 44 | +use crate::pipelines::processors::transforms::SampleState; |
| 45 | +use crate::pipelines::processors::transforms::TransformReclusterCollect; |
| 46 | + |
| 47 | +pub struct TransformReclusterBuilder { |
| 48 | + schema: DataSchemaRef, |
| 49 | + sort_desc: Arc<[SortColumnDescription]>, |
| 50 | + sample_rate: f64, |
| 51 | + seed: u64, |
| 52 | +} |
| 53 | + |
| 54 | +impl TransformReclusterBuilder { |
| 55 | + pub fn build_recluster_sample( |
| 56 | + &self, |
| 57 | + input: Arc<InputPort>, |
| 58 | + output: Arc<OutputPort>, |
| 59 | + ) -> Result<Box<dyn Processor>> { |
| 60 | + self.build_inner(BuilderType::ReclusterSample, input, output, None) |
| 61 | + } |
| 62 | + |
| 63 | + pub fn build_range_partition_indexer( |
| 64 | + &self, |
| 65 | + input: Arc<InputPort>, |
| 66 | + output: Arc<OutputPort>, |
| 67 | + state: Arc<SampleState>, |
| 68 | + ) -> Result<Box<dyn Processor>> { |
| 69 | + self.build_inner( |
| 70 | + BuilderType::RangePartitionIndexer, |
| 71 | + input, |
| 72 | + output, |
| 73 | + Some(state), |
| 74 | + ) |
| 75 | + } |
| 76 | + |
| 77 | + pub fn build_add_order_column( |
| 78 | + &self, |
| 79 | + input: Arc<InputPort>, |
| 80 | + output: Arc<OutputPort>, |
| 81 | + ) -> Result<Box<dyn Processor>> { |
| 82 | + self.build_inner(BuilderType::AddOrderColumn, input, output, None) |
| 83 | + } |
| 84 | + |
| 85 | + fn build_inner( |
| 86 | + &self, |
| 87 | + typ: BuilderType, |
| 88 | + input: Arc<InputPort>, |
| 89 | + output: Arc<OutputPort>, |
| 90 | + state: Option<Arc<SampleState>>, |
| 91 | + ) -> Result<Box<dyn Processor>> { |
| 92 | + let mut build = BuilderInner { |
| 93 | + input, |
| 94 | + output, |
| 95 | + typ, |
| 96 | + base: self, |
| 97 | + state, |
| 98 | + }; |
| 99 | + build.select_row_type() |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +enum BuilderType { |
| 104 | + AddOrderColumn, |
| 105 | + ReclusterSample, |
| 106 | + RangePartitionIndexer, |
| 107 | +} |
| 108 | + |
| 109 | +struct BuilderInner<'a> { |
| 110 | + input: Arc<InputPort>, |
| 111 | + output: Arc<OutputPort>, |
| 112 | + typ: BuilderType, |
| 113 | + base: &'a TransformReclusterBuilder, |
| 114 | + state: Option<Arc<SampleState>>, |
| 115 | +} |
| 116 | + |
| 117 | +impl BuilderInner<'_> { |
| 118 | + pub fn select_row_type(&mut self) -> Result<Box<dyn Processor>> { |
| 119 | + match self.base.sort_desc.as_ref() { |
| 120 | + [desc] => { |
| 121 | + let schema = self.base.schema.clone(); |
| 122 | + let sort_type = schema.field(desc.offset).data_type(); |
| 123 | + assert!(desc.asc); |
| 124 | + |
| 125 | + match_template! { |
| 126 | + T = [ Date => DateType, Timestamp => TimestampType, String => StringType ], |
| 127 | + match sort_type { |
| 128 | + DataType::T => { |
| 129 | + self.visit_type::<SimpleRowsAsc<T>, SimpleRowConverter<T>>() |
| 130 | + }, |
| 131 | + DataType::Number(num_ty) => with_number_mapped_type!(|NUM_TYPE| match num_ty { |
| 132 | + NumberDataType::NUM_TYPE => { |
| 133 | + self.visit_type::<SimpleRowsAsc<NumberType<NUM_TYPE>>, SimpleRowConverter<NumberType<NUM_TYPE>>>() |
| 134 | + } |
| 135 | + }), |
| 136 | + _ => self.visit_type::<CommonRows, CommonConverter>() |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + _ => self.visit_type::<CommonRows, CommonConverter>(), |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + fn visit_type<R, C>(&mut self) -> Result<Box<dyn Processor>> |
| 145 | + where |
| 146 | + R: Rows + 'static, |
| 147 | + C: RowConverter<R> + Send + 'static, |
| 148 | + R::Type: ArgType + Send + Sync, |
| 149 | + <R::Type as AccessType>::Scalar: Ord + Send + Sync, |
| 150 | + { |
| 151 | + match self.typ { |
| 152 | + BuilderType::AddOrderColumn => self.build_add_order_column::<R, C>(), |
| 153 | + BuilderType::ReclusterSample => self.build_recluster_sample::<R::Type>(), |
| 154 | + BuilderType::RangePartitionIndexer => self.build_range_partition_indexer::<R::Type>(), |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + fn build_add_order_column<R, C>(&mut self) -> Result<Box<dyn Processor>> |
| 159 | + where |
| 160 | + R: Rows + 'static, |
| 161 | + C: RowConverter<R> + Send + 'static, |
| 162 | + { |
| 163 | + let inner = TransformAddOrderColumn::<R, C>::try_new( |
| 164 | + self.base.sort_desc.clone(), |
| 165 | + self.base.schema.clone(), |
| 166 | + )?; |
| 167 | + Ok(Transformer::create( |
| 168 | + self.input.clone(), |
| 169 | + self.output.clone(), |
| 170 | + inner, |
| 171 | + )) |
| 172 | + } |
| 173 | + |
| 174 | + fn build_range_partition_indexer<T>(&mut self) -> Result<Box<dyn Processor>> |
| 175 | + where |
| 176 | + T: ArgType + Send + Sync, |
| 177 | + T::Scalar: Ord + Send + Sync, |
| 178 | + { |
| 179 | + Ok(TransformRangePartitionIndexer::<T>::create( |
| 180 | + self.input.clone(), |
| 181 | + self.output.clone(), |
| 182 | + self.state.clone().unwrap(), |
| 183 | + )) |
| 184 | + } |
| 185 | + |
| 186 | + fn build_recluster_sample<T>(&mut self) -> Result<Box<dyn Processor>> |
| 187 | + where |
| 188 | + T: ArgType + Send + Sync, |
| 189 | + T::Scalar: Ord + Send + Sync, |
| 190 | + { |
| 191 | + let offset = self.base.schema.fields().len(); |
| 192 | + Ok(AccumulatingTransformer::create( |
| 193 | + self.input.clone(), |
| 194 | + self.output.clone(), |
| 195 | + TransformReclusterCollect::<T>::new(offset, self.base.sample_rate, self.base.seed), |
| 196 | + )) |
| 197 | + } |
| 198 | +} |
0 commit comments