Coverage Report

Created: 2026-03-17 00:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/format/jni/jni_reader.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#include "jni_reader.h"
19
20
#include <glog/logging.h>
21
22
#include <map>
23
#include <ostream>
24
#include <sstream>
25
26
#include "core/block/block.h"
27
#include "core/types.h"
28
#include "format/jni/jni_data_bridge.h"
29
#include "runtime/descriptors.h"
30
#include "runtime/runtime_state.h"
31
#include "util/jni-util.h"
32
33
namespace doris {
34
#include "common/compile_check_begin.h"
35
class RuntimeProfile;
36
class RuntimeState;
37
38
class Block;
39
} // namespace doris
40
41
namespace doris {
42
43
const std::vector<SlotDescriptor*> JniReader::_s_empty_slot_descs;
44
45
// =========================================================================
46
// JniReader constructors
47
// =========================================================================
48
49
JniReader::JniReader(const std::vector<SlotDescriptor*>& file_slot_descs, RuntimeState* state,
50
                     RuntimeProfile* profile, std::string connector_class,
51
                     std::map<std::string, std::string> scanner_params,
52
                     std::vector<std::string> column_names, int64_t self_split_weight)
53
0
        : _file_slot_descs(file_slot_descs),
54
0
          _state(state),
55
0
          _profile(profile),
56
0
          _connector_class(std::move(connector_class)),
57
0
          _scanner_params(std::move(scanner_params)),
58
0
          _column_names(std::move(column_names)),
59
0
          _self_split_weight(static_cast<int32_t>(self_split_weight)) {
60
0
    _connector_name = split(_connector_class, "/").back();
61
0
}
62
63
JniReader::JniReader(std::string connector_class, std::map<std::string, std::string> scanner_params)
64
0
        : _file_slot_descs(_s_empty_slot_descs),
65
0
          _connector_class(std::move(connector_class)),
66
0
          _scanner_params(std::move(scanner_params)) {
67
0
    _is_table_schema = true;
68
0
    _connector_name = split(_connector_class, "/").back();
69
0
}
70
71
// =========================================================================
72
// JniReader::open  (merged from JniConnector::open)
73
// =========================================================================
74
75
0
Status JniReader::open(RuntimeState* state, RuntimeProfile* profile) {
76
0
    _state = state;
77
0
    _profile = profile;
78
0
    if (_profile) {
79
0
        ADD_TIMER(_profile, _connector_name.c_str());
80
0
        _open_scanner_time = ADD_CHILD_TIMER(_profile, "OpenScannerTime", _connector_name.c_str());
81
0
        _java_scan_time = ADD_CHILD_TIMER(_profile, "JavaScanTime", _connector_name.c_str());
82
0
        _java_append_data_time =
83
0
                ADD_CHILD_TIMER(_profile, "JavaAppendDataTime", _connector_name.c_str());
84
0
        _java_create_vector_table_time =
85
0
                ADD_CHILD_TIMER(_profile, "JavaCreateVectorTableTime", _connector_name.c_str());
86
0
        _fill_block_time = ADD_CHILD_TIMER(_profile, "FillBlockTime", _connector_name.c_str());
87
0
        _max_time_split_weight_counter = _profile->add_conditition_counter(
88
0
                "MaxTimeSplitWeight", TUnit::UNIT, [](int64_t _c, int64_t c) { return c > _c; },
89
0
                _connector_name.c_str());
90
0
    }
91
0
    _java_scan_watcher = 0;
92
93
0
    JNIEnv* env = nullptr;
94
0
    int batch_size = 0;
95
0
    if (!_is_table_schema && _state) {
96
0
        batch_size = _state->batch_size();
97
0
    }
98
0
    RETURN_IF_ERROR(Jni::Env::Get(&env));
99
0
    SCOPED_RAW_TIMER(&_jni_scanner_open_watcher);
100
0
    if (_state) {
101
0
        _scanner_params.emplace("time_zone", _state->timezone());
102
0
    }
103
0
    RETURN_IF_ERROR(_init_jni_scanner(env, batch_size));
104
    // Call org.apache.doris.common.jni.JniScanner#open
105
0
    RETURN_IF_ERROR(_jni_scanner_obj.call_void_method(env, _jni_scanner_open).call());
106
107
0
    RETURN_ERROR_IF_EXC(env);
108
0
    _scanner_opened = true;
109
0
    return Status::OK();
110
0
}
111
112
// =========================================================================
113
// JniReader::get_next_block  (merged from JniConnector::get_next_block)
114
// =========================================================================
115
116
0
Status JniReader::get_next_block(Block* block, size_t* read_rows, bool* eof) {
117
0
    JNIEnv* env = nullptr;
118
0
    RETURN_IF_ERROR(Jni::Env::Get(&env));
119
0
    long meta_address = 0;
120
0
    {
121
0
        SCOPED_RAW_TIMER(&_java_scan_watcher);
122
0
        RETURN_IF_ERROR(_jni_scanner_obj.call_long_method(env, _jni_scanner_get_next_batch)
123
0
                                .call(&meta_address));
124
0
    }
125
0
    if (meta_address == 0) {
126
0
        *read_rows = 0;
127
0
        *eof = true;
128
0
        return Status::OK();
129
0
    }
130
0
    _set_meta(meta_address);
131
0
    long num_rows = _table_meta.next_meta_as_long();
132
0
    if (num_rows == 0) {
133
0
        *read_rows = 0;
134
0
        *eof = true;
135
0
        return Status::OK();
136
0
    }
137
0
    RETURN_IF_ERROR(_fill_block(block, num_rows));
138
0
    *read_rows = num_rows;
139
0
    *eof = false;
140
0
    RETURN_IF_ERROR(_jni_scanner_obj.call_void_method(env, _jni_scanner_release_table).call());
141
0
    _has_read += num_rows;
142
0
    return Status::OK();
143
0
}
144
145
// =========================================================================
146
// JniReader::get_table_schema  (merged from JniConnector::get_table_schema)
147
// =========================================================================
148
149
0
Status JniReader::get_table_schema(std::string& table_schema_str) {
150
0
    JNIEnv* env = nullptr;
151
0
    RETURN_IF_ERROR(Jni::Env::Get(&env));
152
153
0
    Jni::LocalString jstr;
154
0
    RETURN_IF_ERROR(
155
0
            _jni_scanner_obj.call_object_method(env, _jni_scanner_get_table_schema).call(&jstr));
156
0
    Jni::LocalStringBufferGuard cstr;
157
0
    RETURN_IF_ERROR(jstr.get_string_chars(env, &cstr));
158
0
    table_schema_str = std::string {cstr.get()};
159
0
    return Status::OK();
160
0
}
161
162
// =========================================================================
163
// JniReader::close  (merged from JniConnector::close)
164
// =========================================================================
165
166
0
Status JniReader::close() {
167
0
    if (!_closed) {
168
0
        _closed = true;
169
0
        JNIEnv* env = nullptr;
170
0
        RETURN_IF_ERROR(Jni::Env::Get(&env));
171
0
        if (_scanner_opened) {
172
0
            if (_profile) {
173
0
                COUNTER_UPDATE(_open_scanner_time, _jni_scanner_open_watcher);
174
0
                COUNTER_UPDATE(_fill_block_time, _fill_block_watcher);
175
0
            }
176
177
0
            RETURN_ERROR_IF_EXC(env);
178
0
            jlong _append = 0;
179
0
            RETURN_IF_ERROR(
180
0
                    _jni_scanner_obj.call_long_method(env, _jni_scanner_get_append_data_time)
181
0
                            .call(&_append));
182
183
0
            if (_profile) {
184
0
                COUNTER_UPDATE(_java_append_data_time, _append);
185
0
            }
186
187
0
            jlong _create = 0;
188
0
            RETURN_IF_ERROR(
189
0
                    _jni_scanner_obj
190
0
                            .call_long_method(env, _jni_scanner_get_create_vector_table_time)
191
0
                            .call(&_create));
192
193
0
            if (_profile) {
194
0
                COUNTER_UPDATE(_java_create_vector_table_time, _create);
195
0
                COUNTER_UPDATE(_java_scan_time, _java_scan_watcher - _append - _create);
196
0
                _max_time_split_weight_counter->conditional_update(
197
0
                        _jni_scanner_open_watcher + _fill_block_watcher + _java_scan_watcher,
198
0
                        _self_split_weight);
199
0
            }
200
201
            // _fill_block may be failed and returned, we should release table in close.
202
            // org.apache.doris.common.jni.JniScanner#releaseTable is idempotent
203
0
            RETURN_IF_ERROR(
204
0
                    _jni_scanner_obj.call_void_method(env, _jni_scanner_release_table).call());
205
0
            RETURN_IF_ERROR(_jni_scanner_obj.call_void_method(env, _jni_scanner_close).call());
206
0
        }
207
0
    }
208
0
    return Status::OK();
209
0
}
210
211
// =========================================================================
212
// JniReader::_init_jni_scanner  (merged from JniConnector::_init_jni_scanner)
213
// =========================================================================
214
215
0
Status JniReader::_init_jni_scanner(JNIEnv* env, int batch_size) {
216
0
    RETURN_IF_ERROR(
217
0
            Jni::Util::get_jni_scanner_class(env, _connector_class.c_str(), &_jni_scanner_cls));
218
219
0
    Jni::MethodId scanner_constructor;
220
0
    RETURN_IF_ERROR(_jni_scanner_cls.get_method(env, "<init>", "(ILjava/util/Map;)V",
221
0
                                                &scanner_constructor));
222
223
    // prepare constructor parameters
224
0
    Jni::LocalObject hashmap_object;
225
0
    RETURN_IF_ERROR(Jni::Util::convert_to_java_map(env, _scanner_params, &hashmap_object));
226
0
    RETURN_IF_ERROR(_jni_scanner_cls.new_object(env, scanner_constructor)
227
0
                            .with_arg(batch_size)
228
0
                            .with_arg(hashmap_object)
229
0
                            .call(&_jni_scanner_obj));
230
231
0
    RETURN_IF_ERROR(_jni_scanner_cls.get_method(env, "open", "()V", &_jni_scanner_open));
232
0
    RETURN_IF_ERROR(_jni_scanner_cls.get_method(env, "getNextBatchMeta", "()J",
233
0
                                                &_jni_scanner_get_next_batch));
234
0
    RETURN_IF_ERROR(_jni_scanner_cls.get_method(env, "getAppendDataTime", "()J",
235
0
                                                &_jni_scanner_get_append_data_time));
236
0
    RETURN_IF_ERROR(_jni_scanner_cls.get_method(env, "getCreateVectorTableTime", "()J",
237
0
                                                &_jni_scanner_get_create_vector_table_time));
238
0
    RETURN_IF_ERROR(_jni_scanner_cls.get_method(env, "getTableSchema", "()Ljava/lang/String;",
239
0
                                                &_jni_scanner_get_table_schema));
240
0
    RETURN_IF_ERROR(_jni_scanner_cls.get_method(env, "close", "()V", &_jni_scanner_close));
241
0
    RETURN_IF_ERROR(_jni_scanner_cls.get_method(env, "releaseColumn", "(I)V",
242
0
                                                &_jni_scanner_release_column));
243
0
    RETURN_IF_ERROR(
244
0
            _jni_scanner_cls.get_method(env, "releaseTable", "()V", &_jni_scanner_release_table));
245
0
    RETURN_IF_ERROR(_jni_scanner_cls.get_method(env, "getStatistics", "()Ljava/util/Map;",
246
0
                                                &_jni_scanner_get_statistics));
247
0
    return Status::OK();
248
0
}
249
250
// =========================================================================
251
// JniReader::_fill_block  (merged from JniConnector::_fill_block)
252
// =========================================================================
253
254
0
Status JniReader::_fill_block(Block* block, size_t num_rows) {
255
0
    SCOPED_RAW_TIMER(&_fill_block_watcher);
256
0
    JNIEnv* env = nullptr;
257
0
    RETURN_IF_ERROR(Jni::Env::Get(&env));
258
    // Fallback: if _col_name_to_block_idx was not set by the caller (e.g. JdbcScanner),
259
    // build the name-to-position map from the block itself.
260
0
    std::unordered_map<std::string, uint32_t> local_name_to_idx;
261
0
    const std::unordered_map<std::string, uint32_t>* col_map = _col_name_to_block_idx;
262
0
    if (col_map == nullptr) {
263
0
        local_name_to_idx = block->get_name_to_pos_map();
264
0
        col_map = &local_name_to_idx;
265
0
    }
266
0
    for (int i = 0; i < _column_names.size(); ++i) {
267
0
        auto& column_with_type_and_name = block->get_by_position(col_map->at(_column_names[i]));
268
0
        auto& column_ptr = column_with_type_and_name.column;
269
0
        auto& column_type = column_with_type_and_name.type;
270
0
        RETURN_IF_ERROR(JniDataBridge::fill_column(_table_meta, column_ptr, column_type, num_rows));
271
        // Column is not released when fill_column failed. It will be released when releasing table.
272
0
        RETURN_IF_ERROR(_jni_scanner_obj.call_void_method(env, _jni_scanner_release_column)
273
0
                                .with_arg(i)
274
0
                                .call());
275
0
        RETURN_ERROR_IF_EXC(env);
276
0
    }
277
0
    return Status::OK();
278
0
}
279
280
// =========================================================================
281
// JniReader::_get_statistics  (merged from JniConnector::get_statistics)
282
// =========================================================================
283
284
0
Status JniReader::_get_statistics(JNIEnv* env, std::map<std::string, std::string>* result) {
285
0
    result->clear();
286
0
    Jni::LocalObject metrics;
287
0
    RETURN_IF_ERROR(
288
0
            _jni_scanner_obj.call_object_method(env, _jni_scanner_get_statistics).call(&metrics));
289
290
0
    RETURN_IF_ERROR(Jni::Util::convert_to_cpp_map(env, metrics, result));
291
0
    return Status::OK();
292
0
}
293
294
// =========================================================================
295
// JniReader::_collect_profile_before_close
296
// (merged from JniConnector::_collect_profile_before_close)
297
// =========================================================================
298
299
0
void JniReader::_collect_profile_before_close() {
300
0
    if (_scanner_opened && _profile != nullptr) {
301
0
        JNIEnv* env = nullptr;
302
0
        Status st = Jni::Env::Get(&env);
303
0
        if (!st) {
304
0
            LOG(WARNING) << "failed to get jni env when collect profile: " << st;
305
0
            return;
306
0
        }
307
        // update scanner metrics
308
0
        std::map<std::string, std::string> statistics_result;
309
0
        st = _get_statistics(env, &statistics_result);
310
0
        if (!st) {
311
0
            LOG(WARNING) << "failed to get_statistics when collect profile: " << st;
312
0
            return;
313
0
        }
314
315
0
        for (const auto& metric : statistics_result) {
316
0
            std::vector<std::string> type_and_name = split(metric.first, ":");
317
0
            if (type_and_name.size() != 2) {
318
0
                LOG(WARNING) << "Name of JNI Scanner metric should be pattern like "
319
0
                             << "'metricType:metricName'";
320
0
                continue;
321
0
            }
322
0
            long metric_value = std::stol(metric.second);
323
0
            RuntimeProfile::Counter* scanner_counter;
324
0
            if (type_and_name[0] == "timer") {
325
0
                scanner_counter =
326
0
                        ADD_CHILD_TIMER(_profile, type_and_name[1], _connector_name.c_str());
327
0
            } else if (type_and_name[0] == "counter") {
328
0
                scanner_counter = ADD_CHILD_COUNTER(_profile, type_and_name[1], TUnit::UNIT,
329
0
                                                    _connector_name.c_str());
330
0
            } else if (type_and_name[0] == "bytes") {
331
0
                scanner_counter = ADD_CHILD_COUNTER(_profile, type_and_name[1], TUnit::BYTES,
332
0
                                                    _connector_name.c_str());
333
0
            } else {
334
0
                LOG(WARNING) << "Type of JNI Scanner metric should be timer, counter or bytes";
335
0
                continue;
336
0
            }
337
0
            COUNTER_UPDATE(scanner_counter, metric_value);
338
0
        }
339
0
    }
340
0
}
341
342
// =========================================================================
343
// MockJniReader
344
// =========================================================================
345
346
MockJniReader::MockJniReader(const std::vector<SlotDescriptor*>& file_slot_descs,
347
                             RuntimeState* state, RuntimeProfile* profile)
348
0
        : JniReader(
349
0
                  file_slot_descs, state, profile, "org/apache/doris/common/jni/MockJniScanner",
350
0
                  [&]() {
351
0
                      std::ostringstream required_fields;
352
0
                      std::ostringstream columns_types;
353
0
                      int index = 0;
354
0
                      for (const auto& desc : file_slot_descs) {
355
0
                          std::string field = desc->col_name();
356
0
                          std::string type =
357
0
                                  JniDataBridge::get_jni_type_with_different_string(desc->type());
358
0
                          if (index == 0) {
359
0
                              required_fields << field;
360
0
                              columns_types << type;
361
0
                          } else {
362
0
                              required_fields << "," << field;
363
0
                              columns_types << "#" << type;
364
0
                          }
365
0
                          index++;
366
0
                      }
367
0
                      return std::map<String, String> {{"mock_rows", "10240"},
368
0
                                                       {"required_fields", required_fields.str()},
369
0
                                                       {"columns_types", columns_types.str()}};
370
0
                  }(),
371
0
                  [&]() {
372
0
                      std::vector<std::string> names;
373
0
                      for (const auto& desc : file_slot_descs) {
374
0
                          names.emplace_back(desc->col_name());
375
0
                      }
376
0
                      return names;
377
0
                  }()) {}
378
379
0
Status MockJniReader::init_reader() {
380
0
    return open(_state, _profile);
381
0
}
382
383
#include "common/compile_check_end.h"
384
} // namespace doris