OmniSciDB  95562058bd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ExtensionFunctionsBinding.cpp File Reference
#include "ExtensionFunctionsBinding.h"
#include <algorithm>
#include "ExternalExecutor.h"
+ Include dependency graph for ExtensionFunctionsBinding.cpp:

Go to the source code of this file.

Namespaces

 anonymous_namespace{ExtensionFunctionsBinding.cpp}
 

Functions

ExtArgumentType anonymous_namespace{ExtensionFunctionsBinding.cpp}::get_column_arg_elem_type (const ExtArgumentType ext_arg_column_type)
 
ExtArgumentType anonymous_namespace{ExtensionFunctionsBinding.cpp}::get_array_arg_elem_type (const ExtArgumentType ext_arg_array_type)
 
static int anonymous_namespace{ExtensionFunctionsBinding.cpp}::match_arguments (const SQLTypeInfo &arg_type, int sig_pos, const std::vector< ExtArgumentType > &sig_types, int &penalty_score)
 
template<typename T >
bind_function (std::string name, Analyzer::ExpressionPtrVector func_args, const std::vector< T > &ext_funcs)
 
const
table_functions::TableFunction 
bind_table_function (std::string name, Analyzer::ExpressionPtrVector input_args, const std::vector< table_functions::TableFunction > &table_funcs)
 
ExtensionFunction bind_function (std::string name, Analyzer::ExpressionPtrVector func_args)
 
ExtensionFunction bind_function (const Analyzer::FunctionOper *function_oper)
 
const
table_functions::TableFunction 
bind_table_function (std::string name, Analyzer::ExpressionPtrVector input_args)
 
bool is_ext_arg_type_array (const ExtArgumentType ext_arg_type)
 
bool is_ext_arg_type_column (const ExtArgumentType ext_arg_type)
 
bool is_ext_arg_type_geo (const ExtArgumentType ext_arg_type)
 

Function Documentation

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

Definition at line 333 of file ExtensionFunctionsBinding.cpp.

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

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

335  {
336  /* worker function
337 
338  Template type T must implement the following methods:
339 
340  std::vector<ExtArgumentType> getInputArgs()
341  */
342  /*
343  Return extension function/table function that has the following
344  properties
345 
346  1. each argument type in `arg_types` matches with extension
347  function argument types.
348 
349  For scalar types, the matching means that the types are either
350  equal or the argument type is smaller than the corresponding
351  the extension function argument type. This ensures that no
352  information is lost when casting of argument values is
353  required.
354 
355  For array and geo types, the matching means that the argument
356  type matches exactly with a group of extension function
357  argument types. See `match_arguments`.
358 
359  2. has minimal penalty score among all implementations of the
360  extension function with given `name`, see `get_penalty_score`
361  for the definition of penalty score.
362 
363  It is assumed that function_oper and extension functions in
364  ext_funcs have the same name.
365  */
366  int minimal_score = std::numeric_limits<int>::max();
367  int index = -1;
368  int optimal = -1;
369 
370  std::vector<SQLTypeInfo> type_infos;
371  for (auto atype : func_args) {
372  if constexpr (std::is_same_v<T, table_functions::TableFunction>) {
373  if (dynamic_cast<const Analyzer::ColumnVar*>(atype.get())) {
374  auto ti = SQLTypeInfo(kCOLUMN, false);
375  ti.set_subtype(atype->get_type_info().get_type());
376  type_infos.push_back(ti);
377  continue;
378  }
379  }
380  type_infos.push_back(atype->get_type_info());
381  }
382  for (auto ext_func : ext_funcs) {
383  index++;
384  auto ext_func_args = ext_func.getInputArgs();
385  /* In general, `arg_types.size() <= ext_func_args.size()` because
386  non-scalar arguments (such as arrays and geo-objects) are
387  mapped to multiple `ext_func` arguments. */
388  if (func_args.size() <= ext_func_args.size()) {
389  /* argument type must fit into the corresponding signature
390  argument type, reject signature if not */
391  int penalty_score = 0;
392  int pos = 0;
393  for (auto ti : type_infos) {
394  int offset = match_arguments(ti, pos, ext_func_args, penalty_score);
395  if (offset < 0) {
396  // atype does not match with ext_func argument
397  pos = -1;
398  break;
399  }
400  pos += offset;
401  }
402  if (pos >= 0) {
403  // prefer smaller return types
404  penalty_score += ext_arg_type_to_type_info(ext_func.getRet()).get_logical_size();
405  if (penalty_score < minimal_score) {
406  optimal = index;
407  minimal_score = penalty_score;
408  }
409  }
410  }
411  }
412 
413  if (optimal == -1) {
414  /* no extension function found that argument types would match
415  with types in `arg_types` */
416  auto sarg_types = ExtensionFunctionsWhitelist::toString(type_infos);
417  if (!ext_funcs.size()) {
418  throw NativeExecutionError("Function " + name + "(" + sarg_types +
419  ") not supported.");
420  }
421  std::string choices;
422  for (const auto& ext_func : ext_funcs) {
423  choices += "\n " + ext_func.toStringSQL();
424  }
425  throw std::runtime_error(
426  "Function " + name + "(" + sarg_types +
427  ") not supported.\n Existing extension function implementations:" + choices);
428  }
429  return ext_funcs[optimal];
430 }
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)
string name
Definition: setup.py:35
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:

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

Definition at line 439 of file ExtensionFunctionsBinding.cpp.

References ExtensionFunctionsWhitelist::get_ext_funcs(), and setup::name.

440  {
441  // used in RelAlgTranslator.cpp
442  std::vector<ExtensionFunction> ext_funcs =
444  return bind_function<ExtensionFunction>(name, func_args, ext_funcs);
445 }
static std::vector< ExtensionFunction > get_ext_funcs(const std::string &name)
string name
Definition: setup.py:35

+ Here is the call graph for this function:

ExtensionFunction bind_function ( const Analyzer::FunctionOper function_oper)

Definition at line 447 of file ExtensionFunctionsBinding.cpp.

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

447  {
448  // used in ExtensionsIR.cpp
449  auto name = function_oper->getName();
450  Analyzer::ExpressionPtrVector func_args = {};
451  for (size_t i = 0; i < function_oper->getArity(); ++i) {
452  func_args.push_back(function_oper->getOwnArg(i));
453  }
454  return bind_function(name, func_args);
455 }
size_t getArity() const
Definition: Analyzer.h:1360
T bind_function(std::string name, Analyzer::ExpressionPtrVector func_args, const std::vector< T > &ext_funcs)
std::shared_ptr< Analyzer::Expr > getOwnArg(const size_t i) const
Definition: Analyzer.h:1367
std::vector< ExpressionPtr > ExpressionPtrVector
Definition: Analyzer.h:182
std::string getName() const
Definition: Analyzer.h:1358
string name
Definition: setup.py:35

+ Here is the call graph for this function:

const table_functions::TableFunction bind_table_function ( std::string  name,
Analyzer::ExpressionPtrVector  input_args,
const std::vector< table_functions::TableFunction > &  table_funcs 
)

Definition at line 432 of file ExtensionFunctionsBinding.cpp.

References setup::name.

Referenced by bind_table_function(), and RelAlgExecutor::createTableFunctionWorkUnit().

435  {
436  return bind_function<table_functions::TableFunction>(name, input_args, table_funcs);
437 }
string name
Definition: setup.py:35

+ Here is the caller graph for this function:

const table_functions::TableFunction bind_table_function ( std::string  name,
Analyzer::ExpressionPtrVector  input_args 
)

Definition at line 457 of file ExtensionFunctionsBinding.cpp.

References bind_table_function(), and table_functions::TableFunctionsFactory::get_table_funcs().

459  {
460  // used in RelAlgExecutor.cpp
461  std::vector<table_functions::TableFunction> table_funcs =
463  return bind_table_function(name, input_args, table_funcs);
464 }
static std::vector< TableFunction > get_table_funcs(const std::string &name)
const table_functions::TableFunction bind_table_function(std::string name, Analyzer::ExpressionPtrVector input_args, const std::vector< table_functions::TableFunction > &table_funcs)
string name
Definition: setup.py:35

+ Here is the call graph for this function:

bool is_ext_arg_type_array ( const ExtArgumentType  ext_arg_type)

Definition at line 466 of file ExtensionFunctionsBinding.cpp.

References ArrayBool, ArrayDouble, ArrayFloat, ArrayInt16, ArrayInt32, ArrayInt64, and ArrayInt8.

Referenced by CodeGenerator::codegenFunctionOperCastArgs(), ExtensionFunctionsWhitelist::getLLVMDeclarations(), and anonymous_namespace{ExtensionFunctionsBinding.cpp}::match_arguments().

+ Here is the caller graph for this function:

bool is_ext_arg_type_column ( const ExtArgumentType  ext_arg_type)

Definition at line 482 of file ExtensionFunctionsBinding.cpp.

References ColumnBool, ColumnDouble, ColumnFloat, ColumnInt16, ColumnInt32, ColumnInt64, and ColumnInt8.

Referenced by anonymous_namespace{ExtensionFunctionsBinding.cpp}::match_arguments().

+ Here is the caller graph for this function:

bool is_ext_arg_type_geo ( const ExtArgumentType  ext_arg_type)

Definition at line 498 of file ExtensionFunctionsBinding.cpp.

References GeoLineString, GeoMultiPolygon, GeoPoint, and GeoPolygon.

Referenced by CodeGenerator::codegenFunctionOperCastArgs().

498  {
499  switch (ext_arg_type) {
504  return true;
505 
506  default:
507  return false;
508  }
509 }

+ Here is the caller graph for this function: