Coverage Report

Created: 2026-05-17 03:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/format/parquet/vparquet_column_reader.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 "format/parquet/vparquet_column_reader.h"
19
20
#include <cctz/time_zone.h>
21
#include <gen_cpp/parquet_types.h>
22
#include <rapidjson/document.h>
23
#include <sys/types.h>
24
25
#include <algorithm>
26
#include <cmath>
27
#include <deque>
28
#include <limits>
29
#include <map>
30
#include <string_view>
31
#include <utility>
32
#include <vector>
33
34
#include "common/exception.h"
35
#include "common/status.h"
36
#include "core/column/column.h"
37
#include "core/column/column_array.h"
38
#include "core/column/column_map.h"
39
#include "core/column/column_nullable.h"
40
#include "core/column/column_string.h"
41
#include "core/column/column_struct.h"
42
#include "core/column/column_varbinary.h"
43
#include "core/column/column_variant.h"
44
#include "core/data_type/data_type_array.h"
45
#include "core/data_type/data_type_factory.hpp"
46
#include "core/data_type/data_type_jsonb.h"
47
#include "core/data_type/data_type_map.h"
48
#include "core/data_type/data_type_nullable.h"
49
#include "core/data_type/data_type_number.h"
50
#include "core/data_type/data_type_string.h"
51
#include "core/data_type/data_type_struct.h"
52
#include "core/data_type/data_type_variant.h"
53
#include "core/data_type/define_primitive_type.h"
54
#include "core/data_type_serde/data_type_serde.h"
55
#include "core/string_buffer.hpp"
56
#include "core/value/jsonb_value.h"
57
#include "core/value/timestamptz_value.h"
58
#include "core/value/vdatetime_value.h"
59
#include "exec/common/variant_util.h"
60
#include "format/parquet/level_decoder.h"
61
#include "format/parquet/parquet_variant_reader.h"
62
#include "format/parquet/schema_desc.h"
63
#include "format/parquet/vparquet_column_chunk_reader.h"
64
#include "io/fs/tracing_file_reader.h"
65
#include "runtime/runtime_profile.h"
66
#include "util/jsonb_document.h"
67
68
namespace doris {
69
static void fill_struct_null_map(FieldSchema* field, NullMap& null_map,
70
                                 const std::vector<level_t>& rep_levels,
71
11
                                 const std::vector<level_t>& def_levels) {
72
11
    size_t num_levels = def_levels.size();
73
11
    DCHECK_EQ(num_levels, rep_levels.size());
74
11
    size_t origin_size = null_map.size();
75
11
    null_map.resize(origin_size + num_levels);
76
11
    size_t pos = origin_size;
77
26
    for (size_t i = 0; i < num_levels; ++i) {
78
        // skip the levels affect its ancestor or its descendants
79
15
        if (def_levels[i] < field->repeated_parent_def_level ||
80
15
            rep_levels[i] > field->repetition_level) {
81
0
            continue;
82
0
        }
83
15
        if (def_levels[i] >= field->definition_level) {
84
15
            null_map[pos++] = 0;
85
15
        } else {
86
0
            null_map[pos++] = 1;
87
0
        }
88
15
    }
89
11
    null_map.resize(pos);
90
11
}
91
92
static void fill_array_offset(FieldSchema* field, ColumnArray::Offsets64& offsets_data,
93
                              NullMap* null_map_ptr, const std::vector<level_t>& rep_levels,
94
2
                              const std::vector<level_t>& def_levels) {
95
2
    size_t num_levels = rep_levels.size();
96
2
    DCHECK_EQ(num_levels, def_levels.size());
97
2
    size_t origin_size = offsets_data.size();
98
2
    offsets_data.resize(origin_size + num_levels);
99
2
    if (null_map_ptr != nullptr) {
100
2
        null_map_ptr->resize(origin_size + num_levels);
101
2
    }
102
2
    size_t offset_pos = origin_size - 1;
103
8
    for (size_t i = 0; i < num_levels; ++i) {
104
        // skip the levels affect its ancestor or its descendants
105
6
        if (def_levels[i] < field->repeated_parent_def_level ||
106
6
            rep_levels[i] > field->repetition_level) {
107
0
            continue;
108
0
        }
109
6
        if (rep_levels[i] == field->repetition_level) {
110
4
            offsets_data[offset_pos]++;
111
4
            continue;
112
4
        }
113
2
        offset_pos++;
114
2
        offsets_data[offset_pos] = offsets_data[offset_pos - 1];
115
2
        if (def_levels[i] >= field->definition_level) {
116
2
            offsets_data[offset_pos]++;
117
2
        }
118
2
        if (def_levels[i] >= field->definition_level - 1) {
119
2
            (*null_map_ptr)[offset_pos] = 0;
120
2
        } else {
121
0
            (*null_map_ptr)[offset_pos] = 1;
122
0
        }
123
2
    }
124
2
    offsets_data.resize(offset_pos + 1);
125
2
    if (null_map_ptr != nullptr) {
126
2
        null_map_ptr->resize(offset_pos + 1);
127
2
    }
128
2
}
129
130
static constexpr int64_t UNIX_EPOCH_DAYNR = 719528;
131
static constexpr int64_t MICROS_PER_SECOND = 1000000;
132
133
0
static int64_t variant_date_value(const VecDateTimeValue& value) {
134
0
    return value.daynr() - UNIX_EPOCH_DAYNR;
135
0
}
136
137
1
static int64_t variant_date_value(const DateV2Value<DateV2ValueType>& value) {
138
1
    return value.daynr() - UNIX_EPOCH_DAYNR;
139
1
}
140
141
0
static int64_t variant_datetime_value(const VecDateTimeValue& value) {
142
0
    int64_t timestamp = 0;
143
0
    value.unix_timestamp(&timestamp, cctz::utc_time_zone());
144
0
    return timestamp * MICROS_PER_SECOND;
145
0
}
146
147
1
static int64_t variant_datetime_value(const DateV2Value<DateTimeV2ValueType>& value) {
148
1
    int64_t timestamp = 0;
149
1
    value.unix_timestamp(&timestamp, cctz::utc_time_zone());
150
1
    return timestamp * MICROS_PER_SECOND + value.microsecond();
151
1
}
152
153
0
static int64_t variant_datetime_value(const TimestampTzValue& value) {
154
0
    int64_t timestamp = 0;
155
0
    value.unix_timestamp(&timestamp, cctz::utc_time_zone());
156
0
    return timestamp * MICROS_PER_SECOND + value.microsecond();
157
0
}
158
159
82
static int find_child_idx(const FieldSchema& field, std::string_view name) {
160
167
    for (int i = 0; i < field.children.size(); ++i) {
161
146
        if (field.children[i].lower_case_name == name) {
162
61
            return i;
163
61
        }
164
146
    }
165
21
    return -1;
166
82
}
167
168
0
static bool is_variant_wrapper_typed_value_child(const FieldSchema& field) {
169
0
    auto type = remove_nullable(field.data_type);
170
0
    return type->get_primitive_type() == TYPE_STRUCT || type->get_primitive_type() == TYPE_ARRAY;
171
0
}
172
173
static bool is_variant_wrapper_field(const FieldSchema& field,
174
59
                                     bool allow_scalar_typed_value_only_wrapper) {
175
59
    auto type = remove_nullable(field.data_type);
176
59
    if (type->get_primitive_type() != TYPE_STRUCT && type->get_primitive_type() != TYPE_VARIANT) {
177
48
        return false;
178
48
    }
179
180
11
    bool has_metadata = false;
181
11
    bool has_value = false;
182
11
    const FieldSchema* typed_value = nullptr;
183
15
    for (const auto& child : field.children) {
184
15
        if (child.lower_case_name == "metadata") {
185
1
            if (child.physical_type != tparquet::Type::BYTE_ARRAY) {
186
0
                return false;
187
0
            }
188
1
            has_metadata = true;
189
1
            continue;
190
1
        }
191
14
        if (child.lower_case_name == "value") {
192
6
            if (child.physical_type != tparquet::Type::BYTE_ARRAY) {
193
3
                return false;
194
3
            }
195
3
            has_value = true;
196
3
            continue;
197
6
        }
198
8
        if (child.lower_case_name == "typed_value") {
199
3
            typed_value = &child;
200
3
            continue;
201
3
        }
202
5
        return false;
203
8
    }
204
3
    if (has_metadata && has_value) {
205
1
        return type->get_primitive_type() == TYPE_VARIANT || typed_value != nullptr;
206
1
    }
207
2
    if (has_value) {
208
2
        return typed_value != nullptr;
209
2
    }
210
0
    return typed_value != nullptr && (allow_scalar_typed_value_only_wrapper ||
211
0
                                      is_variant_wrapper_typed_value_child(*typed_value));
212
2
}
213
214
19
static bool is_value_only_variant_wrapper_candidate(const FieldSchema& field) {
215
19
    auto type = remove_nullable(field.data_type);
216
19
    if (type->get_primitive_type() != TYPE_STRUCT && type->get_primitive_type() != TYPE_VARIANT) {
217
14
        return false;
218
14
    }
219
220
5
    bool has_value = false;
221
5
    for (const auto& child : field.children) {
222
5
        if (child.lower_case_name == "value" && child.physical_type == tparquet::Type::BYTE_ARRAY) {
223
2
            has_value = true;
224
2
            continue;
225
2
        }
226
3
        return false;
227
5
    }
228
2
    return has_value;
229
5
}
230
231
36
static Status get_binary_field(const Field& field, std::string* value, bool* present) {
232
36
    if (field.is_null()) {
233
4
        *present = false;
234
4
        return Status::OK();
235
4
    }
236
32
    *present = true;
237
32
    switch (field.get_type()) {
238
32
    case TYPE_STRING:
239
32
        *value = field.get<TYPE_STRING>();
240
32
        return Status::OK();
241
0
    case TYPE_CHAR:
242
0
        *value = field.get<TYPE_CHAR>();
243
0
        return Status::OK();
244
0
    case TYPE_VARCHAR:
245
0
        *value = field.get<TYPE_VARCHAR>();
246
0
        return Status::OK();
247
0
    case TYPE_VARBINARY: {
248
0
        auto ref = field.get<TYPE_VARBINARY>().to_string_ref();
249
0
        value->assign(ref.data, ref.size);
250
0
        return Status::OK();
251
0
    }
252
0
    default:
253
0
        return Status::Corruption("Parquet VARIANT binary field has unexpected Doris type {}",
254
0
                                  field.get_type_name());
255
32
    }
256
32
}
257
258
5
static PathInData append_path(const PathInData& prefix, const PathInData& suffix) {
259
5
    if (prefix.empty()) {
260
5
        return suffix;
261
5
    }
262
0
    if (suffix.empty()) {
263
0
        return prefix;
264
0
    }
265
0
    PathInDataBuilder builder;
266
0
    builder.append(prefix.get_parts(), false);
267
0
    builder.append(suffix.get_parts(), false);
268
0
    return builder.build();
269
0
}
270
271
15
static Status make_jsonb_field(std::string_view json, FieldWithDataType* value) {
272
15
    JsonBinaryValue jsonb_value;
273
15
    RETURN_IF_ERROR(jsonb_value.from_json_string(json.data(), json.size()));
274
15
    value->field =
275
15
            Field::create_field<TYPE_JSONB>(JsonbField(jsonb_value.value(), jsonb_value.size()));
276
15
    value->base_scalar_type_id = TYPE_JSONB;
277
15
    value->num_dimensions = 0;
278
15
    value->precision = 0;
279
15
    value->scale = 0;
280
15
    return Status::OK();
281
15
}
282
283
1
static std::string make_null_array_json(size_t elements) {
284
1
    std::string json = "[";
285
2
    for (size_t i = 0; i < elements; ++i) {
286
1
        if (i != 0) {
287
0
            json.push_back(',');
288
0
        }
289
1
        json.append("null");
290
1
    }
291
1
    json.push_back(']');
292
1
    return json;
293
1
}
294
295
10
static Status make_empty_object_field(Field* field) {
296
10
    FieldWithDataType value;
297
10
    RETURN_IF_ERROR(make_jsonb_field("{}", &value));
298
10
    *field = std::move(value.field);
299
10
    return Status::OK();
300
10
}
301
302
static Status insert_jsonb_value(const PathInData& path, std::string_view json,
303
4
                                 VariantMap* values) {
304
4
    FieldWithDataType value;
305
4
    RETURN_IF_ERROR(make_jsonb_field(json, &value));
306
4
    (*values)[path] = std::move(value);
307
4
    return Status::OK();
308
4
}
309
310
4
static Status insert_empty_object_marker(const PathInData& path, VariantMap* values) {
311
4
    return insert_jsonb_value(path, "{}", values);
312
4
}
313
314
37
static bool is_empty_object_marker(const FieldWithDataType& value) {
315
37
    if (value.field.get_type() != TYPE_JSONB) {
316
26
        return false;
317
26
    }
318
11
    const auto& jsonb = value.field.get<TYPE_JSONB>();
319
11
    const JsonbDocument* document = nullptr;
320
11
    Status st =
321
11
            JsonbDocument::checkAndCreateDocument(jsonb.get_value(), jsonb.get_size(), &document);
322
11
    if (!st.ok() || document == nullptr || document->getValue() == nullptr ||
323
11
        !document->getValue()->isObject()) {
324
1
        return false;
325
1
    }
326
10
    return document->getValue()->unpack<ObjectVal>()->numElem() == 0;
327
11
}
328
329
static Status collect_empty_object_markers(const rapidjson::Value& value, PathInDataBuilder* path,
330
1
                                           VariantMap* values) {
331
1
    if (!value.IsObject()) {
332
0
        return Status::OK();
333
0
    }
334
1
    if (value.MemberCount() == 0) {
335
1
        return insert_empty_object_marker(path->build(), values);
336
1
    }
337
0
    for (auto it = value.MemberBegin(); it != value.MemberEnd(); ++it) {
338
0
        if (it->value.IsObject()) {
339
0
            path->append(std::string_view(it->name.GetString(), it->name.GetStringLength()), false);
340
0
            RETURN_IF_ERROR(collect_empty_object_markers(it->value, path, values));
341
0
            path->pop_back();
342
0
        }
343
0
    }
344
0
    return Status::OK();
345
0
}
346
347
static Status add_empty_object_markers_from_json(const std::string& json, const PathInData& prefix,
348
6
                                                 VariantMap* values) {
349
6
    if (json.find("{}") == std::string::npos) {
350
5
        return Status::OK();
351
5
    }
352
1
    rapidjson::Document document;
353
1
    document.Parse(json.data(), json.size());
354
1
    if (document.HasParseError()) {
355
0
        return Status::Corruption("Invalid Parquet VARIANT decoded JSON");
356
0
    }
357
1
    PathInDataBuilder path;
358
1
    path.append(prefix.get_parts(), false);
359
1
    return collect_empty_object_markers(document, &path, values);
360
1
}
361
362
static Status parse_json_to_variant_map(const std::string& json, const PathInData& prefix,
363
6
                                        VariantMap* values) {
364
6
    auto parsed_column = ColumnVariant::create(0, false);
365
6
    ParseConfig parse_config;
366
6
    StringRef json_ref(json.data(), json.size());
367
6
    RETURN_IF_CATCH_EXCEPTION(
368
6
            variant_util::parse_json_to_variant(*parsed_column, json_ref, nullptr, parse_config));
369
6
    Field parsed = (*parsed_column)[0];
370
6
    if (!parsed.is_null()) {
371
5
        auto& parsed_values = parsed.get<TYPE_VARIANT>();
372
5
        for (auto& [path, value] : parsed_values) {
373
5
            (*values)[append_path(prefix, path)] = std::move(value);
374
5
        }
375
5
    }
376
6
    RETURN_IF_ERROR(add_empty_object_markers_from_json(json, prefix, values));
377
6
    return Status::OK();
378
6
}
379
380
2
static Status variant_map_to_json(VariantMap values, std::string* json) {
381
2
    auto variant_column = ColumnVariant::create(0, false);
382
2
    RETURN_IF_CATCH_EXCEPTION(
383
2
            variant_column->insert(Field::create_field<TYPE_VARIANT>(std::move(values))));
384
2
    DataTypeSerDe::FormatOptions options;
385
2
    variant_column->serialize_one_row_to_string(0, json, options);
386
2
    return Status::OK();
387
2
}
388
389
17
static bool path_has_prefix(const PathInData& path, const PathInData& prefix) {
390
17
    const auto& parts = path.get_parts();
391
17
    const auto& prefix_parts = prefix.get_parts();
392
17
    if (parts.size() < prefix_parts.size()) {
393
0
        return false;
394
0
    }
395
18
    for (size_t i = 0; i < prefix_parts.size(); ++i) {
396
1
        if (parts[i] != prefix_parts[i]) {
397
0
            return false;
398
0
        }
399
1
    }
400
17
    return true;
401
17
}
402
403
15
static bool has_descendant_path(const VariantMap& values, const PathInData& prefix) {
404
15
    const size_t prefix_size = prefix.get_parts().size();
405
15
    return std::ranges::any_of(values, [&](const auto& entry) {
406
12
        const auto& path = entry.first;
407
12
        return path.get_parts().size() > prefix_size && path_has_prefix(path, prefix);
408
12
    });
409
15
}
410
411
static void erase_shadowed_empty_object_markers(VariantMap* values,
412
46
                                                const VariantMap& shadowing_values) {
413
76
    for (auto it = values->begin(); it != values->end();) {
414
30
        if (is_empty_object_marker(it->second) &&
415
30
            (has_descendant_path(*values, it->first) ||
416
7
             has_descendant_path(shadowing_values, it->first))) {
417
3
            it = values->erase(it);
418
3
            continue;
419
3
        }
420
27
        ++it;
421
27
    }
422
46
}
423
424
static void erase_shadowed_empty_object_markers(VariantMap* value_values,
425
23
                                                VariantMap* typed_values) {
426
23
    erase_shadowed_empty_object_markers(value_values, *typed_values);
427
23
    erase_shadowed_empty_object_markers(typed_values, *value_values);
428
23
}
429
430
static Status check_no_shredded_value_typed_duplicates(const VariantMap& value_values,
431
                                                       const VariantMap& typed_values,
432
23
                                                       const PathInData& prefix) {
433
23
    const size_t prefix_size = prefix.get_parts().size();
434
23
    for (const auto& value_entry : value_values) {
435
10
        const auto& value_path = value_entry.first;
436
10
        if (!path_has_prefix(value_path, prefix)) {
437
0
            continue;
438
0
        }
439
10
        if (value_path.get_parts().size() == prefix_size) {
440
3
            if (is_empty_object_marker(value_entry.second) &&
441
3
                !has_descendant_path(typed_values, value_path)) {
442
1
                continue;
443
1
            }
444
2
            if (!typed_values.empty()) {
445
0
                return Status::Corruption(
446
0
                        "Parquet VARIANT residual value conflicts with typed_value at path {}",
447
0
                        value_path.get_path());
448
0
            }
449
2
            continue;
450
2
        }
451
7
        for (const auto& typed_entry : typed_values) {
452
4
            const auto& typed_path = typed_entry.first;
453
4
            if (!path_has_prefix(typed_path, prefix)) {
454
0
                continue;
455
0
            }
456
4
            if (typed_path.get_parts().size() == prefix_size) {
457
0
                if (is_empty_object_marker(typed_entry.second) &&
458
0
                    !has_descendant_path(value_values, typed_path)) {
459
0
                    continue;
460
0
                }
461
0
                return Status::Corruption(
462
0
                        "Parquet VARIANT residual value and typed_value contain duplicate field {}",
463
0
                        value_path.get_parts()[prefix_size].key);
464
0
            }
465
4
            if (value_path.get_parts()[prefix_size] == typed_path.get_parts()[prefix_size]) {
466
3
                if (value_path == typed_path && is_empty_object_marker(value_entry.second) &&
467
3
                    is_empty_object_marker(typed_entry.second)) {
468
1
                    continue;
469
1
                }
470
2
                return Status::Corruption(
471
2
                        "Parquet VARIANT residual value and typed_value contain duplicate field {}",
472
2
                        value_path.get_parts()[prefix_size].key);
473
3
            }
474
4
        }
475
7
    }
476
21
    return Status::OK();
477
23
}
478
479
45
static bool has_direct_typed_parent_null(const std::vector<const NullMap*>& null_maps, size_t row) {
480
63
    return std::ranges::any_of(null_maps, [&](const NullMap* null_map) {
481
63
        DCHECK_LT(row, null_map->size());
482
63
        return (*null_map)[row];
483
63
    });
484
45
}
485
486
static void insert_direct_typed_leaf_range(const IColumn& column, size_t start, size_t rows,
487
                                           const std::vector<const NullMap*>& parent_null_maps,
488
8
                                           IColumn* variant_leaf) {
489
8
    auto& nullable_leaf = assert_cast<ColumnNullable&>(*variant_leaf);
490
8
    const IColumn* value_column = &column;
491
8
    const NullMap* leaf_null_map = nullptr;
492
8
    if (const auto* nullable_column = check_and_get_column<ColumnNullable>(&column)) {
493
0
        value_column = &nullable_column->get_nested_column();
494
0
        leaf_null_map = &nullable_column->get_null_map_data();
495
0
    }
496
497
8
    nullable_leaf.get_nested_column().insert_range_from(*value_column, start, rows);
498
8
    auto& null_map = nullable_leaf.get_null_map_data();
499
8
    null_map.reserve(null_map.size() + rows);
500
20
    for (size_t i = 0; i < rows; ++i) {
501
12
        const size_t row = start + i;
502
12
        const bool leaf_is_null = leaf_null_map != nullptr && (*leaf_null_map)[row];
503
12
        null_map.push_back(leaf_is_null || has_direct_typed_parent_null(parent_null_maps, row));
504
12
    }
505
8
}
506
507
27
static bool is_temporal_variant_leaf_type(PrimitiveType type) {
508
27
    switch (type) {
509
2
    case TYPE_TIMEV2:
510
2
    case TYPE_DATE:
511
2
    case TYPE_DATETIME:
512
4
    case TYPE_DATEV2:
513
6
    case TYPE_DATETIMEV2:
514
6
    case TYPE_TIMESTAMPTZ:
515
6
        return true;
516
21
    default:
517
21
        return false;
518
27
    }
519
27
}
520
521
static bool is_uuid_typed_value_field(const FieldSchema& field_schema);
522
static bool contains_uuid_typed_value_field(const FieldSchema& field_schema);
523
524
11
static DataTypePtr direct_variant_leaf_type(const DataTypePtr& data_type) {
525
11
    const auto& type = remove_nullable(data_type);
526
11
    if (is_temporal_variant_leaf_type(type->get_primitive_type())) {
527
3
        return std::make_shared<DataTypeInt64>();
528
3
    }
529
8
    return type;
530
11
}
531
532
14
static DataTypePtr direct_variant_leaf_type(const FieldSchema& field_schema) {
533
14
    const auto& type = remove_nullable(field_schema.data_type);
534
14
    if (is_uuid_typed_value_field(field_schema)) {
535
2
        return std::make_shared<DataTypeString>();
536
2
    }
537
12
    if (type->get_primitive_type() == TYPE_ARRAY) {
538
1
        DORIS_CHECK(!field_schema.children.empty());
539
1
        DataTypePtr nested_type = direct_variant_leaf_type(field_schema.children[0]);
540
1
        if (field_schema.children[0].data_type->is_nullable()) {
541
1
            nested_type = make_nullable(nested_type);
542
1
        }
543
1
        return std::make_shared<DataTypeArray>(nested_type);
544
1
    }
545
11
    return direct_variant_leaf_type(field_schema.data_type);
546
12
}
547
548
5
static bool contains_temporal_variant_leaf_type(const DataTypePtr& data_type) {
549
5
    const auto& type = remove_nullable(data_type);
550
5
    if (is_temporal_variant_leaf_type(type->get_primitive_type())) {
551
0
        return true;
552
0
    }
553
5
    if (type->get_primitive_type() == TYPE_ARRAY) {
554
0
        return contains_temporal_variant_leaf_type(
555
0
                assert_cast<const DataTypeArray*>(type.get())->get_nested_type());
556
0
    }
557
5
    return false;
558
5
}
559
560
static int64_t direct_temporal_variant_value(PrimitiveType type, const IColumn& column,
561
3
                                             size_t row) {
562
3
    switch (type) {
563
1
    case TYPE_TIMEV2:
564
1
        return static_cast<int64_t>(
565
1
                std::llround(assert_cast<const ColumnTimeV2&>(column).get_data()[row]));
566
0
    case TYPE_DATE:
567
0
        return variant_date_value(assert_cast<const ColumnDate&>(column).get_data()[row]);
568
0
    case TYPE_DATETIME:
569
0
        return variant_datetime_value(assert_cast<const ColumnDateTime&>(column).get_data()[row]);
570
1
    case TYPE_DATEV2:
571
1
        return variant_date_value(assert_cast<const ColumnDateV2&>(column).get_data()[row]);
572
1
    case TYPE_DATETIMEV2:
573
1
        return variant_datetime_value(assert_cast<const ColumnDateTimeV2&>(column).get_data()[row]);
574
0
    case TYPE_TIMESTAMPTZ:
575
0
        return variant_datetime_value(
576
0
                assert_cast<const ColumnTimeStampTz&>(column).get_data()[row]);
577
0
    default:
578
0
        DORIS_CHECK(false);
579
0
        return 0;
580
3
    }
581
3
}
582
583
static void insert_direct_typed_temporal_leaf_range(
584
        PrimitiveType type, const IColumn& column, size_t start, size_t rows,
585
3
        const std::vector<const NullMap*>& parent_null_maps, IColumn* variant_leaf) {
586
3
    auto& nullable_leaf = assert_cast<ColumnNullable&>(*variant_leaf);
587
3
    const IColumn* value_column = &column;
588
3
    const NullMap* leaf_null_map = nullptr;
589
3
    if (const auto* nullable_column = check_and_get_column<ColumnNullable>(&column)) {
590
0
        value_column = &nullable_column->get_nested_column();
591
0
        leaf_null_map = &nullable_column->get_null_map_data();
592
0
    }
593
594
3
    auto& data = assert_cast<ColumnInt64&>(nullable_leaf.get_nested_column()).get_data();
595
3
    data.reserve(data.size() + rows);
596
3
    auto& null_map = nullable_leaf.get_null_map_data();
597
3
    null_map.reserve(null_map.size() + rows);
598
6
    for (size_t i = 0; i < rows; ++i) {
599
3
        const size_t row = start + i;
600
3
        data.push_back(direct_temporal_variant_value(type, *value_column, row));
601
3
        const bool leaf_is_null = leaf_null_map != nullptr && (*leaf_null_map)[row];
602
3
        null_map.push_back(leaf_is_null || has_direct_typed_parent_null(parent_null_maps, row));
603
3
    }
604
3
}
605
606
static Status insert_direct_typed_uuid_leaf_range(
607
        const IColumn& column, size_t start, size_t rows,
608
1
        const std::vector<const NullMap*>& parent_null_maps, IColumn* variant_leaf) {
609
1
    auto& nullable_leaf = assert_cast<ColumnNullable&>(*variant_leaf);
610
1
    const IColumn* value_column = &column;
611
1
    const NullMap* leaf_null_map = nullptr;
612
1
    if (const auto* nullable_column = check_and_get_column<ColumnNullable>(&column)) {
613
0
        value_column = &nullable_column->get_nested_column();
614
0
        leaf_null_map = &nullable_column->get_null_map_data();
615
0
    }
616
617
1
    auto& data = assert_cast<ColumnString&>(nullable_leaf.get_nested_column());
618
1
    auto& null_map = nullable_leaf.get_null_map_data();
619
1
    null_map.reserve(null_map.size() + rows);
620
3
    for (size_t i = 0; i < rows; ++i) {
621
2
        const size_t row = start + i;
622
2
        const bool leaf_is_null = leaf_null_map != nullptr && (*leaf_null_map)[row];
623
2
        const bool is_null = leaf_is_null || has_direct_typed_parent_null(parent_null_maps, row);
624
2
        if (is_null) {
625
1
            data.insert_default();
626
1
            null_map.push_back(1);
627
1
            continue;
628
1
        }
629
1
        StringRef bytes = value_column->get_data_at(row);
630
1
        if (bytes.size != 16) {
631
0
            return Status::Corruption("Parquet VARIANT UUID typed_value has invalid length {}",
632
0
                                      bytes.size);
633
0
        }
634
1
        std::string uuid =
635
1
                parquet::format_variant_uuid(reinterpret_cast<const uint8_t*>(bytes.data));
636
1
        data.insert_data(uuid.data(), uuid.size());
637
1
        null_map.push_back(0);
638
1
    }
639
1
    return Status::OK();
640
1
}
641
642
3
static void append_json_string(std::string_view value, std::string* json) {
643
3
    auto column = ColumnString::create();
644
3
    VectorBufferWriter writer(*column);
645
3
    writer.write_json_string(value);
646
3
    writer.commit();
647
3
    json->append(column->get_data_at(0).data, column->get_data_at(0).size);
648
3
}
649
650
static bool is_column_selected(const FieldSchema& field_schema,
651
88
                               const std::set<uint64_t>& column_ids) {
652
88
    return column_ids.empty() || column_ids.find(field_schema.get_column_id()) != column_ids.end();
653
88
}
654
655
static bool has_selected_column(const FieldSchema& field_schema,
656
88
                                const std::set<uint64_t>& column_ids) {
657
88
    if (is_column_selected(field_schema, column_ids)) {
658
80
        return true;
659
80
    }
660
8
    return std::any_of(field_schema.children.begin(), field_schema.children.end(),
661
8
                       [&column_ids](const FieldSchema& child) {
662
5
                           return has_selected_column(child, column_ids);
663
5
                       });
664
88
}
665
666
26
static bool is_direct_variant_leaf_type(const DataTypePtr& data_type) {
667
26
    const auto& type = remove_nullable(data_type);
668
26
    switch (type->get_primitive_type()) {
669
0
    case TYPE_BOOLEAN:
670
0
    case TYPE_TINYINT:
671
0
    case TYPE_SMALLINT:
672
4
    case TYPE_INT:
673
11
    case TYPE_BIGINT:
674
11
    case TYPE_LARGEINT:
675
11
    case TYPE_DECIMALV2:
676
11
    case TYPE_DECIMAL32:
677
12
    case TYPE_DECIMAL64:
678
12
    case TYPE_DECIMAL128I:
679
12
    case TYPE_DECIMAL256:
680
12
    case TYPE_STRING:
681
12
    case TYPE_CHAR:
682
12
    case TYPE_VARCHAR:
683
16
    case TYPE_VARBINARY:
684
16
        return true;
685
1
    case TYPE_TIMEV2:
686
1
    case TYPE_DATE:
687
1
    case TYPE_DATETIME:
688
2
    case TYPE_DATEV2:
689
3
    case TYPE_DATETIMEV2:
690
3
    case TYPE_TIMESTAMPTZ:
691
3
        return true;
692
5
    case TYPE_ARRAY: {
693
5
        const auto* array_type = assert_cast<const DataTypeArray*>(type.get());
694
5
        return !contains_temporal_variant_leaf_type(array_type->get_nested_type()) &&
695
5
               is_direct_variant_leaf_type(array_type->get_nested_type());
696
3
    }
697
2
    default:
698
2
        return false;
699
26
    }
700
26
}
701
702
static bool can_direct_read_typed_value(const FieldSchema& field_schema, bool allow_variant_wrapper,
703
27
                                        const std::set<uint64_t>& column_ids) {
704
27
    if (!has_selected_column(field_schema, column_ids)) {
705
0
        return true;
706
0
    }
707
27
    if (allow_variant_wrapper && is_variant_wrapper_field(field_schema, false)) {
708
0
        const int value_idx = find_child_idx(field_schema, "value");
709
0
        const int typed_value_idx = find_child_idx(field_schema, "typed_value");
710
0
        return (value_idx < 0 ||
711
0
                !has_selected_column(field_schema.children[value_idx], column_ids)) &&
712
0
               typed_value_idx >= 0 &&
713
0
               can_direct_read_typed_value(field_schema.children[typed_value_idx], false,
714
0
                                           column_ids);
715
0
    }
716
717
27
    const auto& type = remove_nullable(field_schema.data_type);
718
27
    if (type->get_primitive_type() == TYPE_STRUCT) {
719
12
        return std::all_of(field_schema.children.begin(), field_schema.children.end(),
720
16
                           [&column_ids](const FieldSchema& child) {
721
16
                               return can_direct_read_typed_value(child, true, column_ids);
722
16
                           });
723
12
    }
724
15
    return is_direct_variant_leaf_type(field_schema.data_type);
725
27
}
726
727
static bool has_selected_direct_typed_leaf(const FieldSchema& field_schema,
728
                                           bool allow_variant_wrapper,
729
7
                                           const std::set<uint64_t>& column_ids) {
730
7
    if (!has_selected_column(field_schema, column_ids)) {
731
2
        return false;
732
2
    }
733
5
    if (allow_variant_wrapper && is_variant_wrapper_field(field_schema, false)) {
734
0
        const int typed_value_idx = find_child_idx(field_schema, "typed_value");
735
0
        DCHECK_GE(typed_value_idx, 0);
736
0
        return has_selected_direct_typed_leaf(field_schema.children[typed_value_idx], false,
737
0
                                              column_ids);
738
0
    }
739
740
5
    const auto& type = remove_nullable(field_schema.data_type);
741
5
    if (type->get_primitive_type() == TYPE_STRUCT) {
742
3
        return std::any_of(field_schema.children.begin(), field_schema.children.end(),
743
3
                           [&column_ids](const FieldSchema& child) {
744
3
                               return has_selected_direct_typed_leaf(child, true, column_ids);
745
3
                           });
746
3
    }
747
2
    return is_direct_variant_leaf_type(field_schema.data_type);
748
5
}
749
750
static bool can_use_direct_typed_only_value(const FieldSchema& variant_field,
751
5
                                            const std::set<uint64_t>& column_ids) {
752
5
    const int value_idx = find_child_idx(variant_field, "value");
753
5
    const int typed_value_idx = find_child_idx(variant_field, "typed_value");
754
5
    return (value_idx < 0 || !has_selected_column(variant_field.children[value_idx], column_ids)) &&
755
5
           typed_value_idx >= 0 &&
756
5
           has_selected_direct_typed_leaf(variant_field.children[typed_value_idx], false,
757
4
                                          column_ids) &&
758
5
           can_direct_read_typed_value(variant_field.children[typed_value_idx], false, column_ids);
759
5
}
760
761
3
static DataTypePtr make_variant_struct_reader_type(const FieldSchema& field) {
762
3
    DataTypes child_types;
763
3
    Strings child_names;
764
3
    child_types.reserve(field.children.size());
765
3
    child_names.reserve(field.children.size());
766
6
    for (const auto& child : field.children) {
767
6
        child_types.push_back(make_nullable(child.data_type));
768
6
        child_names.push_back(child.name);
769
6
    }
770
3
    return std::make_shared<DataTypeStruct>(child_types, child_names);
771
3
}
772
773
static ColumnPtr make_variant_struct_read_column(const FieldSchema& field,
774
2
                                                 const DataTypePtr& variant_struct_type) {
775
2
    if (field.data_type->is_nullable()) {
776
1
        return make_nullable(variant_struct_type)->create_column();
777
1
    }
778
1
    return variant_struct_type->create_column();
779
2
}
780
781
14
static void fill_variant_field_info(FieldWithDataType* value) {
782
14
    FieldInfo info;
783
14
    variant_util::get_field_info(value->field, &info);
784
14
    DCHECK_LE(info.num_dimensions, std::numeric_limits<uint8_t>::max());
785
14
    value->base_scalar_type_id = info.scalar_type_id;
786
14
    value->num_dimensions = static_cast<uint8_t>(info.num_dimensions);
787
14
}
788
789
11
static void fill_variant_leaf_type_info(const DataTypePtr& data_type, FieldWithDataType* value) {
790
11
    auto leaf_type = remove_nullable(data_type);
791
12
    while (leaf_type->get_primitive_type() == TYPE_ARRAY) {
792
1
        leaf_type = remove_nullable(
793
1
                assert_cast<const DataTypeArray*>(leaf_type.get())->get_nested_type());
794
1
    }
795
11
    if (is_decimal(leaf_type->get_primitive_type())) {
796
1
        value->precision = leaf_type->get_precision();
797
1
        value->scale = leaf_type->get_scale();
798
1
    }
799
11
}
800
801
template <PrimitiveType Primitive>
802
0
static Status fill_floating_point_variant_field(const Field& field, FieldWithDataType* value) {
803
0
    const auto typed_value = field.get<Primitive>();
804
0
    if (!std::isfinite(typed_value)) {
805
0
        return Status::NotSupported(
806
0
                "Parquet VARIANT non-finite floating point typed_value is not supported");
807
0
    }
808
0
    value->field = field;
809
0
    fill_variant_field_info(value);
810
0
    return Status::OK();
811
0
}
Unexecuted instantiation: vparquet_column_reader.cpp:_ZN5dorisL33fill_floating_point_variant_fieldILNS_13PrimitiveTypeE8EEENS_6StatusERKNS_5FieldEPNS_17FieldWithDataTypeE
Unexecuted instantiation: vparquet_column_reader.cpp:_ZN5dorisL33fill_floating_point_variant_fieldILNS_13PrimitiveTypeE9EEENS_6StatusERKNS_5FieldEPNS_17FieldWithDataTypeE
812
813
56
static bool is_uuid_typed_value_field(const FieldSchema& field_schema) {
814
56
    return field_schema.parquet_schema.__isset.logicalType &&
815
56
           field_schema.parquet_schema.logicalType.__isset.UUID;
816
56
}
817
818
13
static bool contains_uuid_typed_value_field(const FieldSchema& field_schema) {
819
13
    return is_uuid_typed_value_field(field_schema) ||
820
13
           std::any_of(
821
11
                   field_schema.children.begin(), field_schema.children.end(),
822
11
                   [](const FieldSchema& child) { return contains_uuid_typed_value_field(child); });
823
13
}
824
825
3
static Status uuid_field_to_string(const Field& field, std::string* uuid) {
826
3
    StringRef bytes;
827
3
    switch (field.get_type()) {
828
0
    case TYPE_STRING:
829
0
        bytes = StringRef(field.get<TYPE_STRING>());
830
0
        break;
831
0
    case TYPE_CHAR:
832
0
        bytes = StringRef(field.get<TYPE_CHAR>());
833
0
        break;
834
0
    case TYPE_VARCHAR:
835
0
        bytes = StringRef(field.get<TYPE_VARCHAR>());
836
0
        break;
837
3
    case TYPE_VARBINARY:
838
3
        bytes = field.get<TYPE_VARBINARY>().to_string_ref();
839
3
        break;
840
0
    default:
841
0
        return Status::Corruption("Parquet VARIANT UUID typed_value has unexpected Doris type {}",
842
0
                                  field.get_type_name());
843
3
    }
844
3
    if (bytes.size != 16) {
845
0
        return Status::Corruption("Parquet VARIANT UUID typed_value has invalid length {}",
846
0
                                  bytes.size);
847
0
    }
848
3
    *uuid = parquet::format_variant_uuid(reinterpret_cast<const uint8_t*>(bytes.data));
849
3
    return Status::OK();
850
3
}
851
852
3
static Status fill_uuid_variant_field(const Field& field, FieldWithDataType* value) {
853
3
    std::string uuid;
854
3
    RETURN_IF_ERROR(uuid_field_to_string(field, &uuid));
855
3
    value->field = Field::create_field<TYPE_STRING>(std::move(uuid));
856
3
    value->base_scalar_type_id = TYPE_STRING;
857
3
    return Status::OK();
858
3
}
859
860
1
static uint8_t direct_array_dimensions(const DataTypePtr& data_type) {
861
1
    uint8_t num_dimensions = 0;
862
1
    auto type = remove_nullable(data_type);
863
2
    while (type->get_primitive_type() == TYPE_ARRAY) {
864
1
        ++num_dimensions;
865
1
        type = remove_nullable(assert_cast<const DataTypeArray*>(type.get())->get_nested_type());
866
1
    }
867
1
    return num_dimensions;
868
1
}
869
870
static Status convert_uuid_array_value(const FieldSchema& field_schema, const Field& field,
871
5
                                       Field* converted) {
872
5
    if (field.is_null()) {
873
1
        *converted = Field();
874
1
        return Status::OK();
875
1
    }
876
877
4
    const auto& type = remove_nullable(field_schema.data_type);
878
4
    if (type->get_primitive_type() == TYPE_ARRAY) {
879
2
        if (field_schema.children.empty()) {
880
0
            return Status::Corruption(
881
0
                    "Parquet VARIANT UUID array typed_value has no element schema");
882
0
        }
883
2
        Array converted_elements;
884
2
        const auto& elements = field.get<TYPE_ARRAY>();
885
2
        converted_elements.reserve(elements.size());
886
3
        for (const auto& element : elements) {
887
3
            Field converted_element;
888
3
            RETURN_IF_ERROR(convert_uuid_array_value(field_schema.children[0], element,
889
3
                                                     &converted_element));
890
3
            converted_elements.push_back(std::move(converted_element));
891
3
        }
892
2
        *converted = Field::create_field<TYPE_ARRAY>(std::move(converted_elements));
893
2
        return Status::OK();
894
2
    }
895
896
2
    if (!is_uuid_typed_value_field(field_schema)) {
897
0
        return Status::Corruption("Parquet VARIANT UUID array has non-UUID element schema");
898
0
    }
899
2
    FieldWithDataType value;
900
2
    RETURN_IF_ERROR(fill_uuid_variant_field(field, &value));
901
2
    *converted = std::move(value.field);
902
2
    return Status::OK();
903
2
}
904
905
static Status insert_direct_typed_uuid_array_leaf_range(
906
        const FieldSchema& field_schema, const IColumn& column, size_t start, size_t rows,
907
1
        const std::vector<const NullMap*>& parent_null_maps, IColumn* variant_leaf) {
908
1
    auto& nullable_leaf = assert_cast<ColumnNullable&>(*variant_leaf);
909
1
    const IColumn* value_column = &column;
910
1
    const NullMap* leaf_null_map = nullptr;
911
1
    if (const auto* nullable_column = check_and_get_column<ColumnNullable>(&column)) {
912
0
        value_column = &nullable_column->get_nested_column();
913
0
        leaf_null_map = &nullable_column->get_null_map_data();
914
0
    }
915
916
1
    auto& data = nullable_leaf.get_nested_column();
917
1
    auto& null_map = nullable_leaf.get_null_map_data();
918
1
    null_map.reserve(null_map.size() + rows);
919
3
    for (size_t i = 0; i < rows; ++i) {
920
2
        const size_t row = start + i;
921
2
        const bool leaf_is_null = leaf_null_map != nullptr && (*leaf_null_map)[row];
922
2
        const bool is_null = leaf_is_null || has_direct_typed_parent_null(parent_null_maps, row);
923
2
        if (is_null) {
924
1
            data.insert_default();
925
1
            null_map.push_back(1);
926
1
            continue;
927
1
        }
928
929
1
        Field field;
930
1
        value_column->get(row, field);
931
1
        Field converted;
932
1
        RETURN_IF_ERROR(convert_uuid_array_value(field_schema, field, &converted));
933
1
        data.insert(converted);
934
1
        null_map.push_back(0);
935
1
    }
936
1
    return Status::OK();
937
1
}
938
939
static Status fill_uuid_array_variant_field(const FieldSchema& field_schema, const Field& field,
940
1
                                            FieldWithDataType* value, bool* present) {
941
1
    if (field.is_null()) {
942
0
        *present = false;
943
0
        return Status::OK();
944
0
    }
945
1
    *present = true;
946
1
    RETURN_IF_ERROR(convert_uuid_array_value(field_schema, field, &value->field));
947
1
    value->base_scalar_type_id = TYPE_STRING;
948
1
    value->num_dimensions = direct_array_dimensions(field_schema.data_type);
949
1
    return Status::OK();
950
1
}
951
952
static Status field_to_variant_field(const FieldSchema& field_schema, const Field& field,
953
12
                                     FieldWithDataType* value, bool* present) {
954
12
    if (field.is_null()) {
955
0
        *present = false;
956
0
        return Status::OK();
957
0
    }
958
12
    *present = true;
959
12
    if (is_uuid_typed_value_field(field_schema)) {
960
1
        return fill_uuid_variant_field(field, value);
961
1
    }
962
11
    const DataTypePtr& type = remove_nullable(field_schema.data_type);
963
11
    switch (type->get_primitive_type()) {
964
0
    case TYPE_BOOLEAN:
965
0
    case TYPE_TINYINT:
966
0
    case TYPE_SMALLINT:
967
5
    case TYPE_INT:
968
5
    case TYPE_BIGINT:
969
5
    case TYPE_LARGEINT:
970
5
    case TYPE_DECIMALV2:
971
5
    case TYPE_DECIMAL32:
972
5
    case TYPE_DECIMAL64:
973
5
    case TYPE_DECIMAL128I:
974
5
    case TYPE_DECIMAL256:
975
8
    case TYPE_STRING:
976
8
    case TYPE_CHAR:
977
8
    case TYPE_VARCHAR:
978
10
    case TYPE_VARBINARY:
979
11
    case TYPE_ARRAY:
980
11
        value->field = field;
981
11
        fill_variant_field_info(value);
982
11
        fill_variant_leaf_type_info(type, value);
983
11
        return Status::OK();
984
0
    case TYPE_FLOAT:
985
0
        return fill_floating_point_variant_field<TYPE_FLOAT>(field, value);
986
0
    case TYPE_DOUBLE:
987
0
        return fill_floating_point_variant_field<TYPE_DOUBLE>(field, value);
988
0
    case TYPE_TIMEV2:
989
0
        value->field = Field::create_field<TYPE_BIGINT>(
990
0
                static_cast<int64_t>(std::llround(field.get<TYPE_TIMEV2>())));
991
0
        value->base_scalar_type_id = TYPE_BIGINT;
992
0
        return Status::OK();
993
0
    case TYPE_DATE:
994
0
        value->field = Field::create_field<TYPE_BIGINT>(variant_date_value(field.get<TYPE_DATE>()));
995
0
        value->base_scalar_type_id = TYPE_BIGINT;
996
0
        return Status::OK();
997
0
    case TYPE_DATETIME:
998
0
        value->field = Field::create_field<TYPE_BIGINT>(
999
0
                variant_datetime_value(field.get<TYPE_DATETIME>()));
1000
0
        value->base_scalar_type_id = TYPE_BIGINT;
1001
0
        return Status::OK();
1002
0
    case TYPE_DATEV2:
1003
0
        value->field =
1004
0
                Field::create_field<TYPE_BIGINT>(variant_date_value(field.get<TYPE_DATEV2>()));
1005
0
        value->base_scalar_type_id = TYPE_BIGINT;
1006
0
        return Status::OK();
1007
0
    case TYPE_DATETIMEV2:
1008
0
        value->field = Field::create_field<TYPE_BIGINT>(
1009
0
                variant_datetime_value(field.get<TYPE_DATETIMEV2>()));
1010
0
        value->base_scalar_type_id = TYPE_BIGINT;
1011
0
        return Status::OK();
1012
0
    case TYPE_TIMESTAMPTZ:
1013
0
        value->field = Field::create_field<TYPE_BIGINT>(
1014
0
                variant_datetime_value(field.get<TYPE_TIMESTAMPTZ>()));
1015
0
        value->base_scalar_type_id = TYPE_BIGINT;
1016
0
        return Status::OK();
1017
0
    default:
1018
0
        return Status::Corruption("Unsupported Parquet VARIANT typed_value Doris type {}",
1019
0
                                  type->get_name());
1020
11
    }
1021
11
}
1022
1023
static Status typed_value_to_json(const FieldSchema& typed_value_field, const Field& field,
1024
                                  const std::string& metadata, std::string* json, bool* present);
1025
1026
static Status serialize_field_to_json(const DataTypePtr& data_type, const Field& field,
1027
3
                                      std::string* json) {
1028
3
    MutableColumnPtr column = data_type->create_column();
1029
3
    column->insert(field);
1030
1031
3
    auto json_column = ColumnString::create();
1032
3
    VectorBufferWriter writer(*json_column);
1033
3
    auto serde = data_type->get_serde();
1034
3
    DataTypeSerDe::FormatOptions options;
1035
3
    RETURN_IF_ERROR(serde->serialize_one_cell_to_json(*column, 0, writer, options));
1036
3
    writer.commit();
1037
3
    *json = json_column->get_data_at(0).to_string();
1038
3
    return Status::OK();
1039
3
}
1040
1041
static Status scalar_typed_value_to_json(const FieldSchema& field_schema, const Field& field,
1042
3
                                         std::string* json, bool* present) {
1043
3
    FieldWithDataType value;
1044
3
    RETURN_IF_ERROR(field_to_variant_field(field_schema, field, &value, present));
1045
3
    if (!*present) {
1046
0
        return Status::OK();
1047
0
    }
1048
3
    if (value.field.is_null()) {
1049
0
        *json = "null";
1050
0
        return Status::OK();
1051
0
    }
1052
3
    if (!is_uuid_typed_value_field(field_schema) &&
1053
3
        remove_nullable(field_schema.data_type)->get_primitive_type() == TYPE_VARBINARY) {
1054
0
        return Status::NotSupported(
1055
0
                "Parquet VARIANT binary typed_value cannot be serialized to JSON");
1056
0
    }
1057
1058
3
    DataTypePtr json_type;
1059
3
    if (value.base_scalar_type_id != PrimitiveType::INVALID_TYPE) {
1060
3
        json_type = DataTypeFactory::instance().create_data_type(value.base_scalar_type_id, false,
1061
3
                                                                 value.precision, value.scale);
1062
3
    } else {
1063
0
        json_type = remove_nullable(field_schema.data_type);
1064
0
    }
1065
3
    return serialize_field_to_json(json_type, value.field, json);
1066
3
}
1067
1068
static Status resolve_variant_metadata(const FieldSchema& variant_field, const Struct& fields,
1069
                                       const std::string* inherited_metadata, std::string* metadata,
1070
3
                                       bool* has_metadata) {
1071
3
    *has_metadata = false;
1072
3
    if (inherited_metadata != nullptr) {
1073
3
        *metadata = *inherited_metadata;
1074
3
        *has_metadata = true;
1075
3
    }
1076
1077
3
    const int metadata_idx = find_child_idx(variant_field, "metadata");
1078
3
    if (metadata_idx >= 0) {
1079
0
        bool metadata_present = false;
1080
0
        RETURN_IF_ERROR(get_binary_field(fields[metadata_idx], metadata, &metadata_present));
1081
0
        *has_metadata = metadata_present;
1082
0
    }
1083
3
    return Status::OK();
1084
3
}
1085
1086
static Status variant_typed_value_to_json(const FieldSchema& variant_field, const Struct& fields,
1087
                                          const std::string& metadata, std::string* typed_json,
1088
3
                                          bool* typed_present) {
1089
3
    *typed_present = false;
1090
3
    const int typed_value_idx = find_child_idx(variant_field, "typed_value");
1091
3
    if (typed_value_idx < 0) {
1092
0
        return Status::OK();
1093
0
    }
1094
3
    return typed_value_to_json(variant_field.children[typed_value_idx], fields[typed_value_idx],
1095
3
                               metadata, typed_json, typed_present);
1096
3
}
1097
1098
static Status variant_residual_value_to_json(const FieldSchema& variant_field, const Struct& fields,
1099
                                             const std::string& metadata, bool has_metadata,
1100
3
                                             std::string* value_json, bool* value_present) {
1101
3
    *value_present = false;
1102
3
    const int value_idx = find_child_idx(variant_field, "value");
1103
3
    if (value_idx < 0) {
1104
0
        return Status::OK();
1105
0
    }
1106
1107
3
    std::string value;
1108
3
    RETURN_IF_ERROR(get_binary_field(fields[value_idx], &value, value_present));
1109
3
    if (!*value_present) {
1110
0
        return Status::OK();
1111
0
    }
1112
3
    if (!has_metadata) {
1113
0
        return Status::Corruption("Parquet VARIANT value is present without metadata");
1114
0
    }
1115
3
    return parquet::decode_variant_to_json(StringRef(metadata.data(), metadata.size()),
1116
3
                                           StringRef(value.data(), value.size()), value_json);
1117
3
}
1118
1119
static Status merge_variant_value_and_typed_json(const std::string& value_json,
1120
3
                                                 const std::string& typed_json, std::string* json) {
1121
3
    VariantMap value_values;
1122
3
    RETURN_IF_ERROR(parse_json_to_variant_map(value_json, PathInData(), &value_values));
1123
3
    VariantMap typed_values;
1124
3
    RETURN_IF_ERROR(parse_json_to_variant_map(typed_json, PathInData(), &typed_values));
1125
3
    erase_shadowed_empty_object_markers(&value_values, &typed_values);
1126
3
    auto root_value = value_values.find(PathInData());
1127
3
    if (root_value != value_values.end() && !is_empty_object_marker(root_value->second)) {
1128
0
        return Status::Corruption(
1129
0
                "Parquet VARIANT has conflicting non-object value and typed_value");
1130
0
    }
1131
3
    RETURN_IF_ERROR(
1132
3
            check_no_shredded_value_typed_duplicates(value_values, typed_values, PathInData()));
1133
2
    value_values.merge(std::move(typed_values));
1134
2
    return variant_map_to_json(std::move(value_values), json);
1135
3
}
1136
1137
static Status variant_to_json(const FieldSchema& variant_field, const Field& field,
1138
                              const std::string* inherited_metadata, std::string* json,
1139
3
                              bool* present) {
1140
3
    if (field.is_null()) {
1141
0
        *present = false;
1142
0
        return Status::OK();
1143
0
    }
1144
1145
3
    const auto& fields = field.get<TYPE_STRUCT>();
1146
3
    std::string metadata;
1147
3
    bool has_metadata = false;
1148
3
    RETURN_IF_ERROR(resolve_variant_metadata(variant_field, fields, inherited_metadata, &metadata,
1149
3
                                             &has_metadata));
1150
1151
3
    std::string typed_json;
1152
3
    bool typed_present = false;
1153
3
    RETURN_IF_ERROR(variant_typed_value_to_json(variant_field, fields, metadata, &typed_json,
1154
3
                                                &typed_present));
1155
1156
3
    std::string value_json;
1157
3
    bool value_present = false;
1158
3
    RETURN_IF_ERROR(variant_residual_value_to_json(variant_field, fields, metadata, has_metadata,
1159
3
                                                   &value_json, &value_present));
1160
1161
3
    if (value_present && typed_present) {
1162
3
        RETURN_IF_ERROR(merge_variant_value_and_typed_json(value_json, typed_json, json));
1163
2
        *present = true;
1164
2
        return Status::OK();
1165
3
    }
1166
1167
0
    if (typed_present) {
1168
0
        *json = std::move(typed_json);
1169
0
        *present = true;
1170
0
        return Status::OK();
1171
0
    }
1172
0
    if (value_present) {
1173
0
        *json = std::move(value_json);
1174
0
        *present = true;
1175
0
        return Status::OK();
1176
0
    }
1177
1178
0
    *present = false;
1179
0
    return Status::OK();
1180
0
}
1181
1182
static Status shredded_field_to_json(const FieldSchema& field_schema, const Field& field,
1183
                                     const std::string& metadata, std::string* json, bool* present,
1184
3
                                     bool allow_scalar_typed_value_only_wrapper) {
1185
3
    if (is_variant_wrapper_field(field_schema, allow_scalar_typed_value_only_wrapper)) {
1186
0
        return variant_to_json(field_schema, field, &metadata, json, present);
1187
0
    }
1188
3
    if (is_value_only_variant_wrapper_candidate(field_schema)) {
1189
0
        Status st = variant_to_json(field_schema, field, &metadata, json, present);
1190
0
        if (st.ok()) {
1191
0
            return st;
1192
0
        }
1193
0
        if (!st.is<ErrorCode::CORRUPTION>()) {
1194
0
            return st;
1195
0
        }
1196
0
    }
1197
3
    return typed_value_to_json(field_schema, field, metadata, json, present);
1198
3
}
1199
1200
static Status typed_array_to_json(const FieldSchema& typed_value_field, const Field& field,
1201
0
                                  const std::string& metadata, std::string* json, bool* present) {
1202
0
    if (field.is_null()) {
1203
0
        *present = false;
1204
0
        return Status::OK();
1205
0
    }
1206
0
    if (typed_value_field.children.empty()) {
1207
0
        return Status::Corruption("Parquet VARIANT array typed_value has no element schema");
1208
0
    }
1209
1210
0
    const auto& elements = field.get<TYPE_ARRAY>();
1211
0
    const auto& element_schema = typed_value_field.children[0];
1212
0
    json->clear();
1213
0
    json->push_back('[');
1214
0
    for (size_t i = 0; i < elements.size(); ++i) {
1215
0
        if (i != 0) {
1216
0
            json->push_back(',');
1217
0
        }
1218
0
        std::string element_json;
1219
0
        bool element_present = false;
1220
0
        RETURN_IF_ERROR(shredded_field_to_json(element_schema, elements[i], metadata, &element_json,
1221
0
                                               &element_present, true));
1222
0
        if (element_present) {
1223
0
            json->append(element_json);
1224
0
        } else {
1225
0
            json->append("null");
1226
0
        }
1227
0
    }
1228
0
    json->push_back(']');
1229
0
    *present = true;
1230
0
    return Status::OK();
1231
0
}
1232
1233
static Status typed_struct_to_json(const FieldSchema& typed_value_field, const Field& field,
1234
3
                                   const std::string& metadata, std::string* json, bool* present) {
1235
3
    if (field.is_null()) {
1236
0
        *present = false;
1237
0
        return Status::OK();
1238
0
    }
1239
1240
3
    const auto& fields = field.get<TYPE_STRUCT>();
1241
3
    json->clear();
1242
3
    json->push_back('{');
1243
3
    bool first = true;
1244
6
    for (int i = 0; i < typed_value_field.children.size(); ++i) {
1245
3
        std::string child_json;
1246
3
        bool child_present = false;
1247
3
        RETURN_IF_ERROR(shredded_field_to_json(typed_value_field.children[i], fields[i], metadata,
1248
3
                                               &child_json, &child_present, false));
1249
3
        if (!child_present) {
1250
0
            continue;
1251
0
        }
1252
3
        if (!first) {
1253
0
            json->push_back(',');
1254
0
        }
1255
3
        append_json_string(typed_value_field.children[i].name, json);
1256
3
        json->push_back(':');
1257
3
        json->append(child_json);
1258
3
        first = false;
1259
3
    }
1260
3
    json->push_back('}');
1261
3
    *present = true;
1262
3
    return Status::OK();
1263
3
}
1264
1265
static Status typed_value_to_json(const FieldSchema& typed_value_field, const Field& field,
1266
6
                                  const std::string& metadata, std::string* json, bool* present) {
1267
6
    const DataTypePtr& typed_type = remove_nullable(typed_value_field.data_type);
1268
6
    switch (typed_type->get_primitive_type()) {
1269
3
    case TYPE_STRUCT:
1270
3
        return typed_struct_to_json(typed_value_field, field, metadata, json, present);
1271
0
    case TYPE_ARRAY:
1272
0
        return typed_array_to_json(typed_value_field, field, metadata, json, present);
1273
3
    default:
1274
3
        return scalar_typed_value_to_json(typed_value_field, field, json, present);
1275
6
    }
1276
6
}
1277
1278
static Status typed_value_to_variant_map(const FieldSchema& typed_value_field, const Field& field,
1279
                                         const std::string& metadata, PathInDataBuilder* path,
1280
                                         VariantMap* values, bool* present,
1281
                                         std::deque<std::string>* string_values);
1282
1283
static Status variant_to_variant_map(const FieldSchema& variant_field, const Field& field,
1284
                                     const std::string* inherited_metadata, PathInDataBuilder* path,
1285
                                     VariantMap* values, bool* present,
1286
21
                                     std::deque<std::string>* string_values) {
1287
21
    if (field.is_null()) {
1288
0
        *present = false;
1289
0
        return Status::OK();
1290
0
    }
1291
21
    const auto& fields = field.get<TYPE_STRUCT>();
1292
21
    const int metadata_idx = find_child_idx(variant_field, "metadata");
1293
21
    const int value_idx = find_child_idx(variant_field, "value");
1294
21
    const int typed_value_idx = find_child_idx(variant_field, "typed_value");
1295
1296
21
    std::string metadata;
1297
21
    bool has_metadata = false;
1298
21
    if (inherited_metadata != nullptr) {
1299
2
        metadata = *inherited_metadata;
1300
2
        has_metadata = true;
1301
2
    }
1302
21
    if (metadata_idx >= 0) {
1303
19
        bool metadata_present = false;
1304
19
        RETURN_IF_ERROR(get_binary_field(fields[metadata_idx], &metadata, &metadata_present));
1305
19
        has_metadata = metadata_present;
1306
19
    }
1307
1308
21
    VariantMap value_values;
1309
21
    bool value_present = false;
1310
21
    const PathInData current_path = path->build();
1311
21
    if (value_idx >= 0) {
1312
14
        std::string value;
1313
14
        RETURN_IF_ERROR(get_binary_field(fields[value_idx], &value, &value_present));
1314
14
        if (value_present) {
1315
10
            if (!has_metadata) {
1316
0
                return Status::Corruption("Parquet VARIANT value is present without metadata");
1317
0
            }
1318
10
            RETURN_IF_ERROR(parquet::decode_variant_to_variant_map(
1319
10
                    StringRef(metadata.data(), metadata.size()),
1320
10
                    StringRef(value.data(), value.size()), current_path, &value_values,
1321
10
                    string_values));
1322
10
        }
1323
14
    }
1324
1325
20
    VariantMap typed_values;
1326
20
    bool typed_present = false;
1327
20
    if (typed_value_idx >= 0) {
1328
15
        RETURN_IF_ERROR(typed_value_to_variant_map(variant_field.children[typed_value_idx],
1329
15
                                                   fields[typed_value_idx], metadata, path,
1330
15
                                                   &typed_values, &typed_present, string_values));
1331
15
    }
1332
1333
20
    erase_shadowed_empty_object_markers(&value_values, &typed_values);
1334
20
    auto current_value = value_values.find(current_path);
1335
20
    if (value_present && typed_present && current_value != value_values.end() &&
1336
20
        !is_empty_object_marker(current_value->second)) {
1337
0
        return Status::Corruption(
1338
0
                "Parquet VARIANT has conflicting non-object value and typed_value");
1339
0
    }
1340
20
    RETURN_IF_ERROR(
1341
20
            check_no_shredded_value_typed_duplicates(value_values, typed_values, current_path));
1342
19
    values->merge(std::move(value_values));
1343
19
    values->merge(std::move(typed_values));
1344
19
    *present = value_present || typed_present;
1345
19
    return Status::OK();
1346
20
}
1347
1348
static Status shredded_field_to_variant_map(const FieldSchema& field_schema, const Field& field,
1349
                                            const std::string& metadata, PathInDataBuilder* path,
1350
                                            VariantMap* values, bool* present,
1351
16
                                            std::deque<std::string>* string_values) {
1352
16
    if (is_variant_wrapper_field(field_schema, false)) {
1353
0
        return variant_to_variant_map(field_schema, field, &metadata, path, values, present,
1354
0
                                      string_values);
1355
0
    }
1356
16
    if (is_value_only_variant_wrapper_candidate(field_schema)) {
1357
2
        Status st = variant_to_variant_map(field_schema, field, &metadata, path, values, present,
1358
2
                                           string_values);
1359
2
        if (st.ok()) {
1360
1
            return st;
1361
1
        }
1362
1
        if (!st.is<ErrorCode::CORRUPTION>()) {
1363
0
            return st;
1364
0
        }
1365
1
    }
1366
15
    return typed_value_to_variant_map(field_schema, field, metadata, path, values, present,
1367
15
                                      string_values);
1368
16
}
1369
1370
static Status append_typed_field_to_variant_map(const FieldSchema& typed_value_field,
1371
                                                const Field& field, PathInDataBuilder* path,
1372
9
                                                VariantMap* values, bool* present) {
1373
9
    FieldWithDataType value;
1374
9
    RETURN_IF_ERROR(field_to_variant_field(typed_value_field, field, &value, present));
1375
9
    if (*present) {
1376
9
        (*values)[path->build()] = std::move(value);
1377
9
    }
1378
9
    return Status::OK();
1379
9
}
1380
1381
1
static void move_variant_map_to_field(VariantMap&& element_values, FieldWithDataType* value) {
1382
1
    if (element_values.size() == 1 && element_values.begin()->first.empty()) {
1383
0
        *value = std::move(element_values.begin()->second);
1384
0
        return;
1385
0
    }
1386
1
    value->field = Field::create_field<TYPE_VARIANT>(std::move(element_values));
1387
1
    fill_variant_field_info(value);
1388
1
}
1389
1390
static Status typed_array_to_variant_map(const FieldSchema& typed_value_field, const Field& field,
1391
                                         const std::string& metadata, PathInDataBuilder* path,
1392
                                         VariantMap* values, bool* present,
1393
4
                                         std::deque<std::string>* string_values) {
1394
4
    if (contains_uuid_typed_value_field(typed_value_field) &&
1395
4
        is_direct_variant_leaf_type(typed_value_field.data_type)) {
1396
1
        FieldWithDataType value;
1397
1
        RETURN_IF_ERROR(fill_uuid_array_variant_field(typed_value_field, field, &value, present));
1398
1
        if (*present) {
1399
1
            (*values)[path->build()] = std::move(value);
1400
1
        }
1401
1
        return Status::OK();
1402
1
    }
1403
3
    if (is_direct_variant_leaf_type(typed_value_field.data_type)) {
1404
1
        return append_typed_field_to_variant_map(typed_value_field, field, path, values, present);
1405
1
    }
1406
1407
2
    if (field.is_null()) {
1408
0
        *present = false;
1409
0
        return Status::OK();
1410
0
    }
1411
2
    if (typed_value_field.children.empty()) {
1412
0
        return Status::Corruption("Parquet VARIANT array typed_value has no element schema");
1413
0
    }
1414
1415
2
    const auto& elements = field.get<TYPE_ARRAY>();
1416
2
    const auto& element_schema = typed_value_field.children[0];
1417
2
    Array array;
1418
2
    array.reserve(elements.size());
1419
2
    for (const auto& element : elements) {
1420
2
        if (element.is_null()) {
1421
1
            array.push_back(Field());
1422
1
            continue;
1423
1
        }
1424
1425
1
        VariantMap element_values;
1426
1
        bool element_present = false;
1427
1
        PathInDataBuilder element_path;
1428
1
        RETURN_IF_ERROR(shredded_field_to_variant_map(element_schema, element, metadata,
1429
1
                                                      &element_path, &element_values,
1430
1
                                                      &element_present, string_values));
1431
1
        if (!element_present) {
1432
0
            array.push_back(Field());
1433
0
            continue;
1434
0
        }
1435
1436
1
        FieldWithDataType element_value;
1437
1
        move_variant_map_to_field(std::move(element_values), &element_value);
1438
1
        array.push_back(std::move(element_value.field));
1439
1
    }
1440
1441
2
    FieldWithDataType value;
1442
2
    const size_t elements_count = array.size();
1443
2
    value.field = Field::create_field<TYPE_ARRAY>(std::move(array));
1444
2
    fill_variant_field_info(&value);
1445
2
    if (value.base_scalar_type_id == INVALID_TYPE) {
1446
1
        RETURN_IF_ERROR(make_jsonb_field(make_null_array_json(elements_count), &value));
1447
1
    }
1448
2
    (*values)[path->build()] = std::move(value);
1449
2
    *present = true;
1450
2
    return Status::OK();
1451
2
}
1452
1453
static Status typed_value_to_variant_map(const FieldSchema& typed_value_field, const Field& field,
1454
                                         const std::string& metadata, PathInDataBuilder* path,
1455
                                         VariantMap* values, bool* present,
1456
30
                                         std::deque<std::string>* string_values) {
1457
30
    if (field.is_null()) {
1458
4
        *present = false;
1459
4
        return Status::OK();
1460
4
    }
1461
26
    const DataTypePtr& typed_type = remove_nullable(typed_value_field.data_type);
1462
26
    if (typed_type->get_primitive_type() == TYPE_STRUCT) {
1463
14
        const auto& fields = field.get<TYPE_STRUCT>();
1464
14
        *present = true;
1465
14
        bool has_present_child = false;
1466
29
        for (int i = 0; i < typed_value_field.children.size(); ++i) {
1467
15
            path->append(typed_value_field.children[i].name, false);
1468
15
            bool child_present = false;
1469
15
            RETURN_IF_ERROR(shredded_field_to_variant_map(typed_value_field.children[i], fields[i],
1470
15
                                                          metadata, path, values, &child_present,
1471
15
                                                          string_values));
1472
15
            has_present_child |= child_present;
1473
15
            path->pop_back();
1474
15
        }
1475
14
        if (!has_present_child) {
1476
3
            RETURN_IF_ERROR(insert_empty_object_marker(path->build(), values));
1477
3
        }
1478
14
        return Status::OK();
1479
14
    }
1480
12
    if (typed_type->get_primitive_type() == TYPE_ARRAY) {
1481
4
        return typed_array_to_variant_map(typed_value_field, field, metadata, path, values, present,
1482
4
                                          string_values);
1483
4
    }
1484
1485
8
    return append_typed_field_to_variant_map(typed_value_field, field, path, values, present);
1486
12
}
1487
1488
static bool direct_typed_value_present_at(const FieldSchema& field_schema, const IColumn& column,
1489
                                          size_t row, bool allow_variant_wrapper,
1490
                                          const std::set<uint64_t>& column_ids,
1491
10
                                          const std::vector<const NullMap*>& parent_null_maps) {
1492
10
    if (!has_selected_column(field_schema, column_ids) ||
1493
10
        has_direct_typed_parent_null(parent_null_maps, row)) {
1494
0
        return false;
1495
0
    }
1496
1497
10
    const IColumn* value_column = &column;
1498
10
    if (const auto* nullable_column = check_and_get_column<ColumnNullable>(&column)) {
1499
9
        const auto& null_map = nullable_column->get_null_map_data();
1500
9
        DCHECK_LT(row, null_map.size());
1501
9
        if (null_map[row]) {
1502
2
            return false;
1503
2
        }
1504
7
        value_column = &nullable_column->get_nested_column();
1505
7
    }
1506
1507
8
    if (allow_variant_wrapper && is_variant_wrapper_field(field_schema, false)) {
1508
0
        const int typed_value_idx = find_child_idx(field_schema, "typed_value");
1509
0
        DCHECK_GE(typed_value_idx, 0);
1510
0
        const auto& typed_struct = assert_cast<const ColumnStruct&>(*value_column);
1511
0
        return direct_typed_value_present_at(field_schema.children[typed_value_idx],
1512
0
                                             typed_struct.get_column(typed_value_idx), row, false,
1513
0
                                             column_ids, parent_null_maps);
1514
0
    }
1515
1516
8
    return true;
1517
8
}
1518
1519
static Status append_direct_typed_empty_object_markers(
1520
        const FieldSchema& field_schema, const ColumnStruct& struct_column, size_t start,
1521
        size_t rows, PathInDataBuilder* path, ColumnVariant* batch,
1522
10
        const std::set<uint64_t>& column_ids, const std::vector<const NullMap*>& parent_null_maps) {
1523
10
    DataTypePtr marker_type = make_nullable(std::make_shared<DataTypeJsonb>());
1524
10
    MutableColumnPtr marker_column = marker_type->create_column();
1525
10
    marker_column->insert_default();
1526
10
    bool has_marker = false;
1527
1528
10
    Field empty_object;
1529
10
    RETURN_IF_ERROR(make_empty_object_field(&empty_object));
1530
26
    for (size_t i = 0; i < rows; ++i) {
1531
16
        const size_t row = start + i;
1532
16
        if (has_direct_typed_parent_null(parent_null_maps, row)) {
1533
6
            marker_column->insert_default();
1534
6
            continue;
1535
6
        }
1536
1537
10
        bool has_present_child = false;
1538
12
        for (int child_idx = 0; child_idx < field_schema.children.size(); ++child_idx) {
1539
10
            if (direct_typed_value_present_at(field_schema.children[child_idx],
1540
10
                                              struct_column.get_column(child_idx), row, true,
1541
10
                                              column_ids, parent_null_maps)) {
1542
8
                has_present_child = true;
1543
8
                break;
1544
8
            }
1545
10
        }
1546
1547
10
        if (has_present_child) {
1548
8
            marker_column->insert_default();
1549
8
            continue;
1550
8
        }
1551
2
        marker_column->insert(empty_object);
1552
2
        has_marker = true;
1553
2
    }
1554
1555
10
    if (!has_marker) {
1556
8
        return Status::OK();
1557
8
    }
1558
2
    if (!batch->add_sub_column(path->build(), std::move(marker_column), marker_type)) {
1559
0
        return Status::Corruption("Failed to add Parquet VARIANT empty typed object marker {}",
1560
0
                                  path->build().get_path());
1561
0
    }
1562
2
    return Status::OK();
1563
2
}
1564
1565
static Status append_direct_typed_column_to_batch(const FieldSchema& field_schema,
1566
                                                  const IColumn& column, size_t start, size_t rows,
1567
                                                  PathInDataBuilder* path, ColumnVariant* batch,
1568
                                                  bool allow_variant_wrapper,
1569
                                                  const std::set<uint64_t>& column_ids,
1570
23
                                                  std::vector<const NullMap*> parent_null_maps) {
1571
23
    if (!has_selected_column(field_schema, column_ids)) {
1572
0
        return Status::OK();
1573
0
    }
1574
1575
23
    const IColumn* value_column = &column;
1576
23
    if (const auto* nullable_column = check_and_get_column<ColumnNullable>(&column)) {
1577
22
        parent_null_maps.push_back(&nullable_column->get_null_map_data());
1578
22
        value_column = &nullable_column->get_nested_column();
1579
22
    }
1580
1581
23
    if (allow_variant_wrapper && is_variant_wrapper_field(field_schema, false)) {
1582
0
        const int typed_value_idx = find_child_idx(field_schema, "typed_value");
1583
0
        DCHECK_GE(typed_value_idx, 0);
1584
0
        const auto& typed_struct = assert_cast<const ColumnStruct&>(*value_column);
1585
0
        return append_direct_typed_column_to_batch(
1586
0
                field_schema.children[typed_value_idx], typed_struct.get_column(typed_value_idx),
1587
0
                start, rows, path, batch, false, column_ids, parent_null_maps);
1588
0
    }
1589
1590
23
    const auto& type = remove_nullable(field_schema.data_type);
1591
23
    if (type->get_primitive_type() == TYPE_STRUCT) {
1592
10
        const auto& struct_column = assert_cast<const ColumnStruct&>(*value_column);
1593
24
        for (int i = 0; i < field_schema.children.size(); ++i) {
1594
14
            if (!has_selected_column(field_schema.children[i], column_ids)) {
1595
0
                continue;
1596
0
            }
1597
14
            path->append(field_schema.children[i].name, false);
1598
14
            RETURN_IF_ERROR(append_direct_typed_column_to_batch(
1599
14
                    field_schema.children[i], struct_column.get_column(i), start, rows, path, batch,
1600
14
                    true, column_ids, parent_null_maps));
1601
14
            path->pop_back();
1602
14
        }
1603
10
        return append_direct_typed_empty_object_markers(field_schema, struct_column, start, rows,
1604
10
                                                        path, batch, column_ids, parent_null_maps);
1605
10
    }
1606
1607
13
    DataTypePtr variant_leaf_type = make_nullable(direct_variant_leaf_type(field_schema));
1608
13
    MutableColumnPtr variant_leaf = variant_leaf_type->create_column();
1609
13
    variant_leaf->insert_default();
1610
13
    if (type->get_primitive_type() == TYPE_ARRAY && contains_uuid_typed_value_field(field_schema)) {
1611
1
        RETURN_IF_ERROR(insert_direct_typed_uuid_array_leaf_range(
1612
1
                field_schema, *value_column, start, rows, parent_null_maps, variant_leaf.get()));
1613
12
    } else if (is_uuid_typed_value_field(field_schema)) {
1614
1
        RETURN_IF_ERROR(insert_direct_typed_uuid_leaf_range(*value_column, start, rows,
1615
1
                                                            parent_null_maps, variant_leaf.get()));
1616
11
    } else if (is_temporal_variant_leaf_type(type->get_primitive_type())) {
1617
3
        insert_direct_typed_temporal_leaf_range(type->get_primitive_type(), *value_column, start,
1618
3
                                                rows, parent_null_maps, variant_leaf.get());
1619
8
    } else {
1620
8
        insert_direct_typed_leaf_range(*value_column, start, rows, parent_null_maps,
1621
8
                                       variant_leaf.get());
1622
8
    }
1623
13
    if (!batch->add_sub_column(path->build(), std::move(variant_leaf), variant_leaf_type)) {
1624
0
        return Status::Corruption("Failed to add Parquet VARIANT typed subcolumn {}",
1625
0
                                  path->build().get_path());
1626
0
    }
1627
13
    return Status::OK();
1628
13
}
1629
1630
#ifdef BE_TEST
1631
namespace parquet_variant_reader_test {
1632
9
bool can_direct_read_typed_value_for_test(const FieldSchema& typed_value_field) {
1633
9
    const std::set<uint64_t> column_ids;
1634
9
    return can_direct_read_typed_value(typed_value_field, false, column_ids);
1635
9
}
1636
1637
bool can_use_direct_typed_only_value_for_test(const FieldSchema& variant_field,
1638
5
                                              const std::set<uint64_t>& column_ids) {
1639
5
    return can_use_direct_typed_only_value(variant_field, column_ids);
1640
5
}
1641
1642
Status append_direct_typed_column_to_batch_for_test(const FieldSchema& typed_value_field,
1643
                                                    const IColumn& typed_value_column, size_t start,
1644
9
                                                    size_t rows, ColumnVariant* batch) {
1645
9
    PathInDataBuilder path;
1646
9
    const std::set<uint64_t> column_ids;
1647
9
    return append_direct_typed_column_to_batch(typed_value_field, typed_value_column, start, rows,
1648
9
                                               &path, batch, false, column_ids, {});
1649
9
}
1650
1651
Status read_variant_row_for_test(const FieldSchema& variant_field, const Field& field,
1652
20
                                 bool output_nullable, Field* result, bool* sql_null) {
1653
20
    if (field.is_null()) {
1654
1
        if (!output_nullable) {
1655
0
            return Status::Corruption("Not nullable column has null values in parquet file");
1656
0
        }
1657
1
        *sql_null = true;
1658
1
        return Status::OK();
1659
1
    }
1660
1661
19
    VariantMap values;
1662
19
    bool present = false;
1663
19
    PathInDataBuilder path;
1664
19
    std::deque<std::string> string_values;
1665
19
    RETURN_IF_ERROR(variant_to_variant_map(variant_field, field, nullptr, &path, &values, &present,
1666
19
                                           &string_values));
1667
18
    if (!present) {
1668
1
        values[PathInData()] = FieldWithDataType {.field = Field()};
1669
1
    }
1670
1671
18
    auto variant_column = ColumnVariant::create(0, false);
1672
18
    RETURN_IF_CATCH_EXCEPTION(
1673
18
            variant_column->insert(Field::create_field<TYPE_VARIANT>(std::move(values))));
1674
18
    variant_column->get(0, *result);
1675
18
    *sql_null = false;
1676
18
    return Status::OK();
1677
18
}
1678
1679
Status variant_to_json_for_test(const FieldSchema& variant_field, const Field& field,
1680
                                const std::string& inherited_metadata, std::string* json,
1681
3
                                bool* present) {
1682
3
    return variant_to_json(variant_field, field, &inherited_metadata, json, present);
1683
3
}
1684
1685
1
bool variant_struct_reader_type_is_nullable_for_test(const FieldSchema& variant_field) {
1686
1
    return make_variant_struct_reader_type(variant_field)->is_nullable();
1687
1
}
1688
1689
2
bool variant_struct_reader_column_is_nullable_for_test(const FieldSchema& variant_field) {
1690
2
    auto variant_struct_type = make_variant_struct_reader_type(variant_field);
1691
2
    return make_variant_struct_read_column(variant_field, variant_struct_type)->is_nullable();
1692
2
}
1693
} // namespace parquet_variant_reader_test
1694
#endif
1695
1696
// Existing recursive factory keeps nested reader wiring and shared state in one dispatch point.
1697
// NOLINTNEXTLINE(readability-function-cognitive-complexity,readability-function-size)
1698
Status ParquetColumnReader::create(io::FileReaderSPtr file, FieldSchema* field,
1699
                                   const tparquet::RowGroup& row_group, const RowRanges& row_ranges,
1700
                                   const cctz::time_zone* ctz, io::IOContext* io_ctx,
1701
                                   std::unique_ptr<ParquetColumnReader>& reader,
1702
                                   size_t max_buf_size,
1703
                                   std::unordered_map<int, tparquet::OffsetIndex>& col_offsets,
1704
                                   RuntimeState* state, bool in_collection,
1705
                                   const std::set<uint64_t>& column_ids,
1706
132
                                   const std::set<uint64_t>& filter_column_ids) {
1707
132
    size_t total_rows = row_group.num_rows;
1708
132
    if (field->data_type->get_primitive_type() == TYPE_ARRAY) {
1709
2
        std::unique_ptr<ParquetColumnReader> element_reader;
1710
2
        RETURN_IF_ERROR(create(file, field->children.data(), row_group, row_ranges, ctz, io_ctx,
1711
2
                               element_reader, max_buf_size, col_offsets, state, true, column_ids,
1712
2
                               filter_column_ids));
1713
2
        auto array_reader = ArrayColumnReader::create_unique(row_ranges, total_rows, ctz, io_ctx);
1714
2
        element_reader->set_column_in_nested();
1715
2
        RETURN_IF_ERROR(array_reader->init(std::move(element_reader), field));
1716
2
        array_reader->_filter_column_ids = filter_column_ids;
1717
2
        reader.reset(array_reader.release());
1718
130
    } else if (field->data_type->get_primitive_type() == TYPE_MAP) {
1719
0
        std::unique_ptr<ParquetColumnReader> key_reader;
1720
0
        std::unique_ptr<ParquetColumnReader> value_reader;
1721
1722
0
        if (column_ids.empty() ||
1723
0
            column_ids.find(field->children[0].get_column_id()) != column_ids.end()) {
1724
            // Create key reader
1725
0
            RETURN_IF_ERROR(create(file, field->children.data(), row_group, row_ranges, ctz, io_ctx,
1726
0
                                   key_reader, max_buf_size, col_offsets, state, true, column_ids,
1727
0
                                   filter_column_ids));
1728
0
        } else {
1729
0
            auto skip_reader = std::make_unique<SkipReadingReader>(row_ranges, total_rows, ctz,
1730
0
                                                                   io_ctx, field->children.data());
1731
0
            key_reader = std::move(skip_reader);
1732
0
        }
1733
1734
0
        if (column_ids.empty() ||
1735
0
            column_ids.find(field->children[1].get_column_id()) != column_ids.end()) {
1736
            // Create value reader
1737
0
            RETURN_IF_ERROR(create(file, &field->children[1], row_group, row_ranges, ctz, io_ctx,
1738
0
                                   value_reader, max_buf_size, col_offsets, state, true, column_ids,
1739
0
                                   filter_column_ids));
1740
0
        } else {
1741
0
            auto skip_reader = std::make_unique<SkipReadingReader>(row_ranges, total_rows, ctz,
1742
0
                                                                   io_ctx, &field->children[1]);
1743
0
            value_reader = std::move(skip_reader);
1744
0
        }
1745
1746
0
        auto map_reader = MapColumnReader::create_unique(row_ranges, total_rows, ctz, io_ctx);
1747
0
        key_reader->set_column_in_nested();
1748
0
        value_reader->set_column_in_nested();
1749
0
        RETURN_IF_ERROR(map_reader->init(std::move(key_reader), std::move(value_reader), field));
1750
0
        map_reader->_filter_column_ids = filter_column_ids;
1751
0
        reader.reset(map_reader.release());
1752
130
    } else if (field->data_type->get_primitive_type() == TYPE_STRUCT) {
1753
11
        std::unordered_map<std::string, std::unique_ptr<ParquetColumnReader>> child_readers;
1754
11
        child_readers.reserve(field->children.size());
1755
11
        int non_skip_reader_idx = -1;
1756
37
        for (int i = 0; i < field->children.size(); ++i) {
1757
26
            auto& child = field->children[i];
1758
26
            std::unique_ptr<ParquetColumnReader> child_reader;
1759
26
            if (column_ids.empty() || column_ids.find(child.get_column_id()) != column_ids.end()) {
1760
22
                RETURN_IF_ERROR(create(file, &child, row_group, row_ranges, ctz, io_ctx,
1761
22
                                       child_reader, max_buf_size, col_offsets, state,
1762
22
                                       in_collection, column_ids, filter_column_ids));
1763
22
                child_readers[child.name] = std::move(child_reader);
1764
                // Record the first non-SkippingReader
1765
22
                if (non_skip_reader_idx == -1) {
1766
11
                    non_skip_reader_idx = i;
1767
11
                }
1768
22
            } else {
1769
4
                auto skip_reader = std::make_unique<SkipReadingReader>(row_ranges, total_rows, ctz,
1770
4
                                                                       io_ctx, &child);
1771
4
                skip_reader->_filter_column_ids = filter_column_ids;
1772
4
                child_readers[child.name] = std::move(skip_reader);
1773
4
            }
1774
26
            child_readers[child.name]->set_column_in_nested();
1775
26
        }
1776
        // If all children are SkipReadingReader, force the first child to call create
1777
11
        if (non_skip_reader_idx == -1) {
1778
0
            std::unique_ptr<ParquetColumnReader> child_reader;
1779
0
            RETURN_IF_ERROR(create(file, field->children.data(), row_group, row_ranges, ctz, io_ctx,
1780
0
                                   child_reader, max_buf_size, col_offsets, state, in_collection,
1781
0
                                   column_ids, filter_column_ids));
1782
0
            child_reader->set_column_in_nested();
1783
0
            child_readers[field->children[0].name] = std::move(child_reader);
1784
0
        }
1785
11
        auto struct_reader = StructColumnReader::create_unique(row_ranges, total_rows, ctz, io_ctx);
1786
11
        RETURN_IF_ERROR(struct_reader->init(std::move(child_readers), field));
1787
11
        struct_reader->_filter_column_ids = filter_column_ids;
1788
11
        reader.reset(struct_reader.release());
1789
119
    } else if (remove_nullable(field->data_type)->get_primitive_type() == TYPE_VARIANT) {
1790
0
        auto variant_reader =
1791
0
                VariantColumnReader::create_unique(row_ranges, total_rows, ctz, io_ctx);
1792
0
        RETURN_IF_ERROR(variant_reader->init(file, field, row_group, max_buf_size, col_offsets,
1793
0
                                             state, in_collection, column_ids, filter_column_ids));
1794
0
        variant_reader->_filter_column_ids = filter_column_ids;
1795
0
        reader.reset(variant_reader.release());
1796
119
    } else {
1797
119
        auto physical_index = field->physical_column_index;
1798
119
        const tparquet::OffsetIndex* offset_index =
1799
119
                col_offsets.find(physical_index) != col_offsets.end() ? &col_offsets[physical_index]
1800
119
                                                                      : nullptr;
1801
1802
119
        const tparquet::ColumnChunk& chunk = row_group.columns[physical_index];
1803
119
        if (in_collection) {
1804
3
            if (offset_index == nullptr) {
1805
3
                auto scalar_reader = ScalarColumnReader<true, false>::create_unique(
1806
3
                        row_ranges, total_rows, chunk, offset_index, ctz, io_ctx);
1807
1808
3
                RETURN_IF_ERROR(scalar_reader->init(file, field, max_buf_size, state));
1809
3
                scalar_reader->_filter_column_ids = filter_column_ids;
1810
3
                reader.reset(scalar_reader.release());
1811
3
            } else {
1812
0
                auto scalar_reader = ScalarColumnReader<true, true>::create_unique(
1813
0
                        row_ranges, total_rows, chunk, offset_index, ctz, io_ctx);
1814
1815
0
                RETURN_IF_ERROR(scalar_reader->init(file, field, max_buf_size, state));
1816
0
                scalar_reader->_filter_column_ids = filter_column_ids;
1817
0
                reader.reset(scalar_reader.release());
1818
0
            }
1819
116
        } else {
1820
116
            if (offset_index == nullptr) {
1821
116
                auto scalar_reader = ScalarColumnReader<false, false>::create_unique(
1822
116
                        row_ranges, total_rows, chunk, offset_index, ctz, io_ctx);
1823
1824
116
                RETURN_IF_ERROR(scalar_reader->init(file, field, max_buf_size, state));
1825
116
                scalar_reader->_filter_column_ids = filter_column_ids;
1826
116
                reader.reset(scalar_reader.release());
1827
116
            } else {
1828
0
                auto scalar_reader = ScalarColumnReader<false, true>::create_unique(
1829
0
                        row_ranges, total_rows, chunk, offset_index, ctz, io_ctx);
1830
1831
0
                RETURN_IF_ERROR(scalar_reader->init(file, field, max_buf_size, state));
1832
0
                scalar_reader->_filter_column_ids = filter_column_ids;
1833
0
                reader.reset(scalar_reader.release());
1834
0
            }
1835
116
        }
1836
119
    }
1837
132
    return Status::OK();
1838
132
}
1839
1840
void ParquetColumnReader::_generate_read_ranges(RowRange page_row_range,
1841
274
                                                RowRanges* result_ranges) const {
1842
274
    result_ranges->add(page_row_range);
1843
274
    RowRanges::ranges_intersection(*result_ranges, _row_ranges, result_ranges);
1844
274
}
1845
1846
template <bool IN_COLLECTION, bool OFFSET_INDEX>
1847
Status ScalarColumnReader<IN_COLLECTION, OFFSET_INDEX>::init(io::FileReaderSPtr file,
1848
                                                             FieldSchema* field,
1849
                                                             size_t max_buf_size,
1850
119
                                                             RuntimeState* state) {
1851
119
    _field_schema = field;
1852
119
    auto& chunk_meta = _chunk_meta.meta_data;
1853
119
    int64_t chunk_start = has_dict_page(chunk_meta) ? chunk_meta.dictionary_page_offset
1854
119
                                                    : chunk_meta.data_page_offset;
1855
119
    size_t chunk_len = chunk_meta.total_compressed_size;
1856
119
    size_t prefetch_buffer_size = std::min(chunk_len, max_buf_size);
1857
119
    if ((typeid_cast<doris::io::TracingFileReader*>(file.get()) &&
1858
119
         typeid_cast<io::MergeRangeFileReader*>(
1859
53
                 ((doris::io::TracingFileReader*)(file.get()))->inner_reader().get())) ||
1860
119
        typeid_cast<io::MergeRangeFileReader*>(file.get())) {
1861
        // turn off prefetch data when using MergeRangeFileReader
1862
119
        prefetch_buffer_size = 0;
1863
119
    }
1864
119
    _stream_reader = std::make_unique<io::BufferedFileStreamReader>(file, chunk_start, chunk_len,
1865
119
                                                                    prefetch_buffer_size);
1866
119
    ParquetPageReadContext ctx(
1867
119
            (state == nullptr) ? true : state->query_options().enable_parquet_file_page_cache);
1868
1869
119
    _chunk_reader = std::make_unique<ColumnChunkReader<IN_COLLECTION, OFFSET_INDEX>>(
1870
119
            _stream_reader.get(), &_chunk_meta, field, _offset_index, _total_rows, _io_ctx, ctx);
1871
119
    RETURN_IF_ERROR(_chunk_reader->init());
1872
119
    return Status::OK();
1873
119
}
Unexecuted instantiation: _ZN5doris18ScalarColumnReaderILb1ELb1EE4initESt10shared_ptrINS_2io10FileReaderEEPNS_11FieldSchemaEmPNS_12RuntimeStateE
_ZN5doris18ScalarColumnReaderILb1ELb0EE4initESt10shared_ptrINS_2io10FileReaderEEPNS_11FieldSchemaEmPNS_12RuntimeStateE
Line
Count
Source
1850
3
                                                             RuntimeState* state) {
1851
3
    _field_schema = field;
1852
3
    auto& chunk_meta = _chunk_meta.meta_data;
1853
3
    int64_t chunk_start = has_dict_page(chunk_meta) ? chunk_meta.dictionary_page_offset
1854
3
                                                    : chunk_meta.data_page_offset;
1855
3
    size_t chunk_len = chunk_meta.total_compressed_size;
1856
3
    size_t prefetch_buffer_size = std::min(chunk_len, max_buf_size);
1857
3
    if ((typeid_cast<doris::io::TracingFileReader*>(file.get()) &&
1858
3
         typeid_cast<io::MergeRangeFileReader*>(
1859
0
                 ((doris::io::TracingFileReader*)(file.get()))->inner_reader().get())) ||
1860
3
        typeid_cast<io::MergeRangeFileReader*>(file.get())) {
1861
        // turn off prefetch data when using MergeRangeFileReader
1862
3
        prefetch_buffer_size = 0;
1863
3
    }
1864
3
    _stream_reader = std::make_unique<io::BufferedFileStreamReader>(file, chunk_start, chunk_len,
1865
3
                                                                    prefetch_buffer_size);
1866
3
    ParquetPageReadContext ctx(
1867
3
            (state == nullptr) ? true : state->query_options().enable_parquet_file_page_cache);
1868
1869
3
    _chunk_reader = std::make_unique<ColumnChunkReader<IN_COLLECTION, OFFSET_INDEX>>(
1870
3
            _stream_reader.get(), &_chunk_meta, field, _offset_index, _total_rows, _io_ctx, ctx);
1871
3
    RETURN_IF_ERROR(_chunk_reader->init());
1872
3
    return Status::OK();
1873
3
}
Unexecuted instantiation: _ZN5doris18ScalarColumnReaderILb0ELb1EE4initESt10shared_ptrINS_2io10FileReaderEEPNS_11FieldSchemaEmPNS_12RuntimeStateE
_ZN5doris18ScalarColumnReaderILb0ELb0EE4initESt10shared_ptrINS_2io10FileReaderEEPNS_11FieldSchemaEmPNS_12RuntimeStateE
Line
Count
Source
1850
116
                                                             RuntimeState* state) {
1851
116
    _field_schema = field;
1852
116
    auto& chunk_meta = _chunk_meta.meta_data;
1853
116
    int64_t chunk_start = has_dict_page(chunk_meta) ? chunk_meta.dictionary_page_offset
1854
116
                                                    : chunk_meta.data_page_offset;
1855
116
    size_t chunk_len = chunk_meta.total_compressed_size;
1856
116
    size_t prefetch_buffer_size = std::min(chunk_len, max_buf_size);
1857
116
    if ((typeid_cast<doris::io::TracingFileReader*>(file.get()) &&
1858
116
         typeid_cast<io::MergeRangeFileReader*>(
1859
53
                 ((doris::io::TracingFileReader*)(file.get()))->inner_reader().get())) ||
1860
116
        typeid_cast<io::MergeRangeFileReader*>(file.get())) {
1861
        // turn off prefetch data when using MergeRangeFileReader
1862
116
        prefetch_buffer_size = 0;
1863
116
    }
1864
116
    _stream_reader = std::make_unique<io::BufferedFileStreamReader>(file, chunk_start, chunk_len,
1865
116
                                                                    prefetch_buffer_size);
1866
116
    ParquetPageReadContext ctx(
1867
116
            (state == nullptr) ? true : state->query_options().enable_parquet_file_page_cache);
1868
1869
116
    _chunk_reader = std::make_unique<ColumnChunkReader<IN_COLLECTION, OFFSET_INDEX>>(
1870
116
            _stream_reader.get(), &_chunk_meta, field, _offset_index, _total_rows, _io_ctx, ctx);
1871
116
    RETURN_IF_ERROR(_chunk_reader->init());
1872
116
    return Status::OK();
1873
116
}
1874
1875
template <bool IN_COLLECTION, bool OFFSET_INDEX>
1876
244
Status ScalarColumnReader<IN_COLLECTION, OFFSET_INDEX>::_skip_values(size_t num_values) {
1877
244
    if (num_values == 0) {
1878
142
        return Status::OK();
1879
142
    }
1880
102
    if (_chunk_reader->max_def_level() > 0) {
1881
102
        LevelDecoder& def_decoder = _chunk_reader->def_level_decoder();
1882
102
        size_t skipped = 0;
1883
102
        size_t null_size = 0;
1884
102
        size_t nonnull_size = 0;
1885
217
        while (skipped < num_values) {
1886
115
            level_t def_level = -1;
1887
115
            size_t loop_skip = def_decoder.get_next_run(&def_level, num_values - skipped);
1888
115
            if (loop_skip == 0) {
1889
0
                std::stringstream ss;
1890
0
                const auto& bit_reader = def_decoder.rle_decoder().bit_reader();
1891
0
                ss << "def_decoder buffer (hex): ";
1892
0
                for (size_t i = 0; i < bit_reader.max_bytes(); ++i) {
1893
0
                    ss << std::hex << std::setw(2) << std::setfill('0')
1894
0
                       << static_cast<int>(bit_reader.buffer()[i]) << " ";
1895
0
                }
1896
0
                LOG(WARNING) << ss.str();
1897
0
                return Status::InternalError("Failed to decode definition level.");
1898
0
            }
1899
115
            if (def_level < _field_schema->definition_level) {
1900
8
                null_size += loop_skip;
1901
107
            } else {
1902
107
                nonnull_size += loop_skip;
1903
107
            }
1904
115
            skipped += loop_skip;
1905
115
        }
1906
102
        if (null_size > 0) {
1907
5
            RETURN_IF_ERROR(_chunk_reader->skip_values(null_size, false));
1908
5
        }
1909
102
        if (nonnull_size > 0) {
1910
101
            RETURN_IF_ERROR(_chunk_reader->skip_values(nonnull_size, true));
1911
101
        }
1912
102
    } else {
1913
0
        RETURN_IF_ERROR(_chunk_reader->skip_values(num_values));
1914
0
    }
1915
102
    return Status::OK();
1916
102
}
Unexecuted instantiation: _ZN5doris18ScalarColumnReaderILb1ELb1EE12_skip_valuesEm
Unexecuted instantiation: _ZN5doris18ScalarColumnReaderILb1ELb0EE12_skip_valuesEm
Unexecuted instantiation: _ZN5doris18ScalarColumnReaderILb0ELb1EE12_skip_valuesEm
_ZN5doris18ScalarColumnReaderILb0ELb0EE12_skip_valuesEm
Line
Count
Source
1876
244
Status ScalarColumnReader<IN_COLLECTION, OFFSET_INDEX>::_skip_values(size_t num_values) {
1877
244
    if (num_values == 0) {
1878
142
        return Status::OK();
1879
142
    }
1880
102
    if (_chunk_reader->max_def_level() > 0) {
1881
102
        LevelDecoder& def_decoder = _chunk_reader->def_level_decoder();
1882
102
        size_t skipped = 0;
1883
102
        size_t null_size = 0;
1884
102
        size_t nonnull_size = 0;
1885
217
        while (skipped < num_values) {
1886
115
            level_t def_level = -1;
1887
115
            size_t loop_skip = def_decoder.get_next_run(&def_level, num_values - skipped);
1888
115
            if (loop_skip == 0) {
1889
0
                std::stringstream ss;
1890
0
                const auto& bit_reader = def_decoder.rle_decoder().bit_reader();
1891
0
                ss << "def_decoder buffer (hex): ";
1892
0
                for (size_t i = 0; i < bit_reader.max_bytes(); ++i) {
1893
0
                    ss << std::hex << std::setw(2) << std::setfill('0')
1894
0
                       << static_cast<int>(bit_reader.buffer()[i]) << " ";
1895
0
                }
1896
0
                LOG(WARNING) << ss.str();
1897
0
                return Status::InternalError("Failed to decode definition level.");
1898
0
            }
1899
115
            if (def_level < _field_schema->definition_level) {
1900
8
                null_size += loop_skip;
1901
107
            } else {
1902
107
                nonnull_size += loop_skip;
1903
107
            }
1904
115
            skipped += loop_skip;
1905
115
        }
1906
102
        if (null_size > 0) {
1907
5
            RETURN_IF_ERROR(_chunk_reader->skip_values(null_size, false));
1908
5
        }
1909
102
        if (nonnull_size > 0) {
1910
101
            RETURN_IF_ERROR(_chunk_reader->skip_values(nonnull_size, true));
1911
101
        }
1912
102
    } else {
1913
0
        RETURN_IF_ERROR(_chunk_reader->skip_values(num_values));
1914
0
    }
1915
102
    return Status::OK();
1916
102
}
1917
1918
template <bool IN_COLLECTION, bool OFFSET_INDEX>
1919
Status ScalarColumnReader<IN_COLLECTION, OFFSET_INDEX>::_read_values(size_t num_values,
1920
                                                                     ColumnPtr& doris_column,
1921
                                                                     DataTypePtr& type,
1922
                                                                     FilterMap& filter_map,
1923
244
                                                                     bool is_dict_filter) {
1924
244
    if (num_values == 0) {
1925
0
        return Status::OK();
1926
0
    }
1927
244
    MutableColumnPtr data_column;
1928
244
    std::vector<uint16_t> null_map;
1929
244
    NullMap* map_data_column = nullptr;
1930
244
    if (doris_column->is_nullable()) {
1931
242
        SCOPED_RAW_TIMER(&_decode_null_map_time);
1932
        // doris_column either originates from a mutable block in vparquet_group_reader
1933
        // or is a newly created ColumnPtr, and therefore can be modified.
1934
242
        auto* nullable_column =
1935
242
                assert_cast<ColumnNullable*>(const_cast<IColumn*>(doris_column.get()));
1936
1937
242
        data_column = nullable_column->get_nested_column_ptr();
1938
242
        map_data_column = &(nullable_column->get_null_map_data());
1939
242
        if (_chunk_reader->max_def_level() > 0) {
1940
174
            LevelDecoder& def_decoder = _chunk_reader->def_level_decoder();
1941
174
            size_t has_read = 0;
1942
174
            bool prev_is_null = true;
1943
348
            while (has_read < num_values) {
1944
174
                level_t def_level;
1945
174
                size_t loop_read = def_decoder.get_next_run(&def_level, num_values - has_read);
1946
174
                if (loop_read == 0) {
1947
0
                    std::stringstream ss;
1948
0
                    const auto& bit_reader = def_decoder.rle_decoder().bit_reader();
1949
0
                    ss << "def_decoder buffer (hex): ";
1950
0
                    for (size_t i = 0; i < bit_reader.max_bytes(); ++i) {
1951
0
                        ss << std::hex << std::setw(2) << std::setfill('0')
1952
0
                           << static_cast<int>(bit_reader.buffer()[i]) << " ";
1953
0
                    }
1954
0
                    LOG(WARNING) << ss.str();
1955
0
                    return Status::InternalError("Failed to decode definition level.");
1956
0
                }
1957
1958
174
                bool is_null = def_level < _field_schema->definition_level;
1959
174
                if (!(prev_is_null ^ is_null)) {
1960
57
                    null_map.emplace_back(0);
1961
57
                }
1962
174
                size_t remaining = loop_read;
1963
174
                while (remaining > USHRT_MAX) {
1964
0
                    null_map.emplace_back(USHRT_MAX);
1965
0
                    null_map.emplace_back(0);
1966
0
                    remaining -= USHRT_MAX;
1967
0
                }
1968
174
                null_map.emplace_back((u_short)remaining);
1969
174
                prev_is_null = is_null;
1970
174
                has_read += loop_read;
1971
174
            }
1972
174
        }
1973
242
    } else {
1974
2
        if (_chunk_reader->max_def_level() > 0) {
1975
0
            return Status::Corruption("Not nullable column has null values in parquet file");
1976
0
        }
1977
2
        data_column = doris_column->assume_mutable();
1978
2
    }
1979
244
    if (null_map.empty()) {
1980
70
        size_t remaining = num_values;
1981
70
        while (remaining > USHRT_MAX) {
1982
0
            null_map.emplace_back(USHRT_MAX);
1983
0
            null_map.emplace_back(0);
1984
0
            remaining -= USHRT_MAX;
1985
0
        }
1986
70
        null_map.emplace_back((u_short)remaining);
1987
70
    }
1988
244
    ColumnSelectVector select_vector;
1989
244
    {
1990
244
        SCOPED_RAW_TIMER(&_decode_null_map_time);
1991
244
        RETURN_IF_ERROR(select_vector.init(null_map, num_values, map_data_column, &filter_map,
1992
244
                                           _filter_map_index));
1993
244
        _filter_map_index += num_values;
1994
244
    }
1995
0
    return _chunk_reader->decode_values(data_column, type, select_vector, is_dict_filter);
1996
244
}
Unexecuted instantiation: _ZN5doris18ScalarColumnReaderILb1ELb1EE12_read_valuesEmRNS_3COWINS_7IColumnEE13immutable_ptrIS3_EERSt10shared_ptrIKNS_9IDataTypeEERNS_9FilterMapEb
Unexecuted instantiation: _ZN5doris18ScalarColumnReaderILb1ELb0EE12_read_valuesEmRNS_3COWINS_7IColumnEE13immutable_ptrIS3_EERSt10shared_ptrIKNS_9IDataTypeEERNS_9FilterMapEb
Unexecuted instantiation: _ZN5doris18ScalarColumnReaderILb0ELb1EE12_read_valuesEmRNS_3COWINS_7IColumnEE13immutable_ptrIS3_EERSt10shared_ptrIKNS_9IDataTypeEERNS_9FilterMapEb
_ZN5doris18ScalarColumnReaderILb0ELb0EE12_read_valuesEmRNS_3COWINS_7IColumnEE13immutable_ptrIS3_EERSt10shared_ptrIKNS_9IDataTypeEERNS_9FilterMapEb
Line
Count
Source
1923
244
                                                                     bool is_dict_filter) {
1924
244
    if (num_values == 0) {
1925
0
        return Status::OK();
1926
0
    }
1927
244
    MutableColumnPtr data_column;
1928
244
    std::vector<uint16_t> null_map;
1929
244
    NullMap* map_data_column = nullptr;
1930
244
    if (doris_column->is_nullable()) {
1931
242
        SCOPED_RAW_TIMER(&_decode_null_map_time);
1932
        // doris_column either originates from a mutable block in vparquet_group_reader
1933
        // or is a newly created ColumnPtr, and therefore can be modified.
1934
242
        auto* nullable_column =
1935
242
                assert_cast<ColumnNullable*>(const_cast<IColumn*>(doris_column.get()));
1936
1937
242
        data_column = nullable_column->get_nested_column_ptr();
1938
242
        map_data_column = &(nullable_column->get_null_map_data());
1939
242
        if (_chunk_reader->max_def_level() > 0) {
1940
174
            LevelDecoder& def_decoder = _chunk_reader->def_level_decoder();
1941
174
            size_t has_read = 0;
1942
174
            bool prev_is_null = true;
1943
348
            while (has_read < num_values) {
1944
174
                level_t def_level;
1945
174
                size_t loop_read = def_decoder.get_next_run(&def_level, num_values - has_read);
1946
174
                if (loop_read == 0) {
1947
0
                    std::stringstream ss;
1948
0
                    const auto& bit_reader = def_decoder.rle_decoder().bit_reader();
1949
0
                    ss << "def_decoder buffer (hex): ";
1950
0
                    for (size_t i = 0; i < bit_reader.max_bytes(); ++i) {
1951
0
                        ss << std::hex << std::setw(2) << std::setfill('0')
1952
0
                           << static_cast<int>(bit_reader.buffer()[i]) << " ";
1953
0
                    }
1954
0
                    LOG(WARNING) << ss.str();
1955
0
                    return Status::InternalError("Failed to decode definition level.");
1956
0
                }
1957
1958
174
                bool is_null = def_level < _field_schema->definition_level;
1959
174
                if (!(prev_is_null ^ is_null)) {
1960
57
                    null_map.emplace_back(0);
1961
57
                }
1962
174
                size_t remaining = loop_read;
1963
174
                while (remaining > USHRT_MAX) {
1964
0
                    null_map.emplace_back(USHRT_MAX);
1965
0
                    null_map.emplace_back(0);
1966
0
                    remaining -= USHRT_MAX;
1967
0
                }
1968
174
                null_map.emplace_back((u_short)remaining);
1969
174
                prev_is_null = is_null;
1970
174
                has_read += loop_read;
1971
174
            }
1972
174
        }
1973
242
    } else {
1974
2
        if (_chunk_reader->max_def_level() > 0) {
1975
0
            return Status::Corruption("Not nullable column has null values in parquet file");
1976
0
        }
1977
2
        data_column = doris_column->assume_mutable();
1978
2
    }
1979
244
    if (null_map.empty()) {
1980
70
        size_t remaining = num_values;
1981
70
        while (remaining > USHRT_MAX) {
1982
0
            null_map.emplace_back(USHRT_MAX);
1983
0
            null_map.emplace_back(0);
1984
0
            remaining -= USHRT_MAX;
1985
0
        }
1986
70
        null_map.emplace_back((u_short)remaining);
1987
70
    }
1988
244
    ColumnSelectVector select_vector;
1989
244
    {
1990
244
        SCOPED_RAW_TIMER(&_decode_null_map_time);
1991
244
        RETURN_IF_ERROR(select_vector.init(null_map, num_values, map_data_column, &filter_map,
1992
244
                                           _filter_map_index));
1993
244
        _filter_map_index += num_values;
1994
244
    }
1995
0
    return _chunk_reader->decode_values(data_column, type, select_vector, is_dict_filter);
1996
244
}
1997
1998
/**
1999
 * Load the nested column data of complex type.
2000
 * A row of complex type may be stored across two(or more) pages, and the parameter `align_rows` indicates that
2001
 * whether the reader should read the remaining value of the last row in previous page.
2002
 */
2003
template <bool IN_COLLECTION, bool OFFSET_INDEX>
2004
// Existing nested scalar reader is the central row/page alignment loop for complex values.
2005
// NOLINTNEXTLINE(readability-function-cognitive-complexity,readability-function-size)
2006
Status ScalarColumnReader<IN_COLLECTION, OFFSET_INDEX>::_read_nested_column(
2007
        ColumnPtr& doris_column, DataTypePtr& type, FilterMap& filter_map, size_t batch_size,
2008
13
        size_t* read_rows, bool* eof, bool is_dict_filter) {
2009
13
    _rep_levels.clear();
2010
13
    _def_levels.clear();
2011
2012
    // Handle nullable columns
2013
13
    MutableColumnPtr data_column;
2014
13
    NullMap* map_data_column = nullptr;
2015
13
    if (doris_column->is_nullable()) {
2016
13
        SCOPED_RAW_TIMER(&_decode_null_map_time);
2017
        // doris_column either originates from a mutable block in vparquet_group_reader
2018
        // or is a newly created ColumnPtr, and therefore can be modified.
2019
13
        auto* nullable_column =
2020
13
                const_cast<ColumnNullable*>(assert_cast<const ColumnNullable*>(doris_column.get()));
2021
13
        data_column = nullable_column->get_nested_column_ptr();
2022
13
        map_data_column = &(nullable_column->get_null_map_data());
2023
13
    } else {
2024
0
        if (_field_schema->data_type->is_nullable()) {
2025
0
            return Status::Corruption("Not nullable column has null values in parquet file");
2026
0
        }
2027
0
        data_column = doris_column->assume_mutable();
2028
0
    }
2029
2030
13
    std::vector<uint16_t> null_map;
2031
13
    std::unordered_set<size_t> ancestor_null_indices;
2032
13
    std::vector<uint8_t> nested_filter_map_data;
2033
2034
13
    auto read_and_fill_data = [&](size_t before_rep_level_sz, size_t filter_map_index) {
2035
13
        RETURN_IF_ERROR(_chunk_reader->fill_def(_def_levels));
2036
13
        std::unique_ptr<FilterMap> nested_filter_map = std::make_unique<FilterMap>();
2037
13
        if (filter_map.has_filter()) {
2038
0
            RETURN_IF_ERROR(gen_filter_map(filter_map, filter_map_index, before_rep_level_sz,
2039
0
                                           _rep_levels.size(), nested_filter_map_data,
2040
0
                                           &nested_filter_map));
2041
0
        }
2042
2043
13
        null_map.clear();
2044
13
        ancestor_null_indices.clear();
2045
13
        RETURN_IF_ERROR(gen_nested_null_map(before_rep_level_sz, _rep_levels.size(), null_map,
2046
13
                                            ancestor_null_indices));
2047
2048
13
        ColumnSelectVector select_vector;
2049
13
        {
2050
13
            SCOPED_RAW_TIMER(&_decode_null_map_time);
2051
13
            RETURN_IF_ERROR(select_vector.init(
2052
13
                    null_map,
2053
13
                    _rep_levels.size() - before_rep_level_sz - ancestor_null_indices.size(),
2054
13
                    map_data_column, nested_filter_map.get(), 0, &ancestor_null_indices));
2055
13
        }
2056
2057
13
        RETURN_IF_ERROR(
2058
13
                _chunk_reader->decode_values(data_column, type, select_vector, is_dict_filter));
2059
13
        if (!ancestor_null_indices.empty()) {
2060
0
            RETURN_IF_ERROR(_chunk_reader->skip_values(ancestor_null_indices.size(), false));
2061
0
        }
2062
13
        if (filter_map.has_filter()) {
2063
0
            auto new_rep_sz = before_rep_level_sz;
2064
0
            for (size_t idx = before_rep_level_sz; idx < _rep_levels.size(); idx++) {
2065
0
                if (nested_filter_map_data[idx - before_rep_level_sz]) {
2066
0
                    _rep_levels[new_rep_sz] = _rep_levels[idx];
2067
0
                    _def_levels[new_rep_sz] = _def_levels[idx];
2068
0
                    new_rep_sz++;
2069
0
                }
2070
0
            }
2071
0
            _rep_levels.resize(new_rep_sz);
2072
0
            _def_levels.resize(new_rep_sz);
2073
0
        }
2074
13
        return Status::OK();
2075
13
    };
Unexecuted instantiation: _ZZN5doris18ScalarColumnReaderILb1ELb1EE19_read_nested_columnERNS_3COWINS_7IColumnEE13immutable_ptrIS3_EERSt10shared_ptrIKNS_9IDataTypeEERNS_9FilterMapEmPmPbbENKUlmmE_clEmm
_ZZN5doris18ScalarColumnReaderILb1ELb0EE19_read_nested_columnERNS_3COWINS_7IColumnEE13immutable_ptrIS3_EERSt10shared_ptrIKNS_9IDataTypeEERNS_9FilterMapEmPmPbbENKUlmmE_clEmm
Line
Count
Source
2034
3
    auto read_and_fill_data = [&](size_t before_rep_level_sz, size_t filter_map_index) {
2035
3
        RETURN_IF_ERROR(_chunk_reader->fill_def(_def_levels));
2036
3
        std::unique_ptr<FilterMap> nested_filter_map = std::make_unique<FilterMap>();
2037
3
        if (filter_map.has_filter()) {
2038
0
            RETURN_IF_ERROR(gen_filter_map(filter_map, filter_map_index, before_rep_level_sz,
2039
0
                                           _rep_levels.size(), nested_filter_map_data,
2040
0
                                           &nested_filter_map));
2041
0
        }
2042
2043
3
        null_map.clear();
2044
3
        ancestor_null_indices.clear();
2045
3
        RETURN_IF_ERROR(gen_nested_null_map(before_rep_level_sz, _rep_levels.size(), null_map,
2046
3
                                            ancestor_null_indices));
2047
2048
3
        ColumnSelectVector select_vector;
2049
3
        {
2050
3
            SCOPED_RAW_TIMER(&_decode_null_map_time);
2051
3
            RETURN_IF_ERROR(select_vector.init(
2052
3
                    null_map,
2053
3
                    _rep_levels.size() - before_rep_level_sz - ancestor_null_indices.size(),
2054
3
                    map_data_column, nested_filter_map.get(), 0, &ancestor_null_indices));
2055
3
        }
2056
2057
3
        RETURN_IF_ERROR(
2058
3
                _chunk_reader->decode_values(data_column, type, select_vector, is_dict_filter));
2059
3
        if (!ancestor_null_indices.empty()) {
2060
0
            RETURN_IF_ERROR(_chunk_reader->skip_values(ancestor_null_indices.size(), false));
2061
0
        }
2062
3
        if (filter_map.has_filter()) {
2063
0
            auto new_rep_sz = before_rep_level_sz;
2064
0
            for (size_t idx = before_rep_level_sz; idx < _rep_levels.size(); idx++) {
2065
0
                if (nested_filter_map_data[idx - before_rep_level_sz]) {
2066
0
                    _rep_levels[new_rep_sz] = _rep_levels[idx];
2067
0
                    _def_levels[new_rep_sz] = _def_levels[idx];
2068
0
                    new_rep_sz++;
2069
0
                }
2070
0
            }
2071
0
            _rep_levels.resize(new_rep_sz);
2072
0
            _def_levels.resize(new_rep_sz);
2073
0
        }
2074
3
        return Status::OK();
2075
3
    };
Unexecuted instantiation: _ZZN5doris18ScalarColumnReaderILb0ELb1EE19_read_nested_columnERNS_3COWINS_7IColumnEE13immutable_ptrIS3_EERSt10shared_ptrIKNS_9IDataTypeEERNS_9FilterMapEmPmPbbENKUlmmE_clEmm
_ZZN5doris18ScalarColumnReaderILb0ELb0EE19_read_nested_columnERNS_3COWINS_7IColumnEE13immutable_ptrIS3_EERSt10shared_ptrIKNS_9IDataTypeEERNS_9FilterMapEmPmPbbENKUlmmE_clEmm
Line
Count
Source
2034
10
    auto read_and_fill_data = [&](size_t before_rep_level_sz, size_t filter_map_index) {
2035
10
        RETURN_IF_ERROR(_chunk_reader->fill_def(_def_levels));
2036
10
        std::unique_ptr<FilterMap> nested_filter_map = std::make_unique<FilterMap>();
2037
10
        if (filter_map.has_filter()) {
2038
0
            RETURN_IF_ERROR(gen_filter_map(filter_map, filter_map_index, before_rep_level_sz,
2039
0
                                           _rep_levels.size(), nested_filter_map_data,
2040
0
                                           &nested_filter_map));
2041
0
        }
2042
2043
10
        null_map.clear();
2044
10
        ancestor_null_indices.clear();
2045
10
        RETURN_IF_ERROR(gen_nested_null_map(before_rep_level_sz, _rep_levels.size(), null_map,
2046
10
                                            ancestor_null_indices));
2047
2048
10
        ColumnSelectVector select_vector;
2049
10
        {
2050
10
            SCOPED_RAW_TIMER(&_decode_null_map_time);
2051
10
            RETURN_IF_ERROR(select_vector.init(
2052
10
                    null_map,
2053
10
                    _rep_levels.size() - before_rep_level_sz - ancestor_null_indices.size(),
2054
10
                    map_data_column, nested_filter_map.get(), 0, &ancestor_null_indices));
2055
10
        }
2056
2057
10
        RETURN_IF_ERROR(
2058
10
                _chunk_reader->decode_values(data_column, type, select_vector, is_dict_filter));
2059
10
        if (!ancestor_null_indices.empty()) {
2060
0
            RETURN_IF_ERROR(_chunk_reader->skip_values(ancestor_null_indices.size(), false));
2061
0
        }
2062
10
        if (filter_map.has_filter()) {
2063
0
            auto new_rep_sz = before_rep_level_sz;
2064
0
            for (size_t idx = before_rep_level_sz; idx < _rep_levels.size(); idx++) {
2065
0
                if (nested_filter_map_data[idx - before_rep_level_sz]) {
2066
0
                    _rep_levels[new_rep_sz] = _rep_levels[idx];
2067
0
                    _def_levels[new_rep_sz] = _def_levels[idx];
2068
0
                    new_rep_sz++;
2069
0
                }
2070
0
            }
2071
0
            _rep_levels.resize(new_rep_sz);
2072
0
            _def_levels.resize(new_rep_sz);
2073
0
        }
2074
10
        return Status::OK();
2075
10
    };
2076
2077
15
    while (_current_range_idx < _row_ranges.range_size()) {
2078
13
        size_t left_row =
2079
13
                std::max(_current_row_index, _row_ranges.get_range_from(_current_range_idx));
2080
13
        size_t right_row = std::min(left_row + batch_size - *read_rows,
2081
13
                                    (size_t)_row_ranges.get_range_to(_current_range_idx));
2082
13
        _current_row_index = left_row;
2083
13
        RETURN_IF_ERROR(_chunk_reader->seek_to_nested_row(left_row));
2084
13
        size_t load_rows = 0;
2085
13
        bool cross_page = false;
2086
13
        size_t before_rep_level_sz = _rep_levels.size();
2087
13
        RETURN_IF_ERROR(_chunk_reader->load_page_nested_rows(_rep_levels, right_row - left_row,
2088
13
                                                             &load_rows, &cross_page));
2089
13
        RETURN_IF_ERROR(read_and_fill_data(before_rep_level_sz, _filter_map_index));
2090
13
        _filter_map_index += load_rows;
2091
13
        while (cross_page) {
2092
0
            before_rep_level_sz = _rep_levels.size();
2093
0
            RETURN_IF_ERROR(_chunk_reader->load_cross_page_nested_row(_rep_levels, &cross_page));
2094
0
            RETURN_IF_ERROR(read_and_fill_data(before_rep_level_sz, _filter_map_index - 1));
2095
0
        }
2096
13
        *read_rows += load_rows;
2097
13
        _current_row_index += load_rows;
2098
13
        _current_range_idx += (_current_row_index == _row_ranges.get_range_to(_current_range_idx));
2099
13
        if (*read_rows == batch_size) {
2100
11
            break;
2101
11
        }
2102
13
    }
2103
13
    *eof = _current_range_idx == _row_ranges.range_size();
2104
13
    return Status::OK();
2105
13
}
Unexecuted instantiation: _ZN5doris18ScalarColumnReaderILb1ELb1EE19_read_nested_columnERNS_3COWINS_7IColumnEE13immutable_ptrIS3_EERSt10shared_ptrIKNS_9IDataTypeEERNS_9FilterMapEmPmPbb
_ZN5doris18ScalarColumnReaderILb1ELb0EE19_read_nested_columnERNS_3COWINS_7IColumnEE13immutable_ptrIS3_EERSt10shared_ptrIKNS_9IDataTypeEERNS_9FilterMapEmPmPbb
Line
Count
Source
2008
3
        size_t* read_rows, bool* eof, bool is_dict_filter) {
2009
3
    _rep_levels.clear();
2010
3
    _def_levels.clear();
2011
2012
    // Handle nullable columns
2013
3
    MutableColumnPtr data_column;
2014
3
    NullMap* map_data_column = nullptr;
2015
3
    if (doris_column->is_nullable()) {
2016
3
        SCOPED_RAW_TIMER(&_decode_null_map_time);
2017
        // doris_column either originates from a mutable block in vparquet_group_reader
2018
        // or is a newly created ColumnPtr, and therefore can be modified.
2019
3
        auto* nullable_column =
2020
3
                const_cast<ColumnNullable*>(assert_cast<const ColumnNullable*>(doris_column.get()));
2021
3
        data_column = nullable_column->get_nested_column_ptr();
2022
3
        map_data_column = &(nullable_column->get_null_map_data());
2023
3
    } else {
2024
0
        if (_field_schema->data_type->is_nullable()) {
2025
0
            return Status::Corruption("Not nullable column has null values in parquet file");
2026
0
        }
2027
0
        data_column = doris_column->assume_mutable();
2028
0
    }
2029
2030
3
    std::vector<uint16_t> null_map;
2031
3
    std::unordered_set<size_t> ancestor_null_indices;
2032
3
    std::vector<uint8_t> nested_filter_map_data;
2033
2034
3
    auto read_and_fill_data = [&](size_t before_rep_level_sz, size_t filter_map_index) {
2035
3
        RETURN_IF_ERROR(_chunk_reader->fill_def(_def_levels));
2036
3
        std::unique_ptr<FilterMap> nested_filter_map = std::make_unique<FilterMap>();
2037
3
        if (filter_map.has_filter()) {
2038
3
            RETURN_IF_ERROR(gen_filter_map(filter_map, filter_map_index, before_rep_level_sz,
2039
3
                                           _rep_levels.size(), nested_filter_map_data,
2040
3
                                           &nested_filter_map));
2041
3
        }
2042
2043
3
        null_map.clear();
2044
3
        ancestor_null_indices.clear();
2045
3
        RETURN_IF_ERROR(gen_nested_null_map(before_rep_level_sz, _rep_levels.size(), null_map,
2046
3
                                            ancestor_null_indices));
2047
2048
3
        ColumnSelectVector select_vector;
2049
3
        {
2050
3
            SCOPED_RAW_TIMER(&_decode_null_map_time);
2051
3
            RETURN_IF_ERROR(select_vector.init(
2052
3
                    null_map,
2053
3
                    _rep_levels.size() - before_rep_level_sz - ancestor_null_indices.size(),
2054
3
                    map_data_column, nested_filter_map.get(), 0, &ancestor_null_indices));
2055
3
        }
2056
2057
3
        RETURN_IF_ERROR(
2058
3
                _chunk_reader->decode_values(data_column, type, select_vector, is_dict_filter));
2059
3
        if (!ancestor_null_indices.empty()) {
2060
3
            RETURN_IF_ERROR(_chunk_reader->skip_values(ancestor_null_indices.size(), false));
2061
3
        }
2062
3
        if (filter_map.has_filter()) {
2063
3
            auto new_rep_sz = before_rep_level_sz;
2064
3
            for (size_t idx = before_rep_level_sz; idx < _rep_levels.size(); idx++) {
2065
3
                if (nested_filter_map_data[idx - before_rep_level_sz]) {
2066
3
                    _rep_levels[new_rep_sz] = _rep_levels[idx];
2067
3
                    _def_levels[new_rep_sz] = _def_levels[idx];
2068
3
                    new_rep_sz++;
2069
3
                }
2070
3
            }
2071
3
            _rep_levels.resize(new_rep_sz);
2072
3
            _def_levels.resize(new_rep_sz);
2073
3
        }
2074
3
        return Status::OK();
2075
3
    };
2076
2077
3
    while (_current_range_idx < _row_ranges.range_size()) {
2078
3
        size_t left_row =
2079
3
                std::max(_current_row_index, _row_ranges.get_range_from(_current_range_idx));
2080
3
        size_t right_row = std::min(left_row + batch_size - *read_rows,
2081
3
                                    (size_t)_row_ranges.get_range_to(_current_range_idx));
2082
3
        _current_row_index = left_row;
2083
3
        RETURN_IF_ERROR(_chunk_reader->seek_to_nested_row(left_row));
2084
3
        size_t load_rows = 0;
2085
3
        bool cross_page = false;
2086
3
        size_t before_rep_level_sz = _rep_levels.size();
2087
3
        RETURN_IF_ERROR(_chunk_reader->load_page_nested_rows(_rep_levels, right_row - left_row,
2088
3
                                                             &load_rows, &cross_page));
2089
3
        RETURN_IF_ERROR(read_and_fill_data(before_rep_level_sz, _filter_map_index));
2090
3
        _filter_map_index += load_rows;
2091
3
        while (cross_page) {
2092
0
            before_rep_level_sz = _rep_levels.size();
2093
0
            RETURN_IF_ERROR(_chunk_reader->load_cross_page_nested_row(_rep_levels, &cross_page));
2094
0
            RETURN_IF_ERROR(read_and_fill_data(before_rep_level_sz, _filter_map_index - 1));
2095
0
        }
2096
3
        *read_rows += load_rows;
2097
3
        _current_row_index += load_rows;
2098
3
        _current_range_idx += (_current_row_index == _row_ranges.get_range_to(_current_range_idx));
2099
3
        if (*read_rows == batch_size) {
2100
3
            break;
2101
3
        }
2102
3
    }
2103
3
    *eof = _current_range_idx == _row_ranges.range_size();
2104
3
    return Status::OK();
2105
3
}
Unexecuted instantiation: _ZN5doris18ScalarColumnReaderILb0ELb1EE19_read_nested_columnERNS_3COWINS_7IColumnEE13immutable_ptrIS3_EERSt10shared_ptrIKNS_9IDataTypeEERNS_9FilterMapEmPmPbb
_ZN5doris18ScalarColumnReaderILb0ELb0EE19_read_nested_columnERNS_3COWINS_7IColumnEE13immutable_ptrIS3_EERSt10shared_ptrIKNS_9IDataTypeEERNS_9FilterMapEmPmPbb
Line
Count
Source
2008
10
        size_t* read_rows, bool* eof, bool is_dict_filter) {
2009
10
    _rep_levels.clear();
2010
10
    _def_levels.clear();
2011
2012
    // Handle nullable columns
2013
10
    MutableColumnPtr data_column;
2014
10
    NullMap* map_data_column = nullptr;
2015
10
    if (doris_column->is_nullable()) {
2016
10
        SCOPED_RAW_TIMER(&_decode_null_map_time);
2017
        // doris_column either originates from a mutable block in vparquet_group_reader
2018
        // or is a newly created ColumnPtr, and therefore can be modified.
2019
10
        auto* nullable_column =
2020
10
                const_cast<ColumnNullable*>(assert_cast<const ColumnNullable*>(doris_column.get()));
2021
10
        data_column = nullable_column->get_nested_column_ptr();
2022
10
        map_data_column = &(nullable_column->get_null_map_data());
2023
10
    } else {
2024
0
        if (_field_schema->data_type->is_nullable()) {
2025
0
            return Status::Corruption("Not nullable column has null values in parquet file");
2026
0
        }
2027
0
        data_column = doris_column->assume_mutable();
2028
0
    }
2029
2030
10
    std::vector<uint16_t> null_map;
2031
10
    std::unordered_set<size_t> ancestor_null_indices;
2032
10
    std::vector<uint8_t> nested_filter_map_data;
2033
2034
10
    auto read_and_fill_data = [&](size_t before_rep_level_sz, size_t filter_map_index) {
2035
10
        RETURN_IF_ERROR(_chunk_reader->fill_def(_def_levels));
2036
10
        std::unique_ptr<FilterMap> nested_filter_map = std::make_unique<FilterMap>();
2037
10
        if (filter_map.has_filter()) {
2038
10
            RETURN_IF_ERROR(gen_filter_map(filter_map, filter_map_index, before_rep_level_sz,
2039
10
                                           _rep_levels.size(), nested_filter_map_data,
2040
10
                                           &nested_filter_map));
2041
10
        }
2042
2043
10
        null_map.clear();
2044
10
        ancestor_null_indices.clear();
2045
10
        RETURN_IF_ERROR(gen_nested_null_map(before_rep_level_sz, _rep_levels.size(), null_map,
2046
10
                                            ancestor_null_indices));
2047
2048
10
        ColumnSelectVector select_vector;
2049
10
        {
2050
10
            SCOPED_RAW_TIMER(&_decode_null_map_time);
2051
10
            RETURN_IF_ERROR(select_vector.init(
2052
10
                    null_map,
2053
10
                    _rep_levels.size() - before_rep_level_sz - ancestor_null_indices.size(),
2054
10
                    map_data_column, nested_filter_map.get(), 0, &ancestor_null_indices));
2055
10
        }
2056
2057
10
        RETURN_IF_ERROR(
2058
10
                _chunk_reader->decode_values(data_column, type, select_vector, is_dict_filter));
2059
10
        if (!ancestor_null_indices.empty()) {
2060
10
            RETURN_IF_ERROR(_chunk_reader->skip_values(ancestor_null_indices.size(), false));
2061
10
        }
2062
10
        if (filter_map.has_filter()) {
2063
10
            auto new_rep_sz = before_rep_level_sz;
2064
10
            for (size_t idx = before_rep_level_sz; idx < _rep_levels.size(); idx++) {
2065
10
                if (nested_filter_map_data[idx - before_rep_level_sz]) {
2066
10
                    _rep_levels[new_rep_sz] = _rep_levels[idx];
2067
10
                    _def_levels[new_rep_sz] = _def_levels[idx];
2068
10
                    new_rep_sz++;
2069
10
                }
2070
10
            }
2071
10
            _rep_levels.resize(new_rep_sz);
2072
10
            _def_levels.resize(new_rep_sz);
2073
10
        }
2074
10
        return Status::OK();
2075
10
    };
2076
2077
12
    while (_current_range_idx < _row_ranges.range_size()) {
2078
10
        size_t left_row =
2079
10
                std::max(_current_row_index, _row_ranges.get_range_from(_current_range_idx));
2080
10
        size_t right_row = std::min(left_row + batch_size - *read_rows,
2081
10
                                    (size_t)_row_ranges.get_range_to(_current_range_idx));
2082
10
        _current_row_index = left_row;
2083
10
        RETURN_IF_ERROR(_chunk_reader->seek_to_nested_row(left_row));
2084
10
        size_t load_rows = 0;
2085
10
        bool cross_page = false;
2086
10
        size_t before_rep_level_sz = _rep_levels.size();
2087
10
        RETURN_IF_ERROR(_chunk_reader->load_page_nested_rows(_rep_levels, right_row - left_row,
2088
10
                                                             &load_rows, &cross_page));
2089
10
        RETURN_IF_ERROR(read_and_fill_data(before_rep_level_sz, _filter_map_index));
2090
10
        _filter_map_index += load_rows;
2091
10
        while (cross_page) {
2092
0
            before_rep_level_sz = _rep_levels.size();
2093
0
            RETURN_IF_ERROR(_chunk_reader->load_cross_page_nested_row(_rep_levels, &cross_page));
2094
0
            RETURN_IF_ERROR(read_and_fill_data(before_rep_level_sz, _filter_map_index - 1));
2095
0
        }
2096
10
        *read_rows += load_rows;
2097
10
        _current_row_index += load_rows;
2098
10
        _current_range_idx += (_current_row_index == _row_ranges.get_range_to(_current_range_idx));
2099
10
        if (*read_rows == batch_size) {
2100
8
            break;
2101
8
        }
2102
10
    }
2103
10
    *eof = _current_range_idx == _row_ranges.range_size();
2104
10
    return Status::OK();
2105
10
}
2106
2107
template <bool IN_COLLECTION, bool OFFSET_INDEX>
2108
Status ScalarColumnReader<IN_COLLECTION, OFFSET_INDEX>::read_dict_values_to_column(
2109
2
        MutableColumnPtr& doris_column, bool* has_dict) {
2110
2
    bool loaded;
2111
2
    RETURN_IF_ERROR(_try_load_dict_page(&loaded, has_dict));
2112
2
    if (loaded && *has_dict) {
2113
2
        return _chunk_reader->read_dict_values_to_column(doris_column);
2114
2
    }
2115
0
    return Status::OK();
2116
2
}
Unexecuted instantiation: _ZN5doris18ScalarColumnReaderILb1ELb1EE26read_dict_values_to_columnERNS_3COWINS_7IColumnEE11mutable_ptrIS3_EEPb
Unexecuted instantiation: _ZN5doris18ScalarColumnReaderILb1ELb0EE26read_dict_values_to_columnERNS_3COWINS_7IColumnEE11mutable_ptrIS3_EEPb
Unexecuted instantiation: _ZN5doris18ScalarColumnReaderILb0ELb1EE26read_dict_values_to_columnERNS_3COWINS_7IColumnEE11mutable_ptrIS3_EEPb
_ZN5doris18ScalarColumnReaderILb0ELb0EE26read_dict_values_to_columnERNS_3COWINS_7IColumnEE11mutable_ptrIS3_EEPb
Line
Count
Source
2109
2
        MutableColumnPtr& doris_column, bool* has_dict) {
2110
2
    bool loaded;
2111
2
    RETURN_IF_ERROR(_try_load_dict_page(&loaded, has_dict));
2112
2
    if (loaded && *has_dict) {
2113
2
        return _chunk_reader->read_dict_values_to_column(doris_column);
2114
2
    }
2115
0
    return Status::OK();
2116
2
}
2117
template <bool IN_COLLECTION, bool OFFSET_INDEX>
2118
Result<MutableColumnPtr>
2119
ScalarColumnReader<IN_COLLECTION, OFFSET_INDEX>::convert_dict_column_to_string_column(
2120
0
        const ColumnInt32* dict_column) {
2121
0
    return _chunk_reader->convert_dict_column_to_string_column(dict_column);
2122
0
}
Unexecuted instantiation: _ZN5doris18ScalarColumnReaderILb1ELb1EE36convert_dict_column_to_string_columnEPKNS_12ColumnVectorILNS_13PrimitiveTypeE5EEE
Unexecuted instantiation: _ZN5doris18ScalarColumnReaderILb1ELb0EE36convert_dict_column_to_string_columnEPKNS_12ColumnVectorILNS_13PrimitiveTypeE5EEE
Unexecuted instantiation: _ZN5doris18ScalarColumnReaderILb0ELb1EE36convert_dict_column_to_string_columnEPKNS_12ColumnVectorILNS_13PrimitiveTypeE5EEE
Unexecuted instantiation: _ZN5doris18ScalarColumnReaderILb0ELb0EE36convert_dict_column_to_string_columnEPKNS_12ColumnVectorILNS_13PrimitiveTypeE5EEE
2123
2124
template <bool IN_COLLECTION, bool OFFSET_INDEX>
2125
Status ScalarColumnReader<IN_COLLECTION, OFFSET_INDEX>::_try_load_dict_page(bool* loaded,
2126
2
                                                                            bool* has_dict) {
2127
    // _chunk_reader init will load first page header to check whether has dict page
2128
2
    *loaded = true;
2129
2
    *has_dict = _chunk_reader->has_dict();
2130
2
    return Status::OK();
2131
2
}
Unexecuted instantiation: _ZN5doris18ScalarColumnReaderILb1ELb1EE19_try_load_dict_pageEPbS2_
Unexecuted instantiation: _ZN5doris18ScalarColumnReaderILb1ELb0EE19_try_load_dict_pageEPbS2_
Unexecuted instantiation: _ZN5doris18ScalarColumnReaderILb0ELb1EE19_try_load_dict_pageEPbS2_
_ZN5doris18ScalarColumnReaderILb0ELb0EE19_try_load_dict_pageEPbS2_
Line
Count
Source
2126
2
                                                                            bool* has_dict) {
2127
    // _chunk_reader init will load first page header to check whether has dict page
2128
2
    *loaded = true;
2129
2
    *has_dict = _chunk_reader->has_dict();
2130
2
    return Status::OK();
2131
2
}
2132
2133
template <bool IN_COLLECTION, bool OFFSET_INDEX>
2134
// Existing scalar read path handles page iteration, filtering, and conversion in one dispatch loop.
2135
// NOLINTNEXTLINE(readability-function-cognitive-complexity,readability-function-size)
2136
Status ScalarColumnReader<IN_COLLECTION, OFFSET_INDEX>::read_column_data(
2137
        ColumnPtr& doris_column, const DataTypePtr& type,
2138
        const std::shared_ptr<TableSchemaChangeHelper::Node>& root_node, FilterMap& filter_map,
2139
        size_t batch_size, size_t* read_rows, bool* eof, bool is_dict_filter,
2140
287
        int64_t real_column_size) {
2141
287
    if (_converter == nullptr) {
2142
114
        _converter = parquet::PhysicalToLogicalConverter::get_converter(
2143
114
                _field_schema, _field_schema->data_type, type, _ctz, is_dict_filter);
2144
114
        if (!_converter->support()) {
2145
0
            return Status::InternalError(
2146
0
                    "The column type of '{}' is not supported: {}, is_dict_filter: {}, "
2147
0
                    "src_logical_type: {}, dst_logical_type: {}",
2148
0
                    _field_schema->name, _converter->get_error_msg(), is_dict_filter,
2149
0
                    _field_schema->data_type->get_name(), type->get_name());
2150
0
        }
2151
114
    }
2152
    // !FIXME: We should verify whether the get_physical_column logic is correct, why do we return a doris_column?
2153
287
    ColumnPtr resolved_column =
2154
287
            _converter->get_physical_column(_field_schema->physical_type, _field_schema->data_type,
2155
287
                                            doris_column, type, is_dict_filter);
2156
287
    DataTypePtr& resolved_type = _converter->get_physical_type();
2157
2158
287
    _def_levels.clear();
2159
287
    _rep_levels.clear();
2160
287
    *read_rows = 0;
2161
2162
287
    if (_in_nested) {
2163
13
        RETURN_IF_ERROR(_read_nested_column(resolved_column, resolved_type, filter_map, batch_size,
2164
13
                                            read_rows, eof, is_dict_filter));
2165
13
        return _converter->convert(resolved_column, _field_schema->data_type, type, doris_column,
2166
13
                                   is_dict_filter);
2167
13
    }
2168
2169
274
    int64_t right_row = 0;
2170
274
    if constexpr (OFFSET_INDEX == false) {
2171
274
        RETURN_IF_ERROR(_chunk_reader->parse_page_header());
2172
274
        right_row = _chunk_reader->page_end_row();
2173
274
    } else {
2174
0
        right_row = _chunk_reader->page_end_row();
2175
0
    }
2176
2177
274
    do {
2178
        // generate the row ranges that should be read
2179
274
        RowRanges read_ranges;
2180
274
        _generate_read_ranges(RowRange {_current_row_index, right_row}, &read_ranges);
2181
274
        if (read_ranges.count() == 0) {
2182
            // skip the whole page
2183
63
            _current_row_index = right_row;
2184
211
        } else {
2185
211
            bool skip_whole_batch = false;
2186
            // Determining whether to skip page or batch will increase the calculation time.
2187
            // When the filtering effect is greater than 60%, it is possible to skip the page or batch.
2188
211
            if (filter_map.has_filter() && filter_map.filter_ratio() > 0.6) {
2189
                // lazy read
2190
0
                size_t remaining_num_values = read_ranges.count();
2191
0
                if (batch_size >= remaining_num_values &&
2192
0
                    filter_map.can_filter_all(remaining_num_values, _filter_map_index)) {
2193
                    // We can skip the whole page if the remaining values are filtered by predicate columns
2194
0
                    _filter_map_index += remaining_num_values;
2195
0
                    _current_row_index = right_row;
2196
0
                    *read_rows = remaining_num_values;
2197
0
                    break;
2198
0
                }
2199
0
                skip_whole_batch = batch_size <= remaining_num_values &&
2200
0
                                   filter_map.can_filter_all(batch_size, _filter_map_index);
2201
0
                if (skip_whole_batch) {
2202
0
                    _filter_map_index += batch_size;
2203
0
                }
2204
0
            }
2205
            // load page data to decode or skip values
2206
211
            RETURN_IF_ERROR(_chunk_reader->parse_page_header());
2207
211
            RETURN_IF_ERROR(_chunk_reader->load_page_data_idempotent());
2208
211
            size_t has_read = 0;
2209
344
            for (size_t idx = 0; idx < read_ranges.range_size(); idx++) {
2210
244
                auto range = read_ranges.get_range(idx);
2211
                // generate the skipped values
2212
244
                size_t skip_values = range.from() - _current_row_index;
2213
244
                RETURN_IF_ERROR(_skip_values(skip_values));
2214
244
                _current_row_index += skip_values;
2215
                // generate the read values
2216
244
                size_t read_values =
2217
244
                        std::min((size_t)(range.to() - range.from()), batch_size - has_read);
2218
244
                if (skip_whole_batch) {
2219
0
                    RETURN_IF_ERROR(_skip_values(read_values));
2220
244
                } else {
2221
244
                    RETURN_IF_ERROR(_read_values(read_values, resolved_column, resolved_type,
2222
244
                                                 filter_map, is_dict_filter));
2223
244
                }
2224
244
                has_read += read_values;
2225
244
                *read_rows += read_values;
2226
244
                _current_row_index += read_values;
2227
244
                if (has_read == batch_size) {
2228
111
                    break;
2229
111
                }
2230
244
            }
2231
211
        }
2232
274
    } while (false);
2233
2234
274
    if (right_row == _current_row_index) {
2235
101
        if (!_chunk_reader->has_next_page()) {
2236
101
            *eof = true;
2237
101
        } else {
2238
0
            RETURN_IF_ERROR(_chunk_reader->next_page());
2239
0
        }
2240
101
    }
2241
2242
274
    {
2243
274
        SCOPED_RAW_TIMER(&_convert_time);
2244
274
        RETURN_IF_ERROR(_converter->convert(resolved_column, _field_schema->data_type, type,
2245
274
                                            doris_column, is_dict_filter));
2246
274
    }
2247
274
    return Status::OK();
2248
274
}
Unexecuted instantiation: _ZN5doris18ScalarColumnReaderILb1ELb1EE16read_column_dataERNS_3COWINS_7IColumnEE13immutable_ptrIS3_EERKSt10shared_ptrIKNS_9IDataTypeEERKS8_INS_23TableSchemaChangeHelper4NodeEERNS_9FilterMapEmPmPbbl
_ZN5doris18ScalarColumnReaderILb1ELb0EE16read_column_dataERNS_3COWINS_7IColumnEE13immutable_ptrIS3_EERKSt10shared_ptrIKNS_9IDataTypeEERKS8_INS_23TableSchemaChangeHelper4NodeEERNS_9FilterMapEmPmPbbl
Line
Count
Source
2140
3
        int64_t real_column_size) {
2141
3
    if (_converter == nullptr) {
2142
3
        _converter = parquet::PhysicalToLogicalConverter::get_converter(
2143
3
                _field_schema, _field_schema->data_type, type, _ctz, is_dict_filter);
2144
3
        if (!_converter->support()) {
2145
0
            return Status::InternalError(
2146
0
                    "The column type of '{}' is not supported: {}, is_dict_filter: {}, "
2147
0
                    "src_logical_type: {}, dst_logical_type: {}",
2148
0
                    _field_schema->name, _converter->get_error_msg(), is_dict_filter,
2149
0
                    _field_schema->data_type->get_name(), type->get_name());
2150
0
        }
2151
3
    }
2152
    // !FIXME: We should verify whether the get_physical_column logic is correct, why do we return a doris_column?
2153
3
    ColumnPtr resolved_column =
2154
3
            _converter->get_physical_column(_field_schema->physical_type, _field_schema->data_type,
2155
3
                                            doris_column, type, is_dict_filter);
2156
3
    DataTypePtr& resolved_type = _converter->get_physical_type();
2157
2158
3
    _def_levels.clear();
2159
3
    _rep_levels.clear();
2160
3
    *read_rows = 0;
2161
2162
3
    if (_in_nested) {
2163
3
        RETURN_IF_ERROR(_read_nested_column(resolved_column, resolved_type, filter_map, batch_size,
2164
3
                                            read_rows, eof, is_dict_filter));
2165
3
        return _converter->convert(resolved_column, _field_schema->data_type, type, doris_column,
2166
3
                                   is_dict_filter);
2167
3
    }
2168
2169
0
    int64_t right_row = 0;
2170
0
    if constexpr (OFFSET_INDEX == false) {
2171
0
        RETURN_IF_ERROR(_chunk_reader->parse_page_header());
2172
0
        right_row = _chunk_reader->page_end_row();
2173
    } else {
2174
        right_row = _chunk_reader->page_end_row();
2175
    }
2176
2177
0
    do {
2178
        // generate the row ranges that should be read
2179
0
        RowRanges read_ranges;
2180
0
        _generate_read_ranges(RowRange {_current_row_index, right_row}, &read_ranges);
2181
0
        if (read_ranges.count() == 0) {
2182
            // skip the whole page
2183
0
            _current_row_index = right_row;
2184
0
        } else {
2185
0
            bool skip_whole_batch = false;
2186
            // Determining whether to skip page or batch will increase the calculation time.
2187
            // When the filtering effect is greater than 60%, it is possible to skip the page or batch.
2188
0
            if (filter_map.has_filter() && filter_map.filter_ratio() > 0.6) {
2189
                // lazy read
2190
0
                size_t remaining_num_values = read_ranges.count();
2191
0
                if (batch_size >= remaining_num_values &&
2192
0
                    filter_map.can_filter_all(remaining_num_values, _filter_map_index)) {
2193
                    // We can skip the whole page if the remaining values are filtered by predicate columns
2194
0
                    _filter_map_index += remaining_num_values;
2195
0
                    _current_row_index = right_row;
2196
0
                    *read_rows = remaining_num_values;
2197
0
                    break;
2198
0
                }
2199
0
                skip_whole_batch = batch_size <= remaining_num_values &&
2200
0
                                   filter_map.can_filter_all(batch_size, _filter_map_index);
2201
0
                if (skip_whole_batch) {
2202
0
                    _filter_map_index += batch_size;
2203
0
                }
2204
0
            }
2205
            // load page data to decode or skip values
2206
0
            RETURN_IF_ERROR(_chunk_reader->parse_page_header());
2207
0
            RETURN_IF_ERROR(_chunk_reader->load_page_data_idempotent());
2208
0
            size_t has_read = 0;
2209
0
            for (size_t idx = 0; idx < read_ranges.range_size(); idx++) {
2210
0
                auto range = read_ranges.get_range(idx);
2211
                // generate the skipped values
2212
0
                size_t skip_values = range.from() - _current_row_index;
2213
0
                RETURN_IF_ERROR(_skip_values(skip_values));
2214
0
                _current_row_index += skip_values;
2215
                // generate the read values
2216
0
                size_t read_values =
2217
0
                        std::min((size_t)(range.to() - range.from()), batch_size - has_read);
2218
0
                if (skip_whole_batch) {
2219
0
                    RETURN_IF_ERROR(_skip_values(read_values));
2220
0
                } else {
2221
0
                    RETURN_IF_ERROR(_read_values(read_values, resolved_column, resolved_type,
2222
0
                                                 filter_map, is_dict_filter));
2223
0
                }
2224
0
                has_read += read_values;
2225
0
                *read_rows += read_values;
2226
0
                _current_row_index += read_values;
2227
0
                if (has_read == batch_size) {
2228
0
                    break;
2229
0
                }
2230
0
            }
2231
0
        }
2232
0
    } while (false);
2233
2234
0
    if (right_row == _current_row_index) {
2235
0
        if (!_chunk_reader->has_next_page()) {
2236
0
            *eof = true;
2237
0
        } else {
2238
0
            RETURN_IF_ERROR(_chunk_reader->next_page());
2239
0
        }
2240
0
    }
2241
2242
0
    {
2243
0
        SCOPED_RAW_TIMER(&_convert_time);
2244
0
        RETURN_IF_ERROR(_converter->convert(resolved_column, _field_schema->data_type, type,
2245
0
                                            doris_column, is_dict_filter));
2246
0
    }
2247
0
    return Status::OK();
2248
0
}
Unexecuted instantiation: _ZN5doris18ScalarColumnReaderILb0ELb1EE16read_column_dataERNS_3COWINS_7IColumnEE13immutable_ptrIS3_EERKSt10shared_ptrIKNS_9IDataTypeEERKS8_INS_23TableSchemaChangeHelper4NodeEERNS_9FilterMapEmPmPbbl
_ZN5doris18ScalarColumnReaderILb0ELb0EE16read_column_dataERNS_3COWINS_7IColumnEE13immutable_ptrIS3_EERKSt10shared_ptrIKNS_9IDataTypeEERKS8_INS_23TableSchemaChangeHelper4NodeEERNS_9FilterMapEmPmPbbl
Line
Count
Source
2140
284
        int64_t real_column_size) {
2141
284
    if (_converter == nullptr) {
2142
111
        _converter = parquet::PhysicalToLogicalConverter::get_converter(
2143
111
                _field_schema, _field_schema->data_type, type, _ctz, is_dict_filter);
2144
111
        if (!_converter->support()) {
2145
0
            return Status::InternalError(
2146
0
                    "The column type of '{}' is not supported: {}, is_dict_filter: {}, "
2147
0
                    "src_logical_type: {}, dst_logical_type: {}",
2148
0
                    _field_schema->name, _converter->get_error_msg(), is_dict_filter,
2149
0
                    _field_schema->data_type->get_name(), type->get_name());
2150
0
        }
2151
111
    }
2152
    // !FIXME: We should verify whether the get_physical_column logic is correct, why do we return a doris_column?
2153
284
    ColumnPtr resolved_column =
2154
284
            _converter->get_physical_column(_field_schema->physical_type, _field_schema->data_type,
2155
284
                                            doris_column, type, is_dict_filter);
2156
284
    DataTypePtr& resolved_type = _converter->get_physical_type();
2157
2158
284
    _def_levels.clear();
2159
284
    _rep_levels.clear();
2160
284
    *read_rows = 0;
2161
2162
284
    if (_in_nested) {
2163
10
        RETURN_IF_ERROR(_read_nested_column(resolved_column, resolved_type, filter_map, batch_size,
2164
10
                                            read_rows, eof, is_dict_filter));
2165
10
        return _converter->convert(resolved_column, _field_schema->data_type, type, doris_column,
2166
10
                                   is_dict_filter);
2167
10
    }
2168
2169
274
    int64_t right_row = 0;
2170
274
    if constexpr (OFFSET_INDEX == false) {
2171
274
        RETURN_IF_ERROR(_chunk_reader->parse_page_header());
2172
274
        right_row = _chunk_reader->page_end_row();
2173
    } else {
2174
        right_row = _chunk_reader->page_end_row();
2175
    }
2176
2177
274
    do {
2178
        // generate the row ranges that should be read
2179
274
        RowRanges read_ranges;
2180
274
        _generate_read_ranges(RowRange {_current_row_index, right_row}, &read_ranges);
2181
274
        if (read_ranges.count() == 0) {
2182
            // skip the whole page
2183
63
            _current_row_index = right_row;
2184
211
        } else {
2185
211
            bool skip_whole_batch = false;
2186
            // Determining whether to skip page or batch will increase the calculation time.
2187
            // When the filtering effect is greater than 60%, it is possible to skip the page or batch.
2188
211
            if (filter_map.has_filter() && filter_map.filter_ratio() > 0.6) {
2189
                // lazy read
2190
0
                size_t remaining_num_values = read_ranges.count();
2191
0
                if (batch_size >= remaining_num_values &&
2192
0
                    filter_map.can_filter_all(remaining_num_values, _filter_map_index)) {
2193
                    // We can skip the whole page if the remaining values are filtered by predicate columns
2194
0
                    _filter_map_index += remaining_num_values;
2195
0
                    _current_row_index = right_row;
2196
0
                    *read_rows = remaining_num_values;
2197
0
                    break;
2198
0
                }
2199
0
                skip_whole_batch = batch_size <= remaining_num_values &&
2200
0
                                   filter_map.can_filter_all(batch_size, _filter_map_index);
2201
0
                if (skip_whole_batch) {
2202
0
                    _filter_map_index += batch_size;
2203
0
                }
2204
0
            }
2205
            // load page data to decode or skip values
2206
211
            RETURN_IF_ERROR(_chunk_reader->parse_page_header());
2207
211
            RETURN_IF_ERROR(_chunk_reader->load_page_data_idempotent());
2208
211
            size_t has_read = 0;
2209
344
            for (size_t idx = 0; idx < read_ranges.range_size(); idx++) {
2210
244
                auto range = read_ranges.get_range(idx);
2211
                // generate the skipped values
2212
244
                size_t skip_values = range.from() - _current_row_index;
2213
244
                RETURN_IF_ERROR(_skip_values(skip_values));
2214
244
                _current_row_index += skip_values;
2215
                // generate the read values
2216
244
                size_t read_values =
2217
244
                        std::min((size_t)(range.to() - range.from()), batch_size - has_read);
2218
244
                if (skip_whole_batch) {
2219
0
                    RETURN_IF_ERROR(_skip_values(read_values));
2220
244
                } else {
2221
244
                    RETURN_IF_ERROR(_read_values(read_values, resolved_column, resolved_type,
2222
244
                                                 filter_map, is_dict_filter));
2223
244
                }
2224
244
                has_read += read_values;
2225
244
                *read_rows += read_values;
2226
244
                _current_row_index += read_values;
2227
244
                if (has_read == batch_size) {
2228
111
                    break;
2229
111
                }
2230
244
            }
2231
211
        }
2232
274
    } while (false);
2233
2234
274
    if (right_row == _current_row_index) {
2235
101
        if (!_chunk_reader->has_next_page()) {
2236
101
            *eof = true;
2237
101
        } else {
2238
0
            RETURN_IF_ERROR(_chunk_reader->next_page());
2239
0
        }
2240
101
    }
2241
2242
274
    {
2243
274
        SCOPED_RAW_TIMER(&_convert_time);
2244
274
        RETURN_IF_ERROR(_converter->convert(resolved_column, _field_schema->data_type, type,
2245
274
                                            doris_column, is_dict_filter));
2246
274
    }
2247
274
    return Status::OK();
2248
274
}
2249
2250
Status ArrayColumnReader::init(std::unique_ptr<ParquetColumnReader> element_reader,
2251
2
                               FieldSchema* field) {
2252
2
    _field_schema = field;
2253
2
    _element_reader = std::move(element_reader);
2254
2
    return Status::OK();
2255
2
}
2256
2257
Status ArrayColumnReader::read_column_data(
2258
        ColumnPtr& doris_column, const DataTypePtr& type,
2259
        const std::shared_ptr<TableSchemaChangeHelper::Node>& root_node, FilterMap& filter_map,
2260
        size_t batch_size, size_t* read_rows, bool* eof, bool is_dict_filter,
2261
2
        int64_t real_column_size) {
2262
2
    MutableColumnPtr data_column;
2263
2
    NullMap* null_map_ptr = nullptr;
2264
2
    if (doris_column->is_nullable()) {
2265
2
        auto mutable_column = doris_column->assume_mutable();
2266
2
        auto* nullable_column = assert_cast<ColumnNullable*>(mutable_column.get());
2267
2
        null_map_ptr = &nullable_column->get_null_map_data();
2268
2
        data_column = nullable_column->get_nested_column_ptr();
2269
2
    } else {
2270
0
        if (_field_schema->data_type->is_nullable()) {
2271
0
            return Status::Corruption("Not nullable column has null values in parquet file");
2272
0
        }
2273
0
        data_column = doris_column->assume_mutable();
2274
0
    }
2275
2
    if (type->get_primitive_type() != PrimitiveType::TYPE_ARRAY) {
2276
0
        return Status::Corruption(
2277
0
                "Wrong data type for column '{}', expected Array type, actual type: {}.",
2278
0
                _field_schema->name, type->get_name());
2279
0
    }
2280
2281
2
    ColumnPtr& element_column = assert_cast<ColumnArray&>(*data_column).get_data_ptr();
2282
2
    const DataTypePtr& element_type =
2283
2
            (assert_cast<const DataTypeArray*>(remove_nullable(type).get()))->get_nested_type();
2284
    // read nested column
2285
2
    RETURN_IF_ERROR(_element_reader->read_column_data(element_column, element_type,
2286
2
                                                      root_node->get_element_node(), filter_map,
2287
2
                                                      batch_size, read_rows, eof, is_dict_filter));
2288
2
    if (*read_rows == 0) {
2289
0
        return Status::OK();
2290
0
    }
2291
2292
2
    ColumnArray::Offsets64& offsets_data = assert_cast<ColumnArray&>(*data_column).get_offsets();
2293
    // fill offset and null map
2294
2
    fill_array_offset(_field_schema, offsets_data, null_map_ptr, _element_reader->get_rep_level(),
2295
2
                      _element_reader->get_def_level());
2296
2
    DCHECK_EQ(element_column->size(), offsets_data.back());
2297
2
#ifndef NDEBUG
2298
2
    doris_column->sanity_check();
2299
2
#endif
2300
2
    return Status::OK();
2301
2
}
2302
2303
Status MapColumnReader::init(std::unique_ptr<ParquetColumnReader> key_reader,
2304
                             std::unique_ptr<ParquetColumnReader> value_reader,
2305
0
                             FieldSchema* field) {
2306
0
    _field_schema = field;
2307
0
    _key_reader = std::move(key_reader);
2308
0
    _value_reader = std::move(value_reader);
2309
0
    return Status::OK();
2310
0
}
2311
2312
Status MapColumnReader::read_column_data(
2313
        ColumnPtr& doris_column, const DataTypePtr& type,
2314
        const std::shared_ptr<TableSchemaChangeHelper::Node>& root_node, FilterMap& filter_map,
2315
        size_t batch_size, size_t* read_rows, bool* eof, bool is_dict_filter,
2316
0
        int64_t real_column_size) {
2317
0
    MutableColumnPtr data_column;
2318
0
    NullMap* null_map_ptr = nullptr;
2319
0
    if (doris_column->is_nullable()) {
2320
0
        auto mutable_column = doris_column->assume_mutable();
2321
0
        auto* nullable_column = assert_cast<ColumnNullable*>(mutable_column.get());
2322
0
        null_map_ptr = &nullable_column->get_null_map_data();
2323
0
        data_column = nullable_column->get_nested_column_ptr();
2324
0
    } else {
2325
0
        if (_field_schema->data_type->is_nullable()) {
2326
0
            return Status::Corruption("Not nullable column has null values in parquet file");
2327
0
        }
2328
0
        data_column = doris_column->assume_mutable();
2329
0
    }
2330
0
    if (remove_nullable(type)->get_primitive_type() != PrimitiveType::TYPE_MAP) {
2331
0
        return Status::Corruption(
2332
0
                "Wrong data type for column '{}', expected Map type, actual type id {}.",
2333
0
                _field_schema->name, type->get_name());
2334
0
    }
2335
2336
0
    auto& map = assert_cast<ColumnMap&>(*data_column);
2337
0
    const DataTypePtr& key_type =
2338
0
            assert_cast<const DataTypeMap*>(remove_nullable(type).get())->get_key_type();
2339
0
    const DataTypePtr& value_type =
2340
0
            assert_cast<const DataTypeMap*>(remove_nullable(type).get())->get_value_type();
2341
0
    ColumnPtr& key_column = map.get_keys_ptr();
2342
0
    ColumnPtr& value_column = map.get_values_ptr();
2343
2344
0
    size_t key_rows = 0;
2345
0
    size_t value_rows = 0;
2346
0
    bool key_eof = false;
2347
0
    bool value_eof = false;
2348
0
    int64_t orig_col_column_size = key_column->size();
2349
2350
0
    RETURN_IF_ERROR(_key_reader->read_column_data(key_column, key_type, root_node->get_key_node(),
2351
0
                                                  filter_map, batch_size, &key_rows, &key_eof,
2352
0
                                                  is_dict_filter));
2353
2354
0
    while (value_rows < key_rows && !value_eof) {
2355
0
        size_t loop_rows = 0;
2356
0
        RETURN_IF_ERROR(_value_reader->read_column_data(
2357
0
                value_column, value_type, root_node->get_value_node(), filter_map,
2358
0
                key_rows - value_rows, &loop_rows, &value_eof, is_dict_filter,
2359
0
                key_column->size() - orig_col_column_size));
2360
0
        value_rows += loop_rows;
2361
0
    }
2362
0
    DCHECK_EQ(key_rows, value_rows);
2363
0
    *read_rows = key_rows;
2364
0
    *eof = key_eof;
2365
2366
0
    if (*read_rows == 0) {
2367
0
        return Status::OK();
2368
0
    }
2369
2370
0
    DCHECK_EQ(key_column->size(), value_column->size());
2371
    // fill offset and null map
2372
0
    fill_array_offset(_field_schema, map.get_offsets(), null_map_ptr, _key_reader->get_rep_level(),
2373
0
                      _key_reader->get_def_level());
2374
0
    DCHECK_EQ(key_column->size(), map.get_offsets().back());
2375
0
#ifndef NDEBUG
2376
0
    doris_column->sanity_check();
2377
0
#endif
2378
0
    return Status::OK();
2379
0
}
2380
2381
Status StructColumnReader::init(
2382
        std::unordered_map<std::string, std::unique_ptr<ParquetColumnReader>>&& child_readers,
2383
11
        FieldSchema* field) {
2384
11
    _field_schema = field;
2385
11
    _child_readers = std::move(child_readers);
2386
11
    return Status::OK();
2387
11
}
2388
// Existing struct reader coordinates child readers, missing columns, and selection state.
2389
// NOLINTNEXTLINE(readability-function-cognitive-complexity,readability-function-size)
2390
Status StructColumnReader::read_column_data(
2391
        ColumnPtr& doris_column, const DataTypePtr& type,
2392
        const std::shared_ptr<TableSchemaChangeHelper::Node>& root_node, FilterMap& filter_map,
2393
        size_t batch_size, size_t* read_rows, bool* eof, bool is_dict_filter,
2394
11
        int64_t real_column_size) {
2395
11
    MutableColumnPtr data_column;
2396
11
    NullMap* null_map_ptr = nullptr;
2397
11
    if (doris_column->is_nullable()) {
2398
11
        auto mutable_column = doris_column->assume_mutable();
2399
11
        auto* nullable_column = assert_cast<ColumnNullable*>(mutable_column.get());
2400
11
        null_map_ptr = &nullable_column->get_null_map_data();
2401
11
        data_column = nullable_column->get_nested_column_ptr();
2402
11
    } else {
2403
0
        if (_field_schema->data_type->is_nullable()) {
2404
0
            return Status::Corruption("Not nullable column has null values in parquet file");
2405
0
        }
2406
0
        data_column = doris_column->assume_mutable();
2407
0
    }
2408
11
    if (type->get_primitive_type() != PrimitiveType::TYPE_STRUCT) {
2409
0
        return Status::Corruption(
2410
0
                "Wrong data type for column '{}', expected Struct type, actual type id {}.",
2411
0
                _field_schema->name, type->get_name());
2412
0
    }
2413
2414
11
    auto& doris_struct = assert_cast<ColumnStruct&>(*data_column);
2415
11
    const auto* doris_struct_type = assert_cast<const DataTypeStruct*>(remove_nullable(type).get());
2416
2417
11
    int64_t not_missing_column_id = -1;
2418
11
    size_t not_missing_orig_column_size = 0;
2419
11
    std::vector<size_t> missing_column_idxs {};
2420
11
    std::vector<size_t> skip_reading_column_idxs {};
2421
2422
11
    _read_column_names.clear();
2423
2424
37
    for (size_t i = 0; i < doris_struct.tuple_size(); ++i) {
2425
26
        ColumnPtr& doris_field = doris_struct.get_column_ptr(i);
2426
26
        const auto& doris_type = doris_struct_type->get_element(i);
2427
26
        const auto& doris_name = doris_struct_type->get_element_name(i);
2428
26
        if (!root_node->children_column_exists(doris_name)) {
2429
0
            missing_column_idxs.push_back(i);
2430
0
            VLOG_DEBUG << "[ParquetReader] Missing column in schema: column_idx[" << i
2431
0
                       << "], doris_name: " << doris_name << " (column not exists in root node)";
2432
0
            continue;
2433
0
        }
2434
26
        auto file_name = root_node->children_file_column_name(doris_name);
2435
2436
        // Check if this is a SkipReadingReader - we should skip it when choosing reference column
2437
        // because SkipReadingReader doesn't know the actual data size in nested context
2438
26
        bool is_skip_reader =
2439
26
                dynamic_cast<SkipReadingReader*>(_child_readers[file_name].get()) != nullptr;
2440
2441
26
        if (is_skip_reader) {
2442
            // Store SkipReadingReader columns to fill them later based on reference column size
2443
4
            skip_reading_column_idxs.push_back(i);
2444
4
            continue;
2445
4
        }
2446
2447
        // Only add non-SkipReadingReader columns to _read_column_names
2448
        // This ensures get_rep_level() and get_def_level() return valid levels
2449
22
        _read_column_names.emplace_back(file_name);
2450
2451
22
        size_t field_rows = 0;
2452
22
        bool field_eof = false;
2453
22
        if (not_missing_column_id == -1) {
2454
11
            not_missing_column_id = i;
2455
11
            not_missing_orig_column_size = doris_field->size();
2456
11
            RETURN_IF_ERROR(_child_readers[file_name]->read_column_data(
2457
11
                    doris_field, doris_type, root_node->get_children_node(doris_name), filter_map,
2458
11
                    batch_size, &field_rows, &field_eof, is_dict_filter));
2459
11
            *read_rows = field_rows;
2460
11
            *eof = field_eof;
2461
            /*
2462
             * Considering the issue in the `_read_nested_column` function where data may span across pages, leading
2463
             * to missing definition and repetition levels, when filling the null_map of the struct later, it is
2464
             * crucial to use the definition and repetition levels from the first read column
2465
             * (since `_read_nested_column` is not called repeatedly).
2466
             *
2467
             *  It is worth mentioning that, theoretically, any sub-column can be chosen to fill the null_map,
2468
             *  and selecting the shortest one will offer better performance
2469
             */
2470
11
        } else {
2471
22
            while (field_rows < *read_rows && !field_eof) {
2472
11
                size_t loop_rows = 0;
2473
11
                RETURN_IF_ERROR(_child_readers[file_name]->read_column_data(
2474
11
                        doris_field, doris_type, root_node->get_children_node(doris_name),
2475
11
                        filter_map, *read_rows - field_rows, &loop_rows, &field_eof,
2476
11
                        is_dict_filter));
2477
11
                field_rows += loop_rows;
2478
11
            }
2479
11
            DCHECK_EQ(*read_rows, field_rows);
2480
            //            DCHECK_EQ(*eof, field_eof);
2481
11
        }
2482
22
    }
2483
2484
11
    int64_t missing_column_sz = -1;
2485
2486
11
    if (not_missing_column_id == -1) {
2487
        // All queried columns are missing in the file (e.g., all added after schema change)
2488
        // We need to pick a column from _field_schema children that exists in the file for RL/DL reference
2489
0
        std::string reference_file_column_name;
2490
0
        std::unique_ptr<ParquetColumnReader>* reference_reader = nullptr;
2491
2492
0
        for (const auto& child : _field_schema->children) {
2493
0
            auto it = _child_readers.find(child.name);
2494
0
            if (it != _child_readers.end()) {
2495
                // Skip SkipReadingReader as they don't have valid RL/DL
2496
0
                bool is_skip_reader = dynamic_cast<SkipReadingReader*>(it->second.get()) != nullptr;
2497
0
                if (!is_skip_reader) {
2498
0
                    reference_file_column_name = child.name;
2499
0
                    reference_reader = &(it->second);
2500
0
                    break;
2501
0
                }
2502
0
            }
2503
0
        }
2504
2505
0
        if (reference_reader != nullptr) {
2506
            // Read the reference column to get correct RL/DL information
2507
            // TODO: Optimize by only reading RL/DL without actual data decoding
2508
2509
            // We need to find the FieldSchema for the reference column from _field_schema children
2510
0
            FieldSchema* ref_field_schema = nullptr;
2511
0
            for (auto& child : _field_schema->children) {
2512
0
                if (child.name == reference_file_column_name) {
2513
0
                    ref_field_schema = &child;
2514
0
                    break;
2515
0
                }
2516
0
            }
2517
2518
0
            if (ref_field_schema == nullptr) {
2519
0
                return Status::InternalError(
2520
0
                        "Cannot find field schema for reference column '{}' in struct '{}'",
2521
0
                        reference_file_column_name, _field_schema->name);
2522
0
            }
2523
2524
            // Create a temporary column to hold the data (we'll use its size for missing_column_sz)
2525
0
            ColumnPtr temp_column = ref_field_schema->data_type->create_column();
2526
0
            auto temp_type = ref_field_schema->data_type;
2527
2528
0
            size_t field_rows = 0;
2529
0
            bool field_eof = false;
2530
2531
            // Use ConstNode for the reference column instead of looking up from root_node.
2532
            // The reference column is only used to get RL/DL information for determining the number
2533
            // of elements in the struct. It may be a column that has been dropped from the table
2534
            // schema (e.g., 'removed' field), but still exists in older parquet files.
2535
            // Since we don't need schema mapping for this column (we just need its RL/DL levels),
2536
            // using ConstNode is safe and avoids the issue where the reference column doesn't exist
2537
            // in root_node (because it was dropped from table schema).
2538
0
            auto ref_child_node = TableSchemaChangeHelper::ConstNode::get_instance();
2539
0
            not_missing_orig_column_size = temp_column->size();
2540
2541
0
            RETURN_IF_ERROR((*reference_reader)
2542
0
                                    ->read_column_data(temp_column, temp_type, ref_child_node,
2543
0
                                                       filter_map, batch_size, &field_rows,
2544
0
                                                       &field_eof, is_dict_filter));
2545
2546
0
            *read_rows = field_rows;
2547
0
            *eof = field_eof;
2548
2549
            // Store this reference column name for get_rep_level/get_def_level to use
2550
0
            _read_column_names.emplace_back(reference_file_column_name);
2551
2552
0
            missing_column_sz = temp_column->size() - not_missing_orig_column_size;
2553
0
        } else {
2554
0
            return Status::Corruption(
2555
0
                    "Cannot read struct '{}': all queried columns are missing and no reference "
2556
0
                    "column found in file",
2557
0
                    _field_schema->name);
2558
0
        }
2559
0
    }
2560
2561
    //  This missing_column_sz is not *read_rows. Because read_rows returns the number of rows.
2562
    //  For example: suppose we have a column array<struct<a:int,b:string>>,
2563
    //  where b is a newly added column, that is, a missing column.
2564
    //  There are two rows of data in this column,
2565
    //      [{1,null},{2,null},{3,null}]
2566
    //      [{4,null},{5,null}]
2567
    //  When you first read subcolumn a, you read 5 data items and the value of *read_rows is 2.
2568
    //  You should insert 5 records into subcolumn b instead of 2.
2569
11
    if (missing_column_sz == -1) {
2570
11
        missing_column_sz = doris_struct.get_column(not_missing_column_id).size() -
2571
11
                            not_missing_orig_column_size;
2572
11
    }
2573
2574
    // Fill SkipReadingReader columns with the correct amount of data based on reference column
2575
    // Let SkipReadingReader handle the data filling through its read_column_data method
2576
11
    for (auto idx : skip_reading_column_idxs) {
2577
4
        auto& doris_field = doris_struct.get_column_ptr(idx);
2578
4
        auto& doris_type = const_cast<DataTypePtr&>(doris_struct_type->get_element(idx));
2579
4
        auto& doris_name = const_cast<String&>(doris_struct_type->get_element_name(idx));
2580
4
        auto file_name = root_node->children_file_column_name(doris_name);
2581
2582
4
        size_t field_rows = 0;
2583
4
        bool field_eof = false;
2584
4
        RETURN_IF_ERROR(_child_readers[file_name]->read_column_data(
2585
4
                doris_field, doris_type, root_node->get_children_node(doris_name), filter_map,
2586
4
                missing_column_sz, &field_rows, &field_eof, is_dict_filter, missing_column_sz));
2587
4
    }
2588
2589
    // Fill truly missing columns (not in root_node) with null or default value
2590
11
    for (auto idx : missing_column_idxs) {
2591
0
        auto& doris_field = doris_struct.get_column_ptr(idx);
2592
0
        const auto& doris_type = doris_struct_type->get_element(idx);
2593
0
        DCHECK(doris_type->is_nullable());
2594
0
        auto mutable_column = doris_field->assume_mutable();
2595
0
        auto* nullable_column = static_cast<ColumnNullable*>(mutable_column.get());
2596
0
        nullable_column->insert_many_defaults(missing_column_sz);
2597
0
    }
2598
2599
11
    if (null_map_ptr != nullptr) {
2600
11
        fill_struct_null_map(_field_schema, *null_map_ptr, this->get_rep_level(),
2601
11
                             this->get_def_level());
2602
11
    }
2603
11
#ifndef NDEBUG
2604
11
    doris_column->sanity_check();
2605
11
#endif
2606
11
    return Status::OK();
2607
11
}
2608
2609
Status VariantColumnReader::init(io::FileReaderSPtr file, FieldSchema* field,
2610
                                 const tparquet::RowGroup& row_group, size_t max_buf_size,
2611
                                 std::unordered_map<int, tparquet::OffsetIndex>& col_offsets,
2612
                                 RuntimeState* state, bool in_collection,
2613
                                 const std::set<uint64_t>& column_ids,
2614
0
                                 const std::set<uint64_t>& filter_column_ids) {
2615
0
    _field_schema = field;
2616
0
    _column_ids = column_ids;
2617
0
    _variant_struct_field = std::make_unique<FieldSchema>(*field);
2618
2619
0
    DataTypePtr variant_struct_type = make_variant_struct_reader_type(*field);
2620
0
    _variant_struct_field->data_type = variant_struct_type;
2621
2622
0
    RETURN_IF_ERROR(ParquetColumnReader::create(file, _variant_struct_field.get(), row_group,
2623
0
                                                _row_ranges, _ctz, _io_ctx, _struct_reader,
2624
0
                                                max_buf_size, col_offsets, state, in_collection,
2625
0
                                                column_ids, filter_column_ids));
2626
0
    _struct_reader->set_column_in_nested();
2627
0
    return Status::OK();
2628
0
}
2629
2630
// NOLINTNEXTLINE(readability-function-size): existing variant read path stays local to avoid churn.
2631
Status VariantColumnReader::read_column_data(
2632
        ColumnPtr& doris_column, const DataTypePtr& type,
2633
        const std::shared_ptr<TableSchemaChangeHelper::Node>& root_node, FilterMap& filter_map,
2634
        size_t batch_size, size_t* read_rows, bool* eof, bool is_dict_filter,
2635
0
        int64_t real_column_size) {
2636
0
    (void)root_node;
2637
0
    if (remove_nullable(type)->get_primitive_type() != PrimitiveType::TYPE_VARIANT) {
2638
0
        return Status::Corruption(
2639
0
                "Wrong data type for column '{}', expected Variant type, actual type: {}.",
2640
0
                _field_schema->name, type->get_name());
2641
0
    }
2642
2643
0
    const auto& variant_struct_type = _variant_struct_field->data_type;
2644
0
    ColumnPtr struct_column = make_variant_struct_read_column(*_field_schema, variant_struct_type);
2645
0
    const size_t old_struct_rows = struct_column->size();
2646
0
    auto const_node = TableSchemaChangeHelper::ConstNode::get_instance();
2647
0
    RETURN_IF_ERROR(_struct_reader->read_column_data(struct_column, variant_struct_type, const_node,
2648
0
                                                     filter_map, batch_size, read_rows, eof,
2649
0
                                                     is_dict_filter, real_column_size));
2650
2651
0
    const size_t new_struct_rows = struct_column->size() - old_struct_rows;
2652
0
    if (new_struct_rows == 0) {
2653
0
        return Status::OK();
2654
0
    }
2655
2656
0
    MutableColumnPtr variant_column_ptr;
2657
0
    NullMap* null_map_ptr = nullptr;
2658
0
    auto mutable_column = doris_column->assume_mutable();
2659
0
    if (doris_column->is_nullable()) {
2660
0
        auto* nullable_column = assert_cast<ColumnNullable*>(mutable_column.get());
2661
0
        variant_column_ptr = nullable_column->get_nested_column_ptr();
2662
0
        null_map_ptr = &nullable_column->get_null_map_data();
2663
0
    } else {
2664
0
        if (_field_schema->data_type->is_nullable()) {
2665
0
            return Status::Corruption("Not nullable column has null values in parquet file");
2666
0
        }
2667
0
        variant_column_ptr = std::move(mutable_column);
2668
0
    }
2669
0
    auto* variant_column = assert_cast<ColumnVariant*>(variant_column_ptr.get());
2670
2671
0
    const IColumn* variant_struct_source = struct_column.get();
2672
0
    const NullMap* struct_null_map = nullptr;
2673
0
    if (const auto* nullable_struct = check_and_get_column<ColumnNullable>(variant_struct_source)) {
2674
0
        struct_null_map = &nullable_struct->get_null_map_data();
2675
0
        variant_struct_source = &nullable_struct->get_nested_column();
2676
0
    }
2677
0
    const auto& variant_struct_column = assert_cast<const ColumnStruct&>(*variant_struct_source);
2678
2679
0
    const int typed_value_idx = find_child_idx(*_field_schema, "typed_value");
2680
0
    if (can_use_direct_typed_only_value(*_field_schema, _column_ids)) {
2681
0
        _variant_statistics.variant_direct_typed_value_read_rows +=
2682
0
                static_cast<int64_t>(new_struct_rows);
2683
0
        MutableColumnPtr batch_variant_column =
2684
0
                ColumnVariant::create(variant_column->max_subcolumns_count(),
2685
0
                                      variant_column->enable_doc_mode(), new_struct_rows + 1);
2686
0
        auto* batch_variant = assert_cast<ColumnVariant*>(batch_variant_column.get());
2687
0
        PathInDataBuilder path;
2688
0
        RETURN_IF_ERROR(append_direct_typed_column_to_batch(
2689
0
                _field_schema->children[typed_value_idx],
2690
0
                variant_struct_column.get_column(typed_value_idx), old_struct_rows, new_struct_rows,
2691
0
                &path, batch_variant, false, _column_ids, {}));
2692
0
        variant_column->insert_range_from(*batch_variant_column, 1, new_struct_rows);
2693
0
        if (null_map_ptr != nullptr) {
2694
0
            for (size_t i = old_struct_rows; i < struct_column->size(); ++i) {
2695
0
                null_map_ptr->push_back(struct_null_map != nullptr && (*struct_null_map)[i]);
2696
0
            }
2697
0
        }
2698
0
#ifndef NDEBUG
2699
0
        doris_column->sanity_check();
2700
0
#endif
2701
0
        return Status::OK();
2702
0
    }
2703
2704
0
    _variant_statistics.variant_rowwise_read_rows += static_cast<int64_t>(new_struct_rows);
2705
0
    for (size_t i = old_struct_rows; i < struct_column->size(); ++i) {
2706
0
        if (struct_null_map != nullptr && (*struct_null_map)[i]) {
2707
0
            if (null_map_ptr == nullptr) {
2708
0
                return Status::Corruption("Not nullable column has null values in parquet file");
2709
0
            }
2710
0
            variant_column->insert_default();
2711
0
            null_map_ptr->push_back(1);
2712
0
            continue;
2713
0
        }
2714
0
        VariantMap values;
2715
0
        bool present = false;
2716
0
        PathInDataBuilder path;
2717
0
        std::deque<std::string> string_values;
2718
0
        RETURN_IF_ERROR(variant_to_variant_map(*_field_schema, (*struct_column)[i], nullptr, &path,
2719
0
                                               &values, &present, &string_values));
2720
0
        if (!present) {
2721
0
            values[PathInData()] = FieldWithDataType {.field = Field()};
2722
0
        }
2723
0
        RETURN_IF_CATCH_EXCEPTION(
2724
0
                variant_column->insert(Field::create_field<TYPE_VARIANT>(std::move(values))));
2725
0
        if (null_map_ptr != nullptr) {
2726
0
            null_map_ptr->push_back(0);
2727
0
        }
2728
0
    }
2729
0
#ifndef NDEBUG
2730
0
    doris_column->sanity_check();
2731
0
#endif
2732
0
    return Status::OK();
2733
0
}
2734
2735
template class ScalarColumnReader<true, true>;
2736
template class ScalarColumnReader<true, false>;
2737
template class ScalarColumnReader<false, true>;
2738
template class ScalarColumnReader<false, false>;
2739
2740
}; // namespace doris