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