OmniSciDB  06b3bd477c
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ExtensionFunctionsWhitelist.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #include <boost/algorithm/string/join.hpp>
20 #include <iostream>
21 
25 #include "Shared/StringTransform.h"
26 
27 // Get the list of all type specializations for the given function name.
28 std::vector<ExtensionFunction>* ExtensionFunctionsWhitelist::get(
29  const std::string& name) {
30  const auto it = functions_.find(to_upper(name));
31  if (it == functions_.end()) {
32  return nullptr;
33  }
34  return &it->second;
35 }
36 std::vector<ExtensionFunction>* ExtensionFunctionsWhitelist::get_udf(
37  const std::string& name) {
38  const auto it = udf_functions_.find(to_upper(name));
39  if (it == udf_functions_.end()) {
40  return nullptr;
41  }
42  return &it->second;
43 }
44 
45 std::vector<ExtensionFunction> ExtensionFunctionsWhitelist::get_ext_funcs(
46  const std::string& name) {
47  std::vector<ExtensionFunction> ext_funcs = {};
48  const auto collections = {&functions_, &udf_functions_, &rt_udf_functions_};
49  const auto uname = to_upper(name);
50  for (auto funcs : collections) {
51  const auto it = funcs->find(uname);
52  if (it == funcs->end()) {
53  continue;
54  }
55  auto ext_func_sigs = it->second;
56  std::copy(ext_func_sigs.begin(), ext_func_sigs.end(), std::back_inserter(ext_funcs));
57  }
58  return ext_funcs;
59 }
60 
61 std::vector<ExtensionFunction> ExtensionFunctionsWhitelist::get_ext_funcs(
62  const std::string& name,
63  size_t arity) {
64  std::vector<ExtensionFunction> ext_funcs = {};
65  const auto collections = {&functions_, &udf_functions_, &rt_udf_functions_};
66  const auto uname = to_upper(name);
67  for (auto funcs : collections) {
68  const auto it = funcs->find(uname);
69  if (it == funcs->end()) {
70  continue;
71  }
72  auto ext_func_sigs = it->second;
73  std::copy_if(ext_func_sigs.begin(),
74  ext_func_sigs.end(),
75  std::back_inserter(ext_funcs),
76  [arity](auto sig) { return arity == sig.getArgs().size(); });
77  }
78  return ext_funcs;
79 }
80 
81 std::vector<ExtensionFunction> ExtensionFunctionsWhitelist::get_ext_funcs(
82  const std::string& name,
83  size_t arity,
84  const SQLTypeInfo& rtype) {
85  std::vector<ExtensionFunction> ext_funcs = {};
86  const auto collections = {&functions_, &udf_functions_, &rt_udf_functions_};
87  const auto uname = to_upper(name);
88  for (auto funcs : collections) {
89  const auto it = funcs->find(uname);
90  if (it == funcs->end()) {
91  continue;
92  }
93  auto ext_func_sigs = it->second;
94  std::copy_if(ext_func_sigs.begin(),
95  ext_func_sigs.end(),
96  std::back_inserter(ext_funcs),
97  [arity, rtype](auto sig) {
98  // Ideally, arity should be equal to the number of
99  // sig arguments but there seems to be many cases
100  // where some sig arguments will be represented
101  // with multiple arguments, for instance, array
102  // argument is translated to data pointer and array
103  // size arguments.
104  if (arity > sig.getArgs().size()) {
105  return false;
106  }
107  auto rt = rtype.get_type();
108  auto st = ext_arg_type_to_type_info(sig.getRet()).get_type();
109  return (st == rt || (st == kTINYINT && rt == kBOOLEAN));
110  });
111  }
112  return ext_funcs;
113 }
114 
115 namespace {
116 
117 // Returns the LLVM name for `type`.
118 std::string serialize_type(const ExtArgumentType type) {
119  switch (type) {
121  return "i8"; // clang converts bool to i8
123  return "i8";
125  return "i16";
127  return "i32";
129  return "i64";
131  return "float";
133  return "double";
135  return "void";
137  return "i8*";
139  return "i16*";
141  return "i32*";
143  return "i64*";
145  return "float*";
147  return "double*";
149  return "i1*";
151  return "{i8*, i64, i8}*";
153  return "{i16*, i64, i8}*";
155  return "{i32*, i64, i8}*";
157  return "{i64*, i64, i8}*";
159  return "{float*, i64, i8}*";
161  return "{double*, i64, i8}*";
163  return "{i1*, i64, i8}*";
165  return "geo_point";
167  return "geo_linestring";
169  return "geo_polygon";
171  return "geo_multi_polygon";
173  return "cursor";
175  return "{i8*, i64}";
177  return "{i16*, i64}";
179  return "{i32*, i64}";
181  return "{i64*, i64}";
183  return "{float*, i64}";
185  return "{double*, i64}";
187  return "{i1*, i64}";
188  default:
189  CHECK(false);
190  }
191  CHECK(false);
192  return "";
193 }
194 
195 } // namespace
196 
198  /* This function is mostly used for scalar types.
199  For non-scalar types, NULL is returned as a placeholder.
200  */
201 
202  auto generate_array_type = [](const auto subtype) {
203  auto ti = SQLTypeInfo(kARRAY, false);
204  ti.set_subtype(subtype);
205  return ti;
206  };
207 
208  auto generate_column_type = [](const auto subtype) {
209  auto ti = SQLTypeInfo(kCOLUMN, false);
210  ti.set_subtype(subtype);
211  return ti;
212  };
213 
214  switch (ext_arg_type) {
216  return SQLTypeInfo(kBOOLEAN, false);
218  return SQLTypeInfo(kTINYINT, false);
220  return SQLTypeInfo(kSMALLINT, false);
222  return SQLTypeInfo(kINT, false);
224  return SQLTypeInfo(kBIGINT, false);
226  return SQLTypeInfo(kFLOAT, false);
228  return SQLTypeInfo(kDOUBLE, false);
230  return generate_array_type(kTINYINT);
232  return generate_array_type(kSMALLINT);
234  return generate_array_type(kINT);
236  return generate_array_type(kBIGINT);
238  return generate_array_type(kFLOAT);
240  return generate_array_type(kDOUBLE);
242  return generate_array_type(kBOOLEAN);
244  return generate_column_type(kTINYINT);
246  return generate_column_type(kSMALLINT);
248  return generate_column_type(kINT);
250  return generate_column_type(kBIGINT);
252  return generate_column_type(kFLOAT);
254  return generate_column_type(kDOUBLE);
256  return generate_column_type(kBOOLEAN);
257  default:
258  LOG(WARNING) << "ExtArgumentType `" << serialize_type(ext_arg_type)
259  << "` cannot be converted to SQLTypeInfo. Returning nulltype.";
260  }
261  return SQLTypeInfo(kNULLT, false);
262 }
263 
265  const std::vector<ExtensionFunction>& ext_funcs,
266  std::string tab) {
267  std::string r = "";
268  for (auto sig : ext_funcs) {
269  r += tab + sig.toString() + "\n";
270  }
271  return r;
272 }
273 
275  const std::vector<SQLTypeInfo>& arg_types) {
276  std::string r = "";
277  for (auto sig = arg_types.begin(); sig != arg_types.end();) {
278  r += sig->get_type_name();
279  sig++;
280  if (sig != arg_types.end()) {
281  r += ", ";
282  }
283  }
284  return r;
285 }
286 
288  const std::vector<ExtArgumentType>& sig_types) {
289  std::string r = "";
290  for (auto t = sig_types.begin(); t != sig_types.end();) {
291  r += serialize_type(*t);
292  t++;
293  if (t != sig_types.end()) {
294  r += ", ";
295  }
296  }
297  return r;
298 }
299 
301  return serialize_type(sig_type);
302 }
303 
304 std::string ExtensionFunction::toString() const {
305  return getName() + "(" + ExtensionFunctionsWhitelist::toString(getArgs()) + ") -> " +
307 }
308 
309 // Converts the extension function signatures to their LLVM representation.
311  const std::unordered_set<std::string>& udf_decls) {
312  std::vector<std::string> declarations;
313  for (const auto& kv : functions_) {
314  const auto& signatures = kv.second;
315  CHECK(!signatures.empty());
316  for (const auto& signature : kv.second) {
317  // If there is a udf function declaration matching an extension function signature
318  // do not emit a duplicate declaration.
319  if (!udf_decls.empty() && udf_decls.find(signature.getName()) != udf_decls.end()) {
320  continue;
321  }
322 
323  std::string decl_prefix;
324  std::vector<std::string> arg_strs;
325 
326  if (is_ext_arg_type_array(signature.getRet())) {
327  decl_prefix = "declare void @" + signature.getName();
328  arg_strs.emplace_back(serialize_type(signature.getRet()));
329  } else {
330  decl_prefix =
331  "declare " + serialize_type(signature.getRet()) + " @" + signature.getName();
332  }
333  for (const auto arg : signature.getArgs()) {
334  arg_strs.push_back(serialize_type(arg));
335  }
336  declarations.push_back(decl_prefix + "(" + boost::algorithm::join(arg_strs, ", ") +
337  ");");
338  }
339  }
340 
342  if (kv.second.isRuntime()) {
343  // Runtime UDTFs are defined in LLVM/NVVM IR module
344  continue;
345  }
346  std::string decl_prefix{"declare " + serialize_type(ExtArgumentType::Int32) + " @" +
347  kv.first};
348  std::vector<std::string> arg_strs;
349  for (const auto arg : kv.second.getArgs()) {
350  arg_strs.push_back(serialize_type(arg));
351  }
352  declarations.push_back(decl_prefix + "(" + boost::algorithm::join(arg_strs, ", ") +
353  ");");
354  }
355  return declarations;
356 }
357 
358 namespace {
359 
360 ExtArgumentType deserialize_type(const std::string& type_name) {
361  if (type_name == "bool" || type_name == "i1") {
362  return ExtArgumentType::Bool;
363  }
364  if (type_name == "i8") {
365  return ExtArgumentType::Int8;
366  }
367  if (type_name == "i16") {
368  return ExtArgumentType::Int16;
369  }
370  if (type_name == "i32") {
371  return ExtArgumentType::Int32;
372  }
373  if (type_name == "i64") {
374  return ExtArgumentType::Int64;
375  }
376  if (type_name == "float") {
377  return ExtArgumentType::Float;
378  }
379  if (type_name == "double") {
381  }
382  if (type_name == "void") {
383  return ExtArgumentType::Void;
384  }
385  if (type_name == "i8*") {
386  return ExtArgumentType::PInt8;
387  }
388  if (type_name == "i16*") {
390  }
391  if (type_name == "i32*") {
393  }
394  if (type_name == "i64*") {
396  }
397  if (type_name == "float*") {
399  }
400  if (type_name == "double*") {
402  }
403  if (type_name == "i1*" || type_name == "bool*") {
404  return ExtArgumentType::PBool;
405  }
406  if (type_name == "{i8*, i64, i8}*") {
408  }
409  if (type_name == "{i16*, i64, i8}*") {
411  }
412  if (type_name == "{i32*, i64, i8}*") {
414  }
415  if (type_name == "{i64*, i64, i8}*") {
417  }
418  if (type_name == "{float*, i64, i8}*") {
420  }
421  if (type_name == "{double*, i64, i8}*") {
423  }
424  if (type_name == "{i1*, i64, i8}*" || type_name == "{bool*, i64, i8}*") {
426  }
427  if (type_name == "geo_point") {
429  }
430  if (type_name == "geo_linestring") {
432  }
433  if (type_name == "geo_polygon") {
435  }
436  if (type_name == "geo_multi_polygon") {
438  }
439  if (type_name == "cursor") {
441  }
442  if (type_name == "{i8*, i64}") {
444  }
445  if (type_name == "{i16*, i64}") {
447  }
448  if (type_name == "{i32*, i64}") {
450  }
451  if (type_name == "{i64*, i64}") {
453  }
454  if (type_name == "{float*, i64}") {
456  }
457  if (type_name == "{double*, i64}") {
459  }
460  if (type_name == "{i1*, i64}" || type_name == "{bool*, i64}") {
462  }
463  CHECK(false);
464  return ExtArgumentType::Int16;
465 }
466 
467 std::string drop_suffix(const std::string& str) {
468  const auto idx = str.find("__");
469  if (idx == std::string::npos) {
470  return str;
471  }
472  CHECK_GT(idx, std::string::size_type(0));
473  return str.substr(0, idx);
474 }
475 
476 } // namespace
477 
478 using SignatureMap = std::unordered_map<std::string, std::vector<ExtensionFunction>>;
479 
481  const std::string& json_func_sigs) {
482  rapidjson::Document func_sigs;
483  func_sigs.Parse(json_func_sigs.c_str());
484  CHECK(func_sigs.IsArray());
485  for (auto func_sigs_it = func_sigs.Begin(); func_sigs_it != func_sigs.End();
486  ++func_sigs_it) {
487  CHECK(func_sigs_it->IsObject());
488  const auto name = json_str(field(*func_sigs_it, "name"));
489  const auto ret = deserialize_type(json_str(field(*func_sigs_it, "ret")));
490  std::vector<ExtArgumentType> args;
491  const auto& args_serialized = field(*func_sigs_it, "args");
492  CHECK(args_serialized.IsArray());
493  for (auto args_serialized_it = args_serialized.Begin();
494  args_serialized_it != args_serialized.End();
495  ++args_serialized_it) {
496  args.push_back(deserialize_type(json_str(*args_serialized_it)));
497  }
498  signatures[to_upper(drop_suffix(name))].emplace_back(name, args, ret);
499  }
500 }
501 
502 // Calcite loads the available extensions from `ExtensionFunctions.ast`, adds
503 // them to its operator table and shares the list with the execution layer in
504 // JSON format. Build an in-memory representation of that list here so that it
505 // can be used by getLLVMDeclarations(), when the LLVM IR codegen asks for it.
506 void ExtensionFunctionsWhitelist::add(const std::string& json_func_sigs) {
507  // Valid json_func_sigs example:
508  // [
509  // {
510  // "name":"sum",
511  // "ret":"i32",
512  // "args":[
513  // "i32",
514  // "i32"
515  // ]
516  // }
517  // ]
518 
519  addCommon(functions_, json_func_sigs);
520 }
521 
522 void ExtensionFunctionsWhitelist::addUdfs(const std::string& json_func_sigs) {
523  if (!json_func_sigs.empty()) {
524  addCommon(udf_functions_, json_func_sigs);
525  }
526 }
527 
529  rt_udf_functions_.clear();
530 }
531 
532 void ExtensionFunctionsWhitelist::addRTUdfs(const std::string& json_func_sigs) {
533  if (!json_func_sigs.empty()) {
534  addCommon(rt_udf_functions_, json_func_sigs);
535  }
536 }
537 
538 std::unordered_map<std::string, std::vector<ExtensionFunction>>
540 
541 std::unordered_map<std::string, std::vector<ExtensionFunction>>
543 
544 std::unordered_map<std::string, std::vector<ExtensionFunction>>
static void addUdfs(const std::string &json_func_sigs)
const std::vector< ExtArgumentType > & getArgs() const
const std::string & getName() const
static std::vector< ExtensionFunction > get_ext_funcs(const std::string &name)
static std::unordered_map< std::string, std::vector< ExtensionFunction > > udf_functions_
const ExtArgumentType getRet() const
static std::vector< ExtensionFunction > * get(const std::string &name)
#define LOG(tag)
Definition: Logger.h:188
const std::string json_str(const rapidjson::Value &obj) noexcept
Definition: JsonAccessors.h:44
static std::unordered_map< std::string, std::vector< ExtensionFunction > > rt_udf_functions_
std::string join(T const &container, std::string const &delim)
static void add(const std::string &json_func_sigs)
std::unordered_map< std::string, std::vector< ExtensionFunction >> SignatureMap
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:258
#define CHECK_GT(x, y)
Definition: Logger.h:209
static std::unordered_map< std::string, std::vector< ExtensionFunction > > functions_
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
CHECK(cgen_state)
static std::vector< ExtensionFunction > * get_udf(const std::string &name)
ExtArgumentType deserialize_type(const std::string &type_name)
static void addCommon(std::unordered_map< std::string, std::vector< ExtensionFunction >> &sigs, const std::string &json_func_sigs)
std::string to_upper(const std::string &str)
bool is_ext_arg_type_array(const ExtArgumentType ext_arg_type)
static std::string toString(const std::vector< ExtensionFunction > &ext_funcs, std::string tab="")
static std::vector< std::string > getLLVMDeclarations(const std::unordered_set< std::string > &udf_decls)
static std::unordered_map< std::string, TableFunction > functions_
Definition: sqltypes.h:46
SQLTypeInfo ext_arg_type_to_type_info(const ExtArgumentType ext_arg_type)
static void addRTUdfs(const std::string &json_func_sigs)