Coverage Report

Created: 2026-08-05 20:10

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