forked from heavyai/heavydb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensionFunctionsWhitelist.cpp
More file actions
142 lines (128 loc) · 4.38 KB
/
ExtensionFunctionsWhitelist.cpp
File metadata and controls
142 lines (128 loc) · 4.38 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
/*
* 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 "ExtensionFunctionsWhitelist.h"
#include "JsonAccessors.h"
#include "../Shared/StringTransform.h"
#include <boost/algorithm/string/join.hpp>
// Get the list of all type specializations for the given function name.
std::vector<ExtensionFunction>* ExtensionFunctionsWhitelist::get(const std::string& name) {
const auto it = functions_.find(to_upper(name));
if (it == functions_.end()) {
return nullptr;
}
return &it->second;
}
namespace {
// Returns the LLVM name for `type`.
std::string serialize_type(const ExtArgumentType type) {
switch (type) {
case ExtArgumentType::Int16:
return "i16";
case ExtArgumentType::Int32:
return "i32";
case ExtArgumentType::Int64:
return "i64";
case ExtArgumentType::Float:
return "float";
case ExtArgumentType::Double:
return "double";
default:
CHECK(false);
}
CHECK(false);
return "";
}
} // namespace
// Converts the extension function signatures to their LLVM representation.
std::vector<std::string> ExtensionFunctionsWhitelist::getLLVMDeclarations() {
std::vector<std::string> declarations;
for (const auto& kv : functions_) {
const auto& signatures = kv.second;
CHECK(!signatures.empty());
for (const auto& signature : kv.second) {
std::string decl_prefix{"declare " + serialize_type(signature.getRet()) + " @" + signature.getName()};
std::vector<std::string> arg_strs;
for (const auto arg : signature.getArgs()) {
arg_strs.push_back(serialize_type(arg));
}
declarations.push_back(decl_prefix + "(" + boost::algorithm::join(arg_strs, ", ") + ");");
}
}
return declarations;
}
namespace {
ExtArgumentType deserialize_type(const std::string& type_name) {
if (type_name == "i16") {
return ExtArgumentType::Int16;
}
if (type_name == "i32") {
return ExtArgumentType::Int32;
}
if (type_name == "i64") {
return ExtArgumentType::Int64;
}
if (type_name == "float") {
return ExtArgumentType::Float;
}
if (type_name == "double") {
return ExtArgumentType::Double;
}
CHECK(false);
return ExtArgumentType::Int16;
}
std::string drop_suffix(const std::string& str) {
const auto idx = str.find("__");
if (idx == std::string::npos) {
return str;
}
CHECK_GT(idx, std::string::size_type(0));
return str.substr(0, idx);
}
} // namespace
// Calcite loads the available extensions from `ExtensionFunctions.ast`, adds
// them to its operator table and shares the list with the execution layer in
// JSON format. Build an in-memory representation of that list here so that it
// can be used by getLLVMDeclarations(), when the LLVM IR codegen asks for it.
void ExtensionFunctionsWhitelist::add(const std::string& json_func_sigs) {
// Valid json_func_sigs example:
// [
// {
// "name":"sum",
// "ret":"i32",
// "args":[
// "i32",
// "i32"
// ]
// }
// ]
rapidjson::Document func_sigs;
func_sigs.Parse(json_func_sigs.c_str());
CHECK(func_sigs.IsArray());
for (auto func_sigs_it = func_sigs.Begin(); func_sigs_it != func_sigs.End(); ++func_sigs_it) {
CHECK(func_sigs_it->IsObject());
const auto name = json_str(field(*func_sigs_it, "name"));
const auto ret = deserialize_type(json_str(field(*func_sigs_it, "ret")));
std::vector<ExtArgumentType> args;
const auto& args_serialized = field(*func_sigs_it, "args");
CHECK(args_serialized.IsArray());
for (auto args_serialized_it = args_serialized.Begin(); args_serialized_it != args_serialized.End();
++args_serialized_it) {
args.push_back(deserialize_type(json_str(*args_serialized_it)));
}
functions_[to_upper(drop_suffix(name))].emplace_back(name, args, ret);
}
}
std::unordered_map<std::string, std::vector<ExtensionFunction>> ExtensionFunctionsWhitelist::functions_;