OmniSciDB  0264ff685a
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  const bool is_gpu) {
48  std::vector<ExtensionFunction> ext_funcs = {};
49  const auto collections = {&functions_, &udf_functions_, &rt_udf_functions_};
50  const auto uname = to_upper(name);
51  for (auto funcs : collections) {
52  const auto it = funcs->find(uname);
53  if (it == funcs->end()) {
54  continue;
55  }
56  auto ext_func_sigs = it->second;
57  std::copy_if(ext_func_sigs.begin(),
58  ext_func_sigs.end(),
59  std::back_inserter(ext_funcs),
60  [is_gpu](auto sig) { return (is_gpu ? sig.isGPU() : sig.isCPU()); });
61  }
62  return ext_funcs;
63 }
64 
65 std::vector<ExtensionFunction> ExtensionFunctionsWhitelist::get_ext_funcs(
66  const std::string& name,
67  size_t arity) {
68  std::vector<ExtensionFunction> ext_funcs = {};
69  const auto collections = {&functions_, &udf_functions_, &rt_udf_functions_};
70  const auto uname = to_upper(name);
71  for (auto funcs : collections) {
72  const auto it = funcs->find(uname);
73  if (it == funcs->end()) {
74  continue;
75  }
76  auto ext_func_sigs = it->second;
77  std::copy_if(ext_func_sigs.begin(),
78  ext_func_sigs.end(),
79  std::back_inserter(ext_funcs),
80  [arity](auto sig) { return arity == sig.getArgs().size(); });
81  }
82  return ext_funcs;
83 }
84 
85 std::vector<ExtensionFunction> ExtensionFunctionsWhitelist::get_ext_funcs(
86  const std::string& name,
87  size_t arity,
88  const SQLTypeInfo& rtype) {
89  std::vector<ExtensionFunction> ext_funcs = {};
90  const auto collections = {&functions_, &udf_functions_, &rt_udf_functions_};
91  const auto uname = to_upper(name);
92  for (auto funcs : collections) {
93  const auto it = funcs->find(uname);
94  if (it == funcs->end()) {
95  continue;
96  }
97  auto ext_func_sigs = it->second;
98  std::copy_if(ext_func_sigs.begin(),
99  ext_func_sigs.end(),
100  std::back_inserter(ext_funcs),
101  [arity, rtype](auto sig) {
102  // Ideally, arity should be equal to the number of
103  // sig arguments but there seems to be many cases
104  // where some sig arguments will be represented
105  // with multiple arguments, for instance, array
106  // argument is translated to data pointer and array
107  // size arguments.
108  if (arity > sig.getArgs().size()) {
109  return false;
110  }
111  auto rt = rtype.get_type();
112  auto st = ext_arg_type_to_type_info(sig.getRet()).get_type();
113  return (st == rt || (st == kTINYINT && rt == kBOOLEAN));
114  });
115  }
116  return ext_funcs;
117 }
118 
119 namespace {
120 
121 // Returns the LLVM name for `type`.
122 std::string serialize_type(const ExtArgumentType type, bool byval = true) {
123  switch (type) {
125  return "i8"; // clang converts bool to i8
127  return "i8";
129  return "i16";
131  return "i32";
133  return "i64";
135  return "float";
137  return "double";
139  return "void";
141  return "i8*";
143  return "i16*";
145  return "i32*";
147  return "i64*";
149  return "float*";
151  return "double*";
153  return "i1*";
155  return "{i8*, i64, i8}*";
157  return "{i16*, i64, i8}*";
159  return "{i32*, i64, i8}*";
161  return "{i64*, i64, i8}*";
163  return "{float*, i64, i8}*";
165  return "{double*, i64, i8}*";
167  return "{i1*, i64, i8}*";
169  return "geo_point";
171  return "geo_linestring";
173  return "geo_polygon";
175  return "geo_multi_polygon";
177  return "cursor";
179  return (byval ? "{i8*, i64}" : "i8*");
181  return (byval ? "{i16*, i64}" : "i8*");
183  return (byval ? "{i32*, i64}" : "i8*");
185  return (byval ? "{i64*, i64}" : "i8*");
187  return (byval ? "{float*, i64}" : "i8*");
189  return (byval ? "{double*, i64}" : "i8*");
191  return (byval ? "{i1*, i64}" : "i8*");
193  return "text_encoding_node";
195  return "text_encoding_dict8";
197  return "text_encoding_dict16";
199  return "text_encoding_dict32";
200  default:
201  CHECK(false);
202  }
203  CHECK(false);
204  return "";
205 }
206 
207 std::string drop_suffix(const std::string& str) {
208  const auto idx = str.find("__");
209  if (idx == std::string::npos) {
210  return str;
211  }
212  CHECK_GT(idx, std::string::size_type(0));
213  return str.substr(0, idx);
214 }
215 
216 } // namespace
217 
219  /* This function is mostly used for scalar types.
220  For non-scalar types, NULL is returned as a placeholder.
221  */
222 
223  auto generate_array_type = [](const auto subtype) {
224  auto ti = SQLTypeInfo(kARRAY, false);
225  ti.set_subtype(subtype);
226  return ti;
227  };
228 
229  auto generate_column_type = [](const auto subtype) {
230  auto ti = SQLTypeInfo(kCOLUMN, false);
231  ti.set_subtype(subtype);
232  return ti;
233  };
234 
235  switch (ext_arg_type) {
237  return SQLTypeInfo(kBOOLEAN, false);
239  return SQLTypeInfo(kTINYINT, false);
241  return SQLTypeInfo(kSMALLINT, false);
243  return SQLTypeInfo(kINT, false);
245  return SQLTypeInfo(kBIGINT, false);
247  return SQLTypeInfo(kFLOAT, false);
249  return SQLTypeInfo(kDOUBLE, false);
251  return generate_array_type(kTINYINT);
253  return generate_array_type(kSMALLINT);
255  return generate_array_type(kINT);
257  return generate_array_type(kBIGINT);
259  return generate_array_type(kFLOAT);
261  return generate_array_type(kDOUBLE);
263  return generate_array_type(kBOOLEAN);
265  return generate_column_type(kTINYINT);
267  return generate_column_type(kSMALLINT);
269  return generate_column_type(kINT);
271  return generate_column_type(kBIGINT);
273  return generate_column_type(kFLOAT);
275  return generate_column_type(kDOUBLE);
277  return generate_column_type(kBOOLEAN);
279  return SQLTypeInfo(kTEXT, false, kENCODING_NONE);
283  return SQLTypeInfo(kTEXT, false, kENCODING_DICT);
284  default:
285  LOG(FATAL) << "ExtArgumentType `" << serialize_type(ext_arg_type)
286  << "` cannot be converted to SQLTypeInfo.";
287  }
288  return SQLTypeInfo(kNULLT, false);
289 }
290 
292  const std::vector<ExtensionFunction>& ext_funcs,
293  std::string tab) {
294  std::string r = "";
295  for (auto sig : ext_funcs) {
296  r += tab + sig.toString() + "\n";
297  }
298  return r;
299 }
300 
302  const std::vector<SQLTypeInfo>& arg_types) {
303  std::string r = "";
304  for (auto sig = arg_types.begin(); sig != arg_types.end();) {
305  r += sig->get_type_name();
306  sig++;
307  if (sig != arg_types.end()) {
308  r += ", ";
309  }
310  }
311  return r;
312 }
313 
315  const std::vector<ExtArgumentType>& sig_types) {
316  std::string r = "";
317  for (auto t = sig_types.begin(); t != sig_types.end();) {
318  r += serialize_type(*t);
319  t++;
320  if (t != sig_types.end()) {
321  r += ", ";
322  }
323  }
324  return r;
325 }
326 
328  const std::vector<ExtArgumentType>& sig_types) {
329  std::string r = "";
330  for (auto t = sig_types.begin(); t != sig_types.end();) {
332  t++;
333  if (t != sig_types.end()) {
334  r += ", ";
335  }
336  }
337  return r;
338 }
339 
341  return serialize_type(sig_type);
342 }
343 
345  switch (sig_type) {
347  return "TINYINT";
349  return "SMALLINT";
351  return "INTEGER";
353  return "BIGINT";
355  return "FLOAT";
357  return "DOUBLE";
359  return "BOOLEAN";
361  return "TINYINT[]";
363  return "SMALLINT[]";
365  return "INT[]";
367  return "BIGINT[]";
369  return "FLOAT[]";
371  return "DOUBLE[]";
373  return "BOOLEAN[]";
375  return "ARRAY<TINYINT>";
377  return "ARRAY<SMALLINT>";
379  return "ARRAY<INT>";
381  return "ARRAY<BIGINT>";
383  return "ARRAY<FLOAT>";
385  return "ARRAY<DOUBLE>";
387  return "ARRAY<BOOLEAN>";
389  return "COLUMN<TINYINT>";
391  return "COLUMN<SMALLINT>";
393  return "COLUMN<INT>";
395  return "COLUMN<BIGINT>";
397  return "COLUMN<FLOAT>";
399  return "COLUMN<DOUBLE>";
401  return "COLUMN<BOOLEAN>";
403  return "CURSOR";
405  return "POINT";
407  return "LINESTRING";
409  return "POLYGON";
411  return "MULTIPOLYGON";
413  return "VOID";
415  return "TEXT ENCODING NONE";
417  return "TEXT ENCODING DICT(8)";
419  return "TEXT ENCODING DICT(16)";
421  return "TEXT ENCODING DICT(32)";
422  default:
423  UNREACHABLE();
424  }
425  return "";
426 }
427 
428 const std::string ExtensionFunction::getName(bool keep_suffix) const {
429  return (keep_suffix ? name_ : drop_suffix(name_));
430 }
431 
432 std::string ExtensionFunction::toString() const {
433  return getName() + "(" + ExtensionFunctionsWhitelist::toString(args_) + ") -> " +
434  serialize_type(ret_);
435 }
436 
437 std::string ExtensionFunction::toStringSQL() const {
438  return getName(/* keep_suffix = */ false) + "(" +
441 }
442 
443 // Converts the extension function signatures to their LLVM representation.
445  const std::unordered_set<std::string>& udf_decls) {
446  std::vector<std::string> declarations;
447  for (const auto& kv : functions_) {
448  const auto& signatures = kv.second;
449  CHECK(!signatures.empty());
450  for (const auto& signature : kv.second) {
451  // If there is a udf function declaration matching an extension function signature
452  // do not emit a duplicate declaration.
453  if (!udf_decls.empty() && udf_decls.find(signature.getName()) != udf_decls.end()) {
454  continue;
455  }
456 
457  std::string decl_prefix;
458  std::vector<std::string> arg_strs;
459 
460  if (is_ext_arg_type_array(signature.getRet())) {
461  decl_prefix = "declare void @" + signature.getName();
462  arg_strs.emplace_back(serialize_type(signature.getRet()));
463  } else {
464  decl_prefix =
465  "declare " + serialize_type(signature.getRet()) + " @" + signature.getName();
466  }
467  for (const auto arg : signature.getArgs()) {
468  arg_strs.push_back(serialize_type(arg));
469  }
470  declarations.push_back(decl_prefix + "(" + boost::algorithm::join(arg_strs, ", ") +
471  ");");
472  }
473  }
474 
476  if (kv.second.isRuntime()) {
477  // Runtime UDTFs are defined in LLVM/NVVM IR module
478  continue;
479  }
480  std::string decl_prefix{"declare " + serialize_type(ExtArgumentType::Int32) + " @" +
481  kv.first};
482  std::vector<std::string> arg_strs;
483  for (const auto arg : kv.second.getArgs(/* ensure_column = */ true)) {
484  arg_strs.push_back(serialize_type(arg, /* byval= */ kv.second.isRuntime()));
485  }
486  declarations.push_back(decl_prefix + "(" + boost::algorithm::join(arg_strs, ", ") +
487  ");");
488  }
489  return declarations;
490 }
491 
492 namespace {
493 
495  if (type_name == "bool" || type_name == "i1") {
496  return ExtArgumentType::Bool;
497  }
498  if (type_name == "i8") {
499  return ExtArgumentType::Int8;
500  }
501  if (type_name == "i16") {
502  return ExtArgumentType::Int16;
503  }
504  if (type_name == "i32") {
505  return ExtArgumentType::Int32;
506  }
507  if (type_name == "i64") {
508  return ExtArgumentType::Int64;
509  }
510  if (type_name == "float") {
511  return ExtArgumentType::Float;
512  }
513  if (type_name == "double") {
515  }
516  if (type_name == "void") {
517  return ExtArgumentType::Void;
518  }
519  if (type_name == "i8*") {
520  return ExtArgumentType::PInt8;
521  }
522  if (type_name == "i16*") {
524  }
525  if (type_name == "i32*") {
527  }
528  if (type_name == "i64*") {
530  }
531  if (type_name == "float*") {
533  }
534  if (type_name == "double*") {
536  }
537  if (type_name == "i1*" || type_name == "bool*") {
538  return ExtArgumentType::PBool;
539  }
540  if (type_name == "{i8*, i64, i8}*") {
542  }
543  if (type_name == "{i16*, i64, i8}*") {
545  }
546  if (type_name == "{i32*, i64, i8}*") {
548  }
549  if (type_name == "{i64*, i64, i8}*") {
551  }
552  if (type_name == "{float*, i64, i8}*") {
554  }
555  if (type_name == "{double*, i64, i8}*") {
557  }
558  if (type_name == "{i1*, i64, i8}*" || type_name == "{bool*, i64, i8}*") {
560  }
561  if (type_name == "geo_point") {
563  }
564  if (type_name == "geo_linestring") {
566  }
567  if (type_name == "geo_polygon") {
569  }
570  if (type_name == "geo_multi_polygon") {
572  }
573  if (type_name == "cursor") {
575  }
576  if (type_name == "{i8*, i64}") {
578  }
579  if (type_name == "{i16*, i64}") {
581  }
582  if (type_name == "{i32*, i64}") {
584  }
585  if (type_name == "{i64*, i64}") {
587  }
588  if (type_name == "{float*, i64}") {
590  }
591  if (type_name == "{double*, i64}") {
593  }
594  if (type_name == "{i1*, i64}" || type_name == "{bool*, i64}") {
596  }
597  if (type_name == "text_encoding_none") {
599  }
600  if (type_name == "text_encoding_dict8") {
602  }
603  if (type_name == "text_encoding_dict16") {
605  }
606  if (type_name == "text_encoding_dict32") {
608  }
609  CHECK(false);
610  return ExtArgumentType::Int16;
611 }
612 
613 } // namespace
614 
615 using SignatureMap = std::unordered_map<std::string, std::vector<ExtensionFunction>>;
616 
618  const std::string& json_func_sigs) {
619  rapidjson::Document func_sigs;
620  func_sigs.Parse(json_func_sigs.c_str());
621  CHECK(func_sigs.IsArray());
622  for (auto func_sigs_it = func_sigs.Begin(); func_sigs_it != func_sigs.End();
623  ++func_sigs_it) {
624  CHECK(func_sigs_it->IsObject());
625  const auto name = json_str(field(*func_sigs_it, "name"));
626  const auto ret = deserialize_type(json_str(field(*func_sigs_it, "ret")));
627  std::vector<ExtArgumentType> args;
628  const auto& args_serialized = field(*func_sigs_it, "args");
629  CHECK(args_serialized.IsArray());
630  for (auto args_serialized_it = args_serialized.Begin();
631  args_serialized_it != args_serialized.End();
632  ++args_serialized_it) {
633  args.push_back(deserialize_type(json_str(*args_serialized_it)));
634  }
635  signatures[to_upper(drop_suffix(name))].emplace_back(name, args, ret);
636  }
637 }
638 
639 // Calcite loads the available extensions from `ExtensionFunctions.ast`, adds
640 // them to its operator table and shares the list with the execution layer in
641 // JSON format. Build an in-memory representation of that list here so that it
642 // can be used by getLLVMDeclarations(), when the LLVM IR codegen asks for it.
643 void ExtensionFunctionsWhitelist::add(const std::string& json_func_sigs) {
644  // Valid json_func_sigs example:
645  // [
646  // {
647  // "name":"sum",
648  // "ret":"i32",
649  // "args":[
650  // "i32",
651  // "i32"
652  // ]
653  // }
654  // ]
655 
656  addCommon(functions_, json_func_sigs);
657 }
658 
659 void ExtensionFunctionsWhitelist::addUdfs(const std::string& json_func_sigs) {
660  if (!json_func_sigs.empty()) {
661  addCommon(udf_functions_, json_func_sigs);
662  }
663 }
664 
666  rt_udf_functions_.clear();
667 }
668 
669 void ExtensionFunctionsWhitelist::addRTUdfs(const std::string& json_func_sigs) {
670  if (!json_func_sigs.empty()) {
671  addCommon(rt_udf_functions_, json_func_sigs);
672  }
673 }
674 
675 std::unordered_map<std::string, std::vector<ExtensionFunction>>
677 
678 std::unordered_map<std::string, std::vector<ExtensionFunction>>
680 
681 std::unordered_map<std::string, std::vector<ExtensionFunction>>
683 
684 std::string toString(const ExtArgumentType& sig_type) {
685  return ExtensionFunctionsWhitelist::toString(sig_type);
686 }
static std::vector< ExtensionFunction > get_ext_funcs(const std::string &name, const bool is_gpu)
static void addUdfs(const std::string &json_func_sigs)
static std::unordered_map< std::string, std::vector< ExtensionFunction > > udf_functions_
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)
#define UNREACHABLE()
Definition: Logger.h:241
std::unordered_map< std::string, std::vector< ExtensionFunction > > SignatureMap
#define CHECK_GT(x, y)
Definition: Logger.h:209
name
Definition: setup.py:35
const std::string getName(bool keep_suffix=true) const
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
static std::vector< ExtensionFunction > * get_udf(const std::string &name)
ExtArgumentType deserialize_type(const std::string &type_name)
std::string serialize_type(const ExtArgumentType type, bool byval=true)
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)
Definition: sqltypes.h:51
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)
#define CHECK(condition)
Definition: Logger.h:197
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:311
static std::unordered_map< std::string, TableFunction > functions_
Definition: sqltypes.h:44
constexpr auto type_name() noexcept
SQLTypeInfo ext_arg_type_to_type_info(const ExtArgumentType ext_arg_type)
static std::string toStringSQL(const std::vector< ExtArgumentType > &sig_types)
static void addRTUdfs(const std::string &json_func_sigs)