be/src/format_v2/column_data.h
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 <algorithm> |
21 | | #include <cstddef> |
22 | | #include <cstdint> |
23 | | #include <memory> |
24 | | #include <ostream> |
25 | | #include <string> |
26 | | #include <utility> |
27 | | #include <vector> |
28 | | |
29 | | #include "common/consts.h" |
30 | | #include "common/status.h" |
31 | | #include "core/data_type/data_type.h" |
32 | | #include "core/data_type/data_type_number.h" |
33 | | #include "core/data_type/data_type_string.h" |
34 | | #include "core/field.h" |
35 | | #include "exprs/vexpr_fwd.h" |
36 | | |
37 | | namespace doris::format { |
38 | | |
39 | | // File-local top-level column id. |
40 | | // |
41 | | // Scope: |
42 | | // - Only valid inside one physical file schema returned by FileReader::get_schema(). |
43 | | // - For Parquet, this is the top-level field ordinal in the new reader schema. |
44 | | // - The synthetic row-position column also uses this type, with a reserved negative id. |
45 | | // |
46 | | // Do not use this for table/global column unique ids, block positions, nested child ids, or |
47 | | // slot ids. Nested child ids are carried by LocalColumnIndex::index below. |
48 | | class LocalColumnId { |
49 | | public: |
50 | 900 | constexpr LocalColumnId() = default; |
51 | 4.53k | explicit constexpr LocalColumnId(int32_t id) : _id(id) {} |
52 | | |
53 | 900 | static constexpr LocalColumnId invalid() { return LocalColumnId(); } |
54 | | |
55 | 1.90k | constexpr int32_t value() const { return _id; } |
56 | 368 | constexpr bool is_valid() const { return _id >= 0; } |
57 | | |
58 | 1.41k | constexpr bool operator==(const LocalColumnId& other) const { return _id == other._id; } |
59 | 390 | constexpr bool operator!=(const LocalColumnId& other) const { return !(*this == other); } |
60 | 5.20k | constexpr bool operator<(const LocalColumnId& other) const { return _id < other._id; } |
61 | | |
62 | | private: |
63 | | int32_t _id = -1; |
64 | | }; |
65 | | |
66 | | // Position of a file-local column in the Block produced by one FileScanRequest. |
67 | | // |
68 | | // This is assigned by TableColumnMapper/TableReader after predicate/non-predicate columns are |
69 | | // deduplicated. It is not a file schema id and it is not stable across requests. Use value() only |
70 | | // at the boundary where an existing Block or expression API still expects a size_t/int position. |
71 | | class LocalIndex { |
72 | | public: |
73 | 320 | constexpr LocalIndex() = default; |
74 | 998 | explicit constexpr LocalIndex(size_t index) : _index(index) {} |
75 | | |
76 | 3.14k | constexpr size_t value() const { return _index; } |
77 | 18 | constexpr bool operator==(const LocalIndex& other) const { return _index == other._index; } |
78 | 0 | constexpr bool operator<(const LocalIndex& other) const { return _index < other._index; } |
79 | | |
80 | | private: |
81 | | size_t _index = 0; |
82 | | }; |
83 | | |
84 | | // Position of a table/global output column in the final Block returned by TableReader. |
85 | | // |
86 | | // This type is reserved for boundaries that need to refer to caller-visible column order. It must |
87 | | // not be used to index a file-local Block, because schema evolution and lazy materialization can |
88 | | // make file-local order different from table output order. |
89 | | class GlobalIndex { |
90 | | public: |
91 | 1.86k | constexpr GlobalIndex() = default; |
92 | 1.52k | explicit constexpr GlobalIndex(size_t index) : _index(index) {} |
93 | | |
94 | 42 | constexpr size_t value() const { return _index; } |
95 | 1.34k | constexpr bool operator==(const GlobalIndex& other) const { return _index == other._index; } |
96 | 2.08k | constexpr bool operator<(const GlobalIndex& other) const { return _index < other._index; } |
97 | | |
98 | | private: |
99 | | size_t _index = 0; |
100 | | }; |
101 | | |
102 | | // Index of a split-local constant/default value used to materialize columns that are not read from |
103 | | // the physical file, such as partition columns, added columns with default values, and virtual |
104 | | // table-format columns. |
105 | | // |
106 | | // It is separate from LocalIndex because constants do not occupy a position in the file reader |
107 | | // output block unless an expression explicitly materializes them. |
108 | | class ConstantIndex { |
109 | | public: |
110 | | constexpr ConstantIndex() = default; |
111 | 54 | explicit constexpr ConstantIndex(size_t index) : _index(index) {} |
112 | | |
113 | 84 | constexpr size_t value() const { return _index; } |
114 | 6 | constexpr bool operator==(const ConstantIndex& other) const { return _index == other._index; } |
115 | 0 | constexpr bool operator<(const ConstantIndex& other) const { return _index < other._index; } |
116 | | |
117 | | private: |
118 | | size_t _index = 0; |
119 | | }; |
120 | | |
121 | 4 | inline std::ostream& operator<<(std::ostream& os, const LocalColumnId& id) { |
122 | 4 | return os << id.value(); |
123 | 4 | } |
124 | | |
125 | 0 | inline std::ostream& operator<<(std::ostream& os, const LocalIndex& index) { |
126 | 0 | return os << index.value(); |
127 | 0 | } |
128 | | |
129 | 24 | inline std::ostream& operator<<(std::ostream& os, const GlobalIndex& index) { |
130 | 24 | return os << index.value(); |
131 | 24 | } |
132 | | |
133 | 10 | inline std::ostream& operator<<(std::ostream& os, const ConstantIndex& index) { |
134 | 10 | return os << index.value(); |
135 | 10 | } |
136 | | |
137 | | // A split/file-local constant value used to materialize a table/global column without reading a |
138 | | // physical file column. |
139 | | // |
140 | | // Common producers are partition values, schema-evolution default expressions, generated columns |
141 | | // and table-format virtual columns. The entry is keyed by ConstantIndex in ConstantMap; global_index |
142 | | // keeps the link back to the caller-visible output column. |
143 | | struct ConstantEntry { |
144 | | GlobalIndex global_index; |
145 | | VExprContextSPtr expr; |
146 | | DataTypePtr type; |
147 | | }; |
148 | | |
149 | | // Per mapping/split collection of constants. |
150 | | // |
151 | | // ConstantIndex only has meaning within this container. Keeping constants separate from LocalIndex |
152 | | // makes it explicit that these values do not occupy positions in the file reader output Block. |
153 | | class ConstantMap { |
154 | | public: |
155 | 34 | ConstantIndex add(ConstantEntry entry) { |
156 | 34 | const auto index = ConstantIndex(_entries.size()); |
157 | 34 | _entries.push_back(std::move(entry)); |
158 | 34 | return index; |
159 | 34 | } |
160 | | |
161 | 20 | const ConstantEntry& get(ConstantIndex index) const { |
162 | 20 | DORIS_CHECK(index.value() < _entries.size()); |
163 | 20 | return _entries[index.value()]; |
164 | 20 | } |
165 | | |
166 | 452 | void clear() { _entries.clear(); } |
167 | 4 | bool empty() const { return _entries.empty(); } |
168 | 16 | size_t size() const { return _entries.size(); } |
169 | | |
170 | 0 | const std::vector<ConstantEntry>& entries() const { return _entries; } |
171 | | |
172 | | private: |
173 | | std::vector<ConstantEntry> _entries; |
174 | | }; |
175 | | |
176 | | // Target of a localized filter. |
177 | | // |
178 | | // A filter can either reference a file-local Block position or a constant entry. Unset entries mean |
179 | | // the filter cannot be evaluated below the table-reader finalize stage. |
180 | | struct FilterEntry { |
181 | | enum class Kind { |
182 | | UNSET, |
183 | | LOCAL, |
184 | | CONSTANT, |
185 | | }; |
186 | | |
187 | 276 | static FilterEntry local(LocalIndex index) { |
188 | 276 | return {.kind = Kind::LOCAL, .index = index.value()}; |
189 | 276 | } |
190 | | |
191 | 20 | static FilterEntry constant(ConstantIndex index) { |
192 | 20 | return {.kind = Kind::CONSTANT, .index = index.value()}; |
193 | 20 | } |
194 | | |
195 | 0 | bool is_set() const { return kind != Kind::UNSET; } |
196 | 790 | bool is_local() const { return kind == Kind::LOCAL; } |
197 | 68 | bool is_constant() const { return kind == Kind::CONSTANT; } |
198 | | |
199 | 286 | LocalIndex local_index() const { |
200 | 286 | DORIS_CHECK(is_local()); |
201 | 286 | return LocalIndex(index); |
202 | 286 | } |
203 | | |
204 | 10 | ConstantIndex constant_index() const { |
205 | 10 | DORIS_CHECK(is_constant()); |
206 | 10 | return ConstantIndex(index); |
207 | 10 | } |
208 | | |
209 | | Kind kind = Kind::UNSET; |
210 | | size_t index = 0; |
211 | | }; |
212 | | |
213 | | enum ColumnType { |
214 | | DATA_COLUMN = 0, // normal data column |
215 | | ROW_NUMBER = 1, // row number in a file |
216 | | GLOBAL_ROWID = 2, // global unique row id across files, used by TopN filter |
217 | | }; |
218 | | |
219 | | struct GlobalRowIdContext { |
220 | | uint8_t version = 0; |
221 | | int64_t backend_id = 0; |
222 | | uint32_t file_id = 0; |
223 | | }; |
224 | | |
225 | | // Column schema definition shared by table/global projection and file-local schema matching. |
226 | | // |
227 | | // ColumnDefinition intentionally carries schema identity only. FE column unique ids are translated |
228 | | // to GlobalIndex at the FileScannerV2 boundary and must not appear in table/file reader APIs. |
229 | | struct ColumnDefinition { |
230 | | // Typed identifier value used to match a column against another schema. |
231 | | // |
232 | | // - TYPE_NULL: no explicit identifier. BY_NAME falls back to ColumnDefinition::name. |
233 | | // - TYPE_INT: interpreted by TableColumnMapperOptions::mode as a field id or file position. |
234 | | // - TYPE_STRING: explicit name identifier. |
235 | | // |
236 | | // This is not the id that FileReader uses to read data. For example, a Parquet column can be |
237 | | // matched by its optional Parquet field_id, while the reader still addresses it by a file-local |
238 | | // ordinal. |
239 | | Field identifier; |
240 | | // Reader-local id of this node inside the file schema returned by FileReader::get_schema(). |
241 | | // Top-level fields use the root column ordinal and nested fields use the child ordinal under |
242 | | // their parent. -1 means unset; special virtual file columns may use other negative ids. |
243 | | // Table/global ColumnDefinition values can leave this as -1 because they are not read directly |
244 | | // by a FileReader. |
245 | | int32_t local_id = -1; |
246 | | // Logical table column name. This is also the matching name for by-name file formats. |
247 | | std::string name; |
248 | | // Historical or external names for the same logical field. Table formats such as Iceberg can |
249 | | // use this to resolve partition path keys after column rename. |
250 | | std::vector<std::string> name_mapping {}; |
251 | | DataTypePtr type; |
252 | | // Semantic nested children for this schema node. |
253 | | // |
254 | | // Table/global columns carry projected table children. File-local schemas returned by |
255 | | // FileReader::get_schema() also expose semantic children, not physical reader wrappers. For |
256 | | // example, MAP children are key/value and ARRAY children contain only the element field. |
257 | | std::vector<ColumnDefinition> children {}; |
258 | | // Expression used to materialize missing/default/generated values when the column is not read |
259 | | // directly from the file. |
260 | | VExprContextSPtr default_expr = nullptr; |
261 | | // Partition columns are constants from split metadata and should not be matched against file |
262 | | // schema unless table-format logic explicitly asks for it. |
263 | | bool is_partition_key = false; |
264 | | // File-local column kind. For table/global columns this remains DATA_COLUMN. |
265 | | ColumnType column_type = ColumnType::DATA_COLUMN; |
266 | | |
267 | 0 | bool has_identifier() const { return !identifier.is_null(); } |
268 | 1.90k | bool has_identifier_field_id() const { return identifier.get_type() == TYPE_INT; } |
269 | 1.90k | bool has_identifier_name() const { return identifier.get_type() == TYPE_STRING; } |
270 | | |
271 | | // DuckDB-style helper for BY_FIELD_ID matching. The mapper binds the matching mode once, so a |
272 | | // TYPE_INT identifier is interpreted as a field id only by the field-id matcher. |
273 | 846 | int32_t get_identifier_field_id() const { |
274 | 846 | DORIS_CHECK(has_identifier_field_id()); |
275 | 846 | return identifier.get<TYPE_INT>(); |
276 | 846 | } |
277 | | // DuckDB-style helper for BY_NAME matching. When no explicit string identifier is present, the |
278 | | // logical column name is the identifier. |
279 | 818 | const std::string& get_identifier_name() const { |
280 | 818 | if (identifier.is_null()) { |
281 | 0 | return name; |
282 | 0 | } |
283 | 818 | DORIS_CHECK(has_identifier_name()); |
284 | 818 | return identifier.get<TYPE_STRING>(); |
285 | 818 | } |
286 | | // Helper for BY_INDEX matching. BY_INDEX reuses the TYPE_INT identifier as the table-side file |
287 | | // position, matching DuckDB's typed identifier plus mapper-mode interpretation. |
288 | 44 | int32_t get_identifier_position() const { |
289 | 44 | DORIS_CHECK(has_identifier_field_id()); |
290 | 44 | return identifier.get<TYPE_INT>(); |
291 | 44 | } |
292 | | |
293 | | // Helper for reader-local projection and scan requests. |
294 | 2.35k | int32_t file_local_id() const { |
295 | 2.35k | if (local_id != -1) { |
296 | 2.35k | return local_id; |
297 | 2.35k | } |
298 | 0 | return get_identifier_field_id(); |
299 | 2.35k | } |
300 | | |
301 | | std::string debug_string() const; |
302 | | }; |
303 | | |
304 | | static constexpr int ROW_POSITION_COLUMN_ID = -10001; |
305 | | static constexpr const char* ROW_POSITION_COLUMN_NAME = "__file_row_position"; |
306 | | static constexpr int GLOBAL_ROWID_COLUMN_ID = -10002; |
307 | | |
308 | 60 | inline ColumnDefinition row_position_column_definition() { |
309 | 60 | ColumnDefinition field; |
310 | 60 | field.identifier = Field::create_field<TYPE_INT>(ROW_POSITION_COLUMN_ID); |
311 | 60 | field.local_id = ROW_POSITION_COLUMN_ID; |
312 | 60 | field.name = ROW_POSITION_COLUMN_NAME; |
313 | 60 | field.type = std::make_shared<DataTypeInt64>(); |
314 | 60 | field.column_type = ColumnType::ROW_NUMBER; |
315 | 60 | return field; |
316 | 60 | } |
317 | | |
318 | 12 | inline ColumnDefinition global_rowid_column_definition() { |
319 | 12 | ColumnDefinition field; |
320 | 12 | field.identifier = Field::create_field<TYPE_STRING>(BeConsts::GLOBAL_ROWID_COL); |
321 | 12 | field.local_id = GLOBAL_ROWID_COLUMN_ID; |
322 | 12 | field.name = BeConsts::GLOBAL_ROWID_COL; |
323 | 12 | field.type = std::make_shared<DataTypeString>(); |
324 | 12 | field.column_type = ColumnType::GLOBAL_ROWID; |
325 | 12 | return field; |
326 | 12 | } |
327 | | |
328 | | // Recursive file-local projection path. |
329 | | // |
330 | | // For a root entry in FileScanRequest::{predicate_columns, non_predicate_columns}, index is the |
331 | | // top-level file column id and column_id() is valid. For children, index is the file-local child id |
332 | | // under the parent node. This is the reader schema local id, not an Iceberg/Parquet field id, not a |
333 | | // table child id, and not a child output ordinal. |
334 | | // |
335 | | // project_all_children=true means the whole subtree under this node is needed. When false, children |
336 | | // lists the selected child paths. File readers can use this to avoid constructing readers for |
337 | | // unprojected nested children. |
338 | | struct LocalColumnIndex { |
339 | | int32_t index = -1; |
340 | | bool project_all_children = true; |
341 | | std::vector<LocalColumnIndex> children {}; |
342 | | |
343 | 546 | static LocalColumnIndex top_level(LocalColumnId column_id) { |
344 | 546 | return {.index = column_id.value()}; |
345 | 546 | } |
346 | | |
347 | 448 | static LocalColumnIndex local(int32_t local_id) { return {.index = local_id}; } |
348 | | |
349 | 350 | static LocalColumnIndex partial_local(int32_t local_id) { |
350 | 350 | return {.index = local_id, .project_all_children = false}; |
351 | 350 | } |
352 | | |
353 | 2.02k | LocalColumnId column_id() const { return LocalColumnId(index); } |
354 | 3.44k | int32_t local_id() const { return index; } |
355 | | std::string debug_string() const; |
356 | | }; |
357 | | |
358 | 648 | inline bool is_full_projection(const LocalColumnIndex* projection) { |
359 | 648 | return projection == nullptr || projection->project_all_children; |
360 | 648 | } |
361 | | |
362 | 834 | inline bool is_partial_projection(const LocalColumnIndex* projection) { |
363 | 834 | return projection != nullptr && !projection->project_all_children; |
364 | 834 | } |
365 | | |
366 | | inline const LocalColumnIndex* find_child_projection(const LocalColumnIndex* projection, |
367 | 460 | int32_t local_id) { |
368 | 460 | if (is_full_projection(projection)) { |
369 | 264 | return nullptr; |
370 | 264 | } |
371 | 196 | const auto child_it = std::find_if( |
372 | 196 | projection->children.begin(), projection->children.end(), |
373 | 196 | [&](const LocalColumnIndex& child) { return child.local_id() == local_id; }); |
374 | 196 | return child_it == projection->children.end() ? nullptr : &*child_it; |
375 | 460 | } |
376 | | |
377 | 184 | inline bool is_child_projected(const LocalColumnIndex* projection, int32_t local_id) { |
378 | 184 | return is_full_projection(projection) || find_child_projection(projection, local_id) != nullptr; |
379 | 184 | } |
380 | | |
381 | | // Merge two projection trees that point to the same file-local node. |
382 | | // |
383 | | // A full projection dominates a partial projection. Two partial projections are merged by child id |
384 | | // and recursively union their child paths. The caller must only merge projections for the same |
385 | | // root/child node. |
386 | 62 | inline Status merge_local_column_index(LocalColumnIndex* target, const LocalColumnIndex& source) { |
387 | 62 | DORIS_CHECK(target != nullptr); |
388 | 62 | DORIS_CHECK(target->index == source.index); |
389 | 62 | if (target->project_all_children) { |
390 | 24 | return Status::OK(); |
391 | 24 | } |
392 | 38 | if (source.project_all_children) { |
393 | 2 | target->project_all_children = true; |
394 | 2 | target->children.clear(); |
395 | 2 | return Status::OK(); |
396 | 2 | } |
397 | 38 | for (const auto& source_child : source.children) { |
398 | 38 | auto target_child_it = std::find_if( |
399 | 38 | target->children.begin(), target->children.end(), |
400 | 44 | [&](const LocalColumnIndex& child) { return child.index == source_child.index; }); |
401 | 38 | if (target_child_it == target->children.end()) { |
402 | 26 | target->children.push_back(source_child); |
403 | 26 | continue; |
404 | 26 | } |
405 | 12 | RETURN_IF_ERROR(merge_local_column_index(&*target_child_it, source_child)); |
406 | 12 | } |
407 | 36 | return Status::OK(); |
408 | 36 | } |
409 | | |
410 | | } // namespace doris::format |