Coverage Report

Created: 2026-10-15 11:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/format/parquet/parquet_column_convert.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/parquet_column_convert.h"
19
20
#include <cctz/time_zone.h>
21
#include <glog/logging.h>
22
23
#include <utility>
24
25
#include "common/cast_set.h"
26
#include "core/column/column_fixed_length_object.h"
27
#include "core/column/column_nullable.h"
28
#include "core/data_type/data_type_fixed_length_object.h"
29
#include "core/data_type/data_type_nullable.h"
30
#include "core/data_type/define_primitive_type.h"
31
#include "core/data_type/primitive_type.h"
32
#include "core/value/uuid_value.h"
33
#include "exprs/function/geo/functions_geo.h"
34
35
namespace doris::parquet {
36
const cctz::time_zone ConvertParams::utc0 = cctz::utc_time_zone();
37
38
#define FOR_LOGICAL_DECIMAL_TYPES(M) \
39
0
    M(TYPE_DECIMAL32)                \
40
4
    M(TYPE_DECIMAL64)                \
41
4
    M(TYPE_DECIMAL128I)              \
42
0
    M(TYPE_DECIMAL256)
43
44
327
bool PhysicalToLogicalConverter::is_parquet_native_type(PrimitiveType type) {
45
327
    switch (type) {
46
12
    case TYPE_BOOLEAN:
47
132
    case TYPE_INT:
48
169
    case TYPE_BIGINT:
49
199
    case TYPE_FLOAT:
50
218
    case TYPE_DOUBLE:
51
269
    case TYPE_STRING:
52
269
    case TYPE_CHAR:
53
269
    case TYPE_VARCHAR:
54
269
        return true;
55
58
    default:
56
58
        return false;
57
327
    }
58
327
}
59
60
41
bool PhysicalToLogicalConverter::is_decimal_type(doris::PrimitiveType type) {
61
41
    switch (type) {
62
0
    case TYPE_DECIMAL32:
63
4
    case TYPE_DECIMAL64:
64
4
    case TYPE_DECIMAL128I:
65
4
    case TYPE_DECIMAL256:
66
4
    case TYPE_DECIMALV2:
67
4
        return true;
68
37
    default:
69
37
        return false;
70
41
    }
71
41
}
72
73
ColumnPtr PhysicalToLogicalConverter::get_physical_column(tparquet::Type::type src_physical_type,
74
                                                          DataTypePtr src_logical_type,
75
                                                          ColumnPtr& dst_logical_column,
76
                                                          const DataTypePtr& dst_logical_type,
77
356
                                                          bool is_dict_filter) {
78
356
    if (is_dict_filter) {
79
1
        src_physical_type = tparquet::Type::INT32;
80
1
        src_logical_type = DataTypeFactory::instance().create_data_type(
81
1
                PrimitiveType::TYPE_INT, dst_logical_type->is_nullable());
82
1
    }
83
84
356
    if (!_convert_params->is_type_compatibility && is_consistent() &&
85
356
        _logical_converter->is_consistent()) {
86
308
        if (_cached_src_physical_type == nullptr) {
87
166
            _cached_src_physical_type = dst_logical_type->is_nullable()
88
166
                                                ? make_nullable(src_logical_type)
89
166
                                                : remove_nullable(src_logical_type);
90
166
        }
91
308
        return dst_logical_column;
92
308
    }
93
94
48
    if (!_cached_src_physical_column) {
95
37
        switch (src_physical_type) {
96
0
        case tparquet::Type::type::BOOLEAN:
97
0
            _cached_src_physical_type = std::make_shared<DataTypeUInt8>();
98
0
            break;
99
23
        case tparquet::Type::type::INT32:
100
23
            _cached_src_physical_type = std::make_shared<DataTypeInt32>();
101
23
            break;
102
6
        case tparquet::Type::type::INT64:
103
6
            _cached_src_physical_type = std::make_shared<DataTypeInt64>();
104
6
            break;
105
0
        case tparquet::Type::type::FLOAT:
106
0
            _cached_src_physical_type = std::make_shared<DataTypeFloat32>();
107
0
            break;
108
0
        case tparquet::Type::type::DOUBLE:
109
0
            _cached_src_physical_type = std::make_shared<DataTypeFloat64>();
110
0
            break;
111
4
        case tparquet::Type::type::BYTE_ARRAY:
112
4
            _cached_src_physical_type = std::make_shared<DataTypeString>();
113
4
            break;
114
4
        case tparquet::Type::type::FIXED_LEN_BYTE_ARRAY:
115
4
            _cached_src_physical_type = std::make_shared<DataTypeFixedLengthObject>();
116
4
            break;
117
0
        case tparquet::Type::type::INT96:
118
0
            _cached_src_physical_type = std::make_shared<DataTypeInt8>();
119
0
            break;
120
37
        }
121
37
        const bool is_fixed_length_byte_array =
122
37
                src_physical_type == tparquet::Type::type::FIXED_LEN_BYTE_ARRAY;
123
37
        if (dst_logical_type->is_nullable()) {
124
37
            MutableColumnPtr nested_physical_column;
125
37
            if (is_fixed_length_byte_array) {
126
4
                nested_physical_column = ColumnFixedLengthObject::create(
127
4
                        _convert_params->field_schema->parquet_schema.type_length);
128
33
            } else {
129
33
                nested_physical_column = _cached_src_physical_type->create_column();
130
33
            }
131
37
            _cached_src_physical_column = ColumnNullable::create(std::move(nested_physical_column),
132
37
                                                                 ColumnUInt8::create());
133
37
            _cached_src_physical_type = make_nullable(_cached_src_physical_type);
134
37
        } else {
135
0
            if (is_fixed_length_byte_array) {
136
0
                _cached_src_physical_column = ColumnFixedLengthObject::create(
137
0
                        _convert_params->field_schema->parquet_schema.type_length);
138
0
            } else {
139
0
                _cached_src_physical_column = _cached_src_physical_type->create_column();
140
0
            }
141
0
        }
142
37
    }
143
    // remove the old cached data
144
48
    auto cached_src_physical_column = IColumn::mutate(std::move(_cached_src_physical_column));
145
48
    cached_src_physical_column->clear();
146
48
    _cached_src_physical_column = std::move(cached_src_physical_column);
147
148
48
    return _cached_src_physical_column;
149
48
}
150
151
static void get_decimal_converter(const FieldSchema* field_schema, DataTypePtr src_logical_type,
152
                                  const DataTypePtr& dst_logical_type,
153
                                  ConvertParams* convert_params,
154
4
                                  std::unique_ptr<PhysicalToLogicalConverter>& physical_converter) {
155
4
    const tparquet::SchemaElement& parquet_schema = field_schema->parquet_schema;
156
4
    if (is_decimal(dst_logical_type->get_primitive_type())) {
157
4
        src_logical_type = create_decimal(parquet_schema.precision, parquet_schema.scale, false);
158
4
    }
159
160
4
    tparquet::Type::type src_physical_type = parquet_schema.type;
161
4
    PrimitiveType src_logical_primitive = src_logical_type->get_primitive_type();
162
163
4
    if (src_physical_type == tparquet::Type::FIXED_LEN_BYTE_ARRAY) {
164
2
        switch (src_logical_primitive) {
165
0
#define DISPATCH(LOGICAL_PTYPE)                                                     \
166
2
    case LOGICAL_PTYPE: {                                                           \
167
2
        physical_converter.reset(                                                   \
168
2
                new FixedSizeToDecimal<LOGICAL_PTYPE>(parquet_schema.type_length)); \
169
2
        break;                                                                      \
170
2
    }
171
2
            FOR_LOGICAL_DECIMAL_TYPES(DISPATCH)
172
0
#undef DISPATCH
173
0
        default:
174
0
            physical_converter =
175
0
                    std::make_unique<UnsupportedConverter>(src_physical_type, src_logical_type);
176
2
        }
177
2
    } else if (src_physical_type == tparquet::Type::BYTE_ARRAY) {
178
0
        switch (src_logical_primitive) {
179
0
#define DISPATCH(LOGICAL_PTYPE)                                         \
180
0
    case LOGICAL_PTYPE: {                                               \
181
0
        physical_converter.reset(new StringToDecimal<LOGICAL_PTYPE>()); \
182
0
        break;                                                          \
183
0
    }
184
0
            FOR_LOGICAL_DECIMAL_TYPES(DISPATCH)
185
0
#undef DISPATCH
186
0
        default:
187
0
            physical_converter =
188
0
                    std::make_unique<UnsupportedConverter>(src_physical_type, src_logical_type);
189
0
        }
190
2
    } else if (src_physical_type == tparquet::Type::INT32 ||
191
2
               src_physical_type == tparquet::Type::INT64) {
192
2
        switch (src_logical_primitive) {
193
0
#define DISPATCH(LOGICAL_PTYPE)                                                          \
194
2
    case LOGICAL_PTYPE: {                                                                \
195
2
        if (src_physical_type == tparquet::Type::INT32) {                                \
196
0
            physical_converter.reset(new NumberToDecimal<TYPE_INT, LOGICAL_PTYPE>());    \
197
2
        } else {                                                                         \
198
2
            physical_converter.reset(new NumberToDecimal<TYPE_BIGINT, LOGICAL_PTYPE>()); \
199
2
        }                                                                                \
200
2
        break;                                                                           \
201
2
    }
202
2
            FOR_LOGICAL_DECIMAL_TYPES(DISPATCH)
203
0
#undef DISPATCH
204
0
        default:
205
0
            physical_converter =
206
0
                    std::make_unique<UnsupportedConverter>(src_physical_type, src_logical_type);
207
2
        }
208
2
    } else {
209
0
        physical_converter =
210
0
                std::make_unique<UnsupportedConverter>(src_physical_type, src_logical_type);
211
0
    }
212
4
}
213
214
namespace {
215
216
class SpatialPhysicalConverter final : public PhysicalToLogicalConverter {
217
public:
218
    explicit SpatialPhysicalConverter(DataTypePtr spatial_type)
219
2
            : _spatial_type(std::move(spatial_type)) {}
220
221
2
    Status physical_convert(ColumnPtr& source, ColumnPtr& destination) override {
222
2
        return append_spatial_wkb(source, *get_mutable_inner_column(destination), _spatial_type);
223
2
    }
224
225
private:
226
    DataTypePtr _spatial_type;
227
};
228
229
class NativeUUIDConverter final : public PhysicalToLogicalConverter {
230
public:
231
5
    Status physical_convert(ColumnPtr& src_physical_col, ColumnPtr& src_logical_column) override {
232
5
        const auto from_col = remove_nullable(src_physical_col);
233
5
        const auto src_data = get_fixed_length_physical_data(*from_col, UUIDValue::BINARY_LENGTH);
234
5
        auto& values =
235
5
                assert_cast<ColumnUUID&>(*get_mutable_inner_column(src_logical_column)).get_data();
236
19
        for (size_t i = 0; i < src_data.rows; ++i) {
237
            // File UUIDs use network byte order, unlike the native column's integer layout.
238
14
            values.push_back(UUIDValue::from_big_endian(reinterpret_cast<const uint8_t*>(
239
14
                    src_data.data + i * UUIDValue::BINARY_LENGTH)));
240
14
        }
241
5
        return Status::OK();
242
5
    }
243
};
244
245
class UUIDStringConverter final : public PhysicalToLogicalConverter {
246
public:
247
2
    Status physical_convert(ColumnPtr& src_physical_col, ColumnPtr& src_logical_column) override {
248
2
        const auto from_col = remove_nullable(src_physical_col);
249
2
        const auto src_data = get_fixed_length_physical_data(*from_col, 16);
250
2
        auto& strings = assert_cast<ColumnString&>(*get_mutable_inner_column(src_logical_column));
251
8
        for (size_t i = 0; i < src_data.rows; ++i) {
252
6
            const auto value = UUIDValue::from_big_endian(
253
6
                    reinterpret_cast<const uint8_t*>(src_data.data + i * 16));
254
6
            const auto text = UUIDValue::to_string(value);
255
6
            strings.insert_data(text.data(), text.size());
256
6
        }
257
2
        return Status::OK();
258
2
    }
259
};
260
261
} // namespace
262
263
std::unique_ptr<PhysicalToLogicalConverter> PhysicalToLogicalConverter::get_converter(
264
        const FieldSchema* field_schema, DataTypePtr src_logical_type,
265
        const DataTypePtr& dst_logical_type, const cctz::time_zone* ctz, bool is_dict_filter,
266
327
        bool preserve_binary_uuid) {
267
327
    std::unique_ptr<ConvertParams> convert_params = std::make_unique<ConvertParams>();
268
327
    const tparquet::SchemaElement& parquet_schema = field_schema->parquet_schema;
269
327
    convert_params->init(field_schema, ctz);
270
327
    tparquet::Type::type src_physical_type = parquet_schema.type;
271
327
    std::unique_ptr<PhysicalToLogicalConverter> physical_converter;
272
327
    if (is_dict_filter) {
273
1
        src_physical_type = tparquet::Type::INT32;
274
1
        src_logical_type = DataTypeFactory::instance().create_data_type(
275
1
                PrimitiveType::TYPE_INT, dst_logical_type->is_nullable());
276
1
    }
277
327
    PrimitiveType src_logical_primitive = src_logical_type->get_primitive_type();
278
279
327
    if (field_schema->is_type_compatibility) {
280
0
        if (src_logical_primitive == TYPE_SMALLINT) {
281
0
            physical_converter = std::make_unique<UnsignedIntegerConverter<TYPE_SMALLINT>>();
282
0
        } else if (src_logical_primitive == TYPE_INT) {
283
0
            physical_converter = std::make_unique<UnsignedIntegerConverter<TYPE_INT>>();
284
0
        } else if (src_logical_primitive == TYPE_BIGINT) {
285
0
            physical_converter = std::make_unique<UnsignedIntegerConverter<TYPE_BIGINT>>();
286
0
        } else if (src_logical_primitive == TYPE_LARGEINT) {
287
0
            physical_converter = std::make_unique<UnsignedIntegerConverter<TYPE_LARGEINT>>();
288
0
        } else {
289
0
            physical_converter =
290
0
                    std::make_unique<UnsupportedConverter>(src_physical_type, src_logical_type);
291
0
        }
292
327
    } else if (is_parquet_native_type(src_logical_primitive)) {
293
269
        bool is_string_logical_type = is_string_type(src_logical_primitive);
294
269
        if (is_string_logical_type && src_physical_type == tparquet::Type::FIXED_LEN_BYTE_ARRAY) {
295
5
            if (parquet_schema.logicalType.__isset.UUID && !preserve_binary_uuid) {
296
                // Ordinary Parquet scans expose canonical text, as in File Scanner V2.
297
                // Iceberg retains its raw 16-byte STRING carrier for equality deletes/defaults.
298
2
                if (parquet_schema.type_length != UUIDValue::BINARY_LENGTH) {
299
1
                    physical_converter = std::make_unique<UnsupportedConverter>(src_physical_type,
300
1
                                                                                src_logical_type);
301
1
                } else {
302
1
                    physical_converter = std::make_unique<UUIDStringConverter>();
303
1
                }
304
3
            } else {
305
3
                physical_converter =
306
3
                        std::make_unique<FixedSizeBinaryConverter>(parquet_schema.type_length);
307
3
            }
308
264
        } else if (src_logical_primitive == TYPE_FLOAT &&
309
264
                   src_physical_type == tparquet::Type::FIXED_LEN_BYTE_ARRAY &&
310
264
                   parquet_schema.logicalType.__isset.FLOAT16) {
311
0
            physical_converter =
312
0
                    std::make_unique<Float16PhysicalConverter>(parquet_schema.type_length);
313
264
        } else {
314
264
            physical_converter = std::make_unique<ConsistentPhysicalConverter>();
315
264
        }
316
269
    } else if (src_logical_primitive == TYPE_TINYINT) {
317
10
        physical_converter = std::make_unique<LittleIntPhysicalConverter<TYPE_TINYINT>>();
318
48
    } else if (src_logical_primitive == TYPE_SMALLINT) {
319
7
        physical_converter = std::make_unique<LittleIntPhysicalConverter<TYPE_SMALLINT>>();
320
41
    } else if (is_decimal_type(src_logical_primitive)) {
321
4
        get_decimal_converter(field_schema, src_logical_type, dst_logical_type,
322
4
                              convert_params.get(), physical_converter);
323
37
    } else if (src_logical_primitive == TYPE_DATEV2) {
324
7
        physical_converter = std::make_unique<Int32ToDate>();
325
30
    } else if (src_logical_primitive == TYPE_DATETIMEV2) {
326
16
        if (src_physical_type == tparquet::Type::INT96) {
327
            // int96 only stores nanoseconds in standard parquet file
328
1
            convert_params->reset_time_scale_if_missing(9);
329
1
            physical_converter = std::make_unique<Int96toTimestamp>();
330
15
        } else if (src_physical_type == tparquet::Type::INT64) {
331
15
            convert_params->reset_time_scale_if_missing(src_logical_type->get_scale());
332
15
            physical_converter = std::make_unique<Int64ToTimestamp>();
333
15
        } else {
334
0
            physical_converter =
335
0
                    std::make_unique<UnsupportedConverter>(src_physical_type, src_logical_type);
336
0
        }
337
16
    } else if (src_logical_primitive == TYPE_UUID) {
338
6
        if (src_physical_type == tparquet::Type::FIXED_LEN_BYTE_ARRAY &&
339
6
            std::cmp_equal(parquet_schema.type_length, UUIDValue::BINARY_LENGTH)) {
340
5
            physical_converter = std::make_unique<NativeUUIDConverter>();
341
5
        } else {
342
1
            physical_converter =
343
1
                    std::make_unique<UnsupportedConverter>(src_physical_type, src_logical_type);
344
1
        }
345
8
    } else if (src_logical_primitive == TYPE_GEOMETRY || src_logical_primitive == TYPE_GEOGRAPHY) {
346
2
        if (src_physical_type == tparquet::Type::BYTE_ARRAY) {
347
2
            physical_converter = std::make_unique<SpatialPhysicalConverter>(src_logical_type);
348
2
        } else {
349
0
            physical_converter =
350
0
                    std::make_unique<UnsupportedConverter>(src_physical_type, src_logical_type);
351
0
        }
352
6
    } else if (src_logical_primitive == TYPE_VARBINARY) {
353
4
        if (src_physical_type == tparquet::Type::FIXED_LEN_BYTE_ARRAY) {
354
0
            DCHECK(parquet_schema.logicalType.__isset.UUID) << parquet_schema.name;
355
0
            physical_converter =
356
0
                    std::make_unique<UUIDVarBinaryConverter>(parquet_schema.type_length);
357
4
        } else {
358
4
            DCHECK(src_physical_type == tparquet::Type::BYTE_ARRAY) << src_physical_type;
359
4
            physical_converter = std::make_unique<ConsistentPhysicalConverter>();
360
4
        }
361
4
    } else if (src_logical_primitive == TYPE_TIMESTAMPTZ) {
362
2
        if (src_physical_type == tparquet::Type::INT96) {
363
1
            physical_converter = std::make_unique<Int96toTimestampTz>();
364
1
        } else if (src_physical_type == tparquet::Type::INT64) {
365
1
            DCHECK(src_physical_type == tparquet::Type::INT64) << src_physical_type;
366
1
            DCHECK(parquet_schema.logicalType.__isset.TIMESTAMP) << parquet_schema.name;
367
1
            physical_converter = std::make_unique<Int64ToTimestampTz>();
368
1
        } else {
369
0
            physical_converter =
370
0
                    std::make_unique<UnsupportedConverter>(src_physical_type, src_logical_type);
371
0
        }
372
2
    } else {
373
0
        physical_converter =
374
0
                std::make_unique<UnsupportedConverter>(src_physical_type, src_logical_type);
375
0
    }
376
377
327
    if (physical_converter->support()) {
378
325
        physical_converter->_convert_params = std::move(convert_params);
379
325
        physical_converter->_logical_converter = converter::ColumnTypeConverter::get_converter(
380
325
                src_logical_type, dst_logical_type, converter::FileFormat::PARQUET);
381
325
        if (!physical_converter->_logical_converter->support()) {
382
0
            physical_converter = std::make_unique<UnsupportedConverter>(
383
0
                    "Unsupported type change: " +
384
0
                    physical_converter->_logical_converter->get_error_msg());
385
0
        }
386
325
    }
387
327
    return physical_converter;
388
327
}
389
390
} // namespace doris::parquet