OmniSciDB  340b00dbf6
 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  bool is_runtime)
142  : name_(name)
143  , output_sizer_(output_sizer)
144  , input_args_(input_args)
145  , output_args_(output_args)
146  , is_runtime_(is_runtime) {}
147 
148  std::vector<ExtArgumentType> getArgs(const bool ensure_column = false) const {
149  std::vector<ExtArgumentType> args;
150  args.insert(args.end(), input_args_.begin(), input_args_.end());
151  if (ensure_column) {
152  // map row dtype to column type
153  std::for_each(output_args_.begin(), output_args_.end(), [&args](auto t) {
154  args.push_back(ext_arg_type_ensure_column(t));
155  });
156  } else {
157  args.insert(args.end(), output_args_.begin(), output_args_.end());
158  }
159  return args;
160  }
161  const std::vector<ExtArgumentType>& getInputArgs() const { return input_args_; }
163 
164  SQLTypeInfo getInputSQLType(const size_t idx) const;
165  SQLTypeInfo getOutputSQLType(const size_t idx) const;
166 
167  auto getInputsSize() const { return input_args_.size(); }
168  auto getOutputsSize() const { return output_args_.size(); }
169 
170  auto getName() const { return name_; }
171 
174  }
175 
178  }
179 
182  }
183 
185 
186  size_t getOutputRowSizeParameter() const { return output_sizer_.val; }
187 
188  bool isRuntime() const { return is_runtime_; }
189 
190  bool isGPU() const { return (name_.find("_cpu_") == std::string::npos); }
191 
192  bool isCPU() const { return (name_.find("_gpu_") == std::string::npos); }
193 
194  std::string toString() const {
195  auto result = "TableFunction(" + name_ + ", [";
197  result += "], [";
199  result += "], is_runtime=" + std::string((is_runtime_ ? "true" : "false"));
200  result += ", sizer=" + ::toString(output_sizer_);
201  result += ")";
202  return result;
203  }
204 
205  std::string toStringSQL() const {
206  auto result = name_ + "(";
208  result += ") -> (";
210  result += ")";
211  return result;
212  }
213 
214  private:
215  const std::string name_;
217  const std::vector<ExtArgumentType> input_args_;
218  const std::vector<ExtArgumentType> output_args_;
219  const bool is_runtime_;
220 };
221 
223  public:
224  static void add(const std::string& name,
225  const TableFunctionOutputRowSizer sizer,
226  const std::vector<ExtArgumentType>& input_args,
227  const std::vector<ExtArgumentType>& output_args,
228  bool is_runtime = false);
229 
230  static std::vector<TableFunction> get_table_funcs(const std::string& name,
231  const bool is_gpu);
232  static void init();
233  static void reset();
234 
235  private:
236  static std::unordered_map<std::string, TableFunction> functions_;
237 
238  friend class ::ExtensionFunctionsWhitelist;
239 };
240 
241 } // namespace table_functions
SQLTypeInfo getOutputSQLType(const size_t idx) const
TableFunction(const std::string &name, const TableFunctionOutputRowSizer output_sizer, const std::vector< ExtArgumentType > &input_args, const std::vector< ExtArgumentType > &output_args, bool is_runtime)
const TableFunctionOutputRowSizer output_sizer_
const std::vector< ExtArgumentType > output_args_
std::string to_string(char const *&&v)
SQLTypeInfo getInputSQLType(const size_t idx) const
std::vector< ExtArgumentType > getArgs(const bool ensure_column=false) const
const std::vector< ExtArgumentType > & getInputArgs() const
static std::string toString(const std::vector< ExtensionFunction > &ext_funcs, std::string tab="")
static std::vector< TableFunction > get_table_funcs(const std::string &name, const bool is_gpu)
static std::unordered_map< std::string, TableFunction > functions_
static void add(const std::string &name, const TableFunctionOutputRowSizer sizer, const std::vector< ExtArgumentType > &input_args, const std::vector< ExtArgumentType > &output_args, bool is_runtime=false)
const std::vector< ExtArgumentType > input_args_
string name
Definition: setup.py:35
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)