Coverage Report

Created: 2026-07-08 17:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/scan/access_path_parser.cpp
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
#include "exec/scan/access_path_parser.h"
19
20
#include <fmt/format.h>
21
22
#include <algorithm>
23
#include <charconv>
24
#include <map>
25
#include <string>
26
#include <string_view>
27
#include <utility>
28
29
#include "common/cast_set.h"
30
#include "common/consts.h"
31
#include "core/assert_cast.h"
32
#include "core/data_type/data_type.h"
33
#include "core/data_type/data_type_array.h"
34
#include "core/data_type/data_type_map.h"
35
#include "core/data_type/data_type_nullable.h"
36
#include "core/data_type/data_type_struct.h"
37
#include "runtime/descriptors.h"
38
#include "util/string_util.h"
39
40
namespace doris {
41
namespace {
42
43
107k
bool is_scanner_materialized_virtual_column(const std::string& column_name) {
44
107k
    return column_name == BeConsts::ICEBERG_ROWID_COL;
45
107k
}
46
47
13.0k
bool parse_non_negative_int(std::string_view value, int32_t* result) {
48
13.0k
    DORIS_CHECK(result != nullptr);
49
13.0k
    int32_t parsed = -1;
50
13.0k
    const auto* begin = value.data();
51
13.0k
    const auto* end = begin + value.size();
52
13.0k
    const auto [ptr, ec] = std::from_chars(begin, end, parsed);
53
13.0k
    if (ec != std::errc() || ptr != end || parsed < 0) {
54
7.06k
        return false;
55
7.06k
    }
56
5.98k
    *result = parsed;
57
5.98k
    return true;
58
13.0k
}
59
60
2
std::string access_path_to_string(const std::vector<std::string>& path) {
61
2
    return fmt::format("{}", fmt::join(path, "."));
62
2
}
63
64
format::ColumnDefinition* find_or_add_child(format::ColumnDefinition* parent, int32_t id,
65
72.4k
                                            std::string name, DataTypePtr type) {
66
72.4k
    DORIS_CHECK(parent != nullptr);
67
72.4k
    for (auto& child : parent->children) {
68
37.6k
        if ((child.has_identifier_field_id() && child.get_identifier_field_id() == id) ||
69
37.6k
            child.name == name) {
70
0
            return &child;
71
0
        }
72
37.6k
    }
73
72.4k
    parent->children.push_back({
74
72.4k
            .identifier = Field::create_field<TYPE_INT>(id),
75
72.4k
            .name = std::move(name),
76
72.4k
            .type = std::move(type),
77
72.4k
            .children = {},
78
72.4k
            .default_expr = nullptr,
79
72.4k
            .is_partition_key = false,
80
72.4k
    });
81
72.4k
    return &parent->children.back();
82
72.4k
}
83
84
void inherit_schema_metadata(format::ColumnDefinition* column,
85
72.4k
                             const format::ColumnDefinition* schema_column) {
86
72.4k
    if (column == nullptr || schema_column == nullptr) {
87
53.1k
        return;
88
53.1k
    }
89
19.2k
    column->name_mapping = schema_column->name_mapping;
90
19.2k
}
91
92
const format::ColumnDefinition* find_schema_child_by_path(
93
19.7k
        const format::ColumnDefinition* schema_column, const std::string& child_path) {
94
19.7k
    if (schema_column == nullptr) {
95
12.2k
        return nullptr;
96
12.2k
    }
97
7.51k
    int32_t parsed_field_id = -1;
98
7.51k
    if (parse_non_negative_int(child_path, &parsed_field_id)) {
99
449
        const auto child_it = std::ranges::find_if(
100
536
                schema_column->children, [&](const format::ColumnDefinition& child) {
101
536
                    return child.has_identifier_field_id() &&
102
536
                           child.get_identifier_field_id() == parsed_field_id;
103
536
                });
104
449
        return child_it == schema_column->children.end() ? nullptr : &*child_it;
105
449
    }
106
16.5k
    const auto child_it = std::ranges::find_if(schema_column->children, [&](const auto& child) {
107
16.5k
        if (to_lower(child.name) == to_lower(child_path)) {
108
7.06k
            return true;
109
7.06k
        }
110
9.51k
        return std::ranges::any_of(child.name_mapping, [&](const std::string& alias) {
111
13
            return to_lower(alias) == to_lower(child_path);
112
13
        });
113
16.5k
    });
114
7.06k
    return child_it == schema_column->children.end() ? nullptr : &*child_it;
115
7.51k
}
116
117
72.4k
int32_t schema_field_id(const format::ColumnDefinition* schema_column) {
118
72.4k
    if (schema_column == nullptr || !schema_column->has_identifier_field_id()) {
119
53.1k
        return -1;
120
53.1k
    }
121
19.2k
    return schema_column->get_identifier_field_id();
122
72.4k
}
123
124
71.9k
int32_t schema_field_id_or(const format::ColumnDefinition* schema_column, int32_t fallback) {
125
71.9k
    const auto field_id = schema_field_id(schema_column);
126
71.9k
    return field_id >= 0 ? field_id : fallback;
127
71.9k
}
128
129
std::string schema_field_name_or(const format::ColumnDefinition* schema_column,
130
19.2k
                                 std::string fallback) {
131
19.2k
    return schema_column == nullptr || schema_column->name.empty() ? std::move(fallback)
132
19.2k
                                                                   : schema_column->name;
133
19.2k
}
134
135
struct AccessPathNode {
136
    bool project_all = false;
137
    std::map<std::string, AccessPathNode> children;
138
};
139
140
257
void merge_access_path_node(AccessPathNode* dst, const AccessPathNode& src) {
141
257
    DORIS_CHECK(dst != nullptr);
142
257
    if (dst->project_all) {
143
0
        return;
144
0
    }
145
257
    if (src.project_all) {
146
163
        dst->project_all = true;
147
163
        dst->children.clear();
148
163
        return;
149
163
    }
150
102
    for (const auto& [path, child] : src.children) {
151
102
        merge_access_path_node(&dst->children[path], child);
152
102
    }
153
94
}
154
155
void insert_access_path(AccessPathNode* root, const std::vector<std::string>& path,
156
14.5k
                        size_t path_idx) {
157
14.5k
    DORIS_CHECK(root != nullptr);
158
14.5k
    if (root->project_all) {
159
0
        return;
160
0
    }
161
14.5k
    if (path_idx >= path.size()) {
162
13.8k
        root->project_all = true;
163
13.8k
        root->children.clear();
164
13.8k
        return;
165
13.8k
    }
166
692
    insert_access_path(&root->children[path[path_idx]], path, path_idx + 1);
167
692
}
168
169
Status build_nested_children_from_access_node(format::ColumnDefinition* column,
170
                                              const DataTypePtr& type, const AccessPathNode& node,
171
                                              const std::string& path,
172
                                              const format::ColumnDefinition* schema_column);
173
174
// Expand a full complex-column projection into table-schema children when the table format provides
175
// an external/current schema. Without this, `SELECT complex_col` or `SELECT *` leaves
176
// ColumnDefinition::children empty, so ColumnMapper treats the root complex column as a scalar
177
// mapping and later tries to cast the old file shape to the current table shape directly.
178
//
179
// Examples:
180
//   - STRUCT country/city projected from an old file STRUCT country/population/location should
181
//     create children country and city, so city can be materialized as missing/default.
182
//   - ARRAY<STRUCT<item, quantity>> should create the array element wrapper and then the element
183
//     struct children item and quantity.
184
//   - MAP<STRING, STRUCT<full_name, age>> should create semantic children key/value directly, then
185
//     expand the value struct children full_name and age. Do not introduce a physical entries
186
//     wrapper here: ColumnMapper and TableReader treat MAP children as [key, value].
187
Status build_all_nested_children_from_schema(format::ColumnDefinition* column,
188
                                             const DataTypePtr& type, const std::string& path,
189
110k
                                             const format::ColumnDefinition* schema_column) {
190
110k
    DORIS_CHECK(column != nullptr);
191
192
110k
    const auto nested_type = remove_nullable(type);
193
110k
    AccessPathNode project_all;
194
110k
    project_all.project_all = true;
195
110k
    switch (nested_type->get_primitive_type()) {
196
9.04k
    case TYPE_STRUCT: {
197
9.04k
        const auto& struct_type = assert_cast<const DataTypeStruct&>(*nested_type);
198
28.3k
        for (size_t field_idx = 0; field_idx < struct_type.get_elements().size(); ++field_idx) {
199
19.2k
            const auto field_name = struct_type.get_element_name(field_idx);
200
19.2k
            const auto* schema_child = find_schema_child_by_path(schema_column, field_name);
201
19.2k
            auto* child = find_or_add_child(
202
19.2k
                    column, schema_field_id_or(schema_child, cast_set<int32_t>(field_idx)),
203
19.2k
                    schema_field_name_or(schema_child, field_name),
204
19.2k
                    struct_type.get_element(field_idx));
205
19.2k
            inherit_schema_metadata(child, schema_child);
206
19.2k
            RETURN_IF_ERROR(build_nested_children_from_access_node(
207
19.2k
                    child, child->type, project_all, path + "." + child->name, schema_child));
208
19.2k
        }
209
9.04k
        return Status::OK();
210
9.04k
    }
211
19.4k
    case TYPE_ARRAY: {
212
19.4k
        const auto& array_type = assert_cast<const DataTypeArray&>(*nested_type);
213
19.4k
        const auto* element_schema = schema_column != nullptr && !schema_column->children.empty()
214
19.4k
                                             ? &schema_column->children[0]
215
19.4k
                                             : nullptr;
216
19.4k
        auto* child = find_or_add_child(column, schema_field_id_or(element_schema, 0), "element",
217
19.4k
                                        array_type.get_nested_type());
218
19.4k
        inherit_schema_metadata(child, element_schema);
219
19.4k
        return build_nested_children_from_access_node(child, child->type, project_all, path + ".*",
220
19.4k
                                                      element_schema);
221
9.04k
    }
222
16.4k
    case TYPE_MAP: {
223
16.4k
        const auto& map_type = assert_cast<const DataTypeMap&>(*nested_type);
224
16.4k
        const auto* key_schema = schema_column != nullptr && !schema_column->children.empty()
225
16.4k
                                         ? &schema_column->children[0]
226
16.4k
                                         : nullptr;
227
16.4k
        const auto* value_schema = schema_column != nullptr && schema_column->children.size() > 1
228
16.4k
                                           ? &schema_column->children[1]
229
16.4k
                                           : nullptr;
230
16.4k
        auto* key_child = find_or_add_child(column, schema_field_id_or(key_schema, 0), "key",
231
16.4k
                                            map_type.get_key_type());
232
16.4k
        inherit_schema_metadata(key_child, key_schema);
233
16.4k
        RETURN_IF_ERROR(build_nested_children_from_access_node(
234
16.4k
                key_child, key_child->type, project_all, path + ".KEYS", key_schema));
235
16.4k
        auto* value_child = find_or_add_child(column, schema_field_id_or(value_schema, 1), "value",
236
16.4k
                                              map_type.get_value_type());
237
16.4k
        inherit_schema_metadata(value_child, value_schema);
238
16.4k
        RETURN_IF_ERROR(build_nested_children_from_access_node(
239
16.4k
                value_child, value_child->type, project_all, path + ".VALUES", value_schema));
240
16.4k
        return Status::OK();
241
16.4k
    }
242
65.6k
    default:
243
65.6k
        return Status::OK();
244
110k
    }
245
110k
}
246
247
Status build_struct_children_from_access_node(format::ColumnDefinition* column,
248
                                              const DataTypeStruct& struct_type,
249
                                              const AccessPathNode& node, const std::string& path,
250
402
                                              const format::ColumnDefinition* schema_column) {
251
402
    DORIS_CHECK(column != nullptr);
252
474
    for (const auto& [child_path, child_node] : node.children) {
253
        // Struct children are resolved by name or schema field id. We do not treat a numeric
254
        // child token as a struct ordinal, because `col.0` becomes ambiguous once the struct
255
        // evolves. Position-based access needs a separate design if it is required later.
256
474
        if (child_path == "OFFSET" || child_path == "*" || child_path == "KEYS" ||
257
474
            child_path == "VALUES") {
258
4
            return Status::NotSupported(
259
4
                    "AccessPathParser does not support access path {} for slot {}",
260
4
                    path + "." + child_path, column->name);
261
4
        }
262
263
        // Prefer the table/schema ColumnDefinition because it carries field ids and aliases.
264
        // Fallback to the struct type name only for formats without external schema metadata.
265
470
        const auto* schema_child = find_schema_child_by_path(schema_column, child_path);
266
470
        int32_t field_id = schema_field_id(schema_child);
267
470
        std::string field_name = schema_child == nullptr ? child_path : schema_child->name;
268
470
        DataTypePtr field_type = schema_child == nullptr ? nullptr : schema_child->type;
269
470
        if (field_id < 0 || field_type == nullptr) {
270
24
            for (size_t field_idx = 0; field_idx < struct_type.get_elements().size(); ++field_idx) {
271
21
                if (to_lower(struct_type.get_element_name(field_idx)) == to_lower(field_name)) {
272
13
                    field_id = cast_set<int32_t>(field_idx);
273
13
                    field_name = struct_type.get_element_name(field_idx);
274
13
                    field_type = struct_type.get_element(field_idx);
275
13
                    break;
276
13
                }
277
21
            }
278
16
        }
279
280
470
        if (field_id < 0 || field_type == nullptr) {
281
3
            return Status::NotSupported(
282
3
                    "AccessPathParser does not support access path {} for slot {}",
283
3
                    path + "." + child_path, column->name);
284
3
        }
285
        // TODO: For TVF Parquet files without field ids, this fallback uses the struct ordinal as
286
        // the table child identifier. BY_NAME mapping should instead keep a string identifier and
287
        // let TableColumnMapper resolve the file-local child id from the Parquet schema.
288
467
        auto* child = find_or_add_child(column, field_id, field_name, field_type);
289
467
        inherit_schema_metadata(child, schema_child);
290
467
        RETURN_IF_ERROR(build_nested_children_from_access_node(
291
467
                child, child->type, child_node, path + "." + child_path, schema_child));
292
467
    }
293
395
    return Status::OK();
294
402
}
295
296
Status build_map_children_from_access_node(format::ColumnDefinition* column,
297
                                           const DataTypeMap& map_type, const AccessPathNode& node,
298
                                           const std::string& path,
299
140
                                           const format::ColumnDefinition* schema_column) {
300
140
    DORIS_CHECK(column != nullptr);
301
140
    AccessPathNode key_node;
302
140
    AccessPathNode value_node;
303
140
    bool need_key = false;
304
140
    bool need_value = false;
305
306
157
    for (const auto& [child_path, child_node] : node.children) {
307
157
        if (child_path == "OFFSET") {
308
1
            return Status::NotSupported(
309
1
                    "AccessPathParser does not support access path {} for slot {}",
310
1
                    path + "." + child_path, column->name);
311
1
        }
312
156
        if (child_path == "KEYS") {
313
35
            need_key = true;
314
35
            merge_access_path_node(&key_node, child_node);
315
35
            continue;
316
35
        }
317
121
        if (child_path == "VALUES") {
318
100
            need_key = true;
319
100
            key_node.project_all = true;
320
100
            key_node.children.clear();
321
100
            need_value = true;
322
100
            merge_access_path_node(&value_node, child_node);
323
100
            continue;
324
100
        }
325
21
        if (child_path == "*") {
326
20
            need_key = true;
327
20
            key_node.project_all = true;
328
20
            key_node.children.clear();
329
20
            need_value = true;
330
20
            merge_access_path_node(&value_node, child_node);
331
20
            continue;
332
20
        }
333
1
        return Status::NotSupported("AccessPathParser does not support access path {} for slot {}",
334
1
                                    path + "." + child_path, column->name);
335
21
    }
336
138
    if (need_key && !need_value) {
337
        // A key-only MAP projection is not independently materializable yet. FileScannerV2 can
338
        // describe a projection such as `m.KEYS`, but the downstream file block -> table block path
339
        // still builds a ColumnMap from key column + value column + offsets. If the value child is
340
        // omitted here, TableReader/ColumnMapper cannot reconstruct a valid table MAP column even
341
        // though the query only needs keys.
342
        //
343
        // Example:
344
        //   SELECT map_keys(m) FROM t;
345
        // or
346
        //   SELECT * FROM t WHERE array_contains(map_keys(m), 'k1');
347
        //
348
        // The access path only asks for `m.KEYS`, but the scan still has to read `m.VALUES` as a
349
        // temporary full projection until map materialization supports constructing a table MAP
350
        // from keys only.
351
19
        need_value = true;
352
19
        value_node.project_all = true;
353
19
        value_node.children.clear();
354
19
    }
355
356
138
    if (!need_key && !need_value) {
357
0
        return Status::OK();
358
0
    }
359
360
138
    const auto* key_schema = schema_column != nullptr && !schema_column->children.empty()
361
138
                                     ? &schema_column->children[0]
362
138
                                     : nullptr;
363
138
    const auto* value_schema = schema_column != nullptr && schema_column->children.size() > 1
364
138
                                       ? &schema_column->children[1]
365
138
                                       : nullptr;
366
138
    if (need_key) {
367
138
        auto* key_child = find_or_add_child(column, schema_field_id_or(key_schema, 0), "key",
368
138
                                            map_type.get_key_type());
369
138
        inherit_schema_metadata(key_child, key_schema);
370
138
        RETURN_IF_ERROR(build_nested_children_from_access_node(key_child, key_child->type, key_node,
371
138
                                                               path + ".KEYS", key_schema));
372
138
    }
373
138
    if (need_value) {
374
138
        auto* value_child = find_or_add_child(column, schema_field_id_or(value_schema, 1), "value",
375
138
                                              map_type.get_value_type());
376
138
        inherit_schema_metadata(value_child, value_schema);
377
138
        RETURN_IF_ERROR(build_nested_children_from_access_node(
378
138
                value_child, value_child->type, value_node, path + ".VALUES", value_schema));
379
138
    }
380
137
    return Status::OK();
381
138
}
382
383
Status build_nested_children_from_access_node(format::ColumnDefinition* column,
384
                                              const DataTypePtr& type, const AccessPathNode& node,
385
                                              const std::string& path,
386
111k
                                              const format::ColumnDefinition* schema_column) {
387
111k
    DORIS_CHECK(column != nullptr);
388
111k
    if (node.project_all || node.children.empty()) {
389
110k
        return build_all_nested_children_from_schema(column, type, path, schema_column);
390
110k
    }
391
392
592
    const auto nested_type = remove_nullable(type);
393
592
    switch (nested_type->get_primitive_type()) {
394
402
    case TYPE_STRUCT:
395
402
        return build_struct_children_from_access_node(
396
402
                column, assert_cast<const DataTypeStruct&>(*nested_type), node, path,
397
402
                schema_column);
398
52
    case TYPE_ARRAY: {
399
52
        if (node.children.size() != 1 || !node.children.contains("*")) {
400
2
            return Status::NotSupported(
401
2
                    "AccessPathParser does not support access path {} for slot {}", path,
402
2
                    column->name);
403
2
        }
404
50
        const auto& array_type = assert_cast<const DataTypeArray&>(*nested_type);
405
50
        const auto* element_schema = schema_column != nullptr && !schema_column->children.empty()
406
50
                                             ? &schema_column->children[0]
407
50
                                             : nullptr;
408
50
        auto* child = find_or_add_child(column, schema_field_id_or(element_schema, 0), "element",
409
50
                                        array_type.get_nested_type());
410
50
        inherit_schema_metadata(child, element_schema);
411
50
        return build_nested_children_from_access_node(child, child->type, node.children.at("*"),
412
50
                                                      path + ".*", element_schema);
413
52
    }
414
140
    case TYPE_MAP:
415
140
        return build_map_children_from_access_node(
416
140
                column, assert_cast<const DataTypeMap&>(*nested_type), node, path, schema_column);
417
0
    default:
418
0
        return Status::NotSupported("AccessPathParser does not support access path {} for slot {}",
419
0
                                    path, column->name);
420
592
    }
421
592
}
422
423
} // namespace
424
425
Status AccessPathParser::build_nested_children(format::ColumnDefinition* column,
426
                                               const std::vector<TColumnAccessPath>& access_paths,
427
107k
                                               const format::ColumnDefinition* schema_column) {
428
107k
    DORIS_CHECK(column != nullptr);
429
107k
    if (is_scanner_materialized_virtual_column(column->name)) {
430
49
        return Status::OK();
431
49
    }
432
107k
    if (!is_complex_type(remove_nullable(column->type)->get_primitive_type())) {
433
69.1k
        return Status::OK();
434
69.1k
    }
435
436
38.7k
    AccessPathNode root;
437
    // Build tree for AccessPathNode.
438
    // For example, for access paths ["a.b", "a.c", "d"], the tree will be:
439
    // root
440
    // ├── a
441
    // │   ├── b
442
    // │   └── c
443
    // └── d
444
38.7k
    for (const auto& access_path : access_paths) {
445
        // TODO: Support META access paths if needed. Currently AccessPathParser only supports
446
        // DATA access paths.
447
13.8k
        if (access_path.type != TAccessPathType::DATA || !access_path.__isset.data_access_path) {
448
2
            return Status::NotSupported(
449
2
                    "AccessPathParser only supports DATA access paths for slot {}", column->name);
450
2
        }
451
13.8k
        const auto& path = access_path.data_access_path.path;
452
13.8k
        if (path.empty()) {
453
0
            insert_access_path(&root, path, 0);
454
0
            continue;
455
0
        }
456
13.8k
        int32_t top_level_id = -1;
457
13.8k
        if (to_lower(path.front()) != to_lower(column->name) &&
458
13.8k
            (!parse_non_negative_int(path.front(), &top_level_id) ||
459
5.54k
             !column->has_identifier_field_id() ||
460
5.54k
             top_level_id != column->get_identifier_field_id())) {
461
2
            return Status::NotSupported("AccessPathParser access path {} does not match slot {}",
462
2
                                        access_path_to_string(path), column->name);
463
2
        }
464
13.8k
        insert_access_path(&root, path, 1);
465
13.8k
    }
466
    // Recursively build nested children for the column based on the AccessPathNode tree.
467
38.7k
    return build_nested_children_from_access_node(column, column->type, root, column->name,
468
38.7k
                                                  schema_column);
469
38.7k
}
470
471
Status AccessPathParser::build_nested_children(format::ColumnDefinition* column,
472
                                               const SlotDescriptor* slot_desc,
473
107k
                                               const format::ColumnDefinition* schema_column) {
474
107k
    DORIS_CHECK(column != nullptr);
475
107k
    DORIS_CHECK(slot_desc != nullptr);
476
107k
    return build_nested_children(column, slot_desc->all_access_paths(), schema_column);
477
107k
}
478
479
} // namespace doris