OmniSciDB  04ee39c94c
ExtensionFunctionsBinding.cpp File Reference
#include "ExtensionFunctionsBinding.h"
#include <algorithm>
+ Include dependency graph for ExtensionFunctionsBinding.cpp:

Go to the source code of this file.

Namespaces

 anonymous_namespace{ExtensionFunctionsBinding.cpp}
 

Functions

static int anonymous_namespace{ExtensionFunctionsBinding.cpp}::match_arguments (const SQLTypeInfo &arg_type, int sig_pos, const std::vector< ExtArgumentType > &sig_types, int &penalty_score)
 
ExtensionFunction bind_function (std::string name, Analyzer::ExpressionPtrVector func_args, const std::vector< ExtensionFunction > &ext_funcs)
 
ExtensionFunction bind_function (std::string name, Analyzer::ExpressionPtrVector func_args)
 
ExtensionFunction bind_function (const Analyzer::FunctionOper *function_oper)
 

Function Documentation

◆ bind_function() [1/3]

ExtensionFunction bind_function ( std::string  name,
Analyzer::ExpressionPtrVector  func_args,
const std::vector< ExtensionFunction > &  ext_funcs 
)

Definition at line 231 of file ExtensionFunctionsBinding.cpp.

References ext_arg_type_to_type_info(), anonymous_namespace{ExtensionFunctionsBinding.cpp}::match_arguments(), and ExtensionFunctionsWhitelist::toString().

Referenced by bind_function(), CodeGenerator::codegenFunctionOper(), and RelAlgTranslator::translateFunction().

233  {
234  // worker function
235  /*
236  Return extension function that has the following properties
237 
238  1. each argument type in `arg_types` matches with extension
239  function argument types.
240 
241  For scalar types, the matching means that the types are either
242  equal or the argument type is smaller than the corresponding
243  the extension function argument type. This ensures that no
244  information is lost when casting of argument values is
245  required.
246 
247  For array and geo types, the matching means that the argument
248  type matches exactly with a group of extension function
249  argument types. See `match_arguments`.
250 
251  2. has minimal penalty score among all implementations of the
252  extension function with given `name`, see `get_penalty_score`
253  for the definition of penalty score.
254 
255  It is assumed that function_oper and extension functions in
256  ext_funcs have the same name.
257  */
258  int minimal_score = std::numeric_limits<int>::max();
259  int index = -1;
260  int optimal = -1;
261  for (auto ext_func : ext_funcs) {
262  index++;
263  auto ext_func_args = ext_func.getArgs();
264  /* In general, `arg_types.size() <= ext_func_args.size()` because
265  non-scalar arguments (such as arrays and geo-objects) are
266  mapped to multiple `ext_func` arguments. */
267  if (func_args.size() <= ext_func_args.size()) {
268  /* argument type must fit into the corresponding signature
269  argument type, reject signature if not */
270  int penalty_score = 0;
271  int pos = 0;
272  for (auto atype : func_args) {
273  int offset =
274  match_arguments(atype->get_type_info(), pos, ext_func_args, penalty_score);
275  if (offset < 0) {
276  // atype does not match with ext_func argument
277  pos = -1;
278  break;
279  }
280  pos += offset;
281  }
282  if (pos >= 0) {
283  // prefer smaller return types
284  penalty_score += ext_arg_type_to_type_info(ext_func.getRet()).get_logical_size();
285  if (penalty_score < minimal_score) {
286  optimal = index;
287  minimal_score = penalty_score;
288  }
289  }
290  }
291  }
292 
293  if (optimal == -1) {
294  /* no extension function found that argument types would match
295  with types in `arg_types` */
296  std::vector<SQLTypeInfo> arg_types;
297  for (size_t i = 0; i < func_args.size(); ++i) {
298  arg_types.push_back(func_args[i]->get_type_info());
299  }
300  auto sarg_types = ExtensionFunctionsWhitelist::toString(arg_types);
301  if (!ext_funcs.size()) {
302  throw std::runtime_error("Function " + name + "(" + sarg_types +
303  ") not supported.");
304  }
305  auto choices = ExtensionFunctionsWhitelist::toString(ext_funcs, " ");
306  throw std::runtime_error(
307  "Function " + name + "(" + sarg_types +
308  ") not supported.\n Existing extension function implementations:\n" + choices);
309  }
310  return ext_funcs[optimal];
311 }
static std::string toString(const std::vector< ExtensionFunction > &ext_funcs, std::string tab="")
static int match_arguments(const SQLTypeInfo &arg_type, int sig_pos, const std::vector< ExtArgumentType > &sig_types, int &penalty_score)
SQLTypeInfo ext_arg_type_to_type_info(const ExtArgumentType ext_arg_type)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ bind_function() [2/3]

ExtensionFunction bind_function ( std::string  name,
Analyzer::ExpressionPtrVector  func_args 
)

Definition at line 313 of file ExtensionFunctionsBinding.cpp.

References bind_function(), and ExtensionFunctionsWhitelist::get_ext_funcs().

314  {
315  // used in RelAlgTranslator.cpp
316  std::vector<ExtensionFunction> ext_funcs =
318  return bind_function(name, func_args, ext_funcs);
319 }
static std::vector< ExtensionFunction > get_ext_funcs(const std::string &name)
ExtensionFunction bind_function(std::string name, Analyzer::ExpressionPtrVector func_args, const std::vector< ExtensionFunction > &ext_funcs)
+ Here is the call graph for this function:

◆ bind_function() [3/3]

ExtensionFunction bind_function ( const Analyzer::FunctionOper function_oper)

Definition at line 321 of file ExtensionFunctionsBinding.cpp.

References bind_function(), Analyzer::FunctionOper::getArity(), Analyzer::FunctionOper::getName(), and Analyzer::FunctionOper::getOwnArg().

321  {
322  // used in ExtensionIR.cpp
323  auto name = function_oper->getName();
324  Analyzer::ExpressionPtrVector func_args = {};
325  for (size_t i = 0; i < function_oper->getArity(); ++i) {
326  func_args.push_back(function_oper->getOwnArg(i));
327  }
328  return bind_function(name, func_args);
329 }
std::shared_ptr< Analyzer::Expr > getOwnArg(const size_t i) const
Definition: Analyzer.h:1259
std::string getName() const
Definition: Analyzer.h:1250
ExtensionFunction bind_function(std::string name, Analyzer::ExpressionPtrVector func_args, const std::vector< ExtensionFunction > &ext_funcs)
size_t getArity() const
Definition: Analyzer.h:1252
std::vector< ExpressionPtr > ExpressionPtrVector
Definition: Analyzer.h:181
+ Here is the call graph for this function: