Coverage Report

Created: 2026-09-09 04:04

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