Coverage Report

Created: 2026-07-24 16:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/format_v2/column_mapper.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 <cstddef>
21
#include <cstdint>
22
#include <memory>
23
#include <optional>
24
#include <string>
25
#include <utility>
26
#include <vector>
27
28
#include "common/status.h"
29
#include "core/data_type/data_type.h"
30
#include "core/field.h"
31
#include "exprs/vexpr_fwd.h"
32
#include "format_v2/file_reader.h"
33
34
namespace doris {
35
class RuntimeState;
36
} // namespace doris
37
38
namespace doris::format {
39
40
struct ColumnDefinition;
41
struct TableFilter;
42
43
enum class TableColumnMappingMode {
44
    // Match by ColumnDefinition::identifier TYPE_INT as field id.
45
    BY_FIELD_ID,
46
    // Match by ColumnDefinition::identifier TYPE_STRING, or logical name when identifier is null.
47
    BY_NAME,
48
    // Match top-level columns by file position. This mainly serves Hive1 ORC style files whose
49
    // column names are placeholder values such as `_col0` / `_col1`, where position is the only
50
    // reliable way to select the correct column.
51
    BY_INDEX,
52
};
53
54
enum TableVirtualColumnType {
55
    INVALID = 0, // not a virtual column
56
    // Iceberg v3 row lineage metadata column `_row_id`. Physical non-null values
57
    // are preserved; NULL or missing values inherit first_row_id + row_position.
58
    ROW_ID = 1,
59
    // Iceberg v3 row lineage metadata column `_last_updated_sequence_number`.
60
    // Physical non-null values are preserved; NULL or missing values inherit the
61
    // data file's last_updated_sequence_number.
62
    LAST_UPDATED_SEQUENCE_NUMBER = 2,
63
    // Doris internal Iceberg row locator column `__DORIS_ICEBERG_ROWID_COL__`.
64
    // It is a struct used by delete/update/merge, not the Iceberg `_row_id`.
65
    ICEBERG_ROWID = 3,
66
};
67
68
enum class FilterConversionType {
69
    COPY_DIRECTLY, // filter can be copied directly from file layer without any change, e.g. column type and table type are the same and no complex nested projection is involved.
70
    CAST_FILTER, // filter can be converted from file layer by adding a cast, e.g. column type is nullable but table type is not, or file column has a trivial nested projection but table column has a complex nested projection.
71
    READER_EXPRESSION,
72
    FINALIZE_ONLY, // filter cannot be converted to file layer and should be evaluated at table reader finalize phase, e.g. predicates on ICEBERG_ROW_ID column which is generated by IcebergReader.
73
    CONSTANT,
74
};
75
76
// Nested global-to-local child mapping. The root index points either to a request-local slot or to
77
// a child id, depending on the owner. child_mapping keeps the recursive table-child to file-child
78
// relationship explicit instead of encoding it in ColumnMapping flags.
79
struct IndexMapping {
80
    int32_t index = -1;
81
    std::map<int32_t, std::shared_ptr<IndexMapping>> child_mapping;
82
};
83
84
// Recursive result produced after one table/global column is assigned to a file-local source.
85
struct ColumnMapResult {
86
    std::optional<LocalColumnId> local_column_id;
87
    std::optional<LocalColumnIndex> column_index;
88
    std::optional<IndexMapping> mapping;
89
};
90
91
// Final mapping entry from one global result column to one file-local source.
92
struct ColumnMapEntry {
93
    IndexMapping mapping;
94
    DataTypePtr local_type;
95
    DataTypePtr global_type;
96
    FilterConversionType filter_conversion = FilterConversionType::FINALIZE_ONLY;
97
};
98
99
// Collection of final result-column mappings produced for one file/split.
100
struct ResultColumnMapping {
101
    std::map<GlobalIndex, ColumnMapEntry> global_to_local;
102
};
103
104
// Mapping result from one table column to one file column.
105
// This is the main boundary object between table-level schema semantics and file-local schema
106
// semantics.
107
struct ColumnMapping {
108
    // Position of the top-level projected column in the table/global output block. Table-level
109
    // filters and column predicates refer to this index after FileScannerV2 translates FE ids at
110
    // the scanner boundary.
111
    GlobalIndex global_index;
112
    std::string table_column_name;
113
    // File-reader local id for the mapped node.
114
    //
115
    // For a root mapping it is convertible to LocalColumnId. For a nested mapping it is the
116
    // LocalColumnIndex child id under the parent projection. This is deliberately separated from
117
    // ColumnDefinition::identifier, which is the table-to-file matching key such as Parquet/Iceberg
118
    // field_id or column name.
119
    //
120
    // Empty means the table column is constant, missing, partition-only, or virtual.
121
    std::optional<int32_t> file_local_id;
122
    std::string file_column_name;
123
    // Full file type/children before nested projection pruning. Used to rebuild projected types
124
    // and to localize nested filters that reference children not present in the output projection.
125
    DataTypePtr original_file_type;
126
    std::vector<ColumnDefinition> original_file_children;
127
    // File children after applying the scan projection. The order follows the file-local semantic
128
    // schema, not table child order. TableReader uses this to map table-output children back to the
129
    // file-local block layout when projection, predicate-only children, and schema evolution mix.
130
    std::vector<ColumnDefinition> projected_file_children;
131
    // Split/file-local constant entry when this mapping is produced from partition/default/virtual
132
    // expression instead of physical file data.
133
    std::optional<ConstantIndex> constant_index;
134
    // Effective file type after applying casts/remaps/nested projection pruning.
135
    DataTypePtr file_type;
136
    // Target table/global type after final materialization.
137
    DataTypePtr table_type;
138
139
    // Final projection expression used to convert file-local values into table/global values, such
140
    // as casts, defaults, partition values, generated columns, or complex-column remaps.
141
    VExprContextSPtr projection;
142
143
    // Mapping tree for nested table children. The order follows table output children, while file
144
    // children can be pruned/reordered through each child mapping's file-reader local id.
145
    std::vector<ColumnMapping> child_mappings;
146
    // True when file value can be used directly as table value without cast or child remap.
147
    bool is_trivial = false;
148
    // How filters referencing this table/global column can be converted below table-reader
149
    // finalize. This is metadata for localize_filters() and future constant-filter evaluation.
150
    FilterConversionType filter_conversion = FilterConversionType::FINALIZE_ONLY;
151
    TableVirtualColumnType virtual_column_type = TableVirtualColumnType::INVALID;
152
    VExprContextSPtr default_expr;
153
    // One-row constant owns variable-width payloads; Field<TYPE_VARBINARY> is only a borrowed
154
    // StringView and cannot safely outlive the Base64 decode buffer used to construct it.
155
    ColumnPtr initial_default_column;
156
157
    std::string debug_string() const;
158
};
159
160
struct TableColumnMapperOptions {
161
    TableColumnMappingMode mode = TableColumnMappingMode::BY_FIELD_ID;
162
    bool allow_idless_complex_wrapper_projection = false;
163
    bool enable_row_lineage_virtual_columns = false;
164
165
    std::string debug_string() const;
166
};
167
168
Status clone_table_expr_tree(const VExprSPtr& expr, VExprSPtr* cloned_expr);
169
const Field* find_partition_value(const ColumnDefinition& table_column,
170
                                  const std::map<std::string, Field>& partition_values);
171
// Apply the same case-insensitive logical name, string identifier, and bidirectional alias rules
172
// used by TableColumnMapper's BY_NAME mode.
173
const ColumnDefinition* find_column_by_name(const ColumnDefinition& table_column,
174
                                            const std::vector<ColumnDefinition>& file_schema);
175
176
// Generic mapping layer from table schema to file schema.
177
// Iceberg uses BY_FIELD_ID. Plain by-name formats can reuse this component as well, so keep this
178
// abstraction table-format neutral instead of making it Iceberg-only.
179
class TableColumnMapper {
180
public:
181
    explicit TableColumnMapper(TableColumnMapperOptions options = {})
182
240
            : _options(std::move(options)) {}
183
240
    virtual ~TableColumnMapper() = default;
184
185
    // Build column mappings from table schema to file schema.
186
    // The resulting ColumnMapping describes how each table column is produced from a file column,
187
    // a constant, or an expression. Later projection, filter localization, and table-block
188
    // finalization should all reuse the same mapping.
189
    virtual Status create_mapping(const std::vector<ColumnDefinition>& projected_columns,
190
                                  const std::map<std::string, Field>& partition_values,
191
                                  const std::vector<ColumnDefinition>& file_schema);
192
193
    // Convert a table-level scan request into a file-local scan request. table_filters preserve
194
    // row-level filtering semantics and are rewritten as file-local conjuncts. File-layer pruning
195
    // such as ZoneMap, dictionary, and bloom filter derives from those localized VExpr conjuncts.
196
    virtual Status create_scan_request(const std::vector<TableFilter>& table_filters,
197
                                       const std::vector<ColumnDefinition>& projected_columns,
198
                                       FileScanRequest* file_request,
199
                                       RuntimeState* runtime_state = nullptr);
200
201
    // Localize table-level filters to the file schema.
202
    // Trivial mappings can copy structured predicates directly. Type changes may be localized with
203
    // a safe cast. Expressions that cannot be pushed down safely should be handled by the
204
    // table-level finalize/filter fallback.
205
    virtual Status localize_filters(const std::vector<TableFilter>& table_filters,
206
                                    FileScanRequest* file_request,
207
                                    RuntimeState* runtime_state = nullptr);
208
358
    void clear() {
209
358
        _mappings.clear();
210
358
        _hidden_mappings.clear();
211
358
        _constant_map.clear();
212
358
        _filter_entries.clear();
213
358
        _file_schema.clear();
214
358
        _partition_values.clear();
215
358
    }
216
1.16k
    const std::vector<ColumnMapping>& mappings() const { return _mappings; }
217
47
    const std::map<GlobalIndex, FilterEntry>& filter_entries() const { return _filter_entries; }
218
19
    const ConstantMap& constant_map() const { return _constant_map; }
219
    std::string debug_string() const;
220
221
protected:
222
    // Columnar readers such as Parquet can read predicate columns first, evaluate row filters, and
223
    // lazily read the rest. Row-oriented readers such as CSV/Text materialize one row at a time and
224
    // should keep all required columns in one scan list.
225
160
    virtual bool enable_lazy_materialization() const { return true; }
226
    // Row-oriented readers such as CSV/Text cannot physically read only a nested child from one
227
    // delimited text field. They must scan the whole complex top-level field and let TableReader
228
    // rematerialize the requested table child after row-level filters have run.
229
219
    virtual bool force_full_complex_scan_projection() const { return false; }
230
231
    const ColumnDefinition* _find_file_field(
232
            const ColumnDefinition& table_column,
233
            const std::vector<ColumnDefinition>& file_schema) const;
234
    Status _create_direct_mapping(const ColumnDefinition& table_column,
235
                                  const ColumnDefinition& file_field, ColumnMapping* mapping) const;
236
237
    Status _create_by_index_mapping(const ColumnDefinition& table_column,
238
                                    const std::vector<ColumnDefinition>& file_schema,
239
                                    ColumnMapping* mapping);
240
    Status _build_filter_entries(const FileScanRequest& file_request);
241
    Status _build_result_column_mapping(const FileScanRequest& file_request);
242
243
    void _set_constant_mapping(ColumnMapping* mapping, VExprContextSPtr expr);
244
    Status _create_mapping_for_column(const ColumnDefinition& table_column,
245
                                      GlobalIndex global_index, ColumnMapping* mapping);
246
    Status _create_hidden_filter_mapping(const ColumnDefinition& table_column,
247
                                         GlobalIndex global_index, ColumnMapping* mapping);
248
    Status _build_hidden_filter_mappings(const std::vector<TableFilter>& table_filters);
249
    std::vector<ColumnMapping> _filter_visible_mappings() const;
250
251
    ColumnMapping* _find_mapping(GlobalIndex global_index);
252
    ColumnMapping* _find_filter_mapping(GlobalIndex global_index);
253
254
    TableColumnMapperOptions _options;
255
    // Column mapping for each projected column, in the same order as projected_columns. Each entry
256
    // describes how to get one table/global column from file-local sources, and carries metadata
257
    // for filter localization and result finalize.
258
    std::vector<ColumnMapping> _mappings;
259
    // Predicate-only top-level columns are not output projection columns, so keep their mappings
260
    // here. They are visible only to filter localization and file-reader predicate construction.
261
    std::vector<ColumnMapping> _hidden_mappings;
262
    std::map<GlobalIndex, FilterEntry> _filter_entries;
263
    ConstantMap _constant_map;
264
    // Split-local schema state retained from create_mapping() so create_scan_request() can build
265
    // hidden mappings for top-level filter slots that are absent from projected_columns.
266
    std::vector<ColumnDefinition> _file_schema;
267
    std::map<std::string, Field> _partition_values;
268
};
269
270
// Parquet consumes the full FileScanRequest shape: predicate columns for lazy materialization and
271
// file-local conjuncts for ZoneMap, dictionary, and bloom-filter pruning.
272
class ParquetColumnMapper final : public TableColumnMapper {
273
public:
274
    using TableColumnMapper::TableColumnMapper;
275
};
276
277
// Mapper for readers that always materialize every required file column before filtering. The
278
// table-to-file schema mapping is still generic, but the FileScanRequest layout is simpler:
279
// predicate_columns are not populated.
280
class MaterializedColumnMapper final : public TableColumnMapper {
281
public:
282
    using TableColumnMapper::TableColumnMapper;
283
284
protected:
285
4
    bool enable_lazy_materialization() const override { return false; }
286
7
    bool force_full_complex_scan_projection() const override { return true; }
287
};
288
289
} // namespace doris::format