forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvin_predicate.cpp
More file actions
260 lines (216 loc) · 9.62 KB
/
Copy pathvin_predicate.cpp
File metadata and controls
260 lines (216 loc) · 9.62 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
// 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 "exprs/vin_predicate.h"
#include <fmt/format.h>
#include <gen_cpp/Exprs_types.h>
#include <glog/logging.h>
#include <algorithm>
#include <cstddef>
#include <ostream>
#include "common/status.h"
#include "core/block/block.h"
#include "core/block/column_numbers.h"
#include "core/block/column_with_type_and_name.h"
#include "core/block/columns_with_type_and_name.h"
#include "exprs/expr_zonemap_filter.h"
#include "exprs/function/in.h"
#include "exprs/function/simple_function_factory.h"
#include "exprs/vexpr_context.h"
#include "exprs/vslot_ref.h"
#include "runtime/runtime_state.h"
namespace doris {
class RowDescriptor;
class RuntimeState;
} // namespace doris
namespace doris {
VInPredicate::VInPredicate(const TExprNode& node)
: VExpr(node), _is_not_in(node.in_predicate.is_not_in) {}
Status VInPredicate::prepare(RuntimeState* state, const RowDescriptor& desc,
VExprContext* context) {
RETURN_IF_ERROR_OR_PREPARED(VExpr::prepare(state, desc, context));
if (_children.empty()) {
return Status::InternalError("no Function operator in.");
}
_expr_name =
fmt::format("({} {} set)", _children[0]->expr_name(), _is_not_in ? "not_in" : "in");
ColumnsWithTypeAndName argument_template;
argument_template.reserve(get_num_children());
for (auto child : _children) {
argument_template.emplace_back(nullptr, child->data_type(), child->expr_name());
}
// construct the proper function_name
std::string head(_is_not_in ? "not_" : "");
std::string real_function_name = head + std::string(function_name);
auto arg_type = remove_nullable(argument_template[0].type);
if (is_complex_type(arg_type->get_primitive_type())) {
real_function_name = "collection_" + real_function_name;
}
_function = SimpleFunctionFactory::instance().get_function(real_function_name,
argument_template, _data_type, {});
if (_function == nullptr) {
return Status::NotSupported("Function {} is not implemented", real_function_name);
}
VExpr::register_function_context(state, context);
_prepare_finished = true;
if (state->query_options().__isset.in_list_value_count_threshold) {
_in_list_value_count_threshold = state->query_options().in_list_value_count_threshold;
}
return Status::OK();
}
Status VInPredicate::open(RuntimeState* state, VExprContext* context,
FunctionContext::FunctionStateScope scope) {
DCHECK(_prepare_finished);
for (auto& child : _children) {
RETURN_IF_ERROR(child->open(state, context, scope));
}
RETURN_IF_ERROR(VExpr::init_function_context(state, context, scope, _function));
if (scope == FunctionContext::FRAGMENT_LOCAL) {
RETURN_IF_ERROR(VExpr::get_const_col(context, nullptr));
}
_is_args_all_constant = std::all_of(_children.begin() + 1, _children.end(),
[](const VExprSPtr& expr) { return expr->is_constant(); });
if (scope == FunctionContext::FRAGMENT_LOCAL && _is_args_all_constant &&
!_zonemap_materialized) {
RETURN_IF_ERROR(_materialize_for_zonemap_filter(context));
}
_open_finished = true;
return Status::OK();
}
void VInPredicate::close(VExprContext* context, FunctionContext::FunctionStateScope scope) {
VExpr::close_function_context(context, scope, _function);
VExpr::close(context, scope);
}
Status VInPredicate::evaluate_inverted_index(VExprContext* context, uint32_t segment_num_rows) {
DCHECK_GE(get_num_children(), 2);
return _evaluate_inverted_index(context, _function, segment_num_rows);
}
Status VInPredicate::_materialize_for_zonemap_filter(VExprContext* context) {
_seg_filter_values.clear();
_seg_filter_contains_null = false;
_zonemap_materialized = false;
if (_children.size() < 2 || !_children[0]->is_slot_ref()) {
return Status::OK();
}
const auto data_type = remove_nullable(_children[0]->data_type());
DORIS_CHECK(data_type != nullptr);
if (is_complex_type(data_type->get_primitive_type())) {
return Status::OK();
}
DORIS_CHECK(context != nullptr);
auto* fn_ctx = context->fn_context(_fn_context_index);
DORIS_CHECK(fn_ctx != nullptr);
auto* in_state =
reinterpret_cast<InState*>(fn_ctx->get_function_state(FunctionContext::FRAGMENT_LOCAL));
DORIS_CHECK(in_state != nullptr);
DORIS_CHECK(in_state->use_set);
DORIS_CHECK(in_state->hybrid_set != nullptr);
expr_zonemap::InZonemapMaterializedSet materialized;
RETURN_IF_ERROR(expr_zonemap::materialize_hybrid_set_for_zonemap_filter(
*in_state->hybrid_set, data_type, &materialized));
_seg_filter_contains_null = materialized.contains_null;
_seg_filter_values = std::move(materialized.values);
_seg_filter_min = std::move(materialized.min_value);
_seg_filter_max = std::move(materialized.max_value);
_zonemap_materialized = true;
return Status::OK();
}
ZoneMapFilterResult VInPredicate::evaluate_zonemap_filter(const ZoneMapEvalContext& ctx) const {
if (_is_not_in && _seg_filter_contains_null) {
return ZoneMapFilterResult::kNoMatch;
}
return expr_zonemap::eval_in_zonemap(ctx, get_child(0), _is_not_in, _seg_filter_values,
_seg_filter_min, _seg_filter_max);
}
bool VInPredicate::can_evaluate_zonemap_filter() const {
return _zonemap_materialized && std::dynamic_pointer_cast<VSlotRef>(get_child(0)) != nullptr;
}
ZoneMapFilterResult VInPredicate::evaluate_dictionary_filter(
const DictionaryEvalContext& ctx) const {
return expr_zonemap::eval_in_dictionary(ctx, get_child(0), _is_not_in, _seg_filter_values);
}
bool VInPredicate::can_evaluate_dictionary_filter() const {
return _zonemap_materialized && !_is_not_in &&
std::dynamic_pointer_cast<VSlotRef>(get_child(0)) != nullptr;
}
ZoneMapFilterResult VInPredicate::evaluate_bloom_filter(const BloomFilterEvalContext& ctx) const {
return expr_zonemap::eval_in_bloom_filter(ctx, get_child(0), _is_not_in, _seg_filter_values);
}
bool VInPredicate::can_evaluate_bloom_filter() const {
return _zonemap_materialized && !_is_not_in &&
std::dynamic_pointer_cast<VSlotRef>(get_child(0)) != nullptr;
}
Status VInPredicate::execute_column_impl(VExprContext* context, const Block* block,
const Selector* selector, size_t count,
ColumnPtr& result_column) const {
if (is_const_and_have_executed()) { // const have execute in open function
result_column = get_result_from_const(count);
return Status::OK();
}
if (fast_execute(context, selector, count, result_column)) {
return Status::OK();
}
DCHECK(_open_finished || block == nullptr);
// This is an optimization. For expressions like colA IN (1, 2, 3, 4),
// where all values inside the IN clause are constants,
// a hash set is created during open, and it will not be accessed again during execute
// Here, _children[0] is colA
const size_t args_size = _is_args_all_constant ? 1 : _children.size();
ColumnNumbers arguments;
arguments.reserve(args_size);
Block temp_block;
for (int i = 0; i < args_size; ++i) {
ColumnPtr column;
RETURN_IF_ERROR(_children[i]->execute_column(context, block, selector, count, column));
arguments.push_back(i);
temp_block.insert({column, _children[i]->execute_type(block), _children[i]->expr_name()});
}
int num_columns_without_result = temp_block.columns();
temp_block.insert({nullptr, _data_type, _expr_name});
RETURN_IF_ERROR(_function->execute(context->fn_context(_fn_context_index), temp_block,
arguments, num_columns_without_result, temp_block.rows()));
result_column = temp_block.get_by_position(num_columns_without_result).column;
DCHECK_EQ(result_column->size(), count);
return Status::OK();
}
size_t VInPredicate::estimate_memory(const size_t rows) {
if (is_const_and_have_executed()) {
return 0;
}
size_t estimate_size = 0;
for (int i = 0; i < _children.size(); ++i) {
estimate_size += _children[i]->estimate_memory(rows);
}
if (_data_type->is_nullable()) {
estimate_size += rows * sizeof(uint8_t);
}
estimate_size += rows * sizeof(uint8_t);
return estimate_size;
}
const std::string& VInPredicate::expr_name() const {
return _expr_name;
}
std::string VInPredicate::debug_string() const {
std::stringstream out;
out << "InPredicate(" << children()[0]->debug_string() << " " << _is_not_in << ",[";
int num_children = get_num_children();
for (int i = 1; i < num_children; ++i) {
out << (i == 1 ? "" : " ") << children()[i]->debug_string();
}
out << "])";
return out.str();
}
} // namespace doris