Coverage Report

Created: 2026-09-30 16:28

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