OmniSciDB  340b00dbf6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TableFunctionsFactory.cpp
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 
18 
19 #include <boost/algorithm/string.hpp>
20 #include <mutex>
21 
22 extern bool g_enable_table_functions;
23 
24 namespace table_functions {
25 
26 namespace {
27 
29  auto generate_column_type = [](const auto& subtype) {
30  return SQLTypeInfo(kCOLUMN, 0, 0, false, kENCODING_NONE, 0, subtype);
31  };
32  switch (ext_arg_type) {
34  return SQLTypeInfo(kTINYINT, false);
36  return SQLTypeInfo(kSMALLINT, false);
38  return SQLTypeInfo(kINT, false);
40  return SQLTypeInfo(kBIGINT, false);
42  return SQLTypeInfo(kFLOAT, false);
44  return SQLTypeInfo(kDOUBLE, false);
46  return SQLTypeInfo(kBOOLEAN, false);
48  return generate_column_type(kTINYINT);
50  return generate_column_type(kSMALLINT);
52  return generate_column_type(kINT);
54  return generate_column_type(kBIGINT);
56  return generate_column_type(kFLOAT);
58  return generate_column_type(kDOUBLE);
60  return generate_column_type(kBOOLEAN);
61  default:
62  LOG(WARNING) << "ext_arg_pointer_type_to_type_info: ExtArgumentType `"
64  << "` conversion to SQLTypeInfo not implemented.";
65  UNREACHABLE();
66  }
67  UNREACHABLE();
68  return SQLTypeInfo(kNULLT, false);
69 }
70 
72  switch (ext_arg_type) {
76  return SQLTypeInfo(kTINYINT, false);
80  return SQLTypeInfo(kSMALLINT, false);
84  return SQLTypeInfo(kINT, false);
88  return SQLTypeInfo(kBIGINT, false);
92  return SQLTypeInfo(kFLOAT, false);
96  return SQLTypeInfo(kDOUBLE, false);
100  return SQLTypeInfo(kBOOLEAN, false);
101  default:
102  LOG(WARNING) << "ext_arg_pointer_type_to_type_info: ExtArgumentType `"
104  << "` conversion to SQLTypeInfo not implemented.";
105  UNREACHABLE();
106  }
107  UNREACHABLE();
108  return SQLTypeInfo(kNULLT, false);
109 }
110 
111 } // namespace
112 
114  CHECK_LT(idx, input_args_.size());
116 }
117 
119  CHECK_LT(idx, output_args_.size());
120  // TODO(adb): conditionally handle nulls
122 }
123 
124 void TableFunctionsFactory::add(const std::string& name,
125  const TableFunctionOutputRowSizer sizer,
126  const std::vector<ExtArgumentType>& input_args,
127  const std::vector<ExtArgumentType>& output_args,
128  bool is_runtime) {
129  for (auto it = functions_.begin(); it != functions_.end();) {
130  if (it->second.getName() == name) {
131  if (it->second.isRuntime()) {
132  VLOG(1) << "Overriding existing run-time table function (reset not called?): "
133  << name;
134  it = functions_.erase(it);
135  } else {
136  throw std::runtime_error("Will not override existing load-time table function: " +
137  name);
138  }
139  } else {
140  ++it;
141  }
142  }
143  auto tf = TableFunction(name, sizer, input_args, output_args, is_runtime);
144  functions_.emplace(name, tf);
145 }
146 
147 std::once_flag init_flag;
148 
151  return;
152  }
153  std::call_once(init_flag, []() {
155  "row_copier",
157  std::vector<ExtArgumentType>{ExtArgumentType::ColumnDouble,
159  std::vector<ExtArgumentType>{ExtArgumentType::Double});
161  "row_adder",
163  std::vector<ExtArgumentType>{ExtArgumentType::Int32,
165  ExtArgumentType::ColumnDouble},
166  std::vector<ExtArgumentType>{ExtArgumentType::Double});
168  "row_addsub",
170  std::vector<ExtArgumentType>{ExtArgumentType::Int32,
172  ExtArgumentType::ColumnDouble},
173  std::vector<ExtArgumentType>{ExtArgumentType::Double, ExtArgumentType::Double});
175  "get_max_with_row_offset",
177  std::vector<ExtArgumentType>{ExtArgumentType::ColumnInt32},
178  // ExtArgumentType::Int32},
179  std::vector<ExtArgumentType>{ExtArgumentType::Int32, ExtArgumentType::Int32});
180  });
181 }
182 
183 // removes existing runtime table functions
186  return;
187  }
188  for (auto it = functions_.begin(); it != functions_.end();) {
189  if (it->second.isRuntime()) {
190  it = functions_.erase(it);
191  } else {
192  ++it;
193  }
194  }
195 }
196 
197 namespace {
198 
199 std::string drop_suffix(const std::string& str) {
200  const auto idx = str.find("__");
201  if (idx == std::string::npos) {
202  return str;
203  }
204  CHECK_GT(idx, std::string::size_type(0));
205  return str.substr(0, idx);
206 }
207 
208 } // namespace
209 
210 std::vector<TableFunction> TableFunctionsFactory::get_table_funcs(const std::string& name,
211  const bool is_gpu) {
212  std::vector<TableFunction> table_funcs;
213  auto table_func_name = name;
214  boost::algorithm::to_lower(table_func_name);
215  for (const auto& pair : functions_) {
216  auto fname = drop_suffix(pair.first);
217  if (fname == table_func_name &&
218  (is_gpu ? pair.second.isGPU() : pair.second.isCPU())) {
219  table_funcs.push_back(pair.second);
220  }
221  }
222  return table_funcs;
223 }
224 
225 std::unordered_map<std::string, TableFunction> TableFunctionsFactory::functions_;
226 
227 } // namespace table_functions
SQLTypeInfo getOutputSQLType(const size_t idx) const
std::string to_lower(const std::string &str)
#define LOG(tag)
Definition: Logger.h:188
SQLTypeInfo ext_arg_pointer_type_to_type_info(const ExtArgumentType ext_arg_type)
#define UNREACHABLE()
Definition: Logger.h:241
SQLTypeInfo ext_arg_type_to_type_info_output(const ExtArgumentType ext_arg_type)
const std::vector< ExtArgumentType > output_args_
#define CHECK_GT(x, y)
Definition: Logger.h:209
SQLTypeInfo getInputSQLType(const size_t idx) const
#define CHECK_LT(x, y)
Definition: Logger.h:207
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_
Definition: sqltypes.h:47
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
#define VLOG(n)
Definition: Logger.h:291
bool g_enable_table_functions
Definition: Execute.cpp:98