OmniSciDB  6686921089
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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  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`.
123  bool byval = true,
124  bool declare = false) {
125  switch (type) {
127  return "i8"; // clang converts bool to i8
129  return "i8";
131  return "i16";
133  return "i32";
135  return "i64";
137  return "float";
139  return "double";
141  return "void";
143  return "i8*";
145  return "i16*";
147  return "i32*";
149  return "i64*";
151  return "float*";
153  return "double*";
155  return "i1*";
157  return "{i8*, i64, i8}*";
159  return "{i16*, i64, i8}*";
161  return "{i32*, i64, i8}*";
163  return "{i64*, i64, i8}*";
165  return "{float*, i64, i8}*";
167  return "{double*, i64, i8}*";
169  return "{i1*, i64, i8}*";
171  return "geo_point";
173  return "geo_linestring";
175  return "geo_polygon";
177  return "geo_multi_polygon";
179  return "cursor";
181  return (declare ? (byval ? "{i8*, i64}" : "i8*") : "column_int8");
183  return (declare ? (byval ? "{i16*, i64}" : "i8*") : "column_int16");
185  return (declare ? (byval ? "{i32*, i64}" : "i8*") : "column_int32");
187  return (declare ? (byval ? "{i64*, i64}" : "i8*") : "column_int64");
189  return (declare ? (byval ? "{float*, i64}" : "i8*") : "column_float");
191  return (declare ? (byval ? "{double*, i64}" : "i8*") : "column_double");
193  return (declare ? (byval ? "{i8*, i64}" : "i8*") : "column_bool");
195  return (declare ? (byval ? "{i32*, i64}" : "i8*") : "column_text_encoding_dict");
197  return (declare ? (byval ? "{i8*, i64}*" : "i8*") : "text_encoding_none");
199  return (declare ? "{i8*, i32}*" : "text_encoding_dict");
201  return (declare ? "{i8**, i64, i64}*" : "column_list_int8");
203  return (declare ? "{i8**, i64, i64}*" : "column_list_int16");
205  return (declare ? "{i8**, i64, i64}*" : "column_list_int32");
207  return (declare ? "{i8**, i64, i64}*" : "column_list_int64");
209  return (declare ? "{i8**, i64, i64}*" : "column_list_float");
211  return (declare ? "{i8**, i64, i64}*" : "column_list_double");
213  return (declare ? "{i8**, i64, i64}*" : "column_list_bool");
215  return (declare ? "{i8**, i64, i64}*" : "column_list_text_encoding_dict");
216  default:
217  CHECK(false);
218  }
219  CHECK(false);
220  return "";
221 }
222 
223 std::string drop_suffix(const std::string& str) {
224  const auto idx = str.find("__");
225  if (idx == std::string::npos) {
226  return str;
227  }
228  CHECK_GT(idx, std::string::size_type(0));
229  return str.substr(0, idx);
230 }
231 
232 } // namespace
233 
235  /* This function is mostly used for scalar types.
236  For non-scalar types, NULL is returned as a placeholder.
237  */
238 
239  switch (ext_arg_type) {
241  return SQLTypeInfo(kBOOLEAN, false);
243  return SQLTypeInfo(kTINYINT, false);
245  return SQLTypeInfo(kSMALLINT, false);
247  return SQLTypeInfo(kINT, false);
249  return SQLTypeInfo(kBIGINT, false);
251  return SQLTypeInfo(kFLOAT, false);
253  return SQLTypeInfo(kDOUBLE, false);
259  return generate_array_type(kINT);
263  return generate_array_type(kFLOAT);
273  return generate_column_type(kINT);
283  return generate_column_type(kTEXT, kENCODING_DICT, 0 /* comp_param */);
285  return SQLTypeInfo(kTEXT, false, kENCODING_NONE);
287  return SQLTypeInfo(kTEXT, false, kENCODING_DICT);
293  return generate_column_type(kINT);
303  return generate_column_type(kTEXT, kENCODING_DICT, 0 /* comp_param */);
304  default:
305  LOG(FATAL) << "ExtArgumentType `" << serialize_type(ext_arg_type)
306  << "` cannot be converted to SQLTypeInfo.";
307  }
308  return SQLTypeInfo(kNULLT, false);
309 }
310 
312  const std::vector<ExtensionFunction>& ext_funcs,
313  std::string tab) {
314  std::string r = "";
315  for (auto sig : ext_funcs) {
316  r += tab + sig.toString() + "\n";
317  }
318  return r;
319 }
320 
322  const std::vector<SQLTypeInfo>& arg_types) {
323  std::string r = "";
324  for (auto sig = arg_types.begin(); sig != arg_types.end();) {
325  r += sig->get_type_name();
326  sig++;
327  if (sig != arg_types.end()) {
328  r += ", ";
329  }
330  }
331  return r;
332 }
333 
335  const std::vector<ExtArgumentType>& sig_types) {
336  std::string r = "";
337  for (auto t = sig_types.begin(); t != sig_types.end();) {
338  r += serialize_type(*t);
339  t++;
340  if (t != sig_types.end()) {
341  r += ", ";
342  }
343  }
344  return r;
345 }
346 
348  const std::vector<ExtArgumentType>& sig_types) {
349  std::string r = "";
350  for (auto t = sig_types.begin(); t != sig_types.end();) {
352  t++;
353  if (t != sig_types.end()) {
354  r += ", ";
355  }
356  }
357  return r;
358 }
359 
361  return serialize_type(sig_type);
362 }
363 
365  switch (sig_type) {
367  return "TINYINT";
369  return "SMALLINT";
371  return "INTEGER";
373  return "BIGINT";
375  return "FLOAT";
377  return "DOUBLE";
379  return "BOOLEAN";
381  return "TINYINT[]";
383  return "SMALLINT[]";
385  return "INT[]";
387  return "BIGINT[]";
389  return "FLOAT[]";
391  return "DOUBLE[]";
393  return "BOOLEAN[]";
395  return "ARRAY<TINYINT>";
397  return "ARRAY<SMALLINT>";
399  return "ARRAY<INT>";
401  return "ARRAY<BIGINT>";
403  return "ARRAY<FLOAT>";
405  return "ARRAY<DOUBLE>";
407  return "ARRAY<BOOLEAN>";
409  return "COLUMN<TINYINT>";
411  return "COLUMN<SMALLINT>";
413  return "COLUMN<INT>";
415  return "COLUMN<BIGINT>";
417  return "COLUMN<FLOAT>";
419  return "COLUMN<DOUBLE>";
421  return "COLUMN<BOOLEAN>";
423  return "COLUMN<TEXT ENCODING DICT>";
425  return "CURSOR";
427  return "POINT";
429  return "LINESTRING";
431  return "POLYGON";
433  return "MULTIPOLYGON";
435  return "VOID";
437  return "TEXT ENCODING NONE";
439  return "TEXT ENCODING DICT";
441  return "COLUMNLIST<TINYINT>";
443  return "COLUMNLIST<SMALLINT>";
445  return "COLUMNLIST<INT>";
447  return "COLUMNLIST<BIGINT>";
449  return "COLUMNLIST<FLOAT>";
451  return "COLUMNLIST<DOUBLE>";
453  return "COLUMNLIST<BOOLEAN>";
455  return "COLUMNLIST<TEXT ENCODING DICT>";
456  default:
457  UNREACHABLE();
458  }
459  return "";
460 }
461 
462 const std::string ExtensionFunction::getName(bool keep_suffix) const {
463  return (keep_suffix ? name_ : drop_suffix(name_));
464 }
465 
466 std::string ExtensionFunction::toString() const {
467  return getName() + "(" + ExtensionFunctionsWhitelist::toString(args_) + ") -> " +
469 }
470 
471 std::string ExtensionFunction::toStringSQL() const {
472  return getName(/* keep_suffix = */ false) + "(" +
475 }
476 
477 // Converts the extension function signatures to their LLVM representation.
479  const std::unordered_set<std::string>& udf_decls,
480  const bool is_gpu) {
481  std::vector<std::string> declarations;
482  for (const auto& kv : functions_) {
483  const auto& signatures = kv.second;
484  CHECK(!signatures.empty());
485  for (const auto& signature : kv.second) {
486  // If there is a udf function declaration matching an extension function signature
487  // do not emit a duplicate declaration.
488  if (!udf_decls.empty() && udf_decls.find(signature.getName()) != udf_decls.end()) {
489  continue;
490  }
491 
492  std::string decl_prefix;
493  std::vector<std::string> arg_strs;
494 
495  if (is_ext_arg_type_array(signature.getRet())) {
496  decl_prefix = "declare void @" + signature.getName();
497  arg_strs.emplace_back(serialize_type(signature.getRet()));
498  } else {
499  decl_prefix =
500  "declare " + serialize_type(signature.getRet()) + " @" + signature.getName();
501  }
502  for (const auto arg : signature.getArgs()) {
503  arg_strs.push_back(serialize_type(arg));
504  }
505  declarations.push_back(decl_prefix + "(" + boost::algorithm::join(arg_strs, ", ") +
506  ");");
507  }
508  }
509 
511  if (kv.second.isRuntime() || kv.second.useDefaultSizer()) {
512  // Runtime UDTFs are defined in LLVM/NVVM IR module
513  // UDTFs using default sizer share LLVM IR
514  continue;
515  }
516  if (!((is_gpu && kv.second.isGPU()) || (!is_gpu && kv.second.isCPU()))) {
517  continue;
518  }
519  std::string decl_prefix{"declare " + serialize_type(ExtArgumentType::Int32) + " @" +
520  kv.first};
521  std::vector<std::string> arg_strs;
522  for (const auto arg : kv.second.getArgs(/* ensure_column = */ true)) {
523  arg_strs.push_back(
524  serialize_type(arg, /* byval= */ kv.second.isRuntime(), /* declare= */ true));
525  }
526  declarations.push_back(decl_prefix + "(" + boost::algorithm::join(arg_strs, ", ") +
527  ");");
528  }
529  return declarations;
530 }
531 
532 namespace {
533 
535  if (type_name == "bool" || type_name == "i1") {
536  return ExtArgumentType::Bool;
537  }
538  if (type_name == "i8") {
539  return ExtArgumentType::Int8;
540  }
541  if (type_name == "i16") {
542  return ExtArgumentType::Int16;
543  }
544  if (type_name == "i32") {
545  return ExtArgumentType::Int32;
546  }
547  if (type_name == "i64") {
548  return ExtArgumentType::Int64;
549  }
550  if (type_name == "float") {
551  return ExtArgumentType::Float;
552  }
553  if (type_name == "double") {
555  }
556  if (type_name == "void") {
557  return ExtArgumentType::Void;
558  }
559  if (type_name == "i8*") {
560  return ExtArgumentType::PInt8;
561  }
562  if (type_name == "i16*") {
564  }
565  if (type_name == "i32*") {
567  }
568  if (type_name == "i64*") {
570  }
571  if (type_name == "float*") {
573  }
574  if (type_name == "double*") {
576  }
577  if (type_name == "i1*" || type_name == "bool*") {
578  return ExtArgumentType::PBool;
579  }
580  if (type_name == "{i8*, i64, i8}*") {
582  }
583  if (type_name == "{i16*, i64, i8}*") {
585  }
586  if (type_name == "{i32*, i64, i8}*") {
588  }
589  if (type_name == "{i64*, i64, i8}*") {
591  }
592  if (type_name == "{float*, i64, i8}*") {
594  }
595  if (type_name == "{double*, i64, i8}*") {
597  }
598  if (type_name == "{i1*, i64, i8}*" || type_name == "{bool*, i64, i8}*") {
600  }
601  if (type_name == "geo_point") {
603  }
604  if (type_name == "geo_linestring") {
606  }
607  if (type_name == "geo_polygon") {
609  }
610  if (type_name == "geo_multi_polygon") {
612  }
613  if (type_name == "cursor") {
615  }
616  if (type_name == "column_int8") {
618  }
619  if (type_name == "column_int16") {
621  }
622  if (type_name == "column_int32") {
624  }
625  if (type_name == "column_int64") {
627  }
628  if (type_name == "column_float") {
630  }
631  if (type_name == "column_double") {
633  }
634  if (type_name == "column_bool") {
636  }
637  if (type_name == "column_text_encoding_dict") {
639  }
640  if (type_name == "text_encoding_none") {
642  }
643  if (type_name == "text_encoding_dict") {
645  }
646  if (type_name == "column_list_int8") {
648  }
649  if (type_name == "column_list_int16") {
651  }
652  if (type_name == "column_list_int32") {
654  }
655  if (type_name == "column_list_int64") {
657  }
658  if (type_name == "column_list_float") {
660  }
661  if (type_name == "column_list_double") {
663  }
664  if (type_name == "column_list_bool") {
666  }
667  if (type_name == "column_list_text_encoding_dict") {
669  }
670  CHECK(false);
671  return ExtArgumentType::Int16;
672 }
673 
674 } // namespace
675 
676 using SignatureMap = std::unordered_map<std::string, std::vector<ExtensionFunction>>;
677 
679  const std::string& json_func_sigs) {
680  rapidjson::Document func_sigs;
681  func_sigs.Parse(json_func_sigs.c_str());
682  CHECK(func_sigs.IsArray());
683  for (auto func_sigs_it = func_sigs.Begin(); func_sigs_it != func_sigs.End();
684  ++func_sigs_it) {
685  CHECK(func_sigs_it->IsObject());
686  const auto name = json_str(field(*func_sigs_it, "name"));
687  const auto ret = deserialize_type(json_str(field(*func_sigs_it, "ret")));
688  std::vector<ExtArgumentType> args;
689  const auto& args_serialized = field(*func_sigs_it, "args");
690  CHECK(args_serialized.IsArray());
691  for (auto args_serialized_it = args_serialized.Begin();
692  args_serialized_it != args_serialized.End();
693  ++args_serialized_it) {
694  args.push_back(deserialize_type(json_str(*args_serialized_it)));
695  }
696  signatures[to_upper(drop_suffix(name))].emplace_back(name, args, ret);
697  }
698 }
699 
700 // Calcite loads the available extensions from `ExtensionFunctions.ast`, adds
701 // them to its operator table and shares the list with the execution layer in
702 // JSON format. Build an in-memory representation of that list here so that it
703 // can be used by getLLVMDeclarations(), when the LLVM IR codegen asks for it.
704 void ExtensionFunctionsWhitelist::add(const std::string& json_func_sigs) {
705  // Valid json_func_sigs example:
706  // [
707  // {
708  // "name":"sum",
709  // "ret":"i32",
710  // "args":[
711  // "i32",
712  // "i32"
713  // ]
714  // }
715  // ]
716 
717  addCommon(functions_, json_func_sigs);
718 }
719 
720 void ExtensionFunctionsWhitelist::addUdfs(const std::string& json_func_sigs) {
721  if (!json_func_sigs.empty()) {
722  addCommon(udf_functions_, json_func_sigs);
723  }
724 }
725 
727  rt_udf_functions_.clear();
728 }
729 
730 void ExtensionFunctionsWhitelist::addRTUdfs(const std::string& json_func_sigs) {
731  if (!json_func_sigs.empty()) {
732  addCommon(rt_udf_functions_, json_func_sigs);
733  }
734 }
735 
736 std::unordered_map<std::string, std::vector<ExtensionFunction>>
738 
739 std::unordered_map<std::string, std::vector<ExtensionFunction>>
741 
742 std::unordered_map<std::string, std::vector<ExtensionFunction>>
744 
745 std::string toString(const ExtArgumentType& sig_type) {
746  return ExtensionFunctionsWhitelist::toString(sig_type);
747 }
static std::vector< ExtensionFunction > get_ext_funcs(const std::string &name, const bool is_gpu)
static void addUdfs(const std::string &json_func_sigs)
std::string toString(const ExtArgumentType &sig_type)
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:203
auto generate_column_type(const SQLTypes subtype)
Definition: sqltypes.h:1119
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_
string name
Definition: setup.in.py:72
std::string join(T const &container, std::string const &delim)
const std::vector< ExtArgumentType > args_
static void add(const std::string &json_func_sigs)
#define UNREACHABLE()
Definition: Logger.h:253
std::unordered_map< std::string, std::vector< ExtensionFunction >> SignatureMap
const std::string getName(bool keep_suffix=true) const
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:329
std::string toStringSQL() const
#define CHECK_GT(x, y)
Definition: Logger.h:221
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)
bool is_ext_arg_type_array(const ExtArgumentType ext_arg_type)
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)
auto generate_array_type(const SQLTypes subtype)
Definition: sqltypes.h:1113
Definition: sqltypes.h:52
std::string serialize_type(const ExtArgumentType type, bool byval=true, bool declare=false)
const ExtArgumentType ret_
static std::string toString(const std::vector< ExtensionFunction > &ext_funcs, std::string tab="")
#define CHECK(condition)
Definition: Logger.h:209
char * t
static std::unordered_map< std::string, TableFunction > functions_
Definition: sqltypes.h:45
constexpr auto type_name() noexcept
static std::vector< std::string > getLLVMDeclarations(const std::unordered_set< std::string > &udf_decls, const bool is_gpu=false)
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)