Coverage Report

Created: 2026-10-09 17:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/format/jni/jni_data_bridge.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 "jni_data_bridge.h"
19
20
#include <glog/logging.h>
21
22
#include <sstream>
23
#include <variant>
24
25
#include "core/block/block.h"
26
#include "core/column/column_array.h"
27
#include "core/column/column_map.h"
28
#include "core/column/column_nullable.h"
29
#include "core/column/column_string.h"
30
#include "core/column/column_struct.h"
31
#include "core/column/column_varbinary.h"
32
#include "core/column/variant_v2/column_variant_v2.h"
33
#include "core/data_type/data_type_array.h"
34
#include "core/data_type/data_type_map.h"
35
#include "core/data_type/data_type_nullable.h"
36
#include "core/data_type/data_type_struct.h"
37
#include "core/data_type/data_type_varbinary.h"
38
#include "core/data_type/define_primitive_type.h"
39
#include "core/data_type/primitive_type.h"
40
#include "core/types.h"
41
#include "core/value/decimalv2_value.h"
42
#include "util/string_util.h"
43
#include "util/url_coding.h"
44
45
namespace doris {
46
47
#define FOR_FIXED_LENGTH_TYPES(M)                                  \
48
0
    M(PrimitiveType::TYPE_TINYINT, ColumnInt8, Int8)               \
49
0
    M(PrimitiveType::TYPE_BOOLEAN, ColumnUInt8, UInt8)             \
50
0
    M(PrimitiveType::TYPE_SMALLINT, ColumnInt16, Int16)            \
51
0
    M(PrimitiveType::TYPE_INT, ColumnInt32, Int32)                 \
52
0
    M(PrimitiveType::TYPE_BIGINT, ColumnInt64, Int64)              \
53
0
    M(PrimitiveType::TYPE_LARGEINT, ColumnInt128, Int128)          \
54
0
    M(PrimitiveType::TYPE_FLOAT, ColumnFloat32, Float32)           \
55
0
    M(PrimitiveType::TYPE_DOUBLE, ColumnFloat64, Float64)          \
56
0
    M(PrimitiveType::TYPE_DECIMALV2, ColumnDecimal128V2, Int128)   \
57
0
    M(PrimitiveType::TYPE_DECIMAL128I, ColumnDecimal128V3, Int128) \
58
0
    M(PrimitiveType::TYPE_DECIMAL32, ColumnDecimal32, Int32)       \
59
0
    M(PrimitiveType::TYPE_DECIMAL64, ColumnDecimal64, Int64)       \
60
0
    M(PrimitiveType::TYPE_DATE, ColumnDate, Int64)                 \
61
0
    M(PrimitiveType::TYPE_DATEV2, ColumnDateV2, UInt32)            \
62
0
    M(PrimitiveType::TYPE_DATETIME, ColumnDateTime, Int64)         \
63
0
    M(PrimitiveType::TYPE_DATETIMEV2, ColumnDateTimeV2, UInt64)    \
64
1
    M(PrimitiveType::TYPE_TIMESTAMP_NS, ColumnTimeStampNs, Int64)  \
65
1
    M(PrimitiveType::TYPE_TIMESTAMPTZ, ColumnTimeStampTz, UInt64)  \
66
0
    M(PrimitiveType::TYPE_IPV4, ColumnIPv4, IPv4)                  \
67
0
    M(PrimitiveType::TYPE_IPV6, ColumnIPv6, IPv6)                  \
68
0
    M(PrimitiveType::TYPE_UUID, ColumnUUID, UUIDValueType)
69
70
0
Status JniDataBridge::fill_block(Block* block, const ColumnNumbers& arguments, long table_address) {
71
0
    if (table_address == 0) {
72
0
        return Status::InternalError("table_address is 0");
73
0
    }
74
0
    TableMetaAddress table_meta(table_address);
75
0
    long num_rows = table_meta.next_meta_as_long();
76
0
    for (size_t i : arguments) {
77
0
        if (block->get_by_position(i).column.get() == nullptr) {
78
0
            auto return_type = block->get_data_type(i);
79
0
            bool result_nullable = return_type->is_nullable();
80
0
            ColumnUInt8::MutablePtr null_col = nullptr;
81
0
            if (result_nullable) {
82
0
                return_type = remove_nullable(return_type);
83
0
                null_col = ColumnUInt8::create();
84
0
            }
85
0
            auto res_col = return_type->create_column();
86
0
            if (result_nullable) {
87
0
                block->replace_by_position(
88
0
                        i, ColumnNullable::create(std::move(res_col), std::move(null_col)));
89
0
            } else {
90
0
                block->replace_by_position(i, std::move(res_col));
91
0
            }
92
0
        } else if (is_column_const(*(block->get_by_position(i).column))) {
93
0
            auto doris_column = block->get_by_position(i).column->convert_to_full_column_if_const();
94
0
            bool is_nullable = block->get_by_position(i).type->is_nullable();
95
0
            block->replace_by_position(i, is_nullable ? make_nullable(doris_column) : doris_column);
96
0
        }
97
0
        auto& column_with_type_and_name = block->get_by_position(i);
98
0
        auto& column_ptr = column_with_type_and_name.column;
99
0
        auto& column_type = column_with_type_and_name.type;
100
0
        RETURN_IF_ERROR(fill_column(table_meta, column_ptr, column_type, num_rows));
101
0
    }
102
0
    return Status::OK();
103
0
}
104
105
Status JniDataBridge::fill_column(TableMetaAddress& address, ColumnPtr& doris_column,
106
2
                                  const DataTypePtr& data_type, size_t num_rows) {
107
2
    auto logical_type = data_type->get_primitive_type();
108
2
    void* null_map_ptr = address.next_meta_as_ptr();
109
2
    if (null_map_ptr == nullptr) {
110
        // org.apache.doris.jni.spi.vec.ColumnType.Type#UNSUPPORTED will set column address as 0
111
0
        return Status::InternalError("Unsupported type {} in java side", data_type->get_name());
112
0
    }
113
2
    auto mutable_doris_column = IColumn::mutate(std::move(doris_column));
114
2
    MutableColumnPtr data_column;
115
2
    if (auto* nullable_column = check_and_get_column<ColumnNullable>(mutable_doris_column.get())) {
116
0
        data_column = nullable_column->get_nested_column_ptr();
117
0
        NullMap& null_map = nullable_column->get_null_map_data();
118
0
        size_t origin_size = null_map.size();
119
0
        null_map.resize(origin_size + num_rows);
120
0
        memcpy(null_map.data() + origin_size, static_cast<bool*>(null_map_ptr), num_rows);
121
2
    } else {
122
2
        data_column = mutable_doris_column->get_ptr();
123
2
    }
124
    // Date and DateTime are deprecated and not supported.
125
2
    Status status = Status::OK();
126
2
    switch (logical_type) {
127
0
#define DISPATCH(TYPE_INDEX, COLUMN_TYPE, CPP_TYPE)                                             \
128
1
    case TYPE_INDEX: {                                                                          \
129
1
        auto* data = reinterpret_cast<CPP_TYPE*>(address.next_meta_as_ptr());                   \
130
1
        status = _fill_fixed_length_column<COLUMN_TYPE, CPP_TYPE>(data_column, data, num_rows); \
131
1
        break;                                                                                  \
132
1
    }
133
1
        FOR_FIXED_LENGTH_TYPES(DISPATCH)
134
0
#undef DISPATCH
135
0
    case PrimitiveType::TYPE_STRING:
136
0
        [[fallthrough]];
137
0
    case PrimitiveType::TYPE_CHAR:
138
0
        [[fallthrough]];
139
0
    case PrimitiveType::TYPE_VARCHAR:
140
0
        status = _fill_string_column(address, data_column, num_rows);
141
0
        break;
142
0
    case PrimitiveType::TYPE_ARRAY:
143
0
        status = _fill_array_column(address, data_column, data_type, num_rows);
144
0
        break;
145
0
    case PrimitiveType::TYPE_MAP:
146
0
        status = _fill_map_column(address, data_column, data_type, num_rows);
147
0
        break;
148
0
    case PrimitiveType::TYPE_STRUCT:
149
0
        status = _fill_struct_column(address, data_column, data_type, num_rows);
150
0
        break;
151
0
    case PrimitiveType::TYPE_VARBINARY:
152
0
        status = _fill_varbinary_column(address, data_column, num_rows);
153
0
        break;
154
1
    case PrimitiveType::TYPE_VARIANT:
155
1
        status = _fill_variant_v2_column(address, data_column, num_rows);
156
1
        break;
157
0
    default:
158
0
        status = Status::InvalidArgument("Unsupported type {} in jni scanner",
159
0
                                         data_type->get_name());
160
0
        break;
161
2
    }
162
2
    doris_column = std::move(mutable_doris_column);
163
2
    return status;
164
2
}
165
166
Status JniDataBridge::_fill_varbinary_column(TableMetaAddress& address,
167
0
                                             MutableColumnPtr& doris_column, size_t num_rows) {
168
0
    auto* meta_base = reinterpret_cast<char*>(address.next_meta_as_ptr());
169
0
    auto& varbinary_col = assert_cast<ColumnVarbinary&>(*doris_column);
170
    // Java side writes per-row metadata as 16 bytes: [len: long][addr: long]
171
0
    for (size_t i = 0; i < num_rows; ++i) {
172
        // Read length (first 8 bytes)
173
0
        int64_t len = 0;
174
0
        memcpy(&len, meta_base + 16 * i, sizeof(len));
175
0
        if (len <= 0) {
176
0
            varbinary_col.insert_default();
177
0
        } else {
178
            // Read address (next 8 bytes)
179
0
            uint64_t addr_u = 0;
180
0
            memcpy(&addr_u, meta_base + 16 * i + 8, sizeof(addr_u));
181
0
            const char* src = reinterpret_cast<const char*>(addr_u);
182
0
            varbinary_col.insert_data(src, static_cast<size_t>(len));
183
0
        }
184
0
    }
185
0
    return Status::OK();
186
0
}
187
188
Status JniDataBridge::_fill_variant_v2_column(TableMetaAddress& address,
189
1
                                              MutableColumnPtr& doris_column, size_t num_rows) {
190
1
    const auto metadata_count = static_cast<size_t>(address.next_meta_as_long());
191
1
    const auto* metadata_offsets = reinterpret_cast<const uint32_t*>(address.next_meta_as_ptr());
192
1
    const auto* metadata_bytes = reinterpret_cast<const char*>(address.next_meta_as_ptr());
193
1
    const auto* metadata_ids = reinterpret_cast<const uint32_t*>(address.next_meta_as_ptr());
194
1
    const auto* value_offsets = reinterpret_cast<const uint32_t*>(address.next_meta_as_ptr());
195
1
    const auto* value_bytes = reinterpret_cast<const char*>(address.next_meta_as_ptr());
196
197
1
    auto& variant_column = assert_cast<ColumnVariantV2&>(*doris_column);
198
1
    variant_column.insert_encoded_rows({
199
1
            .metadata_bytes = {metadata_bytes, metadata_offsets[metadata_count]},
200
1
            .metadata_offsets = {metadata_offsets, metadata_count + 1},
201
1
            .meta_ids = {metadata_ids, num_rows},
202
1
            .value_bytes = {value_bytes, value_offsets[num_rows]},
203
1
            .value_offsets = {value_offsets, num_rows + 1},
204
1
    });
205
1
    return Status::OK();
206
1
}
207
208
Status JniDataBridge::_fill_string_column(TableMetaAddress& address, MutableColumnPtr& doris_column,
209
0
                                          size_t num_rows) {
210
0
    auto& string_col = static_cast<ColumnString&>(*doris_column);
211
0
    ColumnString::Chars& string_chars = string_col.get_chars();
212
0
    ColumnString::Offsets& string_offsets = string_col.get_offsets();
213
0
    int* offsets = reinterpret_cast<int*>(address.next_meta_as_ptr());
214
0
    char* chars = reinterpret_cast<char*>(address.next_meta_as_ptr());
215
216
    // This judgment is necessary, otherwise the following statement `offsets[num_rows - 1]` out of bounds
217
    // What's more, This judgment must be placed after `address.next_meta_as_ptr()`
218
    // because `address.next_meta_as_ptr` will make `address._meta_index` plus 1
219
0
    if (num_rows == 0) {
220
0
        return Status::OK();
221
0
    }
222
223
0
    size_t origin_chars_size = string_chars.size();
224
0
    string_chars.resize(origin_chars_size + offsets[num_rows - 1]);
225
0
    memcpy(string_chars.data() + origin_chars_size, chars, offsets[num_rows - 1]);
226
227
0
    size_t origin_offsets_size = string_offsets.size();
228
0
    size_t start_offset = string_offsets[origin_offsets_size - 1];
229
0
    string_offsets.resize(origin_offsets_size + num_rows);
230
0
    for (size_t i = 0; i < num_rows; ++i) {
231
0
        string_offsets[origin_offsets_size + i] =
232
0
                static_cast<unsigned int>(offsets[i] + start_offset);
233
0
    }
234
0
    return Status::OK();
235
0
}
236
237
Status JniDataBridge::_fill_array_column(TableMetaAddress& address, MutableColumnPtr& doris_column,
238
0
                                         const DataTypePtr& data_type, size_t num_rows) {
239
0
    ColumnPtr& element_column = static_cast<ColumnArray&>(*doris_column).get_data_ptr();
240
0
    const DataTypePtr& element_type =
241
0
            (assert_cast<const DataTypeArray*>(remove_nullable(data_type).get()))
242
0
                    ->get_nested_type();
243
0
    ColumnArray::Offsets64& offsets_data = static_cast<ColumnArray&>(*doris_column).get_offsets();
244
245
0
    int64_t* offsets = reinterpret_cast<int64_t*>(address.next_meta_as_ptr());
246
0
    size_t origin_size = offsets_data.size();
247
0
    offsets_data.resize(origin_size + num_rows);
248
0
    size_t start_offset = offsets_data[origin_size - 1];
249
0
    for (size_t i = 0; i < num_rows; ++i) {
250
0
        offsets_data[origin_size + i] = offsets[i] + start_offset;
251
0
    }
252
253
0
    return fill_column(address, element_column, element_type,
254
0
                       offsets_data[origin_size + num_rows - 1] - start_offset);
255
0
}
256
257
Status JniDataBridge::_fill_map_column(TableMetaAddress& address, MutableColumnPtr& doris_column,
258
0
                                       const DataTypePtr& data_type, size_t num_rows) {
259
0
    auto& map = static_cast<ColumnMap&>(*doris_column);
260
0
    const DataTypePtr& key_type =
261
0
            reinterpret_cast<const DataTypeMap*>(remove_nullable(data_type).get())->get_key_type();
262
0
    const DataTypePtr& value_type =
263
0
            reinterpret_cast<const DataTypeMap*>(remove_nullable(data_type).get())
264
0
                    ->get_value_type();
265
0
    ColumnPtr& key_column = map.get_keys_ptr();
266
0
    ColumnPtr& value_column = map.get_values_ptr();
267
0
    ColumnArray::Offsets64& map_offsets = map.get_offsets();
268
269
0
    int64_t* offsets = reinterpret_cast<int64_t*>(address.next_meta_as_ptr());
270
0
    size_t origin_size = map_offsets.size();
271
0
    map_offsets.resize(origin_size + num_rows);
272
0
    size_t start_offset = map_offsets[origin_size - 1];
273
0
    for (size_t i = 0; i < num_rows; ++i) {
274
0
        map_offsets[origin_size + i] = offsets[i] + start_offset;
275
0
    }
276
277
0
    RETURN_IF_ERROR(fill_column(address, key_column, key_type,
278
0
                                map_offsets[origin_size + num_rows - 1] - start_offset));
279
0
    RETURN_IF_ERROR(fill_column(address, value_column, value_type,
280
0
                                map_offsets[origin_size + num_rows - 1] - start_offset));
281
0
    return Status::OK();
282
0
}
283
284
Status JniDataBridge::_fill_struct_column(TableMetaAddress& address, MutableColumnPtr& doris_column,
285
0
                                          const DataTypePtr& data_type, size_t num_rows) {
286
0
    auto& doris_struct = static_cast<ColumnStruct&>(*doris_column);
287
0
    const DataTypeStruct* doris_struct_type =
288
0
            reinterpret_cast<const DataTypeStruct*>(remove_nullable(data_type).get());
289
0
    for (int i = 0; i < doris_struct.tuple_size(); ++i) {
290
0
        ColumnPtr& struct_field = doris_struct.get_column_ptr(i);
291
0
        const DataTypePtr& field_type = doris_struct_type->get_element(i);
292
0
        RETURN_IF_ERROR(fill_column(address, struct_field, field_type, num_rows));
293
0
    }
294
0
    return Status::OK();
295
0
}
296
297
9
std::string JniDataBridge::get_jni_type(const DataTypePtr& data_type) {
298
9
    DataTypePtr type = remove_nullable(data_type);
299
9
    std::ostringstream buffer;
300
9
    switch (type->get_primitive_type()) {
301
0
    case TYPE_BOOLEAN:
302
0
        return "boolean";
303
0
    case TYPE_TINYINT:
304
0
        return "tinyint";
305
0
    case TYPE_SMALLINT:
306
0
        return "smallint";
307
0
    case TYPE_INT:
308
0
        return "int";
309
0
    case TYPE_BIGINT:
310
0
        return "bigint";
311
0
    case TYPE_LARGEINT:
312
0
        return "largeint";
313
0
    case TYPE_FLOAT:
314
0
        return "float";
315
0
    case TYPE_DOUBLE:
316
0
        return "double";
317
0
    case TYPE_IPV4:
318
0
        return "ipv4";
319
0
    case TYPE_IPV6:
320
0
        return "ipv6";
321
0
    case TYPE_UUID:
322
0
        return "uuid";
323
0
    case TYPE_VARCHAR:
324
0
        [[fallthrough]];
325
0
    case TYPE_CHAR:
326
0
        [[fallthrough]];
327
1
    case TYPE_STRING:
328
1
        return "string";
329
0
    case TYPE_DATE:
330
0
        return "datev1";
331
0
    case TYPE_DATEV2:
332
0
        return "datev2";
333
0
    case TYPE_DATETIME:
334
0
        return "datetimev1";
335
0
    case TYPE_DATETIMEV2:
336
0
        [[fallthrough]];
337
0
    case TYPE_TIMEV2: {
338
0
        buffer << "datetimev2(" << type->get_scale() << ")";
339
0
        return buffer.str();
340
0
    }
341
4
    case TYPE_TIMESTAMP_NS:
342
4
        return "timestamp_ns";
343
0
    case TYPE_TIMESTAMPTZ: {
344
0
        buffer << "timestamptz(" << type->get_scale() << ")";
345
0
        return buffer.str();
346
0
    }
347
0
    case TYPE_BINARY:
348
0
        return "binary";
349
0
    case TYPE_DECIMALV2: {
350
0
        buffer << "decimalv2(" << DecimalV2Value::PRECISION << "," << DecimalV2Value::SCALE << ")";
351
0
        return buffer.str();
352
0
    }
353
0
    case TYPE_DECIMAL32: {
354
0
        buffer << "decimal32(" << type->get_precision() << "," << type->get_scale() << ")";
355
0
        return buffer.str();
356
0
    }
357
0
    case TYPE_DECIMAL64: {
358
0
        buffer << "decimal64(" << type->get_precision() << "," << type->get_scale() << ")";
359
0
        return buffer.str();
360
0
    }
361
0
    case TYPE_DECIMAL128I: {
362
0
        buffer << "decimal128(" << type->get_precision() << "," << type->get_scale() << ")";
363
0
        return buffer.str();
364
0
    }
365
1
    case TYPE_STRUCT: {
366
1
        const DataTypeStruct* struct_type = reinterpret_cast<const DataTypeStruct*>(type.get());
367
1
        buffer << "struct<";
368
4
        for (int i = 0; i < struct_type->get_elements().size(); ++i) {
369
3
            if (i != 0) {
370
2
                buffer << ",";
371
2
            }
372
3
            buffer << struct_type->get_element_names()[i] << ":"
373
3
                   << get_jni_type(struct_type->get_element(i));
374
3
        }
375
1
        buffer << ">";
376
1
        return buffer.str();
377
0
    }
378
1
    case TYPE_ARRAY: {
379
1
        const DataTypeArray* array_type = reinterpret_cast<const DataTypeArray*>(type.get());
380
1
        buffer << "array<" << get_jni_type(array_type->get_nested_type()) << ">";
381
1
        return buffer.str();
382
0
    }
383
1
    case TYPE_MAP: {
384
1
        const DataTypeMap* map_type = reinterpret_cast<const DataTypeMap*>(type.get());
385
1
        buffer << "map<" << get_jni_type(map_type->get_key_type()) << ","
386
1
               << get_jni_type(map_type->get_value_type()) << ">";
387
1
        return buffer.str();
388
0
    }
389
0
    case TYPE_VARBINARY:
390
0
        return "varbinary";
391
1
    case TYPE_VARIANT:
392
1
        return "variant";
393
    // bitmap, hll, quantile_state, jsonb are transferred as strings via JNI
394
0
    case TYPE_BITMAP:
395
0
        [[fallthrough]];
396
0
    case TYPE_HLL:
397
0
        [[fallthrough]];
398
0
    case TYPE_QUANTILE_STATE:
399
0
        [[fallthrough]];
400
0
    case TYPE_JSONB:
401
0
        return "string";
402
0
    default:
403
0
        return "unsupported";
404
9
    }
405
9
}
406
407
16
std::string JniDataBridge::get_jni_type_with_different_string(const DataTypePtr& data_type) {
408
16
    DataTypePtr type = remove_nullable(data_type);
409
16
    std::ostringstream buffer;
410
16
    switch (data_type->get_primitive_type()) {
411
0
    case TYPE_BOOLEAN:
412
0
        return "boolean";
413
0
    case TYPE_TINYINT:
414
0
        return "tinyint";
415
0
    case TYPE_SMALLINT:
416
0
        return "smallint";
417
2
    case TYPE_INT:
418
2
        return "int";
419
2
    case TYPE_BIGINT:
420
2
        return "bigint";
421
0
    case TYPE_LARGEINT:
422
0
        return "largeint";
423
0
    case TYPE_FLOAT:
424
0
        return "float";
425
0
    case TYPE_DOUBLE:
426
0
        return "double";
427
0
    case TYPE_IPV4:
428
0
        return "ipv4";
429
0
    case TYPE_IPV6:
430
0
        return "ipv6";
431
0
    case TYPE_UUID:
432
0
        return "uuid";
433
0
    case TYPE_VARCHAR: {
434
0
        buffer << "varchar("
435
0
               << assert_cast<const DataTypeString*>(remove_nullable(data_type).get())->len()
436
0
               << ")";
437
0
        return buffer.str();
438
0
    }
439
0
    case TYPE_DATE:
440
0
        return "datev1";
441
0
    case TYPE_DATEV2:
442
0
        return "datev2";
443
0
    case TYPE_DATETIME:
444
0
        return "datetimev1";
445
0
    case TYPE_DATETIMEV2:
446
0
        [[fallthrough]];
447
0
    case TYPE_TIMEV2: {
448
0
        buffer << "datetimev2(" << data_type->get_scale() << ")";
449
0
        return buffer.str();
450
0
    }
451
0
    case TYPE_TIMESTAMP_NS:
452
0
        return "timestamp_ns";
453
0
    case TYPE_TIMESTAMPTZ: {
454
0
        buffer << "timestamptz(" << data_type->get_scale() << ")";
455
0
        return buffer.str();
456
0
    }
457
0
    case TYPE_BINARY:
458
0
        return "binary";
459
0
    case TYPE_CHAR: {
460
0
        buffer << "char("
461
0
               << assert_cast<const DataTypeString*>(remove_nullable(data_type).get())->len()
462
0
               << ")";
463
0
        return buffer.str();
464
0
    }
465
10
    case TYPE_STRING:
466
10
        return "string";
467
0
    case TYPE_VARBINARY:
468
0
        buffer << "varbinary("
469
0
               << assert_cast<const DataTypeVarbinary*>(remove_nullable(data_type).get())->len()
470
0
               << ")";
471
0
        return buffer.str();
472
1
    case TYPE_VARIANT:
473
1
        return "variant";
474
0
    case TYPE_DECIMALV2: {
475
0
        buffer << "decimalv2(" << DecimalV2Value::PRECISION << "," << DecimalV2Value::SCALE << ")";
476
0
        return buffer.str();
477
0
    }
478
0
    case TYPE_DECIMAL32: {
479
0
        buffer << "decimal32(" << data_type->get_precision() << "," << data_type->get_scale()
480
0
               << ")";
481
0
        return buffer.str();
482
0
    }
483
0
    case TYPE_DECIMAL64: {
484
0
        buffer << "decimal64(" << data_type->get_precision() << "," << data_type->get_scale()
485
0
               << ")";
486
0
        return buffer.str();
487
0
    }
488
0
    case TYPE_DECIMAL128I: {
489
0
        buffer << "decimal128(" << data_type->get_precision() << "," << data_type->get_scale()
490
0
               << ")";
491
0
        return buffer.str();
492
0
    }
493
1
    case TYPE_STRUCT: {
494
1
        const auto* type_struct =
495
1
                assert_cast<const DataTypeStruct*>(remove_nullable(data_type).get());
496
1
        buffer << "struct<";
497
2
        for (int i = 0; i < type_struct->get_elements().size(); ++i) {
498
1
            if (i != 0) {
499
0
                buffer << ",";
500
0
            }
501
1
            buffer << type_struct->get_element_name(i) << ":"
502
1
                   << get_jni_type_with_different_string(type_struct->get_element(i));
503
1
        }
504
1
        buffer << ">";
505
1
        return buffer.str();
506
0
    }
507
0
    case TYPE_ARRAY: {
508
0
        const auto* type_arr = assert_cast<const DataTypeArray*>(remove_nullable(data_type).get());
509
0
        buffer << "array<" << get_jni_type_with_different_string(type_arr->get_nested_type())
510
0
               << ">";
511
0
        return buffer.str();
512
0
    }
513
0
    case TYPE_MAP: {
514
0
        const auto* type_map = assert_cast<const DataTypeMap*>(remove_nullable(data_type).get());
515
0
        buffer << "map<" << get_jni_type_with_different_string(type_map->get_key_type()) << ","
516
0
               << get_jni_type_with_different_string(type_map->get_value_type()) << ">";
517
0
        return buffer.str();
518
0
    }
519
    // bitmap, hll, quantile_state, jsonb are transferred as strings via JNI
520
0
    case TYPE_BITMAP:
521
0
        [[fallthrough]];
522
0
    case TYPE_HLL:
523
0
        [[fallthrough]];
524
0
    case TYPE_QUANTILE_STATE:
525
0
        [[fallthrough]];
526
0
    case TYPE_JSONB:
527
0
        return "string";
528
0
    default:
529
0
        return "unsupported";
530
16
    }
531
16
}
532
533
13
std::string JniDataBridge::encode_schema_values(const std::vector<std::string>& values) {
534
13
    std::vector<std::string> encoded_values;
535
13
    encoded_values.reserve(values.size());
536
14
    for (const auto& value : values) {
537
14
        std::string encoded;
538
14
        base64_encode(value, &encoded);
539
        // Prefix every element so an empty Base64 token remains distinct from an empty list.
540
14
        encoded_values.emplace_back("$" + encoded);
541
14
    }
542
13
    return join(encoded_values, ",");
543
13
}
544
545
10
std::string JniDataBridge::get_jni_type_with_encoded_struct_fields(const DataTypePtr& data_type) {
546
10
    switch (data_type->get_primitive_type()) {
547
2
    case TYPE_STRUCT: {
548
2
        const auto* type_struct =
549
2
                assert_cast<const DataTypeStruct*>(remove_nullable(data_type).get());
550
2
        std::ostringstream buffer;
551
2
        buffer << "struct<";
552
6
        for (int i = 0; i < type_struct->get_elements().size(); ++i) {
553
4
            if (i != 0) {
554
2
                buffer << ",";
555
2
            }
556
4
            std::string encoded_name;
557
4
            base64_encode(type_struct->get_element_name(i), &encoded_name);
558
            // '$' versions the nested-name token and cannot collide with Base64 or grammar
559
            // delimiters; Java rejects an unversioned name in the encoded schema path.
560
4
            buffer << "$" << encoded_name << ":"
561
4
                   << get_jni_type_with_encoded_struct_fields(type_struct->get_element(i));
562
4
        }
563
2
        buffer << ">";
564
2
        return buffer.str();
565
0
    }
566
0
    case TYPE_ARRAY: {
567
0
        const auto* type_array =
568
0
                assert_cast<const DataTypeArray*>(remove_nullable(data_type).get());
569
0
        return "array<" + get_jni_type_with_encoded_struct_fields(type_array->get_nested_type()) +
570
0
               ">";
571
0
    }
572
0
    case TYPE_MAP: {
573
0
        const auto* type_map = assert_cast<const DataTypeMap*>(remove_nullable(data_type).get());
574
0
        return "map<" + get_jni_type_with_encoded_struct_fields(type_map->get_key_type()) + "," +
575
0
               get_jni_type_with_encoded_struct_fields(type_map->get_value_type()) + ">";
576
0
    }
577
8
    default:
578
8
        return get_jni_type_with_different_string(data_type);
579
10
    }
580
10
}
581
582
Status JniDataBridge::_fill_column_meta(const ColumnPtr& doris_column, const DataTypePtr& data_type,
583
0
                                        std::vector<long>& meta_data) {
584
0
    auto logical_type = data_type->get_primitive_type();
585
0
    const IColumn* column = nullptr;
586
    // insert const flag
587
0
    if (is_column_const(*doris_column)) {
588
0
        meta_data.emplace_back((long)1);
589
0
        const auto& const_column = assert_cast<const ColumnConst&>(*doris_column);
590
0
        column = &(const_column.get_data_column());
591
0
    } else {
592
0
        meta_data.emplace_back((long)0);
593
0
        column = &(*doris_column);
594
0
    }
595
596
    // insert null map address
597
0
    const IColumn* data_column = nullptr;
598
0
    if (const auto* nullable_column = check_and_get_column<ColumnNullable>(column)) {
599
0
        data_column = &(nullable_column->get_nested_column());
600
0
        const auto& null_map = nullable_column->get_null_map_data();
601
0
        meta_data.emplace_back((long)null_map.data());
602
0
    } else {
603
0
        meta_data.emplace_back(0);
604
0
        data_column = column;
605
0
    }
606
0
    switch (logical_type) {
607
0
#define DISPATCH(TYPE_INDEX, COLUMN_TYPE, CPP_TYPE)                                          \
608
0
    case TYPE_INDEX: {                                                                       \
609
0
        meta_data.emplace_back(_get_fixed_length_column_address<COLUMN_TYPE>(*data_column)); \
610
0
        break;                                                                               \
611
0
    }
612
0
        FOR_FIXED_LENGTH_TYPES(DISPATCH)
613
0
#undef DISPATCH
614
0
    case PrimitiveType::TYPE_STRING:
615
0
        [[fallthrough]];
616
0
    case PrimitiveType::TYPE_CHAR:
617
0
        [[fallthrough]];
618
0
    case PrimitiveType::TYPE_VARCHAR: {
619
0
        const auto& string_column = assert_cast<const ColumnString&>(*data_column);
620
        // insert offsets
621
0
        meta_data.emplace_back((long)string_column.get_offsets().data());
622
0
        meta_data.emplace_back((long)string_column.get_chars().data());
623
0
        break;
624
0
    }
625
0
    case PrimitiveType::TYPE_ARRAY: {
626
0
        const auto& element_column = assert_cast<const ColumnArray&>(*data_column).get_data_ptr();
627
0
        meta_data.emplace_back(
628
0
                (long)assert_cast<const ColumnArray&>(*data_column).get_offsets().data());
629
0
        const auto& element_type =
630
0
                (assert_cast<const DataTypeArray*>(remove_nullable(data_type).get()))
631
0
                        ->get_nested_type();
632
0
        RETURN_IF_ERROR(_fill_column_meta(element_column, element_type, meta_data));
633
0
        break;
634
0
    }
635
0
    case PrimitiveType::TYPE_STRUCT: {
636
0
        const auto& doris_struct = assert_cast<const ColumnStruct&>(*data_column);
637
0
        const auto* doris_struct_type =
638
0
                assert_cast<const DataTypeStruct*>(remove_nullable(data_type).get());
639
0
        for (int i = 0; i < doris_struct.tuple_size(); ++i) {
640
0
            const auto& struct_field = doris_struct.get_column_ptr(i);
641
0
            const auto& field_type = doris_struct_type->get_element(i);
642
0
            RETURN_IF_ERROR(_fill_column_meta(struct_field, field_type, meta_data));
643
0
        }
644
0
        break;
645
0
    }
646
0
    case PrimitiveType::TYPE_MAP: {
647
0
        const auto& map = assert_cast<const ColumnMap&>(*data_column);
648
0
        const auto& key_type =
649
0
                assert_cast<const DataTypeMap*>(remove_nullable(data_type).get())->get_key_type();
650
0
        const auto& value_type =
651
0
                assert_cast<const DataTypeMap*>(remove_nullable(data_type).get())->get_value_type();
652
0
        const auto& key_column = map.get_keys_ptr();
653
0
        const auto& value_column = map.get_values_ptr();
654
0
        meta_data.emplace_back((long)map.get_offsets().data());
655
0
        RETURN_IF_ERROR(_fill_column_meta(key_column, key_type, meta_data));
656
0
        RETURN_IF_ERROR(_fill_column_meta(value_column, value_type, meta_data));
657
0
        break;
658
0
    }
659
0
    case PrimitiveType::TYPE_VARBINARY: {
660
0
        const auto& varbinary_col = assert_cast<const ColumnVarbinary&>(*data_column);
661
0
        meta_data.emplace_back((long)varbinary_col.get_data().data());
662
0
        break;
663
0
    }
664
0
    default:
665
0
        return Status::InternalError("Unsupported type: {}", data_type->get_name());
666
0
    }
667
0
    return Status::OK();
668
0
}
669
670
0
Status JniDataBridge::to_java_table(Block* block, std::unique_ptr<long[]>& meta) {
671
0
    ColumnNumbers arguments;
672
0
    for (size_t i = 0; i < block->columns(); ++i) {
673
0
        arguments.emplace_back(i);
674
0
    }
675
0
    return to_java_table(block, block->rows(), arguments, meta);
676
0
}
677
678
Status JniDataBridge::to_java_table(Block* block, size_t num_rows, const ColumnNumbers& arguments,
679
0
                                    std::unique_ptr<long[]>& meta) {
680
0
    std::vector<long> meta_data;
681
    // insert number of rows
682
0
    meta_data.emplace_back(num_rows);
683
0
    for (size_t i : arguments) {
684
0
        auto& column_with_type_and_name = block->get_by_position(i);
685
0
        RETURN_IF_ERROR(_fill_column_meta(column_with_type_and_name.column,
686
0
                                          column_with_type_and_name.type, meta_data));
687
0
    }
688
689
0
    meta.reset(new long[meta_data.size()]);
690
0
    memcpy(meta.get(), &meta_data[0], meta_data.size() * 8);
691
0
    return Status::OK();
692
0
}
693
694
std::pair<std::string, std::string> JniDataBridge::parse_table_schema(
695
0
        Block* block, const ColumnNumbers& arguments, bool ignore_column_name) {
696
    // prepare table schema
697
0
    std::ostringstream required_fields;
698
0
    std::ostringstream columns_types;
699
0
    for (int i = 0; i < arguments.size(); ++i) {
700
0
        std::string type = JniDataBridge::get_jni_type(block->get_by_position(arguments[i]).type);
701
0
        if (i == 0) {
702
0
            if (ignore_column_name) {
703
0
                required_fields << "_col_" << arguments[i];
704
0
            } else {
705
0
                required_fields << block->get_by_position(arguments[i]).name;
706
0
            }
707
0
            columns_types << type;
708
0
        } else {
709
0
            if (ignore_column_name) {
710
0
                required_fields << ","
711
0
                                << "_col_" << arguments[i];
712
0
            } else {
713
0
                required_fields << "," << block->get_by_position(arguments[i]).name;
714
0
            }
715
0
            columns_types << "#" << type;
716
0
        }
717
0
    }
718
0
    return std::make_pair(required_fields.str(), columns_types.str());
719
0
}
720
721
0
std::pair<std::string, std::string> JniDataBridge::parse_table_schema(Block* block) {
722
0
    ColumnNumbers arguments;
723
0
    for (size_t i = 0; i < block->columns(); ++i) {
724
0
        arguments.emplace_back(i);
725
0
    }
726
0
    return parse_table_schema(block, arguments, true);
727
0
}
728
729
} // namespace doris