forked from heavyai/heavydb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInValuesIR.cpp
More file actions
176 lines (171 loc) · 7.06 KB
/
InValuesIR.cpp
File metadata and controls
176 lines (171 loc) · 7.06 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
/*
* Copyright 2017 MapD Technologies, Inc.
*
* Licensed 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 "Execute.h"
#include <future>
llvm::Value* Executor::codegen(const Analyzer::InValues* expr, const CompilationOptions& co) {
const auto in_arg = expr->get_arg();
if (is_unnest(in_arg)) {
throw std::runtime_error("IN not supported for unnested expressions");
}
const auto& expr_ti = expr->get_type_info();
CHECK(expr_ti.is_boolean());
const auto lhs_lvs = codegen(in_arg, true, co);
llvm::Value* result{nullptr};
if (expr_ti.get_notnull()) {
result = llvm::ConstantInt::get(llvm::IntegerType::getInt1Ty(cgen_state_->context_), false);
} else {
result = ll_int(int8_t(0));
}
CHECK(result);
if (co.hoist_literals_) { // TODO(alex): remove this constraint
auto in_vals_bitmap = createInValuesBitmap(expr, co);
if (in_vals_bitmap) {
if (in_vals_bitmap->isEmpty()) {
return in_vals_bitmap->hasNull() ? inlineIntNull(SQLTypeInfo(kBOOLEAN, false)) : result;
}
CHECK_EQ(size_t(1), lhs_lvs.size());
return cgen_state_->addInValuesBitmap(in_vals_bitmap)->codegen(lhs_lvs.front(), this);
}
}
if (expr_ti.get_notnull()) {
for (auto in_val : expr->get_value_list()) {
result = cgen_state_->ir_builder_.CreateOr(
result, toBool(codegenCmp(kEQ, kONE, lhs_lvs, in_arg->get_type_info(), in_val.get(), co)));
}
} else {
for (auto in_val : expr->get_value_list()) {
const auto crt = codegenCmp(kEQ, kONE, lhs_lvs, in_arg->get_type_info(), in_val.get(), co);
result = cgen_state_->emitCall("logical_or", {result, crt, inlineIntNull(expr_ti)});
}
}
return result;
}
llvm::Value* Executor::codegen(const Analyzer::InIntegerSet* in_integer_set, const CompilationOptions& co) {
const auto in_arg = in_integer_set->get_arg();
if (is_unnest(in_arg)) {
throw std::runtime_error("IN not supported for unnested expressions");
}
const auto& ti = in_integer_set->get_arg()->get_type_info();
const auto needle_null_val = inline_int_null_val(ti);
if (!co.hoist_literals_) {
// We never run without literal hoisting in real world scenarios, this avoids a crash when testing.
throw std::runtime_error(
"IN subquery with many right-hand side values not supported when literal hoisting is disabled");
}
auto in_vals_bitmap = boost::make_unique<InValuesBitmap>(
in_integer_set->get_value_list(),
needle_null_val,
co.device_type_ == ExecutorDeviceType::GPU ? Data_Namespace::GPU_LEVEL : Data_Namespace::CPU_LEVEL,
deviceCount(co.device_type_),
&catalog_->get_dataMgr());
const auto& in_integer_set_ti = in_integer_set->get_type_info();
CHECK(in_integer_set_ti.is_boolean());
const auto lhs_lvs = codegen(in_arg, true, co);
llvm::Value* result{nullptr};
if (in_integer_set_ti.get_notnull()) {
result = llvm::ConstantInt::get(llvm::IntegerType::getInt1Ty(cgen_state_->context_), false);
} else {
result = ll_int(int8_t(0));
}
CHECK(result);
if (in_vals_bitmap->isEmpty()) {
return in_vals_bitmap->hasNull() ? inlineIntNull(SQLTypeInfo(kBOOLEAN, false)) : result;
}
CHECK_EQ(size_t(1), lhs_lvs.size());
return cgen_state_->addInValuesBitmap(in_vals_bitmap)->codegen(lhs_lvs.front(), this);
}
std::unique_ptr<InValuesBitmap> Executor::createInValuesBitmap(const Analyzer::InValues* in_values,
const CompilationOptions& co) {
const auto& value_list = in_values->get_value_list();
const auto val_count = value_list.size();
const auto& ti = in_values->get_arg()->get_type_info();
if (!(ti.is_integer() || (ti.is_string() && ti.get_compression() == kENCODING_DICT))) {
return nullptr;
}
const auto sdp = ti.is_string() ? getStringDictionaryProxy(ti.get_comp_param(), row_set_mem_owner_, true) : nullptr;
if (val_count > 3) {
typedef decltype(value_list.begin()) ListIterator;
std::vector<int64_t> values;
const auto needle_null_val = inline_int_null_val(ti);
const int worker_count = val_count > 10000 ? cpu_threads() : int(1);
std::vector<std::vector<int64_t>> values_set(worker_count, std::vector<int64_t>());
std::vector<std::future<bool>> worker_threads;
auto start_it = value_list.begin();
for (size_t i = 0, start_val = 0, stride = (val_count + worker_count - 1) / worker_count;
i < val_count && start_val < val_count;
++i, start_val += stride, std::advance(start_it, stride)) {
auto end_it = start_it;
std::advance(end_it, std::min(stride, val_count - start_val));
const auto do_work = [&](
std::vector<int64_t>& out_vals, const ListIterator start, const ListIterator end) -> bool {
for (auto val_it = start; val_it != end; ++val_it) {
const auto& in_val = *val_it;
const auto in_val_const = dynamic_cast<const Analyzer::Constant*>(extract_cast_arg(in_val.get()));
if (!in_val_const) {
return false;
}
const auto& in_val_ti = in_val->get_type_info();
CHECK(in_val_ti == ti);
if (ti.is_string()) {
CHECK(sdp);
const auto string_id = in_val_const->get_is_null()
? needle_null_val
: sdp->getIdOfString(*in_val_const->get_constval().stringval);
if (string_id != StringDictionary::INVALID_STR_ID) {
out_vals.push_back(string_id);
}
} else {
out_vals.push_back(codegenIntConst(in_val_const)->getSExtValue());
}
}
return true;
};
if (worker_count > 1) {
worker_threads.push_back(std::async(std::launch::async, do_work, std::ref(values_set[i]), start_it, end_it));
} else {
do_work(std::ref(values), start_it, end_it);
}
}
bool success = true;
for (auto& worker : worker_threads) {
success &= worker.get();
}
if (!success) {
return nullptr;
}
if (worker_count > 1) {
size_t total_val_count = 0;
for (auto& vals : values_set) {
total_val_count += vals.size();
}
values.reserve(total_val_count);
for (auto& vals : values_set) {
values.insert(values.end(), vals.begin(), vals.end());
}
}
try {
return boost::make_unique<InValuesBitmap>(
values,
needle_null_val,
co.device_type_ == ExecutorDeviceType::GPU ? Data_Namespace::GPU_LEVEL : Data_Namespace::CPU_LEVEL,
deviceCount(co.device_type_),
&catalog_->get_dataMgr());
} catch (...) {
return nullptr;
}
}
return nullptr;
}