Coverage Report

Created: 2026-03-13 14:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/scan/jdbc_scanner.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 "exec/scan/jdbc_scanner.h"
19
20
#include <new>
21
#include <ostream>
22
#include <utility>
23
#include <vector>
24
25
#include "common/logging.h"
26
#include "core/block/block.h"
27
#include "core/block/column_with_type_and_name.h"
28
#include "core/column/column.h"
29
#include "core/data_type/data_type.h"
30
#include "exec/connector/vjdbc_connector.h"
31
#include "exprs/vexpr_context.h"
32
#include "runtime/descriptors.h"
33
#include "runtime/runtime_profile.h"
34
#include "runtime/runtime_state.h"
35
36
namespace doris {
37
38
JdbcScanner::JdbcScanner(RuntimeState* state, doris::JDBCScanLocalState* local_state, int64_t limit,
39
                         const TupleId& tuple_id, const std::string& query_string,
40
                         TOdbcTableType::type table_type, bool is_tvf, RuntimeProfile* profile)
41
2.32k
        : Scanner(state, local_state, limit, profile),
42
2.32k
          _jdbc_eos(false),
43
2.32k
          _tuple_id(tuple_id),
44
2.32k
          _query_string(query_string),
45
2.32k
          _tuple_desc(nullptr),
46
2.32k
          _table_type(table_type),
47
2.32k
          _is_tvf(is_tvf) {
48
2.32k
    _init_profile(local_state->_scanner_profile);
49
2.32k
    _has_prepared = false;
50
2.32k
}
51
52
2.32k
Status JdbcScanner::init(RuntimeState* state, const VExprContextSPtrs& conjuncts) {
53
2.32k
    VLOG_CRITICAL << "JdbcScanner::init";
54
2.32k
    RETURN_IF_ERROR(Scanner::init(state, conjuncts));
55
56
2.32k
    if (state == nullptr) {
57
0
        return Status::InternalError("input pointer is NULL of VJdbcScanNode::init.");
58
0
    }
59
60
    // get tuple desc
61
2.32k
    _tuple_desc = state->desc_tbl().get_tuple_descriptor(_tuple_id);
62
2.32k
    if (_tuple_desc == nullptr) {
63
0
        return Status::InternalError("Failed to get tuple descriptor.");
64
0
    }
65
66
    // get jdbc table info
67
2.32k
    const JdbcTableDescriptor* jdbc_table =
68
2.32k
            static_cast<const JdbcTableDescriptor*>(_tuple_desc->table_desc());
69
2.32k
    if (jdbc_table == nullptr) {
70
0
        return Status::InternalError("jdbc table pointer is NULL of VJdbcScanNode::init.");
71
0
    }
72
2.32k
    _jdbc_param.catalog_id = jdbc_table->jdbc_catalog_id();
73
2.32k
    _jdbc_param.driver_class = jdbc_table->jdbc_driver_class();
74
2.32k
    _jdbc_param.driver_path = jdbc_table->jdbc_driver_url();
75
2.32k
    _jdbc_param.resource_name = jdbc_table->jdbc_resource_name();
76
2.32k
    _jdbc_param.driver_checksum = jdbc_table->jdbc_driver_checksum();
77
2.32k
    _jdbc_param.jdbc_url = jdbc_table->jdbc_url();
78
2.32k
    _jdbc_param.user = jdbc_table->jdbc_user();
79
2.32k
    _jdbc_param.passwd = jdbc_table->jdbc_passwd();
80
2.32k
    _jdbc_param.tuple_desc = _tuple_desc;
81
2.32k
    _jdbc_param.query_string = std::move(_query_string);
82
2.32k
    _jdbc_param.use_transaction = false; // not useful for scanner but only sink.
83
2.32k
    _jdbc_param.table_type = _table_type;
84
2.32k
    _jdbc_param.is_tvf = _is_tvf;
85
2.32k
    _jdbc_param.connection_pool_min_size = jdbc_table->connection_pool_min_size();
86
2.32k
    _jdbc_param.connection_pool_max_size = jdbc_table->connection_pool_max_size();
87
2.32k
    _jdbc_param.connection_pool_max_life_time = jdbc_table->connection_pool_max_life_time();
88
2.32k
    _jdbc_param.connection_pool_max_wait_time = jdbc_table->connection_pool_max_wait_time();
89
2.32k
    _jdbc_param.connection_pool_keep_alive = jdbc_table->connection_pool_keep_alive();
90
91
2.32k
    _local_state->scanner_profile()->add_info_string("JdbcDriverClass", _jdbc_param.driver_class);
92
2.32k
    _local_state->scanner_profile()->add_info_string("JdbcDriverUrl", _jdbc_param.driver_path);
93
2.32k
    _local_state->scanner_profile()->add_info_string("JdbcUrl", _jdbc_param.jdbc_url);
94
2.32k
    _local_state->scanner_profile()->add_info_string("QuerySql", _jdbc_param.query_string);
95
96
2.32k
    _jdbc_connector.reset(new (std::nothrow) JdbcConnector(_jdbc_param));
97
2.32k
    if (_jdbc_connector == nullptr) {
98
0
        return Status::InternalError("new a jdbc scanner failed.");
99
0
    }
100
101
2.32k
    return Status::OK();
102
2.32k
}
103
104
2.32k
Status JdbcScanner::_open_impl(RuntimeState* state) {
105
2.32k
    VLOG_CRITICAL << "JdbcScanner::open";
106
2.32k
    if (state == nullptr) {
107
0
        return Status::InternalError("input pointer is NULL of VJdbcScanNode::open.");
108
0
    }
109
110
2.32k
    if (!_has_prepared) {
111
0
        return Status::InternalError("used before initialize of VJdbcScanNode::open.");
112
0
    }
113
2.32k
    RETURN_IF_CANCELLED(state);
114
2.32k
    RETURN_IF_ERROR(Scanner::_open_impl(state));
115
2.32k
    RETURN_IF_ERROR(_jdbc_connector->open(state, true));
116
2.32k
    RETURN_IF_ERROR(_jdbc_connector->query());
117
2.32k
    return Status::OK();
118
2.32k
}
119
120
4.55k
Status JdbcScanner::_get_block_impl(RuntimeState* state, Block* block, bool* eof) {
121
4.55k
    VLOG_CRITICAL << "JdbcScanner::_get_block_impl";
122
4.55k
    if (nullptr == state || nullptr == block || nullptr == eof) {
123
0
        return Status::InternalError("input is NULL pointer");
124
0
    }
125
126
4.55k
    if (!_has_prepared) {
127
0
        return Status::InternalError("used before initialize of VJdbcScanNode::get_next.");
128
0
    }
129
130
4.55k
    if (_jdbc_eos == true) {
131
0
        *eof = true;
132
0
        _update_profile();
133
0
        return Status::OK();
134
0
    }
135
136
    // only empty block should be here
137
4.55k
    DCHECK(block->rows() == 0);
138
139
4.55k
    do {
140
4.55k
        RETURN_IF_CANCELLED(state);
141
142
4.55k
        RETURN_IF_ERROR(_jdbc_connector->get_next(&_jdbc_eos, block, state->batch_size()));
143
144
4.55k
        if (_jdbc_eos == true) {
145
2.30k
            if (block->rows() == 0) {
146
2.30k
                _update_profile();
147
2.30k
                *eof = true;
148
2.30k
            }
149
2.30k
            break;
150
2.30k
        }
151
152
2.25k
        VLOG_ROW << "NewJdbcScanNode output rows: " << block->rows();
153
2.25k
    } while (block->rows() == 0 && !(*eof));
154
4.55k
    return Status::OK();
155
4.55k
}
156
157
2.32k
void JdbcScanner::_init_profile(const std::shared_ptr<RuntimeProfile>& profile) {
158
2.32k
    _load_jar_timer = ADD_TIMER(profile, "LoadJarTime");
159
2.32k
    _init_connector_timer = ADD_TIMER(profile, "InitConnectorTime");
160
2.32k
    _check_type_timer = ADD_TIMER(profile, "CheckTypeTime");
161
2.32k
    _get_data_timer = ADD_TIMER(profile, "GetDataTime");
162
2.32k
    _read_and_fill_vector_table_timer =
163
2.32k
            ADD_CHILD_TIMER(profile, "ReadAndFillVectorTableTime", "GetDataTime");
164
2.32k
    _jni_setup_timer = ADD_CHILD_TIMER(profile, "JniSetupTime", "GetDataTime");
165
2.32k
    _has_next_timer = ADD_CHILD_TIMER(profile, "HasNextTime", "GetDataTime");
166
2.32k
    _prepare_params_timer = ADD_CHILD_TIMER(profile, "PrepareParamsTime", "GetDataTime");
167
2.32k
    _fill_block_timer = ADD_CHILD_TIMER(profile, "FillBlockTime", "GetDataTime");
168
2.32k
    _cast_timer = ADD_CHILD_TIMER(profile, "CastTime", "GetDataTime");
169
2.32k
    _execte_read_timer = ADD_TIMER(profile, "ExecteReadTime");
170
2.32k
    _connector_close_timer = ADD_TIMER(profile, "ConnectorCloseTime");
171
2.32k
}
172
173
2.30k
void JdbcScanner::_update_profile() {
174
2.30k
    JdbcConnector::JdbcStatistic& jdbc_statistic = _jdbc_connector->get_jdbc_statistic();
175
2.30k
    COUNTER_UPDATE(_load_jar_timer, jdbc_statistic._load_jar_timer);
176
2.30k
    COUNTER_UPDATE(_init_connector_timer, jdbc_statistic._init_connector_timer);
177
2.30k
    COUNTER_UPDATE(_check_type_timer, jdbc_statistic._check_type_timer);
178
2.30k
    COUNTER_UPDATE(_get_data_timer, jdbc_statistic._get_data_timer);
179
2.30k
    COUNTER_UPDATE(_jni_setup_timer, jdbc_statistic._jni_setup_timer);
180
2.30k
    COUNTER_UPDATE(_has_next_timer, jdbc_statistic._has_next_timer);
181
2.30k
    COUNTER_UPDATE(_prepare_params_timer, jdbc_statistic._prepare_params_timer);
182
2.30k
    COUNTER_UPDATE(_read_and_fill_vector_table_timer,
183
2.30k
                   jdbc_statistic._read_and_fill_vector_table_timer);
184
2.30k
    COUNTER_UPDATE(_fill_block_timer, jdbc_statistic._fill_block_timer);
185
2.30k
    COUNTER_UPDATE(_cast_timer, jdbc_statistic._cast_timer);
186
2.30k
    COUNTER_UPDATE(_execte_read_timer, jdbc_statistic._execte_read_timer);
187
2.30k
    COUNTER_UPDATE(_connector_close_timer, jdbc_statistic._connector_close_timer);
188
2.30k
}
189
190
2.32k
Status JdbcScanner::close(RuntimeState* state) {
191
2.32k
    if (!_try_close()) {
192
0
        return Status::OK();
193
0
    }
194
2.32k
    RETURN_IF_ERROR(Scanner::close(state));
195
2.32k
    RETURN_IF_ERROR(_jdbc_connector->close());
196
2.32k
    return Status::OK();
197
2.32k
}
198
} // namespace doris