forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_scan_operator.cpp
More file actions
270 lines (243 loc) · 12.1 KB
/
Copy pathfile_scan_operator.cpp
File metadata and controls
270 lines (243 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "exec/operator/file_scan_operator.h"
#include <fmt/format.h>
#include <memory>
#include "exec/operator/olap_scan_operator.h"
#include "exec/operator/scan_operator.h"
#include "exec/scan/file_scanner.h"
#include "exec/scan/file_scanner_v2.h"
#include "exec/scan/scanner_context.h"
#include "format/format_common.h"
#include "storage/storage_engine.h"
#include "storage/tablet/tablet_manager.h"
namespace doris {
PushDownType FileScanLocalState::_should_push_down_binary_predicate(
VectorizedFnCall* fn_call, VExprContext* expr_ctx, Field& constant_val,
const std::set<std::string> fn_name) const {
if (!fn_name.contains(fn_call->fn().name.function_name)) {
return PushDownType::UNACCEPTABLE;
}
const auto& children = fn_call->children();
DCHECK(children.size() == 2);
DCHECK_EQ(children[0]->node_type(), TExprNodeType::SLOT_REF);
if (children[1]->is_constant()) {
std::shared_ptr<ColumnPtrWrapper> const_col_wrapper;
THROW_IF_ERROR(children[1]->get_const_col(expr_ctx, &const_col_wrapper));
const auto* const_column =
assert_cast<const ColumnConst*>(const_col_wrapper->column_ptr.get());
constant_val = const_column->operator[](0);
return PushDownType::PARTIAL_ACCEPTABLE;
} else {
// only handle constant value
return PushDownType::UNACCEPTABLE;
}
}
bool FileScanLocalState::_push_down_topn(const RuntimePredicate& predicate) {
if (!predicate.target_is_slot(_parent->node_id())) {
return false;
}
auto& p = _parent->cast<FileScanOperatorX>();
const auto slot_id = predicate.get_texpr(_parent->node_id()).nodes[0].slot_ref.slot_id;
auto* slot = p._slot_id_to_slot_desc[slot_id];
DCHECK(slot != nullptr);
return p.can_push_down_column_predicate(slot);
}
int FileScanLocalState::max_scanners_concurrency(RuntimeState* state) const {
// For select * from table limit 10; should just use one thread.
if (should_run_serial()) {
return 1;
}
/*
* The max concurrency of file scanners for each FileScanLocalState is determined by:
* 1. User specified max_file_scanners_concurrency which is set through session variable.
* 2. Default: 16
*
* If this is a serial operator, the max concurrency should multiply by the number of parallel instances of the operator.
*/
return (state->max_file_scanners_concurrency() > 0 ? state->max_file_scanners_concurrency()
: 16) *
(state->query_parallel_instance_num() / _parent->parallelism(state));
}
int FileScanLocalState::min_scanners_concurrency(RuntimeState* state) const {
if (should_run_serial()) {
return 1;
}
/*
* The min concurrency of scanners for each FileScanLocalState is determined by:
* 1. User specified min_file_scanners_concurrency which is set through session variable.
* 2. Default: 1
*
* If this is a serial operator, the max concurrency should multiply by the number of parallel instances of the operator.
*/
return (state->min_file_scanners_concurrency() > 0 ? state->min_file_scanners_concurrency()
: 1) *
(state->query_parallel_instance_num() / _parent->parallelism(state));
}
ScannerScheduler* FileScanLocalState::scan_scheduler(RuntimeState* state) const {
return state->get_query_ctx()->get_remote_scan_scheduler();
}
#ifdef BE_TEST
bool FileScanLocalState::TEST_should_use_file_scanner_v2(const TQueryOptions& query_options,
bool is_load,
const TFileScanRangeParams& scan_params) {
return _should_use_file_scanner_v2(query_options, is_load, scan_params);
}
#endif
bool FileScanLocalState::_should_use_file_scanner_v2(const TQueryOptions& query_options,
bool is_load,
const TFileScanRangeParams& scan_params) {
const bool is_transactional_hive =
scan_params.__isset.table_format_params &&
scan_params.table_format_params.table_format_type == "transactional_hive";
// JNI reader selection is stored per split, but this scan-level selector cannot inspect the
// split yet. Older FEs may omit both the scan-level Paimon marker and split-level reader_type,
// so keep JNI scans on V1 until scanner selection can distinguish every compatibility shape.
return query_options.__isset.enable_file_scanner_v2 && query_options.enable_file_scanner_v2 &&
!is_load && scan_params.format_type != TFileFormatType::FORMAT_WAL &&
scan_params.format_type != TFileFormatType::FORMAT_ES_HTTP &&
scan_params.format_type != TFileFormatType::FORMAT_LANCE &&
scan_params.format_type != TFileFormatType::FORMAT_JNI && !is_transactional_hive;
}
Status FileScanLocalState::_init_scanners(std::list<ScannerSPtr>* scanners) {
if (_split_source->num_scan_ranges() == 0) {
_eos = true;
return Status::OK();
}
auto& id_file_map = state()->get_id_file_map();
if (id_file_map != nullptr) {
id_file_map->set_external_scan_params(state()->get_query_ctx(), _max_scanners);
}
auto& p = _parent->cast<FileScanOperatorX>();
// There's only one scan range for each backend in batch split mode. Each backend only starts up one ScanNode instance.
uint32_t shard_num =
std::min(ScannerScheduler::default_remote_scan_thread_num() / p.parallelism(state()),
_max_scanners);
shard_num = std::max(shard_num, 1U);
_kv_cache = std::make_unique<ShardedKVCache>(shard_num);
const TFileScanRangeParams* scan_params = nullptr;
if (state()->get_query_ctx() != nullptr &&
state()->get_query_ctx()->file_scan_range_params_map.count(parent_id()) > 0) {
scan_params = &state()->get_query_ctx()->file_scan_range_params_map[parent_id()];
} else {
scan_params = _split_source->get_params();
}
const bool is_load =
state()->desc_tbl().get_tuple_descriptor(scan_params->src_tuple_id) != nullptr;
// TODO: Use scanner v2 for all queries.
const bool use_file_scanner_v2 =
_should_use_file_scanner_v2(state()->query_options(), is_load, *scan_params);
_operator_profile->add_info_string("UseScannerV2", use_file_scanner_v2 ? "true" : "false");
for (int i = 0; i < _max_scanners; ++i) {
ScannerSPtr scanner;
if (use_file_scanner_v2) {
scanner = FileScannerV2::create_shared(state(), this, p._limit, _split_source,
_scanner_profile.get(), _kv_cache.get(),
&p._colname_to_slot_id);
} else {
scanner = FileScanner::create_shared(state(), this, p._limit, _split_source,
_scanner_profile.get(), _kv_cache.get(),
&p._colname_to_slot_id);
}
RETURN_IF_ERROR(scanner->init(state(), _conjuncts));
scanners->push_back(std::move(scanner));
}
return Status::OK();
}
std::string FileScanLocalState::name_suffix() const {
if (_parent->nereids_id() == -1) {
return fmt::format("(id={}, table_name={})", _parent->node_id(),
_parent->cast<FileScanOperatorX>()._table_name);
}
return fmt::format("(nereids_id={}, id={}, table_name={})", _parent->nereids_id(),
_parent->node_id(), _parent->cast<FileScanOperatorX>()._table_name);
}
void FileScanLocalState::set_scan_ranges(RuntimeState* state,
const std::vector<TScanRangeParams>& scan_ranges) {
auto& p = _parent->cast<FileScanOperatorX>();
auto calc_max_scanners = [&](int parallel_instance_num) -> int {
int max_scanners =
ScannerScheduler::default_remote_scan_thread_num() / parallel_instance_num;
// For external tables, each scanner is not bound to specific splits.
// Instead, when a scanner is scheduled, it dynamically fetches the next scan range
// from a unified split source for scanning.
// Therefore, the number of scanners only needs to match "max_scanners_concurrency"
// to ensure full-speed execution.
// For 32 core node, the default "max_scanners_concurrency" should be 16
max_scanners = std::min(max_scanners, max_scanners_concurrency(state));
return max_scanners;
};
if (scan_ranges.size() == 1) {
auto scan_range = scan_ranges[0].scan_range.ext_scan_range.file_scan_range;
if (scan_range.__isset.split_source) {
p._batch_split_mode = true;
custom_profile()->add_info_string("BatchSplitMode", "true");
auto split_source = scan_range.split_source;
RuntimeProfile::Counter* get_split_timer = ADD_TIMER(custom_profile(), "GetSplitTime");
_max_scanners = calc_max_scanners(p.parallelism(state));
_split_source = std::make_shared<RemoteSplitSourceConnector>(
state, get_split_timer, split_source.split_source_id, split_source.num_splits,
_max_scanners);
}
}
if (!p._batch_split_mode) {
_max_scanners = calc_max_scanners(p.parallelism(state));
if (_split_source == nullptr) {
_split_source = std::make_shared<LocalSplitSourceConnector>(scan_ranges, _max_scanners);
}
// currently the total number of splits in the bach split mode cannot be accurately obtained,
// so we don't do it in the batch split mode.
_max_scanners = std::min(_max_scanners, _split_source->num_scan_ranges());
}
if (!scan_ranges.empty() &&
scan_ranges[0].scan_range.ext_scan_range.file_scan_range.__isset.params) {
// for compatibility.
// in new implement, the tuple id is set in prepare phase
_output_tuple_id =
scan_ranges[0].scan_range.ext_scan_range.file_scan_range.params.dest_tuple_id;
}
}
Status FileScanLocalState::init(RuntimeState* state, LocalStateInfo& info) {
RETURN_IF_ERROR(ScanLocalState<FileScanLocalState>::init(state, info));
SCOPED_TIMER(_init_timer);
auto& p = _parent->cast<FileScanOperatorX>();
_output_tuple_id = p._output_tuple_id;
return Status::OK();
}
Status FileScanLocalState::_process_conjuncts(RuntimeState* state) {
RETURN_IF_ERROR(ScanLocalState<FileScanLocalState>::_process_conjuncts(state));
if (Base::_eos) {
return Status::OK();
}
// TODO: Push conjuncts down to reader.
return Status::OK();
}
Status FileScanOperatorX::prepare(RuntimeState* state) {
RETURN_IF_ERROR(ScanOperatorX<FileScanLocalState>::prepare(state));
if (state->get_query_ctx() != nullptr &&
state->get_query_ctx()->file_scan_range_params_map.contains(node_id())) {
TFileScanRangeParams& params =
state->get_query_ctx()->file_scan_range_params_map[node_id()];
_output_tuple_id = params.dest_tuple_id;
}
return Status::OK();
}
bool FileScanOperatorX::can_push_down_column_predicate(const SlotDescriptor* slot) const {
// External readers do not fully support VARBINARY column predicates yet.
return slot->type()->get_primitive_type() != TYPE_VARBINARY;
}
} // namespace doris