OmniSciDB  94e8789169
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TableFunctionsFactory.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019 OmniSci, 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 
17 #pragma once
18 
19 #include <string>
20 #include <vector>
21 
23 #include "Shared/toString.h"
25 
26 /*
27 
28  TableFunction represents a User-Defined Table Function (UDTF) and it
29  holds the following information:
30 
31  - the name of a table function that corresponds to its
32  implementation. The name must match the following pattern:
33 
34  \w[\w\d_]*([_][_](gpu_|cpu_|)\d*|)
35 
36  where the first part left to the double underscore is the
37  so-called SQL name of table function that is used in SQL query
38  context, and the right part determines a particular implementation
39  of the table function. One can define many implementations for the
40  same SQL table function with specializations to
41 
42  + different argument types (overloading support)
43 
44  + different execution context, CPU or GPU. When gpu or cpu is not
45  present, the implementation is assumed to be valid for both CPU
46  and GPU contexts.
47 
48  - the output sizer parameter <sizer> that determines the allocated
49  size of the output columns:
50 
51  + UserSpecifiedRowMultiplier - the allocated column size will be
52 
53  <sizer value> * <size of the input columns>
54 
55  where <sizer value> is user-specified integer value as specified
56  in the <sizer> argument position of the table function call.
57 
58  + UserSpecifiedConstantParameter - the allocated column size will
59  be user-specified integer value as specified in the <sizer>
60  argument position of the table function call.
61 
62  + Constant - the allocated output column size will be <sizer>. The
63  table function
64 
65  The actual size of the output column is returned by the table
66  function implementation that must be equal or smaller to the
67  allocated output column size.
68 
69  - the list of input argument types. The input argument type can be a
70  scalar or a column type (that is `Column<scalar>`). Supported
71  scalar types are int8, ..., int64, double, float, bool.
72 
73  - the list of output argument types. The output types of table
74  functions is always some column type. Hence, the output argument
75  types are stored as scalar types that correspond to the data type
76  of the output columns.
77 
78  - a boolean flag specifying the table function is a load-time or
79  run-time function. Run-time functions can be overwitten or removed
80  by users. Load-time functions cannot be redefined in run-time.
81 
82  Future notes:
83 
84  - introduce a list of output column names. Currently, the names of
85  output columns match the pattern
86 
87  out\d+
88 
89  but for better UX it would be nice to enable user-defined names
90  for output columns.
91 
92  */
93 
94 namespace table_functions {
95 
98  const size_t val{0};
99 
100  public:
101  std::string toString() const {
102  switch (type) {
104  return "kUserSpecifiedConstantParameter[" + std::to_string(val) + "]";
106  return "kUserSpecifiedRowMultiplier[" + std::to_string(val) + "]";
108  return "kConstant[" + std::to_string(val) + "]";
109  }
110  return "";
111  }
112 };
113 
115  switch (ext_arg_type) {
130  default:
131  return ext_arg_type;
132  }
133 }
134 
136  public:
137  TableFunction(const std::string& name,
138  const TableFunctionOutputRowSizer output_sizer,
139  const std::vector<ExtArgumentType>& input_args,
140  const std::vector<ExtArgumentType>& output_args,
141  const std::vector<ExtArgumentType>& sql_args,
142  bool is_runtime)
143  : name_(name)
144  , output_sizer_(output_sizer)
145  , input_args_(input_args)
146  , output_args_(output_args)
147  , sql_args_(sql_args)
148  , is_runtime_(is_runtime) {}
149 
150  std::vector<ExtArgumentType> getArgs(const bool ensure_column = false) const {
151  std::vector<ExtArgumentType> args;
152  args.insert(args.end(), input_args_.begin(), input_args_.end());
153  if (ensure_column) {
154  // map row dtype to column type
155  std::for_each(output_args_.begin(), output_args_.end(), [&args](auto t) {
156  args.push_back(ext_arg_type_ensure_column(t));
157  });
158  } else {
159  args.insert(args.end(), output_args_.begin(), output_args_.end());
160  }
161  return args;
162  }
163  const std::vector<ExtArgumentType>& getInputArgs() const { return input_args_; }
164  const std::vector<ExtArgumentType>& getOutputArgs() const { return output_args_; }
165  const std::vector<ExtArgumentType>& getSqlArgs() const { return sql_args_; }
167 
168  SQLTypeInfo getInputSQLType(const size_t idx) const;
169  SQLTypeInfo getOutputSQLType(const size_t idx) const;
170 
171  auto getInputsSize() const { return input_args_.size(); }
172  auto getOutputsSize() const { return output_args_.size(); }
173 
174  auto getName() const { return name_; }
175 
178  }
179 
182  }
183 
186  }
187 
189 
190  size_t getOutputRowSizeParameter() const { return output_sizer_.val; }
191 
192  bool isRuntime() const { return is_runtime_; }
193 
194  inline bool isGPU() const {
195  return (name_.find("_cpu_", name_.find("__")) == std::string::npos);
196  }
197 
198  inline bool isCPU() const {
199  return (name_.find("_gpu_", name_.find("__")) == std::string::npos);
200  }
201 
202  std::string toString() const {
203  auto result = "TableFunction(" + name_ + ", [";
205  result += "], [";
207  result += "], [";
209  result += "], is_runtime=" + std::string((is_runtime_ ? "true" : "false"));
210  result += ", sizer=" + ::toString(output_sizer_);
211  result += ")";
212  return result;
213  }
214 
215  std::string toStringSQL() const {
216  auto result = name_ + "(";
218  result += ") -> (";
220  result += ")";
221  return result;
222  }
223 
224  private:
225  const std::string name_;
227  const std::vector<ExtArgumentType> input_args_;
228  const std::vector<ExtArgumentType> output_args_;
229  const std::vector<ExtArgumentType> sql_args_;
230  const bool is_runtime_;
231 };
232 
234  public:
235  static void add(const std::string& name,
237  const std::vector<ExtArgumentType>& input_args,
238  const std::vector<ExtArgumentType>& output_args,
239  const std::vector<ExtArgumentType>& sql_args,
240  bool is_runtime = false);
241 
242  static std::vector<TableFunction> get_table_funcs(const std::string& name,
243  const bool is_gpu);
244  static std::vector<TableFunction> get_table_funcs(const bool is_runtime = false);
245  static void init();
246  static void reset();
247 
248  private:
249  static std::unordered_map<std::string, TableFunction> functions_;
250 
251  friend class ::ExtensionFunctionsWhitelist;
252 };
253 
254 } // namespace table_functions
SQLTypeInfo getOutputSQLType(const size_t idx) const
const TableFunctionOutputRowSizer output_sizer_
const std::vector< ExtArgumentType > output_args_
std::string to_string(char const *&&v)
const std::vector< ExtArgumentType > sql_args_
SQLTypeInfo getInputSQLType(const size_t idx) const
std::vector< ExtArgumentType > getArgs(const bool ensure_column=false) const
const std::vector< ExtArgumentType > & getOutputArgs() const
TableFunction(const std::string &name, const TableFunctionOutputRowSizer output_sizer, const std::vector< ExtArgumentType > &input_args, const std::vector< ExtArgumentType > &output_args, const std::vector< ExtArgumentType > &sql_args, bool is_runtime)
static void add(const std::string &name, const TableFunctionOutputRowSizer sizer, const std::vector< ExtArgumentType > &input_args, const std::vector< ExtArgumentType > &output_args, const std::vector< ExtArgumentType > &sql_args, bool is_runtime=false)
const std::vector< ExtArgumentType > & getInputArgs() const
static std::string toString(const std::vector< ExtensionFunction > &ext_funcs, std::string tab="")
const std::vector< ExtArgumentType > & getSqlArgs() const
static std::vector< TableFunction > get_table_funcs(const std::string &name, const bool is_gpu)
static std::unordered_map< std::string, TableFunction > functions_
const std::vector< ExtArgumentType > input_args_
string name
Definition: setup.py:44
OutputBufferSizeType getOutputRowSizeType() const
const ExtArgumentType getRet() const
static std::string toStringSQL(const std::vector< ExtArgumentType > &sig_types)
ExtArgumentType ext_arg_type_ensure_column(const ExtArgumentType ext_arg_type)