Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #pragma once |
19 | | |
20 | | #include <gen_cpp/Descriptors_types.h> |
21 | | #include <parallel_hashmap/phmap.h> |
22 | | |
23 | | #include <cstddef> |
24 | | #include <cstdint> |
25 | | #include <map> |
26 | | #include <string> |
27 | | #include <string_view> |
28 | | #include <unordered_map> |
29 | | #include <vector> |
30 | | |
31 | | #include "common/status.h" |
32 | | #include "core/column/column.h" |
33 | | #include "core/data_type/data_type.h" |
34 | | #include "core/field.h" |
35 | | #include "core/string_ref.h" |
36 | | #include "core/types.h" |
37 | | #include "exprs/aggregate/aggregate_function.h" |
38 | | #include "storage/segment/variant/variant_compaction_paths.h" |
39 | | #include "storage/tablet/tablet_fwd.h" |
40 | | #include "storage/tablet/tablet_schema.h" |
41 | | |
42 | | namespace doris { |
43 | | class TabletSchema; |
44 | | enum class FieldType; |
45 | | namespace segment_v2 { |
46 | | struct VariantStatisticsPB; |
47 | | } // namespace segment_v2 |
48 | | class Block; |
49 | | class IColumn; |
50 | | using MutableColumnPtr = IColumn::MutablePtr; |
51 | | struct ColumnWithTypeAndName; |
52 | | enum class ExtractType; |
53 | | template <typename T> |
54 | | class ColumnStr; |
55 | | using ColumnString = ColumnStr<UInt32>; |
56 | | } // namespace doris |
57 | | |
58 | | const std::string SPARSE_COLUMN_PATH = "__DORIS_VARIANT_SPARSE__"; |
59 | | const std::string DOC_VALUE_COLUMN_PATH = "__DORIS_VARIANT_DOC_VALUE__"; |
60 | | namespace doris::variant_util { |
61 | | |
62 | | using PathToNoneNullValues = std::unordered_map<std::string, int64_t>; |
63 | | using PathToDataTypes = std::unordered_map<PathInData, std::vector<DataTypePtr>, PathInData::Hash>; |
64 | | |
65 | 96.8k | inline bool should_record_variant_path_stats(const TabletColumn& column) { |
66 | 96.8k | return !column.variant_enable_nested_group(); |
67 | 96.8k | } |
68 | | |
69 | 0 | inline bool should_write_variant_binary_columns(const TabletColumn& column) { |
70 | 0 | return !column.variant_enable_nested_group(); |
71 | 0 | } |
72 | | |
73 | 11.6k | inline bool should_check_variant_path_stats(const TabletColumn& column) { |
74 | 11.6k | return !column.variant_enable_nested_group(); |
75 | 11.6k | } |
76 | | |
77 | | struct VariantExtendedInfo { |
78 | | PathToNoneNullValues path_to_none_null_values; // key: path, value: number of none null values |
79 | | std::unordered_set<std::string> sparse_paths; // sparse paths in this variant column |
80 | | std::unordered_set<std::string> materialized_paths; // paths some input stores as a subcolumn |
81 | | std::unordered_set<std::string> typed_paths; // typed paths in this variant column |
82 | | std::unordered_set<PathInData, PathInData::Hash> |
83 | | nested_paths; // nested paths in this variant column |
84 | | PathToDataTypes path_to_data_types; // key: path, value: data types |
85 | | bool has_nested_group = false; // whether this variant column has nested group |
86 | | }; |
87 | | |
88 | | /// Returns number of dimensions in Array type. 0 if type is not array. |
89 | | size_t get_number_of_dimensions(const IDataType& type); |
90 | | |
91 | | /// Returns number of dimensions in Array column. 0 if column is not array. |
92 | | size_t get_number_of_dimensions(const IColumn& column); |
93 | | |
94 | | /// Returns type of scalars of Array of arbitrary dimensions. |
95 | | DataTypePtr get_base_type_of_array(const DataTypePtr& type); |
96 | | |
97 | | // Cast column to dst type |
98 | | Status cast_column(const ColumnWithTypeAndName& arg, const DataTypePtr& type, ColumnPtr* result); |
99 | | |
100 | | struct ExtraInfo { |
101 | | // -1 indicates it's not a Frontend generated column |
102 | | int32_t unique_id = -1; |
103 | | int32_t parent_unique_id = -1; |
104 | | PathInData path_info {}; |
105 | | }; |
106 | | |
107 | | TabletColumn get_column_by_type(const DataTypePtr& data_type, const std::string& name, |
108 | | const ExtraInfo& ext_info); |
109 | | |
110 | | // check if the tuple_paths has ambiguous paths |
111 | | // situation: |
112 | | // throw exception if there exists a prefix with matched names, but not matched structure (is Nested, number of dimensions). |
113 | | Status check_variant_has_no_ambiguous_paths(const std::vector<PathInData>& paths); |
114 | | |
115 | | // Pick the tablet schema with the highest schema version as the reference. |
116 | | // Then update all variant columns to there least common types. |
117 | | // Return the final merged schema as common schema. |
118 | | // If base_schema == nullptr then, max schema version tablet schema will be picked as base schema |
119 | | Status get_least_common_schema(const std::vector<TabletSchemaSPtr>& schemas, |
120 | | const TabletSchemaSPtr& base_schema, TabletSchemaSPtr& result, |
121 | | bool check_schema_size = false); |
122 | | |
123 | | // Get least common types for extracted columns which has Path info, |
124 | | // with a speicified variant column's unique id |
125 | | Status update_least_common_schema(const std::vector<TabletSchemaSPtr>& schemas, |
126 | | TabletSchemaSPtr& common_schema, int32_t variant_col_unique_id, |
127 | | std::set<PathInData>* path_set); |
128 | | |
129 | | // inherit attributes like index/agg info from it's parent column |
130 | | void inherit_column_attributes(TabletSchemaSPtr& schema); |
131 | | |
132 | | // source: variant column |
133 | | // target: extracted column from variant column |
134 | | void inherit_column_attributes(const TabletColumn& source, TabletColumn& target, |
135 | | TabletSchemaSPtr* target_schema = nullptr); |
136 | | |
137 | | // Align variant subcolumn BF inheritance with FE BF-supported types. |
138 | | bool is_bf_supported_by_fe_for_variant_subcolumn(FieldType type); |
139 | | |
140 | | bool has_schema_index_diff(const TabletSchema* new_schema, const TabletSchema* old_schema, |
141 | | int32_t new_col_idx, int32_t old_col_idx); |
142 | | |
143 | | // create engine-side ColumnMap<String, String> for variant sparse/doc storage payloads. |
144 | | MutableColumnPtr create_variant_binary_column(); |
145 | | |
146 | | // create TabletColumn Map<String, String> for sparse storage |
147 | | TabletColumn create_sparse_column(const TabletColumn& variant); |
148 | | |
149 | | // Create one bucket sparse column: name = variant.name_lower_case() + "." + SPARSE_COLUMN_PATH + ".b{index}" |
150 | | TabletColumn create_sparse_shard_column(const TabletColumn& variant, int bucket_index); |
151 | | |
152 | | // Physical doc bucket stored as MAP<STRING, STRING>. |
153 | | TabletColumn create_doc_value_column(const TabletColumn& variant, int bucket_index); |
154 | | |
155 | | // Decoded doc bucket used in compaction blocks, nullable with a SQL NULL default for old segments. |
156 | | TabletColumn create_doc_value_compaction_column(const TabletColumn& variant, int bucket_index); |
157 | | |
158 | | // Compute bucket id for given path string using SipHash64(path) % bucket_num. |
159 | | uint32_t variant_binary_shard_of(const StringRef& path, uint32_t bucket_num); |
160 | | |
161 | | void get_field_info(const Field& field, FieldInfo* info); |
162 | | |
163 | | // inherit index from parent column |
164 | | bool inherit_index(const std::vector<const TabletIndex*>& parent_indexes, |
165 | | TabletIndexes& sub_column_indexes, FieldType column_type, |
166 | | const std::string& suffix_path, bool is_array_nested_type = false); |
167 | | |
168 | | bool inherit_index(const std::vector<const TabletIndex*>& parent_indexes, |
169 | | TabletIndexes& sub_column_indexes, const TabletColumn& column); |
170 | | |
171 | | bool inherit_index(const std::vector<const TabletIndex*>& parent_indexes, |
172 | | TabletIndexes& sub_column_indexes, const segment_v2::ColumnMetaPB& column_pb); |
173 | | |
174 | | Status update_least_schema_internal(const std::map<PathInData, DataTypes>& subcolumns_types, |
175 | | TabletSchemaSPtr& common_schema, int32_t variant_col_unique_id, |
176 | | const std::map<std::string, TabletColumnPtr>& typed_columns, |
177 | | std::set<PathInData>* path_set = nullptr); |
178 | | |
179 | | bool generate_sub_column_info(const TabletSchema& schema, int32_t col_unique_id, |
180 | | const std::string& path, |
181 | | TabletSchema::SubColumnInfo* sub_column_info); |
182 | | |
183 | | class VariantCompactionUtil { |
184 | | public: |
185 | | // get the subpaths and sparse paths for the variant column |
186 | | static void get_subpaths(int32_t max_subcolumns_count, const PathToNoneNullValues& path_stats, |
187 | | VariantCompactionPaths& paths_set_info); |
188 | | |
189 | | // collect extended info from the variant column |
190 | | static Status aggregate_variant_extended_info( |
191 | | const RowsetSharedPtr& rs, |
192 | | std::unordered_map<int32_t, VariantExtendedInfo>* uid_to_variant_extended_info); |
193 | | |
194 | | // collect path stats from the variant column |
195 | | static Status aggregate_path_to_stats( |
196 | | const RowsetSharedPtr& rs, |
197 | | std::unordered_map<int32_t, PathToNoneNullValues>* uid_to_path_stats); |
198 | | |
199 | | // Build the temporary schema for compaction, this will reduce the memory usage of compacting |
200 | | // variant columns. `paths` receives that schema's variant path layout. |
201 | | static Status get_extended_compaction_schema(const std::vector<RowsetSharedPtr>& rowsets, |
202 | | TabletSchemaSPtr& target, |
203 | | VariantCompactionPathsMap& paths); |
204 | | |
205 | | // Used to collect all the subcolumns types of variant column from rowsets |
206 | | static TabletSchemaSPtr calculate_variant_extended_schema( |
207 | | const std::vector<RowsetSharedPtr>& rowsets, const TabletSchemaSPtr& base_schema); |
208 | | |
209 | | // Check if the path stats are consistent between inputs rowsets and output rowset. |
210 | | // Used to check the correctness of compaction. |
211 | | static Status check_path_stats(const std::vector<RowsetSharedPtr>& intputs, |
212 | | RowsetSharedPtr output, BaseTabletSPtr tablet); |
213 | | |
214 | | // Calculate statistics about variant data paths from the encoded sparse column |
215 | | static void calculate_variant_stats(const IColumn& encoded_sparse_column, |
216 | | segment_v2::VariantStatisticsPB* stats, |
217 | | size_t max_sparse_column_statistics_size, size_t row_pos, |
218 | | size_t num_rows); |
219 | | |
220 | | static void get_compaction_subcolumns_from_subpaths( |
221 | | VariantCompactionPaths& paths_set_info, const TabletColumnPtr parent_column, |
222 | | const TabletSchemaSPtr& target, const PathToDataTypes& path_to_data_types, |
223 | | const std::unordered_set<std::string>& sparse_paths, TabletSchemaSPtr& output_schema); |
224 | | |
225 | | static void get_compaction_subcolumns_from_data_types(VariantCompactionPaths& paths_set_info, |
226 | | const TabletColumnPtr parent_column, |
227 | | const TabletSchemaSPtr& target, |
228 | | const PathToDataTypes& path_to_data_types, |
229 | | TabletSchemaSPtr& output_schema); |
230 | | |
231 | | static Status get_compaction_typed_columns(const TabletSchemaSPtr& target, |
232 | | const std::unordered_set<std::string>& typed_paths, |
233 | | const TabletColumnPtr parent_column, |
234 | | TabletSchemaSPtr& output_schema, |
235 | | VariantCompactionPaths& paths_set_info); |
236 | | |
237 | | static Status get_compaction_nested_columns( |
238 | | const std::unordered_set<PathInData, PathInData::Hash>& nested_paths, |
239 | | const PathToDataTypes& path_to_data_types, const TabletColumnPtr parent_column, |
240 | | TabletSchemaSPtr& output_schema, VariantCompactionPaths& paths_set_info); |
241 | | }; |
242 | | |
243 | | } // namespace doris::variant_util |