OmniSciDB  a575cb28ea
 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,
126  const std::vector<ExtArgumentType>& input_args,
127  const std::vector<ExtArgumentType>& output_args,
128  const std::vector<ExtArgumentType>& sql_args,
129  bool is_runtime) {
130  for (auto it = functions_.begin(); it != functions_.end();) {
131  if (it->second.getName() == name) {
132  if (it->second.isRuntime()) {
133  VLOG(1) << "Overriding existing run-time table function (reset not called?): "
134  << name;
135  it = functions_.erase(it);
136  } else {
137  throw std::runtime_error("Will not override existing load-time table function: " +
138  name);
139  }
140  } else {
141  ++it;
142  }
143  }
144  auto tf = TableFunction(name, sizer, input_args, output_args, sql_args, is_runtime);
145  functions_.emplace(name, tf);
146 }
147 
148 /*
149  The implementation for `void TableFunctionsFactory::init()` is
150  generated by QueryEngine/scripts/generate_TableFunctionsFactory_init.py
151 */
152 
153 // removes existing runtime table functions
156  return;
157  }
158  for (auto it = functions_.begin(); it != functions_.end();) {
159  if (it->second.isRuntime()) {
160  it = functions_.erase(it);
161  } else {
162  ++it;
163  }
164  }
165 }
166 
167 namespace {
168 
169 std::string drop_suffix(const std::string& str) {
170  const auto idx = str.find("__");
171  if (idx == std::string::npos) {
172  return str;
173  }
174  CHECK_GT(idx, std::string::size_type(0));
175  return str.substr(0, idx);
176 }
177 
178 } // namespace
179 
180 std::vector<TableFunction> TableFunctionsFactory::get_table_funcs(const std::string& name,
181  const bool is_gpu) {
182  std::vector<TableFunction> table_funcs;
183  auto table_func_name = name;
184  boost::algorithm::to_lower(table_func_name);
185  for (const auto& pair : functions_) {
186  auto fname = drop_suffix(pair.first);
187  if (fname == table_func_name &&
188  (is_gpu ? pair.second.isGPU() : pair.second.isCPU())) {
189  table_funcs.push_back(pair.second);
190  }
191  }
192  return table_funcs;
193 }
194 
195 std::vector<TableFunction> TableFunctionsFactory::get_table_funcs(const bool is_runtime) {
196  std::vector<TableFunction> table_funcs;
197  for (const auto& pair : functions_) {
198  if (pair.second.isRuntime() == is_runtime) {
199  table_funcs.push_back(pair.second);
200  }
201  }
202  return table_funcs;
203 }
204 
205 std::unordered_map<std::string, TableFunction> TableFunctionsFactory::functions_;
206 
207 } // 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 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)
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:44
const std::vector< ExtArgumentType > input_args_
string name
Definition: setup.py:44
#define VLOG(n)
Definition: Logger.h:291
bool g_enable_table_functions
Definition: Execute.cpp:102