OmniSciDB  a987f07e93
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RelAlgTranslator Class Reference

#include <RelAlgTranslator.h>

+ Collaboration diagram for RelAlgTranslator:

Public Member Functions

 RelAlgTranslator (const Catalog_Namespace::Catalog &cat, std::shared_ptr< const query_state::QueryState > q_s, const Executor *executor, const std::unordered_map< const RelAlgNode *, int > &input_to_nest_level, const std::vector< JoinType > &join_types, const time_t now, const bool just_explain)
 
std::shared_ptr< Analyzer::Exprtranslate (const RexScalar *rex) const
 
bool generated_geos_ops ()
 
template<typename T >
std::shared_ptr< Analyzer::ExprtranslateRexScalar (RexScalar const *) const
 
template<>
std::shared_ptr< Analyzer::ExprtranslateRexScalar (RexScalar const *rex) const
 
template<>
std::shared_ptr< Analyzer::ExprtranslateRexScalar (RexScalar const *rex) const
 
template<>
std::shared_ptr< Analyzer::ExprtranslateRexScalar (RexScalar const *rex) const
 
template<>
std::shared_ptr< Analyzer::ExprtranslateRexScalar (RexScalar const *rex) const
 
template<>
std::shared_ptr< Analyzer::ExprtranslateRexScalar (RexScalar const *rex) const
 
template<>
std::shared_ptr< Analyzer::ExprtranslateRexScalar (RexScalar const *rex) const
 
template<>
std::shared_ptr< Analyzer::ExprtranslateRexScalar (RexScalar const *rex) const
 

Static Public Member Functions

static std::shared_ptr
< Analyzer::Expr
translateAggregateRex (const RexAgg *rex, const std::vector< std::shared_ptr< Analyzer::Expr >> &scalar_sources)
 
static std::shared_ptr
< Analyzer::Expr
translateLiteral (const RexLiteral *)
 

Private Member Functions

std::shared_ptr< Analyzer::ExprtranslateScalarRex (const RexScalar *rex) const
 
std::shared_ptr< Analyzer::ExprtranslateScalarSubquery (const RexSubQuery *) const
 
std::shared_ptr< Analyzer::ExprtranslateInput (const RexInput *) const
 
std::shared_ptr< Analyzer::ExprtranslateUoper (const RexOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateInOper (const RexOperator *) const
 
std::shared_ptr< Analyzer::ExprgetInIntegerSetExpr (std::shared_ptr< Analyzer::Expr > arg, const ResultSet &val_set) const
 
std::shared_ptr< Analyzer::ExprtranslateOper (const RexOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateOverlapsOper (const RexOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateCase (const RexCase *) const
 
std::shared_ptr< Analyzer::ExprtranslateWidthBucket (const RexFunctionOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateLike (const RexFunctionOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateRegexp (const RexFunctionOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateLikely (const RexFunctionOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateUnlikely (const RexFunctionOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateExtract (const RexFunctionOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateDateadd (const RexFunctionOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateDatePlusMinus (const RexOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateDatediff (const RexFunctionOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateDatepart (const RexFunctionOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateLength (const RexFunctionOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateKeyForString (const RexFunctionOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateSampleRatio (const RexFunctionOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateCurrentUser (const RexFunctionOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateStringOper (const RexFunctionOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateCardinality (const RexFunctionOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateItem (const RexFunctionOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateCurrentDate () const
 
std::shared_ptr< Analyzer::ExprtranslateCurrentTime () const
 
std::shared_ptr< Analyzer::ExprtranslateCurrentTimestamp () const
 
std::shared_ptr< Analyzer::ExprtranslateDatetime (const RexFunctionOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateHPTLiteral (const RexFunctionOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateAbs (const RexFunctionOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateSign (const RexFunctionOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateOffsetInFragment () const
 
std::shared_ptr< Analyzer::ExprtranslateArrayFunction (const RexFunctionOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateFunction (const RexFunctionOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateWindowFunction (const RexWindowFunctionOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateIntervalExprForWindowFraming (std::shared_ptr< Analyzer::Expr > order_key, bool for_preceding_bound, const Analyzer::BinOper *frame_bound_expr) const
 
Analyzer::ExpressionPtrVector translateFunctionArgs (const RexFunctionOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateUnaryGeoFunction (const RexFunctionOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateBinaryGeoFunction (const RexFunctionOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateTernaryGeoFunction (const RexFunctionOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateFunctionWithGeoArg (const RexFunctionOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateGeoComparison (const RexOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateGeoProjection (const RexFunctionOperator *, SQLTypeInfo &, const bool with_bounds) const
 
std::shared_ptr< Analyzer::ExprtranslateUnaryGeoPredicate (const RexFunctionOperator *, SQLTypeInfo &, const bool with_bounds) const
 
std::shared_ptr< Analyzer::ExprtranslateUnaryGeoConstructor (const RexFunctionOperator *, SQLTypeInfo &, const bool with_bounds) const
 
std::shared_ptr< Analyzer::ExprtranslateBinaryGeoPredicate (const RexFunctionOperator *, SQLTypeInfo &, const bool with_bounds) const
 
std::shared_ptr< Analyzer::ExprtranslateBinaryGeoConstructor (const RexFunctionOperator *, SQLTypeInfo &, const bool with_bounds) const
 
std::shared_ptr< Analyzer::ExprtranslateGeoOverlapsOper (const RexOperator *) const
 
std::vector< std::shared_ptr
< Analyzer::Expr > > 
translateGeoFunctionArg (const RexScalar *rex_scalar, SQLTypeInfo &arg_ti, const bool with_bounds, const bool with_render_group, const bool expand_geo_col, const bool is_projection=false, const bool use_geo_expressions=false, const bool try_to_compress=false, const bool allow_gdal_transforms=false) const
 
std::vector< std::shared_ptr
< Analyzer::Expr > > 
translateGeoColumn (const RexInput *, SQLTypeInfo &, const bool with_bounds, const bool with_render_group, const bool expand_geo_col) const
 
std::vector< std::shared_ptr
< Analyzer::Expr > > 
translateGeoLiteral (const RexLiteral *, SQLTypeInfo &, bool) const
 
std::pair< std::shared_ptr
< Analyzer::Expr >
, SQLQualifier
getQuantifiedRhs (const RexScalar *) const
 

Private Attributes

const Catalog_Namespace::Catalogcat_
 
std::shared_ptr< const
query_state::QueryState
query_state_
 
const Executorexecutor_
 
const std::unordered_map
< const RelAlgNode *, int > 
input_to_nest_level_
 
const std::vector< JoinTypejoin_types_
 
time_t now_
 
bool generated_geos_ops_
 
const bool just_explain_
 
robin_hood::unordered_map
< RexScalar const
*, std::shared_ptr
< Analyzer::Expr > > 
cache_
 

Detailed Description

Definition at line 49 of file RelAlgTranslator.h.

Constructor & Destructor Documentation

RelAlgTranslator::RelAlgTranslator ( const Catalog_Namespace::Catalog cat,
std::shared_ptr< const query_state::QueryState q_s,
const Executor executor,
const std::unordered_map< const RelAlgNode *, int > &  input_to_nest_level,
const std::vector< JoinType > &  join_types,
const time_t  now,
const bool  just_explain 
)
inline

Definition at line 51 of file RelAlgTranslator.h.

58  : cat_(cat)
59  , query_state_(q_s)
60  , executor_(executor)
61  , input_to_nest_level_(input_to_nest_level)
62  , join_types_(join_types)
63  , now_(now)
64  , generated_geos_ops_(false)
65  , just_explain_(just_explain) {}
const std::vector< JoinType > join_types_
const Executor * executor_
const std::unordered_map< const RelAlgNode *, int > input_to_nest_level_
std::shared_ptr< const query_state::QueryState > query_state_
const bool just_explain_
const Catalog_Namespace::Catalog & cat_

Member Function Documentation

bool RelAlgTranslator::generated_geos_ops ( )
inline

Definition at line 76 of file RelAlgTranslator.h.

References generated_geos_ops_.

76 { return generated_geos_ops_; }
std::shared_ptr< Analyzer::Expr > RelAlgTranslator::getInIntegerSetExpr ( std::shared_ptr< Analyzer::Expr arg,
const ResultSet val_set 
) const
private

Definition at line 896 of file RelAlgTranslator.cpp.

References threading_serial::async(), result_set::can_use_parallel_algorithms(), cat_, CHECK, CHECK_EQ, CHECK_GE, cpu_threads(), executor_, anonymous_namespace{RelAlgTranslator.cpp}::fill_dictionary_encoded_in_vals(), anonymous_namespace{RelAlgTranslator.cpp}::fill_integer_in_vals(), g_cluster, Catalog_Namespace::Catalog::getDatabaseId(), Catalog_Namespace::Catalog::getStringDictionaryHosts(), inline_int_null_val(), and kENCODING_DICT.

Referenced by translateInOper().

898  {
900  return nullptr;
901  }
902  std::vector<int64_t> value_exprs;
903  const size_t fetcher_count = cpu_threads();
904  std::vector<std::vector<int64_t>> expr_set(fetcher_count);
905  std::vector<std::future<void>> fetcher_threads;
906  const auto& arg_type = arg->get_type_info();
907  const auto entry_count = val_set.entryCount();
908  CHECK_EQ(size_t(1), val_set.colCount());
909  const auto& col_type = val_set.getColType(0);
910  if (g_cluster && arg_type.is_string() &&
911  (col_type.get_comp_param() <= 0 || arg_type.get_comp_param() <= 0)) {
912  // Skip this case for now, see comment for fill_dictionary_encoded_in_vals.
913  return nullptr;
914  }
915  std::atomic<size_t> total_in_vals_count{0};
916  for (size_t i = 0,
917  start_entry = 0,
918  stride = (entry_count + fetcher_count - 1) / fetcher_count;
919  i < fetcher_count && start_entry < entry_count;
920  ++i, start_entry += stride) {
921  expr_set[i].reserve(entry_count / fetcher_count);
922  const auto end_entry = std::min(start_entry + stride, entry_count);
923  if (arg_type.is_string()) {
924  CHECK_EQ(kENCODING_DICT, arg_type.get_compression());
925  // const int32_t dest_dict_id = arg_type.get_comp_param();
926  // const int32_t source_dict_id = col_type.get_comp_param();
927  const DictRef dest_dict_ref(arg_type.get_comp_param(), cat_.getDatabaseId());
928  const DictRef source_dict_ref(col_type.get_comp_param(), cat_.getDatabaseId());
929  const auto dd = executor_->getStringDictionaryProxy(
930  arg_type.get_comp_param(), val_set.getRowSetMemOwner(), true);
931  const auto sd = executor_->getStringDictionaryProxy(
932  col_type.get_comp_param(), val_set.getRowSetMemOwner(), true);
933  CHECK(sd);
934  const auto needle_null_val = inline_int_null_val(arg_type);
935  fetcher_threads.push_back(std::async(
937  [this,
938  &val_set,
939  &total_in_vals_count,
940  sd,
941  dd,
942  source_dict_ref,
943  dest_dict_ref,
944  needle_null_val](
945  std::vector<int64_t>& in_vals, const size_t start, const size_t end) {
946  if (g_cluster) {
947  CHECK_GE(dd->getGeneration(), 0);
949  total_in_vals_count,
950  &val_set,
951  {start, end},
953  source_dict_ref,
954  dest_dict_ref,
955  dd->getGeneration(),
956  needle_null_val);
957  } else {
959  total_in_vals_count,
960  &val_set,
961  {start, end},
962  sd,
963  dd,
964  needle_null_val);
965  }
966  },
967  std::ref(expr_set[i]),
968  start_entry,
969  end_entry));
970  } else {
971  CHECK(arg_type.is_integer());
972  fetcher_threads.push_back(std::async(
974  [&val_set, &total_in_vals_count](
975  std::vector<int64_t>& in_vals, const size_t start, const size_t end) {
976  fill_integer_in_vals(in_vals, total_in_vals_count, &val_set, {start, end});
977  },
978  std::ref(expr_set[i]),
979  start_entry,
980  end_entry));
981  }
982  }
983  for (auto& child : fetcher_threads) {
984  child.get();
985  }
986 
987  val_set.moveToBegin();
988  value_exprs.reserve(entry_count);
989  for (auto& exprs : expr_set) {
990  value_exprs.insert(value_exprs.end(), exprs.begin(), exprs.end());
991  }
992  return makeExpr<Analyzer::InIntegerSet>(
993  arg, value_exprs, arg_type.get_notnull() && col_type.get_notnull());
994 }
#define CHECK_EQ(x, y)
Definition: Logger.h:297
const Executor * executor_
#define CHECK_GE(x, y)
Definition: Logger.h:302
future< Result > async(Fn &&fn, Args &&...args)
void fill_dictionary_encoded_in_vals(std::vector< int64_t > &in_vals, std::atomic< size_t > &total_in_vals_count, const ResultSet *values_rowset, const std::pair< int64_t, int64_t > values_rowset_slice, const StringDictionaryProxy *source_dict, const StringDictionaryProxy *dest_dict, const int64_t needle_null_val)
int getDatabaseId() const
Definition: Catalog.h:304
const std::vector< LeafHostInfo > & getStringDictionaryHosts() const
Definition: Catalog.cpp:2022
#define CHECK(condition)
Definition: Logger.h:289
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)
bool g_cluster
int cpu_threads()
Definition: thread_count.h:25
bool can_use_parallel_algorithms(const ResultSet &rows)
Definition: ResultSet.cpp:1561
void fill_integer_in_vals(std::vector< int64_t > &in_vals, std::atomic< size_t > &total_in_vals_count, const ResultSet *values_rowset, const std::pair< int64_t, int64_t > values_rowset_slice)
const Catalog_Namespace::Catalog & cat_

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::pair< std::shared_ptr< Analyzer::Expr >, SQLQualifier > RelAlgTranslator::getQuantifiedRhs ( const RexScalar rex_scalar) const
private

Definition at line 58 of file RelAlgTranslator.cpp.

References CHECK_EQ, RexFunctionOperator::getName(), kALL, kANY, kCAST, kONE, and translateScalarRex().

Referenced by translateOper().

58  {
59  std::shared_ptr<Analyzer::Expr> rhs;
60  SQLQualifier sql_qual{kONE};
61  const auto rex_operator = dynamic_cast<const RexOperator*>(rex_scalar);
62  if (!rex_operator) {
63  return std::make_pair(rhs, sql_qual);
64  }
65  const auto rex_function = dynamic_cast<const RexFunctionOperator*>(rex_operator);
66  const auto qual_str = rex_function ? rex_function->getName() : "";
67  if (qual_str == "PG_ANY"sv || qual_str == "PG_ALL"sv) {
68  CHECK_EQ(size_t(1), rex_function->size());
69  rhs = translateScalarRex(rex_function->getOperand(0));
70  sql_qual = (qual_str == "PG_ANY"sv) ? kANY : kALL;
71  }
72  if (!rhs && rex_operator->getOperator() == kCAST) {
73  CHECK_EQ(size_t(1), rex_operator->size());
74  std::tie(rhs, sql_qual) = getQuantifiedRhs(rex_operator->getOperand(0));
75  }
76  return std::make_pair(rhs, sql_qual);
77 }
Definition: sqldefs.h:71
#define CHECK_EQ(x, y)
Definition: Logger.h:297
SQLQualifier
Definition: sqldefs.h:71
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
Definition: sqldefs.h:48
std::pair< std::shared_ptr< Analyzer::Expr >, SQLQualifier > getQuantifiedRhs(const RexScalar *) const
Definition: sqldefs.h:71
Definition: sqldefs.h:71
const std::string & getName() const
Definition: RelAlgDag.h:500

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translate ( const RexScalar rex) const

Definition at line 259 of file RelAlgTranslator.cpp.

References cache_, and translateScalarRex().

Referenced by anonymous_namespace{RelAlgExecutor.cpp}::get_inputs_meta(), RelAlgExecutor::makeJoinQuals(), anonymous_namespace{RelAlgExecutor.cpp}::translate_quals(), anonymous_namespace{RelAlgExecutor.cpp}::translate_scalar_sources(), anonymous_namespace{RelAlgExecutor.cpp}::translate_scalar_sources_for_update(), and anonymous_namespace{RelAlgExecutor.cpp}::translate_targets().

259  {
260  ScopeGuard clear_cache{[this] { cache_.clear(); }};
261  return translateScalarRex(rex);
262 }
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
robin_hood::unordered_map< RexScalar const *, std::shared_ptr< Analyzer::Expr > > cache_

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateAbs ( const RexFunctionOperator rex_function) const
private

Definition at line 1544 of file RelAlgTranslator.cpp.

References CHECK, CHECK_EQ, RexOperator::getOperand(), kBOOLEAN, kLT, kONE, kUMINUS, anonymous_namespace{RelAlgTranslator.cpp}::makeNumericConstant(), RexOperator::size(), and translateScalarRex().

Referenced by translateFunction().

1545  {
1546  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
1547  expr_list;
1548  CHECK_EQ(size_t(1), rex_function->size());
1549  const auto operand = translateScalarRex(rex_function->getOperand(0));
1550  const auto& operand_ti = operand->get_type_info();
1551  CHECK(operand_ti.is_number());
1552  const auto zero = makeNumericConstant(operand_ti, 0);
1553  const auto lt_zero = makeExpr<Analyzer::BinOper>(kBOOLEAN, kLT, kONE, operand, zero);
1554  const auto uminus_operand =
1555  makeExpr<Analyzer::UOper>(operand_ti.get_type(), kUMINUS, operand);
1556  expr_list.emplace_back(lt_zero, uminus_operand);
1557  return makeExpr<Analyzer::CaseExpr>(operand_ti, false, expr_list, operand);
1558 }
#define CHECK_EQ(x, y)
Definition: Logger.h:297
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
size_t size() const
Definition: RelAlgDag.h:270
const RexScalar * getOperand(const size_t idx) const
Definition: RelAlgDag.h:272
std::shared_ptr< Analyzer::Constant > makeNumericConstant(const SQLTypeInfo &ti, const long val)
Definition: sqldefs.h:71
#define CHECK(condition)
Definition: Logger.h:289
Definition: sqldefs.h:32

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateAggregateRex ( const RexAgg rex,
const std::vector< std::shared_ptr< Analyzer::Expr >> &  scalar_sources 
)
static

Definition at line 277 of file RelAlgTranslator.cpp.

References CHECK_LE, CHECK_LT, Datum::doubleval, g_cluster, get_agg_type(), RexAgg::getKind(), RexAgg::getOperand(), anonymous_namespace{RelAlgTranslator.cpp}::is_agg_supported_for_type(), anonymous_namespace{RelAlgOptimizer.cpp}::is_distinct(), RexAgg::isDistinct(), kAPPROX_COUNT_DISTINCT, kAPPROX_QUANTILE, kBOOLEAN, kCOUNT_IF, kDOUBLE, kINT, kMODE, kSUM_IF, RexAgg::size(), and ThriftSerializers::takes_arg().

Referenced by anonymous_namespace{RelAlgExecutor.cpp}::translate_targets().

279  {
280  SQLAgg agg_kind = rex->getKind();
281  const bool is_distinct = rex->isDistinct();
282  const bool takes_arg{rex->size() > 0};
283  std::shared_ptr<Analyzer::Expr> arg_expr;
284  std::shared_ptr<Analyzer::Expr> arg1; // 2nd aggregate parameter
285  if (takes_arg) {
286  const auto operand = rex->getOperand(0);
287  CHECK_LT(operand, scalar_sources.size());
288  CHECK_LE(rex->size(), 2u);
289  arg_expr = scalar_sources[operand];
290  switch (agg_kind) {
292  if (rex->size() == 2) {
293  auto const const_arg1 = std::dynamic_pointer_cast<Analyzer::Constant>(
294  scalar_sources[rex->getOperand(1)]);
295  if (!const_arg1 || const_arg1->get_type_info().get_type() != kINT ||
296  const_arg1->get_constval().intval < 1 ||
297  const_arg1->get_constval().intval > 100) {
298  throw std::runtime_error(
299  "APPROX_COUNT_DISTINCT's second parameter should be SMALLINT literal "
300  "between "
301  "1 and 100");
302  }
303  arg1 = scalar_sources[rex->getOperand(1)];
304  }
305  break;
306  case kAPPROX_QUANTILE:
307  if (g_cluster) {
308  throw std::runtime_error(
309  "APPROX_PERCENTILE/MEDIAN is not supported in distributed mode at this "
310  "time.");
311  }
312  // If second parameter is not given then APPROX_MEDIAN is assumed.
313  if (rex->size() == 2) {
314  arg1 = std::dynamic_pointer_cast<Analyzer::Constant>(
315  std::dynamic_pointer_cast<Analyzer::Constant>(
316  scalar_sources[rex->getOperand(1)])
317  ->add_cast(SQLTypeInfo(kDOUBLE)));
318  } else {
319 #ifdef _WIN32
320  Datum median;
321  median.doubleval = 0.5;
322 #else
323  constexpr Datum median{.doubleval = 0.5};
324 #endif
325  arg1 = std::make_shared<Analyzer::Constant>(kDOUBLE, false, median);
326  }
327  break;
328  case kMODE:
329  if (g_cluster) {
330  throw std::runtime_error(
331  "MODE is not supported in distributed mode at this time.");
332  }
333  break;
334  case kCOUNT_IF:
335  if (rex->isDistinct()) {
336  throw std::runtime_error(
337  "Currently, COUNT_IF function does not support DISTINCT qualifier.");
338  }
339  break;
340  case kSUM_IF:
341  arg1 = scalar_sources[rex->getOperand(1)];
342  if (arg1->get_type_info().get_type() != kBOOLEAN) {
343  throw std::runtime_error("Conditional argument must be a boolean expression.");
344  }
345  break;
346  default:
347  break;
348  }
349  const auto& arg_ti = arg_expr->get_type_info();
350  if (!is_agg_supported_for_type(agg_kind, arg_ti)) {
351  throw std::runtime_error("Aggregate on " + arg_ti.get_type_name() +
352  " is not supported yet.");
353  }
354  }
355  const auto agg_ti = get_agg_type(agg_kind, arg_expr.get());
356  return makeExpr<Analyzer::AggExpr>(agg_ti, agg_kind, arg_expr, is_distinct, arg1);
357 }
SQLAgg
Definition: sqldefs.h:73
SQLAgg getKind() const
Definition: RelAlgDag.h:813
size_t getOperand(size_t idx) const
Definition: RelAlgDag.h:819
SQLTypeInfo get_agg_type(const SQLAgg agg_kind, const Analyzer::Expr *arg_expr)
bool is_agg_supported_for_type(const SQLAgg &agg_kind, const SQLTypeInfo &arg_ti)
#define CHECK_LT(x, y)
Definition: Logger.h:299
#define CHECK_LE(x, y)
Definition: Logger.h:300
bool takes_arg(const TargetInfo &target_info)
bool isDistinct() const
Definition: RelAlgDag.h:815
size_t size() const
Definition: RelAlgDag.h:817
bool g_cluster
Definition: sqltypes.h:60
Definition: Datum.h:67
bool is_distinct(const size_t input_idx, const RelAlgNode *node)
Definition: sqldefs.h:83
double doubleval
Definition: Datum.h:74

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Analyzer::ExpressionPtr RelAlgTranslator::translateArrayFunction ( const RexFunctionOperator rex_function) const
private

Definition at line 1586 of file RelAlgTranslator.cpp.

References CHECK, get_nullable_logical_type_info(), SQLTypeInfo::get_subtype(), RexOperator::getType(), kARRAY, kBOOLEAN, kENCODING_FIXED, kNULLT, to_string(), TRANSIENT_DICT_ID, and translateFunctionArgs().

Referenced by translateFunction().

1587  {
1588  if (rex_function->getType().get_subtype() == kNULLT) {
1589  auto sql_type = rex_function->getType();
1590  CHECK(sql_type.get_type() == kARRAY);
1591 
1592  // FIX-ME: Deal with NULL arrays
1593  auto translated_function_args(translateFunctionArgs(rex_function));
1594  if (translated_function_args.size() > 0) {
1595  const auto first_element_logical_type =
1596  get_nullable_logical_type_info(translated_function_args[0]->get_type_info());
1597 
1598  auto diff_elem_itr =
1599  std::find_if(translated_function_args.begin(),
1600  translated_function_args.end(),
1601  [first_element_logical_type](const auto expr) {
1602  return first_element_logical_type !=
1603  get_nullable_logical_type_info(expr->get_type_info());
1604  });
1605  if (diff_elem_itr != translated_function_args.end()) {
1606  throw std::runtime_error(
1607  "Element " +
1608  std::to_string(diff_elem_itr - translated_function_args.begin()) +
1609  " is not of the same type as other elements of the array. Consider casting "
1610  "to force this condition.\nElement Type: " +
1611  get_nullable_logical_type_info((*diff_elem_itr)->get_type_info())
1612  .to_string() +
1613  "\nArray type: " + first_element_logical_type.to_string());
1614  }
1615 
1616  if (first_element_logical_type.is_string() &&
1617  !first_element_logical_type.is_dict_encoded_string()) {
1618  sql_type.set_subtype(first_element_logical_type.get_type());
1619  sql_type.set_compression(kENCODING_FIXED);
1620  } else if (first_element_logical_type.is_dict_encoded_string()) {
1621  sql_type.set_subtype(first_element_logical_type.get_type());
1622  sql_type.set_comp_param(TRANSIENT_DICT_ID);
1623  } else {
1624  sql_type.set_subtype(first_element_logical_type.get_type());
1625  sql_type.set_scale(first_element_logical_type.get_scale());
1626  sql_type.set_precision(first_element_logical_type.get_precision());
1627  }
1628 
1629  return makeExpr<Analyzer::ArrayExpr>(sql_type, translated_function_args);
1630  } else {
1631  // defaulting to valid sub-type for convenience
1632  sql_type.set_subtype(kBOOLEAN);
1633  return makeExpr<Analyzer::ArrayExpr>(sql_type, translated_function_args);
1634  }
1635  } else {
1636  return makeExpr<Analyzer::ArrayExpr>(rex_function->getType(),
1637  translateFunctionArgs(rex_function));
1638  }
1639 }
HOST DEVICE SQLTypes get_subtype() const
Definition: sqltypes.h:381
SQLTypeInfo get_nullable_logical_type_info(const SQLTypeInfo &type_info)
Definition: sqltypes.h:1227
const SQLTypeInfo & getType() const
Definition: RelAlgDag.h:284
std::string to_string(char const *&&v)
Analyzer::ExpressionPtrVector translateFunctionArgs(const RexFunctionOperator *) const
#define TRANSIENT_DICT_ID
Definition: sqltypes.h:310
#define CHECK(condition)
Definition: Logger.h:289

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateBinaryGeoConstructor ( const RexFunctionOperator rex_function,
SQLTypeInfo ti,
const bool  with_bounds 
) const
private

Definition at line 953 of file RelAlgTranslatorGeo.cpp.

References func_resolve, SQLTypeInfo::get_input_srid(), SQLTypeInfo::get_output_srid(), RexFunctionOperator::getName(), RexOperator::getOperand(), Geospatial::GeoBase::kBUFFER, Geospatial::GeoBase::kCONCAVEHULL, Geospatial::GeoBase::kDIFFERENCE, kDOUBLE, kENCODING_NONE, kGEOMETRY, Geospatial::GeoBase::kINTERSECTION, kMULTIPOLYGON, Geospatial::GeoBase::kUNION, SQLTypeInfo::set_comp_param(), SQLTypeInfo::set_compression(), SQLTypeInfo::set_input_srid(), SQLTypeInfo::set_output_srid(), SQLTypeInfo::set_subtype(), SQLTypeInfo::set_type(), translateGeoFunctionArg(), and translateScalarRex().

Referenced by translateFunction(), and translateGeoFunctionArg().

956  {
957 #ifndef ENABLE_GEOS
958  throw QueryNotSupported(rex_function->getName() +
959  " geo constructor requires enabled GEOS support");
960 #endif
962  if (rex_function->getName() == "ST_Difference"sv) {
964  } else if (rex_function->getName() == "ST_Union"sv) {
966  } else if (rex_function->getName() == "ST_Buffer"sv) {
968  } else if (rex_function->getName() == "ST_ConcaveHull"sv) {
970  }
971 
974  SQLTypeInfo arg0_ti;
975  SQLTypeInfo arg1_ti;
976  if (func_resolve(rex_function->getName(),
977  "ST_Intersection"sv,
978  "ST_Difference"sv,
979  "ST_Union"sv,
980  "ST_Buffer"sv,
981  "ST_ConcaveHull"sv)) {
982  // First arg: geometry
983  geoargs0 = translateGeoFunctionArg(rex_function->getOperand(0),
984  arg0_ti,
985  false,
986  false,
987  true,
988  true,
989  false,
990  false,
991  /* allow_gdal_transforms = */ true);
992  }
993  if (func_resolve(rex_function->getName(),
994  "ST_Intersection"sv,
995  "ST_Difference"sv,
996  "ST_Union"sv)) {
997  // Second arg: geometry
998  geoargs1 = translateGeoFunctionArg(rex_function->getOperand(1),
999  arg1_ti,
1000  false,
1001  false,
1002  true,
1003  true,
1004  false,
1005  false,
1006  /* allow_gdal_transforms = */ true);
1007  if (arg0_ti.get_output_srid() != arg1_ti.get_output_srid()) {
1008  throw QueryNotSupported(rex_function->getName() +
1009  " geo constructor requires arguments with matching srids");
1010  }
1011  } else if (func_resolve(rex_function->getName(), "ST_Buffer"sv, "ST_ConcaveHull"sv)) {
1012  // Second arg: double scalar
1013  auto param_expr = translateScalarRex(rex_function->getOperand(1));
1014  arg1_ti = SQLTypeInfo(kDOUBLE, false);
1015  if (param_expr->get_type_info().get_type() != kDOUBLE) {
1016  param_expr = param_expr->add_cast(arg1_ti);
1017  }
1018  geoargs1 = {param_expr};
1019  }
1020 
1021  // Record the optional transform request that can be sent by an ecompassing TRANSFORM
1022  auto srid = ti.get_output_srid();
1023  // Build the typeinfo of the constructed geometry
1024  SQLTypeInfo arg_ti = arg0_ti;
1025  arg_ti.set_type(kMULTIPOLYGON);
1026  arg_ti.set_subtype(kGEOMETRY);
1027  arg_ti.set_compression(kENCODING_NONE); // Constructed geometries are not compressed
1028  arg_ti.set_comp_param(0);
1029  arg_ti.set_input_srid(arg0_ti.get_output_srid());
1030  if (srid > 0) {
1031  if (arg_ti.get_input_srid() > 0) {
1032  // Constructed geometry to be transformed to srid given by encompassing transform
1033  arg_ti.set_output_srid(srid);
1034  } else {
1035  throw QueryNotSupported("Transform of geo constructor " + rex_function->getName() +
1036  " requires its argument(s) to have a valid srid");
1037  }
1038  } else {
1039  arg_ti.set_output_srid(arg_ti.get_input_srid()); // No encompassing transform
1040  }
1041  // If there was an output transform, it's now embedded into arg_ti and the geo operator.
1042  // Now de-register the transform from the return typeinfo:
1043  ti = arg_ti;
1045  return makeExpr<Analyzer::GeoBinOper>(op, arg_ti, arg0_ti, arg1_ti, geoargs0, geoargs1);
1046 }
void set_compression(EncodingType c)
Definition: sqltypes.h:501
auto func_resolve
std::vector< std::shared_ptr< Analyzer::Expr > > translateGeoFunctionArg(const RexScalar *rex_scalar, SQLTypeInfo &arg_ti, const bool with_bounds, const bool with_render_group, const bool expand_geo_col, const bool is_projection=false, const bool use_geo_expressions=false, const bool try_to_compress=false, const bool allow_gdal_transforms=false) const
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const RexScalar * getOperand(const size_t idx) const
Definition: RelAlgDag.h:272
HOST DEVICE void set_subtype(SQLTypes st)
Definition: sqltypes.h:491
void set_input_srid(int d)
Definition: sqltypes.h:494
void set_output_srid(int s)
Definition: sqltypes.h:496
void set_comp_param(int p)
Definition: sqltypes.h:502
HOST DEVICE int get_input_srid() const
Definition: sqltypes.h:384
std::vector< ExpressionPtr > ExpressionPtrVector
Definition: Analyzer.h:190
const std::string & getName() const
Definition: RelAlgDag.h:500
HOST DEVICE int get_output_srid() const
Definition: sqltypes.h:386
HOST DEVICE void set_type(SQLTypes t)
Definition: sqltypes.h:490

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateBinaryGeoFunction ( const RexFunctionOperator rex_function) const
private

Definition at line 1373 of file RelAlgTranslatorGeo.cpp.

References run_benchmark_import::args, CHECK, CHECK_EQ, CHECK_GT, Datum::doubleval, fold_expr(), func_resolve, g_enable_geo_ops_on_uncompressed_coords, SQLTypeInfo::get_comp_param(), SQLTypeInfo::get_compression(), Geospatial::get_compression_scheme(), SQLTypeInfo::get_input_srid(), SQLTypeInfo::get_output_srid(), SQLTypeInfo::get_subtype(), SQLTypeInfo::get_type(), RexFunctionOperator::getName(), RexOperator::getOperand(), RexOperator::getType(), Datum::intval, kBOOLEAN, kDOUBLE, kENCODING_GEOINT, kENCODING_NONE, kGEOGRAPHY, kINT, kLE, kLINESTRING, kMULTILINESTRING, kMULTIPOLYGON, kNOT, kNULLT, kONE, kPOINT, kPOLYGON, run_benchmark_import::result, RexOperator::size(), spatial_type::suffix(), TOLERANCE_GEOINT32, translateGeoColumn(), translateGeoFunctionArg(), and translateScalarRex().

Referenced by translateFunction(), and translateTernaryGeoFunction().

1374  {
1375  auto function_name = rex_function->getName();
1376  auto return_type = rex_function->getType();
1377 
1378  if (function_name == "ST_Overlaps"sv) {
1379  // Overlaps join is the only implementation supported for now, only translate bounds
1380  CHECK_EQ(size_t(2), rex_function->size());
1381  auto extract_geo_bounds_from_input =
1382  [this, &rex_function](const size_t index) -> std::shared_ptr<Analyzer::Expr> {
1383  const auto rex_input =
1384  dynamic_cast<const RexInput*>(rex_function->getOperand(index));
1385  if (rex_input) {
1386  SQLTypeInfo ti;
1387  const auto exprs = translateGeoColumn(rex_input, ti, true, false, false);
1388  CHECK_GT(exprs.size(), size_t(0));
1389  if (ti.get_type() == kPOINT) {
1390  throw std::runtime_error("ST_Overlaps is not supported for point arguments.");
1391  } else {
1392  return exprs.back();
1393  }
1394  } else {
1395  throw std::runtime_error(
1396  "Only inputs are supported as arguments to ST_Overlaps for now.");
1397  }
1398  };
1399  std::vector<std::shared_ptr<Analyzer::Expr>> geo_args;
1400  geo_args.push_back(extract_geo_bounds_from_input(0));
1401  geo_args.push_back(extract_geo_bounds_from_input(1));
1402 
1403  return makeExpr<Analyzer::FunctionOper>(return_type, function_name, geo_args);
1404  }
1405 
1406  if (function_name == "ST_Distance"sv || function_name == "ST_MaxDistance"sv) {
1407  CHECK_EQ(size_t(2), rex_function->size());
1408  std::vector<std::shared_ptr<Analyzer::Expr>> args;
1409  int legacy_transform_srid = 0;
1410  for (size_t i = 0; i < rex_function->size(); i++) {
1411  SQLTypeInfo arg0_ti; // discard
1412  auto geoargs = translateGeoFunctionArg(rex_function->getOperand(i),
1413  arg0_ti,
1414  /*with_bounds=*/false, // TODO
1415  /*with_render_group=*/false,
1416  /*expand_geo_col=*/false,
1417  /*is_projection = */ false,
1418  /*use_geo_expressions=*/true);
1419  if (arg0_ti.get_input_srid() != arg0_ti.get_output_srid() &&
1420  arg0_ti.get_output_srid() > 0 &&
1421  std::dynamic_pointer_cast<Analyzer::ColumnVar>(geoargs.front())) {
1422  // legacy transform
1423  CHECK(legacy_transform_srid == 0 ||
1424  legacy_transform_srid == arg0_ti.get_output_srid());
1425  legacy_transform_srid = arg0_ti.get_output_srid();
1426  }
1427  args.insert(args.end(), geoargs.begin(), geoargs.end());
1428  }
1429  return makeExpr<Analyzer::GeoOperator>(
1430  SQLTypeInfo(kDOUBLE, /*not_null=*/false),
1431  function_name,
1432  args,
1433  legacy_transform_srid > 0 ? std::make_optional<int>(legacy_transform_srid)
1434  : std::nullopt);
1435  }
1436 
1437  bool swap_args = false;
1438  bool with_bounds = false;
1439  bool negate_result = false;
1440  Analyzer::ExpressionPtr threshold_expr = nullptr;
1441  Analyzer::ExpressionPtr compare_expr = nullptr;
1442  if (function_name == "ST_DWithin"sv) {
1443  CHECK_EQ(size_t(3), rex_function->size());
1444  function_name = "ST_Distance";
1445  return_type = SQLTypeInfo(kDOUBLE, false);
1446  // Inject ST_DWithin's short-circuiting threshold into ST_MaxDistance
1447  threshold_expr = translateScalarRex(rex_function->getOperand(2));
1448  } else if (function_name == "ST_Equals"sv) {
1449  // Translate ST_Equals(g1,g2) to ST_Distance(g1,g2)<=0.0
1450  CHECK_EQ(size_t(2), rex_function->size());
1451  function_name = "ST_Distance";
1452  return_type = SQLTypeInfo(kDOUBLE, false);
1453  threshold_expr = nullptr;
1454  Datum d;
1455  d.doubleval = 0.0;
1456  compare_expr = makeExpr<Analyzer::Constant>(kDOUBLE, false, d);
1457  } else if (function_name == "ST_DFullyWithin"sv) {
1458  CHECK_EQ(size_t(3), rex_function->size());
1459  function_name = "ST_MaxDistance";
1460  return_type = SQLTypeInfo(kDOUBLE, false);
1461  // TODO: inject ST_DFullyWithin's short-circuiting threshold into ST_MaxDistance
1462  threshold_expr = nullptr;
1463  } else if (function_name == "ST_Distance"sv) {
1464  // TODO: pick up an outside short-circuiting threshold and inject into ST_Distance
1465  threshold_expr = nullptr;
1466  } else if (function_name == "ST_MaxDistance"sv) {
1467  // TODO: pick up an outside short-circuiting threshold and inject into
1468  // ST_MaxDistance
1469  threshold_expr = nullptr;
1470  } else {
1471  CHECK_EQ(size_t(2), rex_function->size());
1472  }
1473  if (function_name == "ST_Within"sv) {
1474  function_name = "ST_Contains";
1475  swap_args = true;
1476  } else if (function_name == "ST_Disjoint"sv) {
1477  function_name = "ST_Intersects";
1478  negate_result = true;
1479  }
1480  if (func_resolve(
1481  function_name, "ST_Contains"sv, "ST_Intersects"sv, "ST_Approx_Overlaps"sv)) {
1482  with_bounds = true;
1483  }
1484 
1485  std::vector<std::shared_ptr<Analyzer::Expr>> geoargs;
1486  SQLTypeInfo arg0_ti;
1487  SQLTypeInfo arg1_ti;
1488 
1489  // Proactively try to compress the first arg of ST_Intersects to preempt arg swap
1490  bool try_to_compress_arg0 = g_enable_geo_ops_on_uncompressed_coords &&
1491  func_resolve(function_name, "ST_Intersects"sv);
1492 
1493  auto geoargs0 = translateGeoFunctionArg(rex_function->getOperand(swap_args ? 1 : 0),
1494  arg0_ti,
1495  with_bounds,
1496  false,
1497  false,
1498  false,
1499  false,
1500  try_to_compress_arg0);
1501  geoargs.insert(geoargs.end(), geoargs0.begin(), geoargs0.end());
1502 
1503  // If first arg is compressed, try to compress the second one to be able to
1504  // switch to faster implementations working directly on uncompressed coords
1505  bool try_to_compress_arg1 =
1507  func_resolve(function_name, "ST_Contains"sv, "ST_Intersects"sv) &&
1508  arg0_ti.get_compression() == kENCODING_GEOINT &&
1509  arg0_ti.get_output_srid() == 4326);
1510 
1511  auto geoargs1 = translateGeoFunctionArg(rex_function->getOperand(swap_args ? 0 : 1),
1512  arg1_ti,
1513  with_bounds,
1514  false,
1515  false,
1516  false,
1517  false,
1518  try_to_compress_arg1);
1519  geoargs.insert(geoargs.end(), geoargs1.begin(), geoargs1.end());
1520 
1521  if (arg0_ti.get_subtype() != kNULLT && arg0_ti.get_subtype() != arg1_ti.get_subtype()) {
1522  throw QueryNotSupported(rex_function->getName() +
1523  " accepts either two GEOGRAPHY or two GEOMETRY arguments");
1524  }
1525  // Check SRID match if at least one is set/valid
1526  if ((arg0_ti.get_output_srid() > 0 || arg1_ti.get_output_srid() > 0) &&
1527  arg0_ti.get_output_srid() != arg1_ti.get_output_srid()) {
1528  throw QueryNotSupported(rex_function->getName() + " cannot accept different SRIDs");
1529  }
1530  if (compare_expr) {
1531  // We could fold the check to false here if argument geo types are different, e.g.
1532  // POLYGON vs POINT. However, tiny POLYGON could be "spatially" equal to a POINT.
1533  if (arg0_ti.get_type() != kPOINT || arg1_ti.get_type() != kPOINT) {
1534  // ST_Equals is translated to a simple distance check for POINTs,
1535  // otherwise geometries are passed to GEOS's Equals
1536  return nullptr;
1537  }
1538  // Look at POINT compression modes.
1539  if (arg0_ti.get_compression() != arg1_ti.get_compression()) {
1540  if ((arg0_ti.get_compression() == kENCODING_GEOINT &&
1541  arg0_ti.get_comp_param() == 32 &&
1542  arg1_ti.get_compression() == kENCODING_NONE) ||
1543  (arg0_ti.get_compression() == kENCODING_NONE &&
1544  arg1_ti.get_compression() == kENCODING_GEOINT &&
1545  arg0_ti.get_comp_param() == 32)) {
1546  // Spatial equality comparison of a compressed point vs uncompressed point.
1547  // Introduce tolerance into distance calculation and comparison, translate
1548  // ST_Equals(g1,g2) to ST_Distance(g1,g2,thereshold=tolerance)<=tolerance
1549  Datum tolerance;
1550  // Tolerance representing 0.44" to cover shifts due to GEOINT(32) compression
1551  tolerance.doubleval = TOLERANCE_GEOINT32;
1552  threshold_expr = makeExpr<Analyzer::Constant>(kDOUBLE, false, tolerance);
1553  compare_expr = threshold_expr;
1554  } else {
1555  throw QueryNotSupported(
1556  rex_function->getName() +
1557  " unable to calculate compression tolerance for arguments");
1558  }
1559  }
1560  }
1561  if (arg0_ti.get_type() == kMULTILINESTRING || arg1_ti.get_type() == kMULTILINESTRING) {
1562  throw QueryNotSupported(rex_function->getName() +
1563  " currently doesn't support this argument combination");
1564  }
1565 
1566  auto can_use_compressed_coords = [](const SQLTypeInfo& i0_ti,
1567  const Analyzer::ExpressionPtrVector& i0_operands,
1568  const SQLTypeInfo& i1_ti,
1569  const Analyzer::ExpressionPtrVector& i1_operands) {
1570  const bool i0_is_poly =
1571  i0_ti.get_type() == kPOLYGON || i0_ti.get_type() == kMULTIPOLYGON;
1572  const bool i1_is_point = i1_ti.get_type() == kPOINT;
1573  const bool i1_is_literal =
1574  i1_operands.size() == 1 && std::dynamic_pointer_cast<const Analyzer::Constant>(
1575  i1_operands.front()) != nullptr;
1576  return (i0_is_poly && !i1_is_literal && i1_is_point &&
1577  i0_ti.get_compression() == kENCODING_GEOINT &&
1578  i0_ti.get_input_srid() == i0_ti.get_output_srid() &&
1579  i0_ti.get_compression() == i1_ti.get_compression() &&
1580  i1_ti.get_input_srid() == i1_ti.get_output_srid());
1581  };
1582  if (g_enable_geo_ops_on_uncompressed_coords && function_name == "ST_Contains"sv) {
1583  if (can_use_compressed_coords(arg0_ti, geoargs0, arg1_ti, geoargs1)) {
1584  // Switch to Contains implementation working directly on uncompressed coords
1585  function_name = "ST_cContains";
1586  }
1587  }
1588  if (g_enable_geo_ops_on_uncompressed_coords && function_name == "ST_Intersects"sv) {
1589  if (can_use_compressed_coords(arg0_ti, geoargs0, arg1_ti, geoargs1)) {
1590  // Switch to Intersects implementation working directly on uncompressed coords
1591  function_name = "ST_cIntersects";
1592  } else if (can_use_compressed_coords(arg1_ti, geoargs1, arg0_ti, geoargs0)) {
1593  // Switch to Intersects implementation working on uncompressed coords, swapped args
1594  function_name = "ST_cIntersects";
1595  geoargs.clear();
1596  geoargs.insert(geoargs.end(), geoargs1.begin(), geoargs1.end());
1597  geoargs.insert(geoargs.end(), geoargs0.begin(), geoargs0.end());
1598  auto tmp_ti = arg0_ti;
1599  arg0_ti = arg1_ti;
1600  arg1_ti = tmp_ti;
1601  }
1602  }
1603 
1604  std::string specialized_geofunc{function_name + suffix(arg0_ti.get_type()) +
1605  suffix(arg1_ti.get_type())};
1606 
1607  if (arg0_ti.get_subtype() == kGEOGRAPHY && arg0_ti.get_output_srid() == 4326) {
1608  // Need to call geodesic runtime functions
1609  if (function_name == "ST_Distance"sv) {
1610  if ((arg0_ti.get_type() == kPOINT && arg1_ti.get_type() == kPOINT) ||
1611  (arg0_ti.get_type() == kLINESTRING && arg1_ti.get_type() == kPOINT) ||
1612  (arg0_ti.get_type() == kPOINT && arg1_ti.get_type() == kLINESTRING)) {
1613  // Geodesic distance between points
1614  specialized_geofunc += "_Geodesic"s;
1615  } else {
1616  throw QueryNotSupported(function_name +
1617  " currently doesn't accept non-POINT geographies");
1618  }
1619  } else if (rex_function->getName() == "ST_Contains"sv) {
1620  // We currently don't have a geodesic implementation of ST_Contains,
1621  // allowing calls to a [less precise] cartesian implementation.
1622  } else {
1623  throw QueryNotSupported(function_name + " doesn't accept geographies");
1624  }
1625  } else if (function_name == "ST_Distance"sv && rex_function->size() == 3) {
1626  if (arg0_ti.get_type() == kPOINT && arg1_ti.get_type() == kPOINT) {
1627  // Cartesian distance between points used by ST_DWithin - switch to faster Squared
1628  specialized_geofunc += "_Squared"s;
1629  }
1630  }
1631 
1632  // Add first input's compression mode and SRID args to enable on-the-fly
1633  // decompression/transforms
1634  Datum input_compression0;
1635  input_compression0.intval = Geospatial::get_compression_scheme(arg0_ti);
1636  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_compression0));
1637  Datum input_srid0;
1638  input_srid0.intval = arg0_ti.get_input_srid();
1639  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_srid0));
1640 
1641  // Add second input's compression mode and SRID args to enable on-the-fly
1642  // decompression/transforms
1643  Datum input_compression1;
1644  input_compression1.intval = Geospatial::get_compression_scheme(arg1_ti);
1645  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_compression1));
1646  Datum input_srid1;
1647  input_srid1.intval = arg1_ti.get_input_srid();
1648  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_srid1));
1649 
1650  // Add output SRID arg to enable on-the-fly transforms
1651  Datum output_srid;
1652  output_srid.intval = arg0_ti.get_output_srid();
1653  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, output_srid));
1654 
1655  // Some geo distance functions will be injected with a short-circuit threshold.
1656  // Threshold value would come from Geo comparison operations or from other outer
1657  // geo operations, e.g. ST_DWithin
1658  // At this point, only ST_Distance_LineString_LineString requires a threshold arg.
1659  // TODO: Other combinations that involve LINESTRING, POLYGON and MULTIPOLYGON args
1660  // TODO: Inject threshold into ST_MaxDistance
1661  if (function_name == "ST_Distance"sv && arg0_ti.get_subtype() != kGEOGRAPHY &&
1662  (arg0_ti.get_type() != kPOINT || arg1_ti.get_type() != kPOINT)) {
1663  if (threshold_expr) {
1664  if (threshold_expr->get_type_info().get_type() != kDOUBLE) {
1665  const auto& threshold_ti = SQLTypeInfo(kDOUBLE, false);
1666  threshold_expr = threshold_expr->add_cast(threshold_ti);
1667  }
1668  threshold_expr = fold_expr(threshold_expr.get());
1669  } else {
1670  Datum d;
1671  d.doubleval = 0.0;
1672  threshold_expr = makeExpr<Analyzer::Constant>(kDOUBLE, false, d);
1673  }
1674  geoargs.push_back(threshold_expr);
1675  }
1676 
1677  auto result =
1678  makeExpr<Analyzer::FunctionOper>(return_type, specialized_geofunc, geoargs);
1679  if (negate_result) {
1680  return makeExpr<Analyzer::UOper>(kBOOLEAN, kNOT, result);
1681  }
1682  if (compare_expr) {
1683  return makeExpr<Analyzer::BinOper>(kBOOLEAN, kLE, kONE, result, compare_expr);
1684  }
1685  return result;
1686 }
HOST DEVICE SQLTypes get_subtype() const
Definition: sqltypes.h:381
#define CHECK_EQ(x, y)
Definition: Logger.h:297
auto func_resolve
std::vector< std::shared_ptr< Analyzer::Expr > > translateGeoFunctionArg(const RexScalar *rex_scalar, SQLTypeInfo &arg_ti, const bool with_bounds, const bool with_render_group, const bool expand_geo_col, const bool is_projection=false, const bool use_geo_expressions=false, const bool try_to_compress=false, const bool allow_gdal_transforms=false) const
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const SQLTypeInfo & getType() const
Definition: RelAlgDag.h:284
size_t size() const
Definition: RelAlgDag.h:270
const RexScalar * getOperand(const size_t idx) const
Definition: RelAlgDag.h:272
Definition: sqldefs.h:34
int32_t get_compression_scheme(const SQLTypeInfo &ti)
Definition: Compression.cpp:23
std::shared_ptr< Analyzer::Expr > ExpressionPtr
Definition: Analyzer.h:188
bool g_enable_geo_ops_on_uncompressed_coords
Definition: Execute.cpp:114
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:380
#define CHECK_GT(x, y)
Definition: Logger.h:301
int32_t intval
Definition: Datum.h:71
#define TOLERANCE_GEOINT32
std::string suffix(SQLTypes type)
Definition: Codegen.cpp:70
Definition: sqldefs.h:71
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:388
HOST DEVICE int get_comp_param() const
Definition: sqltypes.h:389
HOST DEVICE int get_input_srid() const
Definition: sqltypes.h:384
#define CHECK(condition)
Definition: Logger.h:289
std::vector< ExpressionPtr > ExpressionPtrVector
Definition: Analyzer.h:190
Definition: sqltypes.h:60
const std::string & getName() const
Definition: RelAlgDag.h:500
Definition: Datum.h:67
std::vector< std::shared_ptr< Analyzer::Expr > > translateGeoColumn(const RexInput *, SQLTypeInfo &, const bool with_bounds, const bool with_render_group, const bool expand_geo_col) const
Definition: sqldefs.h:38
std::shared_ptr< Analyzer::Expr > fold_expr(const Analyzer::Expr *expr)
double doubleval
Definition: Datum.h:74
HOST DEVICE int get_output_srid() const
Definition: sqltypes.h:386

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateBinaryGeoPredicate ( const RexFunctionOperator rex_function,
SQLTypeInfo ti,
const bool  with_bounds 
) const
private

Definition at line 1066 of file RelAlgTranslatorGeo.cpp.

References RexFunctionOperator::getName(), RexOperator::getOperand(), kBOOLEAN, Geospatial::GeoBase::kEQUALS, and translateGeoFunctionArg().

Referenced by translateFunction(), and translateGeoFunctionArg().

1069  {
1070  if (rex_function->getName() != "ST_Equals"sv) {
1071  throw QueryNotSupported(rex_function->getName() + " geo predicate is not supported");
1072  }
1073 #ifndef ENABLE_GEOS
1074  throw QueryNotSupported(rex_function->getName() +
1075  " geo predicate requires enabled GEOS support");
1076 #endif
1077  SQLTypeInfo arg0_ti;
1078  auto geoargs0 = translateGeoFunctionArg(
1079  rex_function->getOperand(0), arg0_ti, false, false, true, true);
1080  SQLTypeInfo arg1_ti;
1081  auto geoargs1 = translateGeoFunctionArg(
1082  rex_function->getOperand(1), arg1_ti, false, false, true, true);
1083  ti = SQLTypeInfo(kBOOLEAN, false);
1085  return makeExpr<Analyzer::GeoBinOper>(op, ti, arg0_ti, arg1_ti, geoargs0, geoargs1);
1086 }
std::vector< std::shared_ptr< Analyzer::Expr > > translateGeoFunctionArg(const RexScalar *rex_scalar, SQLTypeInfo &arg_ti, const bool with_bounds, const bool with_render_group, const bool expand_geo_col, const bool is_projection=false, const bool use_geo_expressions=false, const bool try_to_compress=false, const bool allow_gdal_transforms=false) const
const RexScalar * getOperand(const size_t idx) const
Definition: RelAlgDag.h:272
const std::string & getName() const
Definition: RelAlgDag.h:500

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Analyzer::ExpressionPtr RelAlgTranslator::translateCardinality ( const RexFunctionOperator rex_function) const
private

Definition at line 1474 of file RelAlgTranslator.cpp.

References RexFunctionOperator::getName(), RexOperator::getOperand(), RexOperator::getType(), kARRAY, anonymous_namespace{RelAlgTranslator.cpp}::makeNumericConstant(), and translateScalarRex().

Referenced by translateFunction().

1475  {
1476  const auto ret_ti = rex_function->getType();
1477  const auto arg = translateScalarRex(rex_function->getOperand(0));
1478  const auto arg_ti = arg->get_type_info();
1479  if (!arg_ti.is_array()) {
1480  throw std::runtime_error(rex_function->getName() + " expects an array expression.");
1481  }
1482  if (arg_ti.get_subtype() == kARRAY) {
1483  throw std::runtime_error(rex_function->getName() +
1484  " expects one-dimension array expression.");
1485  }
1486  const auto array_size = arg_ti.get_size();
1487  const auto array_elem_size = arg_ti.get_elem_type().get_array_context_logical_size();
1488 
1489  if (array_size > 0) {
1490  if (array_elem_size <= 0) {
1491  throw std::runtime_error(rex_function->getName() +
1492  ": unexpected array element type.");
1493  }
1494  // Return cardinality of a fixed length array
1495  return makeNumericConstant(ret_ti, array_size / array_elem_size);
1496  }
1497  // Variable length array cardinality will be calculated at runtime
1498  return makeExpr<Analyzer::CardinalityExpr>(arg);
1499 }
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const SQLTypeInfo & getType() const
Definition: RelAlgDag.h:284
const RexScalar * getOperand(const size_t idx) const
Definition: RelAlgDag.h:272
std::shared_ptr< Analyzer::Constant > makeNumericConstant(const SQLTypeInfo &ti, const long val)
const std::string & getName() const
Definition: RelAlgDag.h:500

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateCase ( const RexCase rex_case) const
private

Definition at line 1053 of file RelAlgTranslator.cpp.

References RexCase::branchCount(), executor_, RexCase::getElse(), RexCase::getThen(), RexCase::getWhen(), Parser::CaseExpr::normalize(), and translateScalarRex().

1054  {
1055  std::shared_ptr<Analyzer::Expr> else_expr;
1056  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
1057  expr_list;
1058  for (size_t i = 0; i < rex_case->branchCount(); ++i) {
1059  const auto when_expr = translateScalarRex(rex_case->getWhen(i));
1060  const auto then_expr = translateScalarRex(rex_case->getThen(i));
1061  expr_list.emplace_back(when_expr, then_expr);
1062  }
1063  if (rex_case->getElse()) {
1064  else_expr = translateScalarRex(rex_case->getElse());
1065  }
1066  return Parser::CaseExpr::normalize(expr_list, else_expr, executor_);
1067 }
const RexScalar * getThen(const size_t idx) const
Definition: RelAlgDag.h:443
static std::shared_ptr< Analyzer::Expr > normalize(const std::list< std::pair< std::shared_ptr< Analyzer::Expr >, std::shared_ptr< Analyzer::Expr >>> &, const std::shared_ptr< Analyzer::Expr >, const Executor *executor=nullptr)
const Executor * executor_
const RexScalar * getElse() const
Definition: RelAlgDag.h:448
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const RexScalar * getWhen(const size_t idx) const
Definition: RelAlgDag.h:438
size_t branchCount() const
Definition: RelAlgDag.h:436

+ Here is the call graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateCurrentDate ( ) const
private

Definition at line 1510 of file RelAlgTranslator.cpp.

References Datum::bigintval, is_null(), kDATE, and now_.

Referenced by translateFunction().

1510  {
1511  constexpr bool is_null = false;
1512  Datum datum;
1513  datum.bigintval = now_ - now_ % (24 * 60 * 60); // Assumes 0 < now_.
1514  return makeExpr<Analyzer::Constant>(kDATE, is_null, datum);
1515 }
CONSTEXPR DEVICE bool is_null(const T &value)
int64_t bigintval
Definition: Datum.h:72
Definition: sqltypes.h:68
Definition: Datum.h:67

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateCurrentTime ( ) const
private

Definition at line 1517 of file RelAlgTranslator.cpp.

References Datum::bigintval, is_null(), kTIME, and now_.

Referenced by translateFunction().

1517  {
1518  constexpr bool is_null = false;
1519  Datum datum;
1520  datum.bigintval = now_ % (24 * 60 * 60); // Assumes 0 < now_.
1521  return makeExpr<Analyzer::Constant>(kTIME, is_null, datum);
1522 }
Definition: sqltypes.h:64
CONSTEXPR DEVICE bool is_null(const T &value)
int64_t bigintval
Definition: Datum.h:72
Definition: Datum.h:67

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateCurrentTimestamp ( ) const
private

Definition at line 1524 of file RelAlgTranslator.cpp.

References Parser::TimestampLiteral::get(), and now_.

Referenced by translateDatetime(), and translateFunction().

1524  {
1526 }
static std::shared_ptr< Analyzer::Expr > get(const int64_t)
Definition: ParserNode.cpp:222

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateCurrentUser ( const RexFunctionOperator rex_function) const
private

Definition at line 1404 of file RelAlgTranslator.cpp.

References Parser::UserLiteral::get(), and query_state_.

Referenced by translateFunction().

1405  {
1406  std::string user{"SESSIONLESS_USER"};
1407  if (query_state_) {
1408  user = query_state_->getConstSessionInfo()->get_currentUser().userName;
1409  }
1410  return Parser::UserLiteral::get(user);
1411 }
static std::shared_ptr< Analyzer::Expr > get(const std::string &)
Definition: ParserNode.cpp:236
std::shared_ptr< const query_state::QueryState > query_state_

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateDateadd ( const RexFunctionOperator rex_function) const
private

Definition at line 1227 of file RelAlgTranslator.cpp.

References CHECK_EQ, field(), RexOperator::getOperand(), kBIGINT, kTIME, kTIMESTAMP, RexOperator::size(), to_dateadd_field(), translateScalarRex(), and anonymous_namespace{RelAlgTranslator.cpp}::validate_datetime_datepart_argument().

Referenced by translateFunction().

1228  {
1229  CHECK_EQ(size_t(3), rex_function->size());
1230  const auto timeunit = translateScalarRex(rex_function->getOperand(0));
1231  const auto timeunit_lit = std::dynamic_pointer_cast<Analyzer::Constant>(timeunit);
1233  const auto number_units = translateScalarRex(rex_function->getOperand(1));
1234  const auto number_units_const =
1235  std::dynamic_pointer_cast<Analyzer::Constant>(number_units);
1236  if (number_units_const && number_units_const->get_is_null()) {
1237  throw std::runtime_error("The 'Interval' argument literal must not be 'null'.");
1238  }
1239  const auto cast_number_units = number_units->add_cast(SQLTypeInfo(kBIGINT, false));
1240  const auto datetime = translateScalarRex(rex_function->getOperand(2));
1241  const auto& datetime_ti = datetime->get_type_info();
1242  if (datetime_ti.get_type() == kTIME) {
1243  throw std::runtime_error("DateAdd operation not supported for TIME.");
1244  }
1245  const auto& field = to_dateadd_field(*timeunit_lit->get_constval().stringval);
1246  const int dim = datetime_ti.get_dimension();
1247  return makeExpr<Analyzer::DateaddExpr>(
1248  SQLTypeInfo(kTIMESTAMP, dim, 0, false), field, cast_number_units, datetime);
1249 }
#define CHECK_EQ(x, y)
Definition: Logger.h:297
Definition: sqltypes.h:64
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
size_t size() const
Definition: RelAlgDag.h:270
const RexScalar * getOperand(const size_t idx) const
Definition: RelAlgDag.h:272
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
void validate_datetime_datepart_argument(const std::shared_ptr< Analyzer::Constant > literal_expr)
DateaddField to_dateadd_field(const std::string &field)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateDatediff ( const RexFunctionOperator rex_function) const
private

Definition at line 1342 of file RelAlgTranslator.cpp.

References CHECK_EQ, field(), RexOperator::getOperand(), kBIGINT, RexOperator::size(), to_datediff_field(), translateScalarRex(), and anonymous_namespace{RelAlgTranslator.cpp}::validate_datetime_datepart_argument().

Referenced by translateFunction().

1343  {
1344  CHECK_EQ(size_t(3), rex_function->size());
1345  const auto timeunit = translateScalarRex(rex_function->getOperand(0));
1346  const auto timeunit_lit = std::dynamic_pointer_cast<Analyzer::Constant>(timeunit);
1348  const auto start = translateScalarRex(rex_function->getOperand(1));
1349  const auto end = translateScalarRex(rex_function->getOperand(2));
1350  const auto field = to_datediff_field(*timeunit_lit->get_constval().stringval);
1351  return makeExpr<Analyzer::DatediffExpr>(SQLTypeInfo(kBIGINT, false), field, start, end);
1352 }
#define CHECK_EQ(x, y)
Definition: Logger.h:297
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
size_t size() const
Definition: RelAlgDag.h:270
const RexScalar * getOperand(const size_t idx) const
Definition: RelAlgDag.h:272
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
DatetruncField to_datediff_field(const std::string &field)
void validate_datetime_datepart_argument(const std::shared_ptr< Analyzer::Constant > literal_expr)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateDatepart ( const RexFunctionOperator rex_function) const
private

Definition at line 1354 of file RelAlgTranslator.cpp.

References CHECK_EQ, ExtractExpr::generate(), RexOperator::getOperand(), RexOperator::size(), to_datepart_field(), translateScalarRex(), and anonymous_namespace{RelAlgTranslator.cpp}::validate_datetime_datepart_argument().

Referenced by translateFunction().

1355  {
1356  CHECK_EQ(size_t(2), rex_function->size());
1357  const auto timeunit = translateScalarRex(rex_function->getOperand(0));
1358  const auto timeunit_lit = std::dynamic_pointer_cast<Analyzer::Constant>(timeunit);
1360  const auto from_expr = translateScalarRex(rex_function->getOperand(1));
1361  return ExtractExpr::generate(
1362  from_expr, to_datepart_field(*timeunit_lit->get_constval().stringval));
1363 }
#define CHECK_EQ(x, y)
Definition: Logger.h:297
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
size_t size() const
Definition: RelAlgDag.h:270
const RexScalar * getOperand(const size_t idx) const
Definition: RelAlgDag.h:272
ExtractField to_datepart_field(const std::string &field)
const std::shared_ptr< Analyzer::Expr > generate() const
void validate_datetime_datepart_argument(const std::shared_ptr< Analyzer::Constant > literal_expr)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateDatePlusMinus ( const RexOperator rex_operator) const
private

Definition at line 1260 of file RelAlgTranslator.cpp.

References run_benchmark_import::args, CHECK, daMONTH, daSECOND, dtMONTH, dtSECOND, fold_expr(), anonymous_namespace{RelAlgTranslator.cpp}::get_datetimeplus_rewrite_funcname(), RexOperator::getOperand(), RexOperator::getOperator(), RexOperator::getType(), kBIGINT, kDATE, kDIVIDE, kINTERVAL_DAY_TIME, kINTERVAL_YEAR_MONTH, kMINUS, kMULTIPLY, kONE, kPLUS, kTIME, kTIMESTAMP, kUMINUS, anonymous_namespace{RelAlgTranslator.cpp}::makeNumericConstant(), run_benchmark_import::result, rewrite_to_date_trunc(), RexOperator::size(), and translateScalarRex().

Referenced by translateOper().

1261  {
1262  if (rex_operator->size() != 2) {
1263  return nullptr;
1264  }
1265  const auto datetime = translateScalarRex(rex_operator->getOperand(0));
1266  const auto datetime_ti = datetime->get_type_info();
1267  if (!datetime_ti.is_timestamp() && !datetime_ti.is_date()) {
1268  if (datetime_ti.get_type() == kTIME) {
1269  throw std::runtime_error("DateTime addition/subtraction not supported for TIME.");
1270  }
1271  return nullptr;
1272  }
1273  const auto rhs = translateScalarRex(rex_operator->getOperand(1));
1274  const auto rhs_ti = rhs->get_type_info();
1275  if (rhs_ti.get_type() == kTIMESTAMP || rhs_ti.get_type() == kDATE) {
1276  if (datetime_ti.is_high_precision_timestamp() ||
1277  rhs_ti.is_high_precision_timestamp()) {
1278  throw std::runtime_error(
1279  "High Precision timestamps are not supported for TIMESTAMPDIFF operation. "
1280  "Use "
1281  "DATEDIFF.");
1282  }
1283  auto bigint_ti = SQLTypeInfo(kBIGINT, false);
1284  const auto& rex_operator_ti = rex_operator->getType();
1285  const auto datediff_field =
1286  (rex_operator_ti.get_type() == kINTERVAL_DAY_TIME) ? dtSECOND : dtMONTH;
1287  auto result =
1288  makeExpr<Analyzer::DatediffExpr>(bigint_ti, datediff_field, rhs, datetime);
1289  // multiply 1000 to result since expected result should be in millisecond precision.
1290  if (rex_operator_ti.get_type() == kINTERVAL_DAY_TIME) {
1291  return makeExpr<Analyzer::BinOper>(bigint_ti.get_type(),
1292  kMULTIPLY,
1293  kONE,
1294  result,
1295  makeNumericConstant(bigint_ti, 1000));
1296  } else {
1297  return result;
1298  }
1299  }
1300  const auto op = rex_operator->getOperator();
1301  if (op == kPLUS) {
1302  std::vector<std::shared_ptr<Analyzer::Expr>> args = {datetime, rhs};
1303  auto dt_plus = makeExpr<Analyzer::FunctionOper>(
1304  datetime_ti, get_datetimeplus_rewrite_funcname(op), args);
1305  const auto date_trunc = rewrite_to_date_trunc(dt_plus.get());
1306  if (date_trunc) {
1307  return date_trunc;
1308  }
1309  }
1310  const auto interval = fold_expr(rhs.get());
1311  auto interval_ti = interval->get_type_info();
1312  auto bigint_ti = SQLTypeInfo(kBIGINT, false);
1313  const auto interval_lit = std::dynamic_pointer_cast<Analyzer::Constant>(interval);
1314  if (interval_ti.get_type() == kINTERVAL_DAY_TIME) {
1315  std::shared_ptr<Analyzer::Expr> interval_sec;
1316  if (interval_lit) {
1317  interval_sec =
1318  makeNumericConstant(bigint_ti,
1319  (op == kMINUS ? -interval_lit->get_constval().bigintval
1320  : interval_lit->get_constval().bigintval) /
1321  1000);
1322  } else {
1323  interval_sec = makeExpr<Analyzer::BinOper>(bigint_ti.get_type(),
1324  kDIVIDE,
1325  kONE,
1326  interval,
1327  makeNumericConstant(bigint_ti, 1000));
1328  if (op == kMINUS) {
1329  interval_sec =
1330  std::make_shared<Analyzer::UOper>(bigint_ti, false, kUMINUS, interval_sec);
1331  }
1332  }
1333  return makeExpr<Analyzer::DateaddExpr>(datetime_ti, daSECOND, interval_sec, datetime);
1334  }
1335  CHECK(interval_ti.get_type() == kINTERVAL_YEAR_MONTH);
1336  const auto interval_months = op == kMINUS ? std::make_shared<Analyzer::UOper>(
1337  bigint_ti, false, kUMINUS, interval)
1338  : interval;
1339  return makeExpr<Analyzer::DateaddExpr>(datetime_ti, daMONTH, interval_months, datetime);
1340 }
Definition: sqltypes.h:64
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const SQLTypeInfo & getType() const
Definition: RelAlgDag.h:284
size_t size() const
Definition: RelAlgDag.h:270
const RexScalar * getOperand(const size_t idx) const
Definition: RelAlgDag.h:272
Definition: sqldefs.h:40
std::shared_ptr< Analyzer::Constant > makeNumericConstant(const SQLTypeInfo &ti, const long val)
SQLOps getOperator() const
Definition: RelAlgDag.h:282
Definition: sqltypes.h:68
Definition: sqldefs.h:39
Definition: sqldefs.h:71
#define CHECK(condition)
Definition: Logger.h:289
std::string get_datetimeplus_rewrite_funcname(const SQLOps &op)
std::shared_ptr< Analyzer::Expr > rewrite_to_date_trunc(const Analyzer::FunctionOper *dt_plus)
std::shared_ptr< Analyzer::Expr > fold_expr(const Analyzer::Expr *expr)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateDatetime ( const RexFunctionOperator rex_function) const
private

Definition at line 1528 of file RelAlgTranslator.cpp.

References CHECK, CHECK_EQ, RexOperator::getOperand(), RexOperator::size(), translateCurrentTimestamp(), and translateScalarRex().

Referenced by translateFunction().

1529  {
1530  CHECK_EQ(size_t(1), rex_function->size());
1531  const auto arg = translateScalarRex(rex_function->getOperand(0));
1532  const auto arg_lit = std::dynamic_pointer_cast<Analyzer::Constant>(arg);
1533  const std::string datetime_err{R"(Only DATETIME('NOW') supported for now.)"};
1534  if (!arg_lit || arg_lit->get_is_null()) {
1535  throw std::runtime_error(datetime_err);
1536  }
1537  CHECK(arg_lit->get_type_info().is_string());
1538  if (*arg_lit->get_constval().stringval != "NOW"sv) {
1539  throw std::runtime_error(datetime_err);
1540  }
1541  return translateCurrentTimestamp();
1542 }
#define CHECK_EQ(x, y)
Definition: Logger.h:297
std::shared_ptr< Analyzer::Expr > translateCurrentTimestamp() const
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
size_t size() const
Definition: RelAlgDag.h:270
const RexScalar * getOperand(const size_t idx) const
Definition: RelAlgDag.h:272
#define CHECK(condition)
Definition: Logger.h:289

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateExtract ( const RexFunctionOperator rex_function) const
private

Definition at line 1168 of file RelAlgTranslator.cpp.

References CHECK_EQ, ExtractExpr::generate(), DateTruncExpr::generate(), RexFunctionOperator::getName(), RexOperator::getOperand(), RexOperator::size(), translateScalarRex(), and anonymous_namespace{RelAlgTranslator.cpp}::validate_datetime_datepart_argument().

Referenced by translateFunction().

1169  {
1170  CHECK_EQ(size_t(2), rex_function->size());
1171  const auto timeunit = translateScalarRex(rex_function->getOperand(0));
1172  const auto timeunit_lit = std::dynamic_pointer_cast<Analyzer::Constant>(timeunit);
1174  const auto from_expr = translateScalarRex(rex_function->getOperand(1));
1175  const bool is_date_trunc = rex_function->getName() == "PG_DATE_TRUNC"sv;
1176  if (is_date_trunc) {
1177  return DateTruncExpr::generate(from_expr, *timeunit_lit->get_constval().stringval);
1178  } else {
1179  return ExtractExpr::generate(from_expr, *timeunit_lit->get_constval().stringval);
1180  }
1181 }
#define CHECK_EQ(x, y)
Definition: Logger.h:297
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
size_t size() const
Definition: RelAlgDag.h:270
const RexScalar * getOperand(const size_t idx) const
Definition: RelAlgDag.h:272
const std::shared_ptr< Analyzer::Expr > generate() const
const std::string & getName() const
Definition: RelAlgDag.h:500
void validate_datetime_datepart_argument(const std::shared_ptr< Analyzer::Constant > literal_expr)
const std::shared_ptr< Analyzer::Expr > generate() const

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateFunction ( const RexFunctionOperator rex_function) const
private

Definition at line 1641 of file RelAlgTranslator.cpp.

References run_benchmark_import::args, bind_function(), CHECK, CHECK_EQ, CHECK_LE, CHECK_LT, ext_arg_type_to_type_info(), func_resolve, RexFunctionOperator::getName(), RexOperator::getOperand(), RexOperator::getType(), Int64, SQLTypeInfo::is_decimal(), kDIVIDE, kONE, kSMALLINT, kTEXT, LOG, Parser::OperExpr::normalize(), PBool, PDouble, PFloat, PInt16, PInt32, PInt64, PInt8, rewrite_to_date_trunc(), SQLTypeInfo::set_notnull(), RexOperator::size(), Datum::smallintval, translateAbs(), translateArrayFunction(), translateBinaryGeoConstructor(), translateBinaryGeoFunction(), translateBinaryGeoPredicate(), translateCardinality(), translateCurrentDate(), translateCurrentTime(), translateCurrentTimestamp(), translateCurrentUser(), translateDateadd(), translateDatediff(), translateDatepart(), translateDatetime(), translateExtract(), translateFunctionArgs(), translateFunctionWithGeoArg(), translateGeoProjection(), translateHPTLiteral(), translateItem(), translateKeyForString(), translateLength(), translateLike(), translateLikely(), translateOffsetInFragment(), translateRegexp(), translateSampleRatio(), translateScalarRex(), translateSign(), translateStringOper(), translateTernaryGeoFunction(), translateUnaryGeoConstructor(), translateUnaryGeoFunction(), translateUnaryGeoPredicate(), translateUnlikely(), translateWidthBucket(), and logger::WARNING.

1642  {
1643  if (func_resolve(rex_function->getName(), "LIKE"sv, "PG_ILIKE"sv)) {
1644  return translateLike(rex_function);
1645  }
1646  if (rex_function->getName() == "REGEXP_LIKE"sv) {
1647  return translateRegexp(rex_function);
1648  }
1649  if (rex_function->getName() == "LIKELY"sv) {
1650  return translateLikely(rex_function);
1651  }
1652  if (rex_function->getName() == "UNLIKELY"sv) {
1653  return translateUnlikely(rex_function);
1654  }
1655  if (func_resolve(rex_function->getName(), "PG_EXTRACT"sv, "PG_DATE_TRUNC"sv)) {
1656  return translateExtract(rex_function);
1657  }
1658  if (rex_function->getName() == "DATEADD"sv) {
1659  return translateDateadd(rex_function);
1660  }
1661  if (rex_function->getName() == "DATEDIFF"sv) {
1662  return translateDatediff(rex_function);
1663  }
1664  if (rex_function->getName() == "DATEPART"sv) {
1665  return translateDatepart(rex_function);
1666  }
1667  if (func_resolve(rex_function->getName(), "LENGTH"sv, "CHAR_LENGTH"sv)) {
1668  return translateLength(rex_function);
1669  }
1670  if (rex_function->getName() == "KEY_FOR_STRING"sv) {
1671  return translateKeyForString(rex_function);
1672  }
1673  if (rex_function->getName() == "WIDTH_BUCKET"sv) {
1674  return translateWidthBucket(rex_function);
1675  }
1676  if (rex_function->getName() == "SAMPLE_RATIO"sv) {
1677  return translateSampleRatio(rex_function);
1678  }
1679  if (rex_function->getName() == "CURRENT_USER"sv) {
1680  return translateCurrentUser(rex_function);
1681  }
1682  if (func_resolve(rex_function->getName(),
1683  "LOWER"sv,
1684  "UPPER"sv,
1685  "INITCAP"sv,
1686  "REVERSE"sv,
1687  "REPEAT"sv,
1688  "||"sv,
1689  "LPAD"sv,
1690  "RPAD"sv,
1691  "TRIM"sv,
1692  "LTRIM"sv,
1693  "RTRIM"sv,
1694  "SUBSTRING"sv,
1695  "OVERLAY"sv,
1696  "REPLACE"sv,
1697  "SPLIT_PART"sv,
1698  "REGEXP_REPLACE"sv,
1699  "REGEXP_SUBSTR"sv,
1700  "REGEXP_MATCH"sv,
1701  "JSON_VALUE"sv,
1702  "BASE64_ENCODE"sv,
1703  "BASE64_DECODE"sv,
1704  "TRY_CAST"sv,
1705  "POSITION"sv)) {
1706  return translateStringOper(rex_function);
1707  }
1708  if (func_resolve(rex_function->getName(), "CARDINALITY"sv, "ARRAY_LENGTH"sv)) {
1709  return translateCardinality(rex_function);
1710  }
1711  if (rex_function->getName() == "ITEM"sv) {
1712  return translateItem(rex_function);
1713  }
1714  if (rex_function->getName() == "CURRENT_DATE"sv) {
1715  return translateCurrentDate();
1716  }
1717  if (rex_function->getName() == "CURRENT_TIME"sv) {
1718  return translateCurrentTime();
1719  }
1720  if (rex_function->getName() == "CURRENT_TIMESTAMP"sv) {
1721  return translateCurrentTimestamp();
1722  }
1723  if (rex_function->getName() == "NOW"sv) {
1724  return translateCurrentTimestamp();
1725  }
1726  if (rex_function->getName() == "DATETIME"sv) {
1727  return translateDatetime(rex_function);
1728  }
1729  if (func_resolve(rex_function->getName(), "usTIMESTAMP"sv, "nsTIMESTAMP"sv)) {
1730  return translateHPTLiteral(rex_function);
1731  }
1732  if (rex_function->getName() == "ABS"sv) {
1733  return translateAbs(rex_function);
1734  }
1735  if (rex_function->getName() == "SIGN"sv) {
1736  return translateSign(rex_function);
1737  }
1738  if (func_resolve(rex_function->getName(), "CEIL"sv, "FLOOR"sv)) {
1739  return makeExpr<Analyzer::FunctionOperWithCustomTypeHandling>(
1740  rex_function->getType(),
1741  rex_function->getName(),
1742  translateFunctionArgs(rex_function));
1743  } else if (rex_function->getName() == "ROUND"sv) {
1744  std::vector<std::shared_ptr<Analyzer::Expr>> args =
1745  translateFunctionArgs(rex_function);
1746 
1747  if (rex_function->size() == 1) {
1748  // push a 0 constant if 2nd operand is missing.
1749  // this needs to be done as calcite returns
1750  // only the 1st operand without defaulting the 2nd one
1751  // when the user did not specify the 2nd operand.
1752  SQLTypes t = kSMALLINT;
1753  Datum d;
1754  d.smallintval = 0;
1755  args.push_back(makeExpr<Analyzer::Constant>(t, false, d));
1756  }
1757 
1758  // make sure we have only 2 operands
1759  CHECK(args.size() == 2);
1760 
1761  if (!args[0]->get_type_info().is_number()) {
1762  throw std::runtime_error("Only numeric 1st operands are supported");
1763  }
1764 
1765  // the 2nd operand does not need to be a constant
1766  // it can happily reference another integer column
1767  if (!args[1]->get_type_info().is_integer()) {
1768  throw std::runtime_error("Only integer 2nd operands are supported");
1769  }
1770 
1771  // Calcite may upcast decimals in a way that is
1772  // incompatible with the extension function input. Play it safe and stick with the
1773  // argument type instead.
1774  const SQLTypeInfo ret_ti = args[0]->get_type_info().is_decimal()
1775  ? args[0]->get_type_info()
1776  : rex_function->getType();
1777 
1778  return makeExpr<Analyzer::FunctionOperWithCustomTypeHandling>(
1779  ret_ti, rex_function->getName(), args);
1780  }
1781  if (rex_function->getName() == "DATETIME_PLUS"sv) {
1782  auto dt_plus = makeExpr<Analyzer::FunctionOper>(rex_function->getType(),
1783  rex_function->getName(),
1784  translateFunctionArgs(rex_function));
1785  const auto date_trunc = rewrite_to_date_trunc(dt_plus.get());
1786  if (date_trunc) {
1787  return date_trunc;
1788  }
1789  return translateDateadd(rex_function);
1790  }
1791  if (rex_function->getName() == "/INT"sv) {
1792  CHECK_EQ(size_t(2), rex_function->size());
1793  std::shared_ptr<Analyzer::Expr> lhs = translateScalarRex(rex_function->getOperand(0));
1794  std::shared_ptr<Analyzer::Expr> rhs = translateScalarRex(rex_function->getOperand(1));
1795  const auto rhs_lit = std::dynamic_pointer_cast<Analyzer::Constant>(rhs);
1796  return Parser::OperExpr::normalize(kDIVIDE, kONE, lhs, rhs);
1797  }
1798  if (rex_function->getName() == "Reinterpret"sv) {
1799  CHECK_EQ(size_t(1), rex_function->size());
1800  return translateScalarRex(rex_function->getOperand(0));
1801  }
1802  if (func_resolve(rex_function->getName(),
1803  "ST_X"sv,
1804  "ST_Y"sv,
1805  "ST_XMin"sv,
1806  "ST_YMin"sv,
1807  "ST_XMax"sv,
1808  "ST_YMax"sv,
1809  "ST_NRings"sv,
1810  "ST_NumGeometries"sv,
1811  "ST_NPoints"sv,
1812  "ST_Length"sv,
1813  "ST_Perimeter"sv,
1814  "ST_Area"sv,
1815  "ST_SRID"sv,
1816  "HeavyDB_Geo_PolyBoundsPtr"sv,
1817  "HeavyDB_Geo_PolyRenderGroup"sv)) {
1818  CHECK_EQ(rex_function->size(), size_t(1));
1819  return translateUnaryGeoFunction(rex_function);
1820  }
1821  if (func_resolve(rex_function->getName(), "ST_ConvexHull"sv)) {
1822  CHECK_EQ(rex_function->size(), size_t(1));
1823  SQLTypeInfo ti;
1824  return translateUnaryGeoConstructor(rex_function, ti, false);
1825  }
1826  if (func_resolve(rex_function->getName(),
1827  "convert_meters_to_pixel_width"sv,
1828  "convert_meters_to_pixel_height"sv,
1829  "is_point_in_view"sv,
1830  "is_point_size_in_view"sv)) {
1831  return translateFunctionWithGeoArg(rex_function);
1832  }
1833  if (func_resolve(rex_function->getName(),
1834  "ST_Distance"sv,
1835  "ST_MaxDistance"sv,
1836  "ST_Intersects"sv,
1837  "ST_Disjoint"sv,
1838  "ST_Contains"sv,
1839  "ST_Overlaps"sv,
1840  "ST_Approx_Overlaps"sv,
1841  "ST_Within"sv)) {
1842  CHECK_EQ(rex_function->size(), size_t(2));
1843  return translateBinaryGeoFunction(rex_function);
1844  }
1845  if (func_resolve(rex_function->getName(), "ST_DWithin"sv, "ST_DFullyWithin"sv)) {
1846  CHECK_EQ(rex_function->size(), size_t(3));
1847  return translateTernaryGeoFunction(rex_function);
1848  }
1849  if (rex_function->getName() == "OFFSET_IN_FRAGMENT"sv) {
1850  CHECK_EQ(size_t(0), rex_function->size());
1851  return translateOffsetInFragment();
1852  }
1853  if (rex_function->getName() == "ARRAY"sv) {
1854  // Var args; currently no check. Possible fix-me -- can array have 0 elements?
1855  return translateArrayFunction(rex_function);
1856  }
1857  if (func_resolve(rex_function->getName(),
1858  "ST_GeomFromText"sv,
1859  "ST_GeogFromText"sv,
1860  "ST_Centroid"sv,
1861  "ST_SetSRID"sv,
1862  "ST_Point"sv, // TODO: where should this and below live?
1863  "ST_PointN"sv,
1864  "ST_StartPoint"sv,
1865  "ST_EndPoint"sv,
1866  "ST_Transform"sv)) {
1867  SQLTypeInfo ti;
1868  return translateGeoProjection(rex_function, ti, false);
1869  }
1870  if (func_resolve(rex_function->getName(),
1871  "ST_Intersection"sv,
1872  "ST_Difference"sv,
1873  "ST_Union"sv,
1874  "ST_Buffer"sv,
1875  "ST_ConcaveHull"sv)) {
1876  CHECK_EQ(rex_function->size(), size_t(2));
1877  SQLTypeInfo ti;
1878  return translateBinaryGeoConstructor(rex_function, ti, false);
1879  }
1880  if (func_resolve(rex_function->getName(), "ST_IsEmpty"sv, "ST_IsValid"sv)) {
1881  CHECK_EQ(rex_function->size(), size_t(1));
1882  SQLTypeInfo ti;
1883  return translateUnaryGeoPredicate(rex_function, ti, false);
1884  }
1885  if (func_resolve(rex_function->getName(), "ST_Equals"sv)) {
1886  CHECK_EQ(rex_function->size(), size_t(2));
1887  // Attempt to generate a distance based check for points
1888  if (auto distance_check = translateBinaryGeoFunction(rex_function)) {
1889  return distance_check;
1890  }
1891  SQLTypeInfo ti;
1892  return translateBinaryGeoPredicate(rex_function, ti, false);
1893  }
1894 
1895  auto arg_expr_list = translateFunctionArgs(rex_function);
1896  if (rex_function->getName() == std::string("||") ||
1897  rex_function->getName() == std::string("SUBSTRING")) {
1898  SQLTypeInfo ret_ti(kTEXT, false);
1899  return makeExpr<Analyzer::FunctionOper>(
1900  ret_ti, rex_function->getName(), arg_expr_list);
1901  }
1902 
1903  // Reset possibly wrong return type of rex_function to the return
1904  // type of the optimal valid implementation. The return type can be
1905  // wrong in the case of multiple implementations of UDF functions
1906  // that have different return types but Calcite specifies the return
1907  // type according to the first implementation.
1908  SQLTypeInfo ret_ti;
1909  try {
1910  auto ext_func_sig = bind_function(rex_function->getName(), arg_expr_list);
1911  auto ext_func_args = ext_func_sig.getInputArgs();
1912  CHECK_LE(arg_expr_list.size(), ext_func_args.size());
1913  for (size_t i = 0, di = 0; i < arg_expr_list.size(); i++) {
1914  CHECK_LT(i + di, ext_func_args.size());
1915  auto ext_func_arg = ext_func_args[i + di];
1916  if (ext_func_arg == ExtArgumentType::PInt8 ||
1917  ext_func_arg == ExtArgumentType::PInt16 ||
1918  ext_func_arg == ExtArgumentType::PInt32 ||
1919  ext_func_arg == ExtArgumentType::PInt64 ||
1920  ext_func_arg == ExtArgumentType::PFloat ||
1921  ext_func_arg == ExtArgumentType::PDouble ||
1922  ext_func_arg == ExtArgumentType::PBool) {
1923  di++;
1924  // pointer argument follows length argument:
1925  CHECK(ext_func_args[i + di] == ExtArgumentType::Int64);
1926  }
1927  // fold casts on constants
1928  if (auto constant =
1929  std::dynamic_pointer_cast<Analyzer::Constant>(arg_expr_list[i])) {
1930  auto ext_func_arg_ti = ext_arg_type_to_type_info(ext_func_arg);
1931  if (ext_func_arg_ti != arg_expr_list[i]->get_type_info()) {
1932  arg_expr_list[i] = constant->add_cast(ext_func_arg_ti);
1933  }
1934  }
1935  }
1936 
1937  ret_ti = ext_arg_type_to_type_info(ext_func_sig.getRet());
1938  } catch (ExtensionFunctionBindingError& e) {
1939  LOG(WARNING) << "RelAlgTranslator::translateFunction: " << e.what();
1940  throw;
1941  }
1942 
1943  // By default, the extension function type will not allow nulls. If one of the arguments
1944  // is nullable, the extension function must also explicitly allow nulls.
1945  bool arguments_not_null = true;
1946  for (const auto& arg_expr : arg_expr_list) {
1947  if (!arg_expr->get_type_info().get_notnull()) {
1948  arguments_not_null = false;
1949  break;
1950  }
1951  }
1952  ret_ti.set_notnull(arguments_not_null);
1953 
1954  return makeExpr<Analyzer::FunctionOper>(ret_ti, rex_function->getName(), arg_expr_list);
1955 }
#define CHECK_EQ(x, y)
Definition: Logger.h:297
auto func_resolve
std::shared_ptr< Analyzer::Expr > translateOffsetInFragment() const
std::shared_ptr< Analyzer::Expr > translateCurrentTimestamp() const
std::shared_ptr< Analyzer::Expr > translateBinaryGeoPredicate(const RexFunctionOperator *, SQLTypeInfo &, const bool with_bounds) const
std::shared_ptr< Analyzer::Expr > translateRegexp(const RexFunctionOperator *) const
SQLTypes
Definition: sqltypes.h:53
std::shared_ptr< Analyzer::Expr > translateUnlikely(const RexFunctionOperator *) const
#define LOG(tag)
Definition: Logger.h:283
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const SQLTypeInfo & getType() const
Definition: RelAlgDag.h:284
size_t size() const
Definition: RelAlgDag.h:270
const RexScalar * getOperand(const size_t idx) const
Definition: RelAlgDag.h:272
std::shared_ptr< Analyzer::Expr > translateDateadd(const RexFunctionOperator *) const
static std::shared_ptr< Analyzer::Expr > normalize(const SQLOps optype, const SQLQualifier qual, std::shared_ptr< Analyzer::Expr > left_expr, std::shared_ptr< Analyzer::Expr > right_expr, const Executor *executor=nullptr)
Definition: ParserNode.cpp:370
std::shared_ptr< Analyzer::Expr > translateAbs(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateItem(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateGeoProjection(const RexFunctionOperator *, SQLTypeInfo &, const bool with_bounds) const
std::shared_ptr< Analyzer::Expr > translateDatediff(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateSign(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateUnaryGeoFunction(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateDatetime(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateStringOper(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateCurrentTime() const
int16_t smallintval
Definition: Datum.h:70
std::tuple< T, std::vector< SQLTypeInfo > > bind_function(std::string name, Analyzer::ExpressionPtrVector func_args, const std::vector< T > &ext_funcs, const std::string processor)
#define CHECK_LT(x, y)
Definition: Logger.h:299
Definition: sqltypes.h:67
Analyzer::ExpressionPtrVector translateFunctionArgs(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateUnaryGeoPredicate(const RexFunctionOperator *, SQLTypeInfo &, const bool with_bounds) const
Definition: sqldefs.h:71
#define CHECK_LE(x, y)
Definition: Logger.h:300
std::shared_ptr< Analyzer::Expr > translateUnaryGeoConstructor(const RexFunctionOperator *, SQLTypeInfo &, const bool with_bounds) const
std::shared_ptr< Analyzer::Expr > translateArrayFunction(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateCurrentUser(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateSampleRatio(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateLike(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateLikely(const RexFunctionOperator *) const
void set_notnull(bool n)
Definition: sqltypes.h:497
#define CHECK(condition)
Definition: Logger.h:289
std::shared_ptr< Analyzer::Expr > translateTernaryGeoFunction(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateBinaryGeoFunction(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateWidthBucket(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateFunctionWithGeoArg(const RexFunctionOperator *) const
const std::string & getName() const
Definition: RelAlgDag.h:500
std::shared_ptr< Analyzer::Expr > translateCurrentDate() const
std::shared_ptr< Analyzer::Expr > translateCardinality(const RexFunctionOperator *) const
Definition: Datum.h:67
bool is_decimal() const
Definition: sqltypes.h:579
std::shared_ptr< Analyzer::Expr > translateHPTLiteral(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateDatepart(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateBinaryGeoConstructor(const RexFunctionOperator *, SQLTypeInfo &, const bool with_bounds) const
std::shared_ptr< Analyzer::Expr > rewrite_to_date_trunc(const Analyzer::FunctionOper *dt_plus)
SQLTypeInfo ext_arg_type_to_type_info(const ExtArgumentType ext_arg_type)
std::shared_ptr< Analyzer::Expr > translateKeyForString(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateLength(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateExtract(const RexFunctionOperator *) const

+ Here is the call graph for this function:

Analyzer::ExpressionPtrVector RelAlgTranslator::translateFunctionArgs ( const RexFunctionOperator rex_function) const
private

Definition at line 2587 of file RelAlgTranslator.cpp.

References run_benchmark_import::args, RexOperator::getOperand(), RexOperator::size(), and translateScalarRex().

Referenced by translateArrayFunction(), translateFunction(), translateKeyForString(), and translateStringOper().

2588  {
2589  std::vector<std::shared_ptr<Analyzer::Expr>> args;
2590  for (size_t i = 0; i < rex_function->size(); ++i) {
2591  args.push_back(translateScalarRex(rex_function->getOperand(i)));
2592  }
2593  return args;
2594 }
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
size_t size() const
Definition: RelAlgDag.h:270
const RexScalar * getOperand(const size_t idx) const
Definition: RelAlgDag.h:272

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateFunctionWithGeoArg ( const RexFunctionOperator rex_function) const
private

Definition at line 1814 of file RelAlgTranslatorGeo.cpp.

References run_benchmark_import::args, CHECK, CHECK_EQ, func_resolve, Geospatial::get_compression_scheme(), RexFunctionOperator::getName(), RexOperator::getOperand(), RexOperator::getType(), Datum::intval, kINT, kPOINT, RexOperator::size(), translateGeoFunctionArg(), and translateScalarRex().

Referenced by translateFunction().

1815  {
1816  std::string specialized_geofunc{rex_function->getName()};
1817  if (func_resolve(rex_function->getName(),
1818  "convert_meters_to_pixel_width"sv,
1819  "convert_meters_to_pixel_height"sv)) {
1820  CHECK_EQ(rex_function->size(), 6u);
1821  SQLTypeInfo arg_ti;
1822  std::vector<std::shared_ptr<Analyzer::Expr>> args;
1823  args.push_back(translateScalarRex(rex_function->getOperand(0)));
1824  auto geoargs =
1825  translateGeoFunctionArg(rex_function->getOperand(1), arg_ti, false, true, false);
1826  // only works on points
1827  if (arg_ti.get_type() != kPOINT) {
1828  throw QueryNotSupported(rex_function->getName() +
1829  " expects a point for the second argument");
1830  }
1831 
1832  args.insert(args.end(), geoargs.begin(), geoargs.begin() + 1);
1833 
1834  // Add compression information
1835  Datum input_compression;
1836  input_compression.intval = Geospatial::get_compression_scheme(arg_ti);
1837  args.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_compression));
1838  if (arg_ti.get_input_srid() != 4326) {
1839  throw QueryNotSupported(
1840  rex_function->getName() +
1841  " currently only supports points of with SRID WGS84/EPSG:4326");
1842  }
1843  Datum input_srid;
1844  input_srid.intval = arg_ti.get_input_srid();
1845  args.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_srid));
1846  Datum output_srid;
1847  // Forcing web-mercator projection for now
1848  // TODO(croot): check that the input-to-output conversion routines exist?
1849  output_srid.intval =
1850  arg_ti.get_output_srid() != 900913 ? 900913 : arg_ti.get_output_srid();
1851  args.push_back(makeExpr<Analyzer::Constant>(kINT, false, output_srid));
1852 
1853  args.push_back(translateScalarRex(rex_function->getOperand(2)));
1854  args.push_back(translateScalarRex(rex_function->getOperand(3)));
1855  args.push_back(translateScalarRex(rex_function->getOperand(4)));
1856  args.push_back(translateScalarRex(rex_function->getOperand(5)));
1857  return makeExpr<Analyzer::FunctionOper>(
1858  rex_function->getType(), specialized_geofunc, args);
1859  } else if (rex_function->getName() == "is_point_in_view"sv) {
1860  CHECK_EQ(rex_function->size(), 5u);
1861  SQLTypeInfo arg_ti;
1862  std::vector<std::shared_ptr<Analyzer::Expr>> args;
1863  auto geoargs =
1864  translateGeoFunctionArg(rex_function->getOperand(0), arg_ti, false, true, false);
1865  // only works on points
1866  if (arg_ti.get_type() != kPOINT) {
1867  throw QueryNotSupported(rex_function->getName() +
1868  " expects a point for the second argument");
1869  }
1870 
1871  args.insert(args.end(), geoargs.begin(), geoargs.begin() + 1);
1872 
1873  // Add compression information
1874  Datum input_compression;
1875  input_compression.intval = Geospatial::get_compression_scheme(arg_ti);
1876  args.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_compression));
1877  if (arg_ti.get_input_srid() != 4326) {
1878  throw QueryNotSupported(
1879  rex_function->getName() +
1880  " currently only supports points of with SRID WGS84/EPSG:4326");
1881  }
1882  args.push_back(translateScalarRex(rex_function->getOperand(1)));
1883  args.push_back(translateScalarRex(rex_function->getOperand(2)));
1884  args.push_back(translateScalarRex(rex_function->getOperand(3)));
1885  args.push_back(translateScalarRex(rex_function->getOperand(4)));
1886  return makeExpr<Analyzer::FunctionOper>(
1887  rex_function->getType(), specialized_geofunc, args);
1888  } else if (rex_function->getName() == "is_point_size_in_view"sv) {
1889  CHECK_EQ(rex_function->size(), 6u);
1890  SQLTypeInfo arg_ti;
1891  std::vector<std::shared_ptr<Analyzer::Expr>> args;
1892  auto geoargs =
1893  translateGeoFunctionArg(rex_function->getOperand(0), arg_ti, false, true, false);
1894  // only works on points
1895  if (arg_ti.get_type() != kPOINT) {
1896  throw QueryNotSupported(rex_function->getName() +
1897  " expects a point for the second argument");
1898  }
1899 
1900  args.insert(args.end(), geoargs.begin(), geoargs.begin() + 1);
1901 
1902  // Add compression information
1903  Datum input_compression;
1904  input_compression.intval = Geospatial::get_compression_scheme(arg_ti);
1905  args.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_compression));
1906  if (arg_ti.get_input_srid() != 4326) {
1907  throw QueryNotSupported(
1908  rex_function->getName() +
1909  " currently only supports points of with SRID WGS84/EPSG:4326");
1910  }
1911  args.push_back(translateScalarRex(rex_function->getOperand(1)));
1912  args.push_back(translateScalarRex(rex_function->getOperand(2)));
1913  args.push_back(translateScalarRex(rex_function->getOperand(3)));
1914  args.push_back(translateScalarRex(rex_function->getOperand(4)));
1915  args.push_back(translateScalarRex(rex_function->getOperand(5)));
1916  return makeExpr<Analyzer::FunctionOper>(
1917  rex_function->getType(), specialized_geofunc, args);
1918  }
1919  CHECK(false);
1920  return nullptr;
1921 }
#define CHECK_EQ(x, y)
Definition: Logger.h:297
auto func_resolve
std::vector< std::shared_ptr< Analyzer::Expr > > translateGeoFunctionArg(const RexScalar *rex_scalar, SQLTypeInfo &arg_ti, const bool with_bounds, const bool with_render_group, const bool expand_geo_col, const bool is_projection=false, const bool use_geo_expressions=false, const bool try_to_compress=false, const bool allow_gdal_transforms=false) const
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const SQLTypeInfo & getType() const
Definition: RelAlgDag.h:284
size_t size() const
Definition: RelAlgDag.h:270
const RexScalar * getOperand(const size_t idx) const
Definition: RelAlgDag.h:272
int32_t get_compression_scheme(const SQLTypeInfo &ti)
Definition: Compression.cpp:23
int32_t intval
Definition: Datum.h:71
#define CHECK(condition)
Definition: Logger.h:289
Definition: sqltypes.h:60
const std::string & getName() const
Definition: RelAlgDag.h:500
Definition: Datum.h:67

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::vector< std::shared_ptr< Analyzer::Expr > > RelAlgTranslator::translateGeoColumn ( const RexInput rex_input,
SQLTypeInfo ti,
const bool  with_bounds,
const bool  with_render_group,
const bool  expand_geo_col 
) const
private

Definition at line 28 of file RelAlgTranslatorGeo.cpp.

References run_benchmark_import::args, cat_, CHECK, CHECK_GE, CHECK_LT, ColumnDescriptor::columnType, SQLTypeInfo::get_physical_coord_cols(), SQLTypeInfo::get_type(), RexAbstractInput::getIndex(), Catalog_Namespace::Catalog::getMetadataForColumnBySpi(), RexInput::getSourceNode(), SQLTypeInfo::has_bounds(), SQLTypeInfo::has_render_group(), input_to_nest_level_, IS_GEO, and SPIMAP_GEO_PHYSICAL_INPUT.

Referenced by translateBinaryGeoFunction(), translateGeoFunctionArg(), and translateGeoOverlapsOper().

33  {
34  std::vector<std::shared_ptr<Analyzer::Expr>> args;
35  const auto source = rex_input->getSourceNode();
36  const auto it_rte_idx = input_to_nest_level_.find(source);
37  CHECK(it_rte_idx != input_to_nest_level_.end());
38  const int rte_idx = it_rte_idx->second;
39  const auto& in_metainfo = source->getOutputMetainfo();
40 
41  int32_t table_id{0};
42  int column_id{-1};
43  const auto scan_source = dynamic_cast<const RelScan*>(source);
44  if (scan_source) {
45  // We're at leaf (scan) level and not supposed to have input metadata,
46  // the name and type information come directly from the catalog.
47  CHECK(in_metainfo.empty());
48 
49  const auto td = scan_source->getTableDescriptor();
50  table_id = td->tableId;
51 
52  const auto gcd = cat_.getMetadataForColumnBySpi(table_id, rex_input->getIndex() + 1);
53  CHECK(gcd);
54  ti = gcd->columnType;
55  column_id = gcd->columnId;
56 
57  } else {
58  // Likely backed by a temp table. Read the table ID from the source node and negate it
59  // (see RelAlgTranslator::translateInput)
60  table_id = -source->getId();
61 
62  if (with_bounds || with_render_group) {
63  throw QueryNotSupported(
64  "Geospatial columns not yet supported in intermediate results.");
65  }
66 
67  CHECK(!in_metainfo.empty());
68  CHECK_GE(rte_idx, 0);
69  column_id = rex_input->getIndex();
70  CHECK_LT(static_cast<size_t>(column_id), in_metainfo.size());
71  ti = in_metainfo[column_id].get_type_info();
72  if (expand_geo_col && ti.is_geometry()) {
73  throw QueryNotSupported(
74  "Geospatial columns not yet supported in this temporary table context.");
75  }
76  }
77 
78  if (!IS_GEO(ti.get_type())) {
79  throw QueryNotSupported(
80  "Geospatial expression and operator require geospatial column as their input "
81  "argument(s)");
82  }
83 
84  // Return geo column reference. The geo column may be expanded if required for extension
85  // function arguments. Otherwise, the geo column reference will be translated into
86  // physical columns as required. Bounds column will be added if present and requested.
87  if (expand_geo_col) {
88  for (auto i = 0; i < ti.get_physical_coord_cols(); i++) {
89  const auto pcd = cat_.getMetadataForColumnBySpi(
90  table_id, SPIMAP_GEO_PHYSICAL_INPUT(rex_input->getIndex(), i + 1));
91  auto pcol_ti = pcd->columnType;
92  args.push_back(std::make_shared<Analyzer::ColumnVar>(
93  pcol_ti, table_id, pcd->columnId, rte_idx));
94  }
95  } else {
96  args.push_back(
97  std::make_shared<Analyzer::ColumnVar>(ti, table_id, column_id, rte_idx));
98  }
99  if (with_bounds && ti.has_bounds()) {
100  const auto bounds_cd = cat_.getMetadataForColumnBySpi(
101  table_id,
102  SPIMAP_GEO_PHYSICAL_INPUT(rex_input->getIndex(),
103  ti.get_physical_coord_cols() + 1));
104  auto bounds_ti = bounds_cd->columnType;
105  args.push_back(std::make_shared<Analyzer::ColumnVar>(
106  bounds_ti, table_id, bounds_cd->columnId, rte_idx));
107  }
108  if (with_render_group && ti.has_render_group()) {
109  const auto render_group_cd = cat_.getMetadataForColumnBySpi(
110  table_id,
111  SPIMAP_GEO_PHYSICAL_INPUT(rex_input->getIndex(),
112  ti.get_physical_coord_cols() + 2));
113  auto render_group_ti = render_group_cd->columnType;
114  args.push_back(std::make_shared<Analyzer::ColumnVar>(
115  render_group_ti, table_id, render_group_cd->columnId, rte_idx));
116  }
117  return args;
118 }
#define SPIMAP_GEO_PHYSICAL_INPUT(c, i)
Definition: Catalog.h:84
#define CHECK_GE(x, y)
Definition: Logger.h:302
unsigned getIndex() const
Definition: RelAlgDag.h:77
const ColumnDescriptor * getMetadataForColumnBySpi(const int tableId, const size_t spi) const
Definition: Catalog.cpp:2083
const std::unordered_map< const RelAlgNode *, int > input_to_nest_level_
#define CHECK_LT(x, y)
Definition: Logger.h:299
const RelAlgNode * getSourceNode() const
Definition: RelAlgDag.h:389
#define CHECK(condition)
Definition: Logger.h:289
SQLTypeInfo columnType
#define IS_GEO(T)
Definition: sqltypes.h:298
const Catalog_Namespace::Catalog & cat_

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateGeoComparison ( const RexOperator rex_operator) const
private

Definition at line 1793 of file RelAlgTranslatorGeo.cpp.

References fold_expr(), RexOperator::getOperand(), RexOperator::getOperator(), kBOOLEAN, kDOUBLE, kONE, RexOperator::size(), and translateScalarRex().

Referenced by translateOper().

1794  {
1795  if (rex_operator->size() != size_t(2)) {
1796  return nullptr;
1797  }
1798 
1799  auto geo_distance_expr = translateScalarRex(rex_operator->getOperand(0));
1800  auto func_oper = dynamic_cast<Analyzer::GeoOperator*>(geo_distance_expr.get());
1801  if (func_oper && func_oper->getName() == "ST_Distance"sv) {
1802  const auto& distance_ti = SQLTypeInfo(kDOUBLE, false);
1803  auto distance_expr = translateScalarRex(rex_operator->getOperand(1));
1804  if (distance_expr->get_type_info().get_type() != kDOUBLE) {
1805  distance_expr = distance_expr->add_cast(distance_ti);
1806  }
1807  distance_expr = fold_expr(distance_expr.get());
1808  return makeExpr<Analyzer::BinOper>(
1809  kBOOLEAN, rex_operator->getOperator(), kONE, geo_distance_expr, distance_expr);
1810  }
1811  return nullptr;
1812 }
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
size_t size() const
Definition: RelAlgDag.h:270
const RexScalar * getOperand(const size_t idx) const
Definition: RelAlgDag.h:272
SQLOps getOperator() const
Definition: RelAlgDag.h:282
Definition: sqldefs.h:71
std::shared_ptr< Analyzer::Expr > fold_expr(const Analyzer::Expr *expr)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::vector< std::shared_ptr< Analyzer::Expr > > RelAlgTranslator::translateGeoFunctionArg ( const RexScalar rex_scalar,
SQLTypeInfo arg_ti,
const bool  with_bounds,
const bool  with_render_group,
const bool  expand_geo_col,
const bool  is_projection = false,
const bool  use_geo_expressions = false,
const bool  try_to_compress = false,
const bool  allow_gdal_transforms = false 
) const
private

Definition at line 270 of file RelAlgTranslatorGeo.cpp.

References run_benchmark_import::args, CHECK, CHECK_EQ, CHECK_GE, Geospatial::GeoTypesFactory::createGeoType(), fold_expr(), func_resolve, SQLTypeInfo::get_compression(), SQLTypeInfo::get_input_srid(), SQLTypeInfo::get_output_srid(), SQLTypeInfo::get_subtype(), Analyzer::anonymous_namespace{Analyzer.cpp}::get_ti_from_geo(), SQLTypeInfo::get_type(), IS_GEO, anonymous_namespace{RelAlgExecutor.cpp}::is_projection(), IS_STRING, spatial_type::Transform::isUtm(), kARRAY, kCAST, kDOUBLE, kENCODING_GEOINT, kENCODING_NONE, kGEOGRAPHY, kGEOMETRY, kINT, kLINESTRING, kNULLT, kPOINT, kSMALLINT, kTEXT, kTINYINT, SQLTypeInfo::set_comp_param(), SQLTypeInfo::set_compression(), SQLTypeInfo::set_input_srid(), SQLTypeInfo::set_notnull(), SQLTypeInfo::set_output_srid(), SQLTypeInfo::set_subtype(), SQLTypeInfo::set_type(), to_string(), translateBinaryGeoConstructor(), translateBinaryGeoPredicate(), translateGeoColumn(), translateGeoLiteral(), translateInput(), translateLiteral(), translateScalarRex(), translateUnaryGeoConstructor(), and translateUnaryGeoPredicate().

Referenced by translateBinaryGeoConstructor(), translateBinaryGeoFunction(), translateBinaryGeoPredicate(), translateFunctionWithGeoArg(), translateGeoProjection(), translateTernaryGeoFunction(), translateUnaryGeoConstructor(), translateUnaryGeoFunction(), and translateUnaryGeoPredicate().

279  {
280  std::vector<std::shared_ptr<Analyzer::Expr>> geoargs;
281 
282  const auto rex_input = dynamic_cast<const RexInput*>(rex_scalar);
283  if (rex_input) {
284  const auto input = translateInput(rex_input);
285  const auto column = dynamic_cast<const Analyzer::ColumnVar*>(input.get());
286  if (!column || !column->get_type_info().is_geometry()) {
287  throw QueryNotSupported("Geo function is expecting a geo column argument");
288  }
289  if (use_geo_expressions) {
290  arg_ti = column->get_type_info();
291  return {makeExpr<Analyzer::GeoColumnVar>(column, with_bounds, with_render_group)};
292  }
293  return translateGeoColumn(
294  rex_input, arg_ti, with_bounds, with_render_group, expand_geo_col);
295  }
296  const auto rex_function = dynamic_cast<const RexFunctionOperator*>(rex_scalar);
297  if (rex_function) {
298  if (rex_function->getName() == "ST_Transform"sv) {
299  CHECK_EQ(size_t(2), rex_function->size());
300  const auto rex_scalar0 =
301  dynamic_cast<const RexScalar*>(rex_function->getOperand(0));
302  if (!rex_scalar0) {
303  throw QueryNotSupported(rex_function->getName() + ": unexpected first argument");
304  }
305 
306  const auto rex_literal =
307  dynamic_cast<const RexLiteral*>(rex_function->getOperand(1));
308  if (!rex_literal) {
309  throw QueryNotSupported(rex_function->getName() +
310  ": second argument is expected to be a literal");
311  }
312  const auto e = translateLiteral(rex_literal);
313  auto ce = std::dynamic_pointer_cast<Analyzer::Constant>(e);
314  if (!ce || !e->get_type_info().is_integer()) {
315  throw QueryNotSupported(rex_function->getName() + ": expecting integer SRID");
316  }
317  int32_t srid = 0;
318  if (e->get_type_info().get_type() == kSMALLINT) {
319  srid = static_cast<int32_t>(ce->get_constval().smallintval);
320  } else if (e->get_type_info().get_type() == kTINYINT) {
321  srid = static_cast<int32_t>(ce->get_constval().tinyintval);
322  } else if (e->get_type_info().get_type() == kINT) {
323  srid = static_cast<int32_t>(ce->get_constval().intval);
324  } else {
325  throw QueryNotSupported(rex_function->getName() + ": expecting integer SRID");
326  }
327  bool allow_result_gdal_transform = false;
328  const auto rex_function0 = dynamic_cast<const RexFunctionOperator*>(rex_scalar0);
329  if (rex_function0 && func_resolve(rex_function0->getName(),
330  "ST_Intersection"sv,
331  "ST_Difference"sv,
332  "ST_Union"sv,
333  "ST_Buffer"sv,
334  "ST_ConcaveHull"sv,
335  "ST_ConvexHull"sv)) {
336  // TODO: the design of geo operators currently doesn't allow input srid overrides.
337  // For example, in case of ST_Area(ST_Transform(ST_Buffer(geo_column,0), 900913))
338  // we can ask geos runtime to transform ST_Buffer's output from 4326 to 900913,
339  // however, ST_Area geo operator would still rely on the first arg's typeinfo
340  // to codegen srid arg values in the ST_Area_ extension function call. And it will
341  // still pick up that transform so the coords will be transformed to 900913 twice.
342 
343  // Sink result transform into geos runtime
344  // allow_result_gdal_transform = true;
345  }
346  if (!allow_gdal_transforms && !allow_result_gdal_transform) {
347  if (srid != 900913 && ((use_geo_expressions || is_projection) && srid != 4326 &&
349  throw QueryNotSupported(rex_function->getName() + ": unsupported output SRID " +
350  std::to_string(srid));
351  }
352  }
353  arg_ti.set_output_srid(srid); // Forward output srid down to argument translation
354  bool arg0_use_geo_expressions = is_projection ? true : use_geo_expressions;
355  if (allow_gdal_transforms) {
356  arg0_use_geo_expressions = false;
357  }
358  auto arg0 = translateGeoFunctionArg(rex_scalar0,
359  arg_ti,
360  with_bounds,
361  with_render_group,
362  expand_geo_col,
364  arg0_use_geo_expressions);
365 
366  if (use_geo_expressions) {
367  CHECK_EQ(arg0.size(), size_t(1));
368  auto arg0_ti = arg0.front()->get_type_info(); // make a copy so we can override
369  arg0_ti.set_output_srid(srid);
370  if (arg0_ti.get_type() == kPOINT) {
371  // the output type is going to be fully transformed, so set the input srid to
372  // the output srid
373  const auto input_srid = arg0_ti.get_input_srid();
374  arg0_ti.set_input_srid(srid);
375  // geo transforms projections leave the result decompressed in a register
376  arg0_ti.set_compression(kENCODING_NONE);
377  arg0_ti.set_comp_param(0);
378  // reset recursive arg_ti, as the output type of transform will be properly
379  // transformed to the desired SRID
380  arg_ti.set_output_srid(srid);
381  arg_ti.set_input_srid(srid);
382  return {makeExpr<Analyzer::GeoTransformOperator>(
383  arg0_ti, rex_function->getName(), arg0, input_srid, srid)};
384  } else {
385  if (auto geo_constant =
386  std::dynamic_pointer_cast<Analyzer::GeoConstant>(arg0.front())) {
387  // fold transform
388  auto cast_geo_constant = geo_constant->add_cast(arg0_ti);
389  // update return type info
390  arg_ti = cast_geo_constant->get_type_info();
391  return {cast_geo_constant};
392  } else if (auto col_var =
393  std::dynamic_pointer_cast<Analyzer::ColumnVar>(arg0.front())) {
394  const auto& col_ti = col_var->get_type_info();
395  CHECK(col_ti.is_geometry());
396  if (col_ti.get_type() != kPOINT) {
397  arg_ti.set_input_srid(col_ti.get_input_srid());
398  // fall through to transform code below
399  }
400  } else {
401  if (!allow_gdal_transforms && !allow_result_gdal_transform) {
402  throw std::runtime_error(
403  "Transform on non-POINT geospatial types not yet supported in this "
404  "context.");
405  }
406  }
407  }
408  }
409 
410  if (arg_ti.get_input_srid() > 0) {
411  if (!allow_gdal_transforms && !allow_result_gdal_transform) {
412  if (arg_ti.get_input_srid() != 4326) {
413  throw QueryNotSupported(rex_function->getName() +
414  ": unsupported input SRID " +
415  std::to_string(arg_ti.get_input_srid()));
416  }
417  }
418  // Established that the input SRID is valid
419  if (allow_result_gdal_transform) {
420  // If gdal transform has been allowed, then it has been sunk into geos runtime.
421  // The returning geometry has already been transformed, de-register transform.
422  if (arg_ti.get_input_srid() != srid) {
423  arg_ti.set_input_srid(srid);
424  }
425  }
426  arg_ti.set_output_srid(srid);
427  } else {
428  throw QueryNotSupported(rex_function->getName() +
429  ": unexpected input SRID, unable to transform");
430  }
431  return arg0;
432  } else if (func_resolve(
433  rex_function->getName(), "ST_GeomFromText"sv, "ST_GeogFromText"sv)) {
434  CHECK(rex_function->size() == size_t(1) || rex_function->size() == size_t(2));
435  if (use_geo_expressions) {
436  int32_t srid = 0;
437  if (rex_function->size() == 2) {
438  // user supplied srid
439  const auto rex_literal =
440  dynamic_cast<const RexLiteral*>(rex_function->getOperand(1));
441  if (!rex_literal) {
442  throw QueryNotSupported(rex_function->getName() +
443  ": second argument is expected to be a literal");
444  }
445  const auto e = translateLiteral(rex_literal);
446  auto ce = std::dynamic_pointer_cast<Analyzer::Constant>(e);
447  if (!ce || !e->get_type_info().is_integer()) {
448  throw QueryNotSupported(rex_function->getName() + ": expecting integer SRID");
449  }
450  if (e->get_type_info().get_type() == kSMALLINT) {
451  srid = static_cast<int32_t>(ce->get_constval().smallintval);
452  } else if (e->get_type_info().get_type() == kTINYINT) {
453  srid = static_cast<int32_t>(ce->get_constval().tinyintval);
454  } else if (e->get_type_info().get_type() == kINT) {
455  srid = static_cast<int32_t>(ce->get_constval().intval);
456  } else {
457  throw QueryNotSupported(rex_function->getName() + " expecting integer SRID");
458  }
459  if (srid != 0 && srid != 4326 && srid != 900913) {
460  throw QueryNotSupported(rex_function->getName() + ": unsupported SRID " +
461  std::to_string(srid));
462  }
463  }
464  arg_ti.set_input_srid(srid); // Input SRID
465  // leave the output srid unset in case a transform was above us
466 
467  if (rex_function->getName() == "ST_GeogFromText"sv) {
468  arg_ti.set_subtype(kGEOGRAPHY);
469  } else {
470  arg_ti.set_subtype(kGEOMETRY);
471  }
472 
473  auto func_args = translateGeoFunctionArg(rex_function->getOperand(0),
474  arg_ti,
475  with_bounds,
476  with_render_group,
477  expand_geo_col,
479  use_geo_expressions);
480  CHECK_GE(func_args.size(), size_t(1));
481  return func_args;
482  }
483 
484  // First - register srid, then send it to geo literal translation
485  int32_t srid = 0;
486  if (rex_function->size() == 2) {
487  const auto rex_literal =
488  dynamic_cast<const RexLiteral*>(rex_function->getOperand(1));
489  if (!rex_literal) {
490  throw QueryNotSupported(rex_function->getName() +
491  ": second argument is expected to be a literal");
492  }
493  const auto e = translateLiteral(rex_literal);
494  auto ce = std::dynamic_pointer_cast<Analyzer::Constant>(e);
495  if (!ce || !e->get_type_info().is_integer()) {
496  throw QueryNotSupported(rex_function->getName() + ": expecting integer SRID");
497  }
498  if (e->get_type_info().get_type() == kSMALLINT) {
499  srid = static_cast<int32_t>(ce->get_constval().smallintval);
500  } else if (e->get_type_info().get_type() == kTINYINT) {
501  srid = static_cast<int32_t>(ce->get_constval().tinyintval);
502  } else if (e->get_type_info().get_type() == kINT) {
503  srid = static_cast<int32_t>(ce->get_constval().intval);
504  } else {
505  throw QueryNotSupported(rex_function->getName() + " expecting integer SRID");
506  }
507  if (srid != 0 && srid != 4326 && srid != 900913) {
508  throw QueryNotSupported(rex_function->getName() + ": unsupported SRID " +
509  std::to_string(srid));
510  }
511  }
512  arg_ti.set_input_srid(srid); // Input SRID
513  arg_ti.set_output_srid(srid); // Output SRID is the same - no transform
514 
515  const auto rex_literal =
516  dynamic_cast<const RexLiteral*>(rex_function->getOperand(0));
517  if (!rex_literal) {
518  throw QueryNotSupported(rex_function->getName() +
519  " expects a string literal as first argument");
520  }
521  auto arg0 = translateGeoLiteral(rex_literal, arg_ti, with_bounds);
522  arg_ti.set_subtype((rex_function->getName() == "ST_GeogFromText"sv) ? kGEOGRAPHY
523  : kGEOMETRY);
524  return arg0;
525  } else if (rex_function->getName() == "ST_PointN"sv) {
526  // uses geo expressions
527  const auto rex_scalar0 =
528  dynamic_cast<const RexScalar*>(rex_function->getOperand(0));
529  if (!rex_scalar0) {
530  throw QueryNotSupported(rex_function->getName() +
531  ": expects scalar as first argument");
532  }
533  auto arg0 = translateGeoFunctionArg(rex_scalar0,
534  arg_ti,
535  with_bounds,
536  with_render_group,
537  expand_geo_col,
538  /*is_projection=*/false,
539  /*use_geo_expressions=*/true);
540  CHECK_EQ(arg0.size(), size_t(1));
541  CHECK(arg0.front());
542  if (arg0.front()->get_type_info().get_type() != kLINESTRING) {
543  throw QueryNotSupported(rex_function->getName() +
544  " expects LINESTRING as first argument");
545  }
546  const auto rex_literal =
547  dynamic_cast<const RexLiteral*>(rex_function->getOperand(1));
548  if (!rex_literal) {
549  throw QueryNotSupported(rex_function->getName() +
550  ": second argument is expected to be a literal");
551  }
552  const auto e = translateLiteral(rex_literal);
553  auto ce = std::dynamic_pointer_cast<Analyzer::Constant>(e);
554  if (!ce || !e->get_type_info().is_integer()) {
555  throw QueryNotSupported(rex_function->getName() +
556  ": expecting integer index as second argument");
557  }
558  int32_t index = 0;
559  if (e->get_type_info().get_type() == kSMALLINT) {
560  index = static_cast<int32_t>(ce->get_constval().smallintval);
561  } else if (e->get_type_info().get_type() == kTINYINT) {
562  index = static_cast<int32_t>(ce->get_constval().tinyintval);
563  } else if (e->get_type_info().get_type() == kINT) {
564  index = static_cast<int32_t>(ce->get_constval().intval);
565  } else {
566  throw QueryNotSupported(rex_function->getName() + " expecting integer index");
567  }
568  if (index == 0) {
569  // maybe we will just return NULL here?
570  throw QueryNotSupported(rex_function->getName() + ": invalid index");
571  }
572  arg0.push_back(e);
573  auto oper_ti =
574  arg0.front()->get_type_info(); // make a copy so we can reset nullness and type
575  oper_ti.set_type(kPOINT);
576  oper_ti.set_notnull(false);
577 
578  arg_ti = oper_ti; // TODO: remove
579 
580  return {makeExpr<Analyzer::GeoOperator>(oper_ti, rex_function->getName(), arg0)};
581 
582  } else if (rex_function->getName() == "ST_StartPoint"sv ||
583  rex_function->getName() == "ST_EndPoint"sv) {
584  std::vector<std::shared_ptr<Analyzer::Expr>> args;
585  CHECK_EQ(size_t(1), rex_function->size());
586  const auto arg_exprs = translateGeoFunctionArg(rex_function->getOperand(0),
587  arg_ti,
588  with_bounds,
589  with_render_group,
590  expand_geo_col,
592  /*use_geo_expressions=*/true);
593  CHECK_EQ(arg_exprs.size(), size_t(1));
594  CHECK(arg_exprs.front());
595  const auto arg_expr_ti = arg_exprs.front()->get_type_info();
596  if (arg_expr_ti.get_type() != kLINESTRING) {
597  throw QueryNotSupported(rex_function->getName() +
598  " expected LINESTRING argument. Received " +
599  arg_expr_ti.toString());
600  }
601  args.push_back(arg_exprs.front());
602 
603  auto oper_ti = args.back()->get_type_info(); // make a copy so we can override type
604  oper_ti.set_type(kPOINT);
605 
606  arg_ti = oper_ti; // TODO: remove
607 
608  return {makeExpr<Analyzer::GeoOperator>(oper_ti, rex_function->getName(), args)};
609  } else if (rex_function->getName() == "ST_SRID"sv) {
610  CHECK_EQ(size_t(1), rex_function->size());
611  const auto rex_scalar0 =
612  dynamic_cast<const RexScalar*>(rex_function->getOperand(0));
613  if (!rex_scalar0) {
614  throw QueryNotSupported(rex_function->getName() +
615  ": expects scalar as first argument");
616  }
617  auto arg0 = translateGeoFunctionArg(
618  rex_scalar0, arg_ti, with_bounds, with_render_group, expand_geo_col);
619  if (!IS_GEO(arg_ti.get_type())) {
620  throw QueryNotSupported(rex_function->getName() + " expects geometry argument");
621  }
622  return arg0;
623  } else if (rex_function->getName() == "ST_SetSRID"sv) {
624  CHECK_EQ(size_t(2), rex_function->size());
625  const auto rex_literal =
626  dynamic_cast<const RexLiteral*>(rex_function->getOperand(1));
627  if (!rex_literal) {
628  throw QueryNotSupported(rex_function->getName() +
629  ": second argument is expected to be a literal");
630  }
631  const auto e = translateLiteral(rex_literal);
632  auto ce = std::dynamic_pointer_cast<Analyzer::Constant>(e);
633  if (!ce || !e->get_type_info().is_integer()) {
634  throw QueryNotSupported(rex_function->getName() + ": expecting integer SRID");
635  }
636  int32_t srid = 0;
637  if (e->get_type_info().get_type() == kSMALLINT) {
638  srid = static_cast<int32_t>(ce->get_constval().smallintval);
639  } else if (e->get_type_info().get_type() == kTINYINT) {
640  srid = static_cast<int32_t>(ce->get_constval().tinyintval);
641  } else if (e->get_type_info().get_type() == kINT) {
642  srid = static_cast<int32_t>(ce->get_constval().intval);
643  } else {
644  throw QueryNotSupported(rex_function->getName() + ": expecting integer SRID");
645  }
646 
647  const auto rex_scalar0 =
648  dynamic_cast<const RexScalar*>(rex_function->getOperand(0));
649  if (!rex_scalar0) {
650  throw QueryNotSupported(rex_function->getName() +
651  ": expects scalar as first argument");
652  }
653 
654  // Only convey the request to compress if dealing with 4326 geo
655  auto arg0 = translateGeoFunctionArg(rex_scalar0,
656  arg_ti,
657  with_bounds,
658  with_render_group,
659  expand_geo_col,
660  is_projection,
661  use_geo_expressions,
662  (try_to_compress && (srid == 4326)));
663 
664  CHECK(!arg0.empty() && arg0.front());
665  if (!IS_GEO(arg_ti.get_type()) && !use_geo_expressions) {
666  throw QueryNotSupported(rex_function->getName() + " expects geometry argument");
667  }
668  arg_ti.set_input_srid(srid); // Input SRID
669  arg_ti.set_output_srid(srid); // Output SRID is the same - no transform
670  if (auto geo_expr = std::dynamic_pointer_cast<Analyzer::GeoExpr>(arg0.front())) {
671  CHECK_EQ(arg0.size(), size_t(1));
672  auto ti = geo_expr->get_type_info();
673  ti.set_input_srid(srid);
674  ti.set_output_srid(srid);
675  return {geo_expr->add_cast(ti)};
676  }
677  return arg0;
678  } else if (rex_function->getName() == "CastToGeography"sv) {
679  CHECK_EQ(size_t(1), rex_function->size());
680  const auto rex_scalar0 =
681  dynamic_cast<const RexScalar*>(rex_function->getOperand(0));
682  if (!rex_scalar0) {
683  throw QueryNotSupported(rex_function->getName() +
684  ": expects scalar as first argument");
685  }
686  auto arg0 = translateGeoFunctionArg(rex_scalar0,
687  arg_ti,
688  with_bounds,
689  with_render_group,
690  expand_geo_col,
691  /*is_projection=*/false,
692  use_geo_expressions);
693  CHECK(!arg0.empty());
694  if (auto geo_expr = std::dynamic_pointer_cast<Analyzer::GeoExpr>(arg0.front())) {
695  auto arg_ti = geo_expr->get_type_info(); // make a copy
696  arg_ti.set_subtype(kGEOGRAPHY);
697  return {geo_expr->add_cast(arg_ti)};
698  }
699  if (use_geo_expressions) {
700  arg_ti = arg0.front()->get_type_info();
701  arg_ti.set_subtype(kGEOGRAPHY);
702  arg0.front()->set_type_info(arg_ti);
703  }
704  if (!IS_GEO(arg_ti.get_type())) {
705  throw QueryNotSupported(rex_function->getName() + " expects geometry argument");
706  }
707  if (arg_ti.get_output_srid() != 4326) {
708  throw QueryNotSupported(rex_function->getName() +
709  " expects geometry with SRID=4326");
710  }
711  arg_ti.set_subtype(kGEOGRAPHY);
712  return arg0;
713  } else if (rex_function->getName() == "ST_Point"sv) {
714  CHECK_EQ(size_t(2), rex_function->size());
715  arg_ti.set_type(kPOINT);
716  arg_ti.set_subtype(kGEOMETRY);
717  arg_ti.set_input_srid(0);
718  arg_ti.set_output_srid(0);
720 
721  auto coord1 = translateScalarRex(rex_function->getOperand(0));
722  auto coord2 = translateScalarRex(rex_function->getOperand(1));
723  auto d_ti = SQLTypeInfo(kDOUBLE, false);
724  auto cast_coord1 = coord1->add_cast(d_ti);
725  auto cast_coord2 = coord2->add_cast(d_ti);
726  // First try to fold to geo literal
727  auto folded_coord1 = fold_expr(cast_coord1.get());
728  auto folded_coord2 = fold_expr(cast_coord2.get());
729  auto const_coord1 = std::dynamic_pointer_cast<Analyzer::Constant>(folded_coord1);
730  auto const_coord2 = std::dynamic_pointer_cast<Analyzer::Constant>(folded_coord2);
731  if (const_coord1 && const_coord2 && !use_geo_expressions) {
732  CHECK(const_coord1->get_type_info().get_type() == kDOUBLE);
733  CHECK(const_coord2->get_type_info().get_type() == kDOUBLE);
734  std::string wkt = "POINT(" +
735  std::to_string(const_coord1->get_constval().doubleval) + " " +
736  std::to_string(const_coord2->get_constval().doubleval) + ")";
737  RexLiteral rex_literal{wkt, kTEXT, kNULLT, 0, 0, 0, 0};
738  auto args = translateGeoLiteral(&rex_literal, arg_ti, false);
739  CHECK(arg_ti.get_type() == kPOINT);
740  return args;
741  }
742  const auto is_local_alloca = !is_projection;
743  if (!is_local_alloca || use_geo_expressions) {
744  if (try_to_compress) {
745  arg_ti.set_input_srid(4326);
746  arg_ti.set_output_srid(4326);
747  }
748  return {makeExpr<Analyzer::GeoOperator>(
749  arg_ti,
750  rex_function->getName(),
751  std::vector<std::shared_ptr<Analyzer::Expr>>{folded_coord1, folded_coord2})};
752  }
753  // Couldn't fold to geo literal, construct [and compress] on the fly
754  auto da_ti = SQLTypeInfo(kARRAY, true);
755  da_ti.set_subtype(kDOUBLE);
756  da_ti.set_size(16);
757  if (try_to_compress) {
758  // Switch to compressed coord array
759  da_ti.set_subtype(kINT);
760  da_ti.set_size(8);
761  da_ti.set_input_srid(4326);
762  da_ti.set_output_srid(4326);
763  da_ti.set_compression(kENCODING_GEOINT);
764  da_ti.set_comp_param(32);
765  // Register point compression
766  arg_ti.set_input_srid(4326);
767  arg_ti.set_output_srid(4326);
768  arg_ti.set_compression(kENCODING_GEOINT);
769  arg_ti.set_comp_param(32);
770  }
771  auto cast_coords = {folded_coord1, folded_coord2};
772  auto ae = makeExpr<Analyzer::ArrayExpr>(da_ti, cast_coords, false, is_local_alloca);
773  SQLTypeInfo tia_ti = da_ti;
774  tia_ti.set_subtype(kTINYINT);
775  return {makeExpr<Analyzer::UOper>(tia_ti, false, kCAST, ae)};
776  } else if (rex_function->getName() == "ST_Centroid"sv) {
777  CHECK_EQ(size_t(1), rex_function->size());
778  arg_ti.set_type(kPOINT);
779  arg_ti.set_subtype(kGEOMETRY);
780  arg_ti.set_input_srid(0);
781  arg_ti.set_output_srid(0);
782  arg_ti.set_compression(kENCODING_NONE);
783 
784  SQLTypeInfo geo_ti;
785  int legacy_transform_srid = 0; // discard
786  auto geoargs = translateGeoFunctionArg(rex_function->getOperand(0),
787  geo_ti,
788  /*with_bounds=*/false,
789  /*with_render_group=*/false,
790  /*expand_geo_col=*/true,
791  /*is_projection=*/false,
792  /*use_geo_expressions=*/true);
793  CHECK_EQ(geoargs.size(), size_t(1));
794  if (geo_ti.get_output_srid() > 0) {
795  // Pick up the arg's srid
796  arg_ti.set_input_srid(geo_ti.get_output_srid());
797  arg_ti.set_output_srid(geo_ti.get_output_srid());
798  }
799  if (try_to_compress) {
800  // Point compression is requested by a higher level [4326] operation
801  if (geo_ti.get_output_srid() == 0) {
802  // srid-less geo is considered and is forced to be 4326
803  arg_ti.set_input_srid(4326);
804  arg_ti.set_output_srid(4326);
805  } else {
806  CHECK_EQ(arg_ti.get_output_srid(), 4326);
807  }
808  arg_ti.set_compression(kENCODING_GEOINT);
809  arg_ti.set_comp_param(32);
810  }
811  if (geo_ti.get_input_srid() != geo_ti.get_output_srid() &&
812  geo_ti.get_output_srid() > 0 &&
813  std::dynamic_pointer_cast<Analyzer::ColumnVar>(geoargs.front())) {
814  // Centroid argument is transformed before use,
815  // pass the transform to the geo operator
816  legacy_transform_srid = geo_ti.get_output_srid();
817  }
818  return {makeExpr<Analyzer::GeoOperator>(
819  arg_ti,
820  rex_function->getName(),
821  std::vector<std::shared_ptr<Analyzer::Expr>>{geoargs.front()},
822  legacy_transform_srid > 0 ? std::make_optional<int>(legacy_transform_srid)
823  : std::nullopt)};
824  } else if (func_resolve(rex_function->getName(), "ST_ConvexHull"sv)) {
825  CHECK_EQ(size_t(1), rex_function->size());
826  // What geo type will the constructor return? Could be anything.
827  return {translateUnaryGeoConstructor(rex_function, arg_ti, with_bounds)};
828  } else if (func_resolve(rex_function->getName(),
829  "ST_Intersection"sv,
830  "ST_Difference"sv,
831  "ST_Union"sv,
832  "ST_Buffer"sv,
833  "ST_ConcaveHull"sv)) {
834  CHECK_EQ(size_t(2), rex_function->size());
835  // What geo type will the constructor return? Could be anything.
836  return {translateBinaryGeoConstructor(rex_function, arg_ti, with_bounds)};
837  } else if (func_resolve(rex_function->getName(), "ST_IsEmpty"sv, "ST_IsValid"sv)) {
838  CHECK_EQ(size_t(1), rex_function->size());
839  return {translateUnaryGeoPredicate(rex_function, arg_ti, with_bounds)};
840  } else if (func_resolve(rex_function->getName(), "ST_Equals"sv)) {
841  CHECK_EQ(size_t(2), rex_function->size());
842  return {translateBinaryGeoPredicate(rex_function, arg_ti, with_bounds)};
843  } else {
844  throw QueryNotSupported("Unsupported argument: " + rex_function->getName());
845  }
846  }
847  const auto rex_literal = dynamic_cast<const RexLiteral*>(rex_scalar);
848  if (rex_literal) {
849  if (use_geo_expressions) {
850  const auto translated_literal = translateLiteral(rex_literal);
851  auto const translated_literal_type = translated_literal->get_type_info().get_type();
852  if (!IS_STRING(translated_literal_type) && !IS_GEO(translated_literal_type)) {
853  // This stops crashes in the createGeoType call below due to datum.stringval
854  // being uninitialized when the datum isn't even a string, let alone a geo string
855  // There needs to be specific handling for ST_NumGeometries in the code above
856  // but I don't know what category it would fall over (it's not GEOS, and it
857  // returns an INT, not a BOOL or other geo)
858  // simon.eves 8/15/22
859  throw QueryNotSupported("Geospatial function requires geo literal.");
860  }
861  const auto constant_expr =
862  dynamic_cast<const Analyzer::Constant*>(translated_literal.get());
863  CHECK(constant_expr);
864  if (constant_expr->get_is_null()) {
865  // TODO: we could lift this limitation by assuming a minimum type per function
866  throw QueryNotSupported("Geospatial functions require typed nulls.");
867  }
868  const auto& datum = constant_expr->get_constval();
869  CHECK(datum.stringval);
870  auto geospatial_base = Geospatial::GeoTypesFactory::createGeoType(*datum.stringval);
871  CHECK(geospatial_base);
872  SQLTypeInfo ti;
873  ti.set_type(get_ti_from_geo(geospatial_base.get()));
874  if (arg_ti.get_subtype() == kGEOGRAPHY) {
876  } else {
878  }
879  ti.set_input_srid(arg_ti.get_input_srid());
880  ti.set_output_srid(arg_ti.get_output_srid() == 0 ? arg_ti.get_input_srid()
881  : arg_ti.get_output_srid());
882  // TODO: remove dependence on arg_ti
883  if (ti.get_output_srid() == 4326 || arg_ti.get_compression() == kENCODING_GEOINT) {
885  ti.set_comp_param(32);
886  }
887  ti.set_notnull(true);
888  // Before removing dependence on arg_ti need to note that ST_Transform uses it
889  // as a vehicle to pass transform output SRID to its args.
890  // arg_ti is also expected to be filled with relevant data, which wasn't done here.
891  // Not filling arg_ti with the geo constant data (which went to ti instead)
892  // resulted in GeoConstant::add_cast adopting a corrupt type info,
893  // which later killed codegen. Need to complete arg_ti composition:
894  arg_ti = ti;
895  return {makeExpr<Analyzer::GeoConstant>(std::move(geospatial_base), ti)};
896  }
897  return translateGeoLiteral(rex_literal, arg_ti, with_bounds);
898  }
899  throw QueryNotSupported("Geo function argument not supported");
900 }
void set_compression(EncodingType c)
Definition: sqltypes.h:501
#define CHECK_EQ(x, y)
Definition: Logger.h:297
static std::unique_ptr< GeoBase > createGeoType(const std::string &wkt_or_wkb_hex)
Definition: Types.cpp:1061
auto func_resolve
std::shared_ptr< Analyzer::Expr > translateBinaryGeoPredicate(const RexFunctionOperator *, SQLTypeInfo &, const bool with_bounds) const
std::vector< std::shared_ptr< Analyzer::Expr > > translateGeoFunctionArg(const RexScalar *rex_scalar, SQLTypeInfo &arg_ti, const bool with_bounds, const bool with_render_group, const bool expand_geo_col, const bool is_projection=false, const bool use_geo_expressions=false, const bool try_to_compress=false, const bool allow_gdal_transforms=false) const
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
HOST DEVICE void set_subtype(SQLTypes st)
Definition: sqltypes.h:491
#define CHECK_GE(x, y)
Definition: Logger.h:302
Definition: sqldefs.h:48
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:380
std::string to_string(char const *&&v)
std::shared_ptr< Analyzer::Expr > translateInput(const RexInput *) const
std::vector< std::shared_ptr< Analyzer::Expr > > translateGeoLiteral(const RexLiteral *, SQLTypeInfo &, bool) const
void set_input_srid(int d)
Definition: sqltypes.h:494
static std::shared_ptr< Analyzer::Expr > translateLiteral(const RexLiteral *)
void set_output_srid(int s)
Definition: sqltypes.h:496
void set_comp_param(int p)
Definition: sqltypes.h:502
Definition: sqltypes.h:67
std::shared_ptr< Analyzer::Expr > translateUnaryGeoPredicate(const RexFunctionOperator *, SQLTypeInfo &, const bool with_bounds) const
std::shared_ptr< Analyzer::Expr > translateUnaryGeoConstructor(const RexFunctionOperator *, SQLTypeInfo &, const bool with_bounds) const
bool is_projection(const RelAlgExecutionUnit &ra_exe_unit)
static bool isUtm(unsigned const srid)
Definition: Transform.h:43
#define IS_STRING(T)
Definition: sqltypes.h:297
HOST DEVICE int get_input_srid() const
Definition: sqltypes.h:384
void set_notnull(bool n)
Definition: sqltypes.h:497
#define CHECK(condition)
Definition: Logger.h:289
Definition: sqltypes.h:60
std::vector< std::shared_ptr< Analyzer::Expr > > translateGeoColumn(const RexInput *, SQLTypeInfo &, const bool with_bounds, const bool with_render_group, const bool expand_geo_col) const
#define IS_GEO(T)
Definition: sqltypes.h:298
std::shared_ptr< Analyzer::Expr > translateBinaryGeoConstructor(const RexFunctionOperator *, SQLTypeInfo &, const bool with_bounds) const
SQLTypes get_ti_from_geo(const Geospatial::GeoBase *geo)
Definition: Analyzer.cpp:3775
std::shared_ptr< Analyzer::Expr > fold_expr(const Analyzer::Expr *expr)
HOST DEVICE int get_output_srid() const
Definition: sqltypes.h:386
HOST DEVICE void set_type(SQLTypes t)
Definition: sqltypes.h:490

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::vector< std::shared_ptr< Analyzer::Expr > > RelAlgTranslator::translateGeoLiteral ( const RexLiteral rex_literal,
SQLTypeInfo ti,
bool  with_bounds 
) const
private

Definition at line 120 of file RelAlgTranslatorGeo.cpp.

References run_benchmark_import::args, CHECK, Geospatial::compress_coords(), Datum::doubleval, SQLTypeInfo::get_compression(), SQLTypeInfo::get_output_srid(), SQLTypeInfo::get_type(), Geospatial::GeoTypesFactory::getGeoColumns(), SQLTypeInfo::has_bounds(), Datum::intval, kARRAY, kDOUBLE, kENCODING_GEOINT, kGEOMETRY, kINT, kMULTILINESTRING, kMULTIPOLYGON, kPOLYGON, kTEXT, kTINYINT, SQLTypeInfo::set_comp_param(), SQLTypeInfo::set_compression(), SQLTypeInfo::set_input_srid(), SQLTypeInfo::set_output_srid(), SQLTypeInfo::set_size(), SQLTypeInfo::set_subtype(), Datum::tinyintval, and translateLiteral().

Referenced by translateGeoFunctionArg().

123  {
124  CHECK(rex_literal);
125  if (rex_literal->getType() != kTEXT) {
126  throw std::runtime_error("Geo literals must be strings");
127  }
128 
129  // TODO: use geo conversion here
130  const auto e = translateLiteral(rex_literal);
131  auto wkt = std::dynamic_pointer_cast<Analyzer::Constant>(e);
132  CHECK(wkt);
133  std::vector<double> coords;
134  std::vector<double> bounds;
135  std::vector<int> ring_sizes;
136  std::vector<int> poly_rings;
137  int32_t srid = ti.get_output_srid();
139  *wkt->get_constval().stringval, ti, coords, bounds, ring_sizes, poly_rings)) {
140  throw QueryNotSupported("Could not read geometry from text");
141  }
143  ti.set_input_srid(srid);
144  ti.set_output_srid(srid);
145  // Compress geo literals by default
146  if (srid == 4326) {
148  ti.set_comp_param(32);
149  }
150 
151  std::vector<std::shared_ptr<Analyzer::Expr>> args;
152 
153  std::vector<uint8_t> compressed_coords = Geospatial::compress_coords(coords, ti);
154  std::list<std::shared_ptr<Analyzer::Expr>> compressed_coords_exprs;
155  for (auto cc : compressed_coords) {
156  Datum d;
157  d.tinyintval = cc;
158  auto e = makeExpr<Analyzer::Constant>(kTINYINT, false, d);
159  compressed_coords_exprs.push_back(e);
160  }
161  SQLTypeInfo arr_ti = SQLTypeInfo(kARRAY, true);
162  arr_ti.set_subtype(kTINYINT);
163  arr_ti.set_size(compressed_coords.size() * sizeof(int8_t));
164  arr_ti.set_compression(ti.get_compression());
165  arr_ti.set_comp_param((ti.get_compression() == kENCODING_GEOINT) ? 32 : 64);
166  args.push_back(makeExpr<Analyzer::Constant>(arr_ti, false, compressed_coords_exprs));
167 
168  auto lit_type = ti.get_type();
169  if (lit_type == kMULTILINESTRING || lit_type == kPOLYGON || lit_type == kMULTIPOLYGON) {
170  // [linest]ring sizes
171  std::list<std::shared_ptr<Analyzer::Expr>> ring_size_exprs;
172  for (auto c : ring_sizes) {
173  Datum d;
174  d.intval = c;
175  auto e = makeExpr<Analyzer::Constant>(kINT, false, d);
176  ring_size_exprs.push_back(e);
177  }
178  SQLTypeInfo arr_ti = SQLTypeInfo(kARRAY, true);
179  arr_ti.set_subtype(kINT);
180  arr_ti.set_size(ring_sizes.size() * sizeof(int32_t));
181  args.push_back(makeExpr<Analyzer::Constant>(arr_ti, false, ring_size_exprs));
182 
183  // poly rings
184  if (lit_type == kMULTIPOLYGON) {
185  std::list<std::shared_ptr<Analyzer::Expr>> poly_rings_exprs;
186  for (auto c : poly_rings) {
187  Datum d;
188  d.intval = c;
189  auto e = makeExpr<Analyzer::Constant>(kINT, false, d);
190  poly_rings_exprs.push_back(e);
191  }
192  SQLTypeInfo arr_ti = SQLTypeInfo(kARRAY, true);
193  arr_ti.set_subtype(kINT);
194  arr_ti.set_size(poly_rings.size() * sizeof(int32_t));
195  args.push_back(makeExpr<Analyzer::Constant>(arr_ti, false, poly_rings_exprs));
196  }
197  }
198 
199  if (with_bounds && ti.has_bounds()) {
200  // bounds
201  std::list<std::shared_ptr<Analyzer::Expr>> bounds_exprs;
202  for (auto b : bounds) {
203  Datum d;
204  d.doubleval = b;
205  auto e = makeExpr<Analyzer::Constant>(kDOUBLE, false, d);
206  bounds_exprs.push_back(e);
207  }
208  SQLTypeInfo arr_ti = SQLTypeInfo(kARRAY, true);
209  arr_ti.set_subtype(kDOUBLE);
210  arr_ti.set_size(bounds.size() * sizeof(double));
211  args.push_back(makeExpr<Analyzer::Constant>(arr_ti, false, bounds_exprs));
212  }
213 
214  return args;
215 }
int8_t tinyintval
Definition: Datum.h:69
void set_compression(EncodingType c)
Definition: sqltypes.h:501
void set_size(int s)
Definition: sqltypes.h:498
HOST DEVICE void set_subtype(SQLTypes st)
Definition: sqltypes.h:491
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:380
int32_t intval
Definition: Datum.h:71
void set_input_srid(int d)
Definition: sqltypes.h:494
std::vector< uint8_t > compress_coords(const std::vector< double > &coords, const SQLTypeInfo &ti)
Definition: Compression.cpp:52
static std::shared_ptr< Analyzer::Expr > translateLiteral(const RexLiteral *)
bool has_bounds() const
Definition: sqltypes.h:467
void set_output_srid(int s)
Definition: sqltypes.h:496
void set_comp_param(int p)
Definition: sqltypes.h:502
static bool getGeoColumns(const std::string &wkt_or_wkb_hex, SQLTypeInfo &ti, std::vector< double > &coords, std::vector< double > &bounds, std::vector< int > &ring_sizes, std::vector< int > &poly_rings, const bool promote_poly_to_mpoly=false)
Definition: Types.cpp:1079
Definition: sqltypes.h:67
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:388
#define CHECK(condition)
Definition: Logger.h:289
Definition: sqltypes.h:60
Definition: Datum.h:67
double doubleval
Definition: Datum.h:74
HOST DEVICE int get_output_srid() const
Definition: sqltypes.h:386

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateGeoOverlapsOper ( const RexOperator rex_operator) const
private

Definition at line 1923 of file RelAlgTranslatorGeo.cpp.

References CHECK, CHECK_EQ, CHECK_GT, SQLTypeInfo::get_type(), RexOperator::getOperand(), kBOOLEAN, kONE, kOVERLAPS, kPOINT, RexOperator::size(), and translateGeoColumn().

Referenced by translateOverlapsOper().

1924  {
1925  CHECK_EQ(rex_operator->size(), 2u);
1926 
1927  auto translate_input =
1928  [&](const RexScalar* operand) -> std::shared_ptr<Analyzer::Expr> {
1929  const auto input = dynamic_cast<const RexInput*>(operand);
1930  CHECK(input);
1931 
1932  SQLTypeInfo ti;
1933  const auto exprs = translateGeoColumn(input, ti, true, false, false);
1934  CHECK_GT(exprs.size(), 0u);
1935  if (ti.get_type() == kPOINT) {
1936  return exprs.front();
1937  } else {
1938  return exprs.back();
1939  }
1940  };
1941 
1942  SQLQualifier sql_qual{kONE};
1943  SQLOps sql_op{kOVERLAPS};
1944  return makeExpr<Analyzer::BinOper>(SQLTypeInfo(kBOOLEAN, false),
1945  false,
1946  sql_op,
1947  sql_qual,
1948  translate_input(rex_operator->getOperand(1)),
1949  translate_input(rex_operator->getOperand(0)));
1950 }
#define CHECK_EQ(x, y)
Definition: Logger.h:297
SQLQualifier
Definition: sqldefs.h:71
size_t size() const
Definition: RelAlgDag.h:270
const RexScalar * getOperand(const size_t idx) const
Definition: RelAlgDag.h:272
SQLOps
Definition: sqldefs.h:28
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:380
#define CHECK_GT(x, y)
Definition: Logger.h:301
Definition: sqldefs.h:71
#define CHECK(condition)
Definition: Logger.h:289
std::vector< std::shared_ptr< Analyzer::Expr > > translateGeoColumn(const RexInput *, SQLTypeInfo &, const bool with_bounds, const bool with_render_group, const bool expand_geo_col) const

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateGeoProjection ( const RexFunctionOperator rex_function,
SQLTypeInfo ti,
const bool  with_bounds 
) const
private

Definition at line 902 of file RelAlgTranslatorGeo.cpp.

References CHECK, RelRexToStringConfig::defaults(), func_resolve, RexFunctionOperator::getName(), RexOperator::getOperand(), Geospatial::GeoBase::kPROJECTION, RexFunctionOperator::toString(), and translateGeoFunctionArg().

Referenced by translateFunction().

905  {
906  // note that this is a bit of a misnomer, as ST_SetSRID embedded in a transform will
907  // eventually use geo expressions -- just not here
908  const bool use_geo_projections = !(rex_function->getName() == "ST_GeomFromText" ||
909  rex_function->getName() == "ST_GeogFromText" ||
910  rex_function->getName() == "ST_SetSRID");
911  auto geoargs = translateGeoFunctionArg(rex_function,
912  ti,
913  /*with_bounds=*/false,
914  /*with_render_group=*/false,
915  /*expand_geo_col=*/true,
916  /*is_projection=*/true,
917  /*use_geo_expressions=*/use_geo_projections);
918  CHECK(!geoargs.empty());
919  if (std::dynamic_pointer_cast<const Analyzer::GeoExpr>(geoargs.front()) &&
920  !geoargs.front()->get_type_info().is_array()) {
921  if (rex_function->getName() == "ST_Transform" &&
922  std::dynamic_pointer_cast<const Analyzer::GeoConstant>(geoargs.front())) {
923  return makeExpr<Analyzer::GeoUOper>(
924  Geospatial::GeoBase::GeoOp::kPROJECTION, ti, ti, geoargs);
925  }
926  // GeoExpression
927  return geoargs.front();
928  }
929  bool allow_gdal_transform = false;
930  if (rex_function->getName() == "ST_Transform") {
931  const auto rex_scalar0 = dynamic_cast<const RexScalar*>(rex_function->getOperand(0));
932  const auto rex_function0 = dynamic_cast<const RexFunctionOperator*>(rex_scalar0);
933  if (rex_function0 && func_resolve(rex_function0->getName(),
934  "ST_Intersection"sv,
935  "ST_Difference"sv,
936  "ST_Union"sv,
937  "ST_Buffer"sv,
938  "ST_ConcaveHull"sv,
939  "ST_ConvexHull"sv)) {
940  // Allow projection of gdal-transformed geos outputs
941  allow_gdal_transform = true;
942  }
943  }
944  if (use_geo_projections && !allow_gdal_transform) {
945  throw std::runtime_error("Geospatial projection for function " +
946  rex_function->toString(RelRexToStringConfig::defaults()) +
947  " not yet supported in this context");
948  }
949  return makeExpr<Analyzer::GeoUOper>(
950  Geospatial::GeoBase::GeoOp::kPROJECTION, ti, ti, geoargs);
951 }
auto func_resolve
std::vector< std::shared_ptr< Analyzer::Expr > > translateGeoFunctionArg(const RexScalar *rex_scalar, SQLTypeInfo &arg_ti, const bool with_bounds, const bool with_render_group, const bool expand_geo_col, const bool is_projection=false, const bool use_geo_expressions=false, const bool try_to_compress=false, const bool allow_gdal_transforms=false) const
const RexScalar * getOperand(const size_t idx) const
Definition: RelAlgDag.h:272
static RelRexToStringConfig defaults()
Definition: RelAlgDag.h:49
#define CHECK(condition)
Definition: Logger.h:289
std::string toString(RelRexToStringConfig config=RelRexToStringConfig::defaults()) const override
Definition: RelAlgDag.h:502
const std::string & getName() const
Definition: RelAlgDag.h:500

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateHPTLiteral ( const RexFunctionOperator rex_function) const
private

Definition at line 2639 of file RelAlgTranslator.cpp.

References CHECK_EQ, RexOperator::getOperand(), RexOperator::getType(), RexOperator::size(), to_string(), and translateScalarRex().

Referenced by translateFunction().

2640  {
2641  /* since calcite uses Avatica package called DateTimeUtils to parse timestamp strings.
2642  Therefore any string having fractional seconds more 3 places after the decimal
2643  (milliseconds) will get truncated to 3 decimal places, therefore we lose precision
2644  (us|ns). Issue: [BE-2461] Here we are hijacking literal cast to Timestamp(6|9) from
2645  calcite and translating them to generate our own casts.
2646  */
2647  CHECK_EQ(size_t(1), rex_function->size());
2648  const auto operand = translateScalarRex(rex_function->getOperand(0));
2649  const auto& operand_ti = operand->get_type_info();
2650  const auto& target_ti = rex_function->getType();
2651  if (!operand_ti.is_string()) {
2652  throw std::runtime_error(
2653  "High precision timestamp cast argument must be a string. Input type is: " +
2654  operand_ti.get_type_name());
2655  } else if (!target_ti.is_high_precision_timestamp()) {
2656  throw std::runtime_error(
2657  "Cast target type should be high precision timestamp. Input type is: " +
2658  target_ti.get_type_name());
2659  } else if (target_ti.get_dimension() != 6 && target_ti.get_dimension() != 9) {
2660  throw std::runtime_error(
2661  "Cast target type should be TIMESTAMP(6|9). Input type is: TIMESTAMP(" +
2662  std::to_string(target_ti.get_dimension()) + ")");
2663  } else {
2664  return operand->add_cast(target_ti);
2665  }
2666 }
#define CHECK_EQ(x, y)
Definition: Logger.h:297
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const SQLTypeInfo & getType() const
Definition: RelAlgDag.h:284
size_t size() const
Definition: RelAlgDag.h:270
const RexScalar * getOperand(const size_t idx) const
Definition: RelAlgDag.h:272
std::string to_string(char const *&&v)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateInOper ( const RexOperator rex_operator) const
private

Definition at line 676 of file RelAlgTranslator.cpp.

References CHECK, CHECK_EQ, anonymous_namespace{RelAlgTranslator.cpp}::datum_from_scalar_tv(), g_enable_watchdog, anonymous_namespace{RelAlgTranslator.cpp}::get_in_values_expr(), getInIntegerSetExpr(), RexOperator::getOperand(), just_explain_, kCAST, kENCODING_DICT, kENCODING_NONE, run_benchmark_import::result, RexOperator::size(), and translateScalarRex().

Referenced by translateOper().

677  {
678  if (just_explain_) {
679  throw std::runtime_error("EXPLAIN is not supported with sub-queries");
680  }
681  CHECK(rex_operator->size() == 2);
682  const auto lhs = translateScalarRex(rex_operator->getOperand(0));
683  const auto rhs = rex_operator->getOperand(1);
684  const auto rex_subquery = dynamic_cast<const RexSubQuery*>(rhs);
685  CHECK(rex_subquery);
686  auto ti = lhs->get_type_info();
687  auto result = rex_subquery->getExecutionResult();
688  CHECK(result);
689  auto& row_set = result->getRows();
690  CHECK_EQ(size_t(1), row_set->colCount());
691  const auto& rhs_ti = row_set->getColType(0);
692  if (rhs_ti.get_type() != ti.get_type()) {
693  throw std::runtime_error(
694  "The two sides of the IN operator must have the same type; found " +
695  ti.get_type_name() + " and " + rhs_ti.get_type_name());
696  }
697  row_set->moveToBegin();
698  if (row_set->entryCount() > 10000) {
699  std::shared_ptr<Analyzer::Expr> expr;
700  if ((ti.is_integer() || (ti.is_string() && ti.get_compression() == kENCODING_DICT)) &&
701  !row_set->getQueryMemDesc().didOutputColumnar()) {
702  expr = getInIntegerSetExpr(lhs, *row_set);
703  // Handle the highly unlikely case when the InIntegerSet ended up being tiny.
704  // Just let it fall through the usual InValues path at the end of this method,
705  // its codegen knows to use inline comparisons for few values.
706  if (expr && std::static_pointer_cast<Analyzer::InIntegerSet>(expr)
707  ->get_value_list()
708  .size() <= 100) {
709  expr = nullptr;
710  }
711  } else {
712  expr = get_in_values_expr(lhs, *row_set);
713  }
714  if (expr) {
715  return expr;
716  }
717  }
718  std::list<std::shared_ptr<Analyzer::Expr>> value_exprs;
719  while (true) {
720  auto row = row_set->getNextRow(true, false);
721  if (row.empty()) {
722  break;
723  }
724  if (g_enable_watchdog && value_exprs.size() >= 10000) {
725  throw std::runtime_error(
726  "Unable to handle 'expr IN (subquery)', subquery returned 10000+ rows.");
727  }
728  auto scalar_tv = boost::get<ScalarTargetValue>(&row[0]);
729  Datum d{0};
730  bool is_null_const{false};
731  std::tie(d, is_null_const) = datum_from_scalar_tv(scalar_tv, ti);
732  if (ti.is_string() && ti.get_compression() != kENCODING_NONE) {
733  auto ti_none_encoded = ti;
734  ti_none_encoded.set_compression(kENCODING_NONE);
735  auto none_encoded_string = makeExpr<Analyzer::Constant>(ti, is_null_const, d);
736  auto dict_encoded_string =
737  std::make_shared<Analyzer::UOper>(ti, false, kCAST, none_encoded_string);
738  value_exprs.push_back(dict_encoded_string);
739  } else {
740  value_exprs.push_back(makeExpr<Analyzer::Constant>(ti, is_null_const, d));
741  }
742  }
743  return makeExpr<Analyzer::InValues>(lhs, value_exprs);
744 }
#define CHECK_EQ(x, y)
Definition: Logger.h:297
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
size_t size() const
Definition: RelAlgDag.h:270
const RexScalar * getOperand(const size_t idx) const
Definition: RelAlgDag.h:272
Definition: sqldefs.h:48
std::shared_ptr< Analyzer::Expr > getInIntegerSetExpr(std::shared_ptr< Analyzer::Expr > arg, const ResultSet &val_set) const
bool g_enable_watchdog
std::shared_ptr< Analyzer::Expr > get_in_values_expr(std::shared_ptr< Analyzer::Expr > arg, const ResultSet &val_set)
#define CHECK(condition)
Definition: Logger.h:289
std::pair< Datum, bool > datum_from_scalar_tv(const ScalarTargetValue *scalar_tv, const SQLTypeInfo &ti) noexcept
const bool just_explain_
Definition: Datum.h:67

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateInput ( const RexInput rex_input) const
private

Definition at line 478 of file RelAlgTranslator.cpp.

References cat_, CHECK, CHECK_EQ, CHECK_GE, CHECK_LE, CHECK_LT, RelRexToStringConfig::defaults(), RexAbstractInput::getIndex(), Catalog_Namespace::Catalog::getMetadataForColumnBySpi(), RelAlgNode::getOutputMetainfo(), RexInput::getSourceNode(), input_to_nest_level_, join_types_, kTEXT, and LEFT.

Referenced by translateGeoFunctionArg().

479  {
480  const auto source = rex_input->getSourceNode();
481  const auto it_rte_idx = input_to_nest_level_.find(source);
482  CHECK(it_rte_idx != input_to_nest_level_.end())
483  << "Not found in input_to_nest_level_, source="
484  << source->toString(RelRexToStringConfig::defaults());
485  const int rte_idx = it_rte_idx->second;
486  const auto scan_source = dynamic_cast<const RelScan*>(source);
487  const auto& in_metainfo = source->getOutputMetainfo();
488  if (scan_source) {
489  // We're at leaf (scan) level and not supposed to have input metadata,
490  // the name and type information come directly from the catalog.
491  CHECK(in_metainfo.empty());
492  const auto table_desc = scan_source->getTableDescriptor();
493  const auto cd =
494  cat_.getMetadataForColumnBySpi(table_desc->tableId, rex_input->getIndex() + 1);
495  CHECK(cd);
496  auto col_ti = cd->columnType;
497  if (col_ti.is_string()) {
498  col_ti.set_type(kTEXT);
499  }
500  if (cd->isVirtualCol) {
501  // TODO(alex): remove at some point, we only need this fixup for backwards
502  // compatibility with old imported data
503  CHECK_EQ("rowid", cd->columnName);
504  col_ti.set_size(8);
505  }
506  CHECK_LE(static_cast<size_t>(rte_idx), join_types_.size());
507  if (rte_idx > 0 && join_types_[rte_idx - 1] == JoinType::LEFT) {
508  col_ti.set_notnull(false);
509  }
510  return std::make_shared<Analyzer::ColumnVar>(
511  col_ti, table_desc->tableId, cd->columnId, rte_idx);
512  }
513  CHECK(!in_metainfo.empty()) << "for "
514  << source->toString(RelRexToStringConfig::defaults());
515  CHECK_GE(rte_idx, 0);
516  const size_t col_id = rex_input->getIndex();
517  CHECK_LT(col_id, in_metainfo.size());
518  auto col_ti = in_metainfo[col_id].get_type_info();
519 
520  if (join_types_.size() > 0) {
521  CHECK_LE(static_cast<size_t>(rte_idx), join_types_.size());
522  if (rte_idx > 0 && join_types_[rte_idx - 1] == JoinType::LEFT) {
523  col_ti.set_notnull(false);
524  }
525  }
526 
527  return std::make_shared<Analyzer::ColumnVar>(col_ti, -source->getId(), col_id, rte_idx);
528 }
const std::vector< JoinType > join_types_
#define CHECK_EQ(x, y)
Definition: Logger.h:297
#define CHECK_GE(x, y)
Definition: Logger.h:302
unsigned getIndex() const
Definition: RelAlgDag.h:77
const ColumnDescriptor * getMetadataForColumnBySpi(const int tableId, const size_t spi) const
Definition: Catalog.cpp:2083
const std::unordered_map< const RelAlgNode *, int > input_to_nest_level_
#define CHECK_LT(x, y)
Definition: Logger.h:299
Definition: sqltypes.h:67
#define CHECK_LE(x, y)
Definition: Logger.h:300
static RelRexToStringConfig defaults()
Definition: RelAlgDag.h:49
const RelAlgNode * getSourceNode() const
Definition: RelAlgDag.h:389
#define CHECK(condition)
Definition: Logger.h:289
const std::vector< TargetMetaInfo > & getOutputMetainfo() const
Definition: RelAlgDag.h:876
const Catalog_Namespace::Catalog & cat_

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateIntervalExprForWindowFraming ( std::shared_ptr< Analyzer::Expr order_key,
bool  for_preceding_bound,
const Analyzer::BinOper frame_bound_expr 
) const
private

Definition at line 2342 of file RelAlgTranslator.cpp.

References Datum::bigintval, CHECK_NE, daINVALID, anonymous_namespace{RelAlgTranslator.cpp}::determineTimeUnit(), anonymous_namespace{RelAlgTranslator.cpp}::determineTimeValMultiplierForTimeType(), Analyzer::BinOper::get_left_operand(), Analyzer::BinOper::get_right_operand(), Analyzer::Expr::get_type_info(), kBIGINT, kDATE, kDAY, kDECIMAL, kDOUBLE, kHOUR, kINT, kMINUTE, kMONTH, kNUMERIC, kSECOND, kSMALLINT, kTIME, kTIMESTAMP, kTINYINT, kYEAR, to_dateadd_field(), and UNREACHABLE.

Referenced by translateWindowFunction().

2345  {
2346  // translate time interval expression and prepare appropriate frame bound expression:
2347  // a) manually compute time unit datum: time type
2348  // b) use dateadd expression: date and timestamp
2349  const auto order_key_ti = order_key->get_type_info();
2350  const auto frame_bound_ti = frame_bound_expr->get_type_info();
2351  const auto time_val_expr =
2352  dynamic_cast<const Analyzer::Constant*>(frame_bound_expr->get_left_operand());
2353  const auto time_unit_val_expr =
2354  dynamic_cast<const Analyzer::Constant*>(frame_bound_expr->get_right_operand());
2355  ExtractField time_unit =
2356  determineTimeUnit(frame_bound_ti.get_type(), time_unit_val_expr);
2357  bool invalid_time_unit_type = false;
2358  bool invalid_frame_bound_expr_type = false;
2359  Datum d;
2360  auto prepare_time_value_datum = [&d,
2361  &invalid_frame_bound_expr_type,
2362  &time_val_expr,
2363  &for_preceding_bound](bool is_timestamp_second) {
2364  // currently, Calcite only accepts interval with second, so to represent
2365  // smaller time units like millisecond, we have to use decimal point like
2366  // INTERVAL 0.003 SECOND (for millisecond)
2367  // thus, depending on what time unit we want to represent, Calcite analyzes
2368  // the time value to one of following two types: integer and decimal (and
2369  // numeric) types
2370  switch (time_val_expr->get_type_info().get_type()) {
2371  case kTINYINT: {
2372  d.bigintval = time_val_expr->get_constval().tinyintval;
2373  break;
2374  }
2375  case kSMALLINT: {
2376  d.bigintval = time_val_expr->get_constval().smallintval;
2377  break;
2378  }
2379  case kINT: {
2380  d.bigintval = time_val_expr->get_constval().intval;
2381  break;
2382  }
2383  case kBIGINT: {
2384  d.bigintval = time_val_expr->get_constval().bigintval;
2385  break;
2386  }
2387  case kDECIMAL:
2388  case kNUMERIC: {
2389  if (!is_timestamp_second) {
2390  // date and time type only use integer type as their time value
2391  invalid_frame_bound_expr_type = true;
2392  break;
2393  }
2394  d.bigintval = time_val_expr->get_constval().bigintval;
2395  break;
2396  }
2397  case kDOUBLE: {
2398  if (!is_timestamp_second) {
2399  // date and time type only use integer type as their time value
2400  invalid_frame_bound_expr_type = true;
2401  break;
2402  }
2403  d.bigintval = time_val_expr->get_constval().doubleval *
2404  pow(10, time_val_expr->get_type_info().get_scale());
2405  break;
2406  }
2407  default: {
2408  invalid_frame_bound_expr_type = true;
2409  break;
2410  }
2411  }
2412  if (for_preceding_bound) {
2413  d.bigintval *= -1;
2414  }
2415  };
2416 
2417  switch (order_key_ti.get_type()) {
2418  case kTIME: {
2419  if (time_val_expr->get_type_info().is_integer()) {
2420  if (time_unit == kSECOND || time_unit == kMINUTE || time_unit == kHOUR) {
2421  const auto time_multiplier = determineTimeValMultiplierForTimeType(
2422  frame_bound_ti.get_type(), time_unit_val_expr);
2423  switch (time_val_expr->get_type_info().get_type()) {
2424  case kTINYINT: {
2425  d.bigintval = time_val_expr->get_constval().tinyintval * time_multiplier;
2426  break;
2427  }
2428  case kSMALLINT: {
2429  d.bigintval = time_val_expr->get_constval().smallintval * time_multiplier;
2430  break;
2431  }
2432  case kINT: {
2433  d.bigintval = time_val_expr->get_constval().intval * time_multiplier;
2434  break;
2435  }
2436  case kBIGINT: {
2437  d.bigintval = time_val_expr->get_constval().bigintval * time_multiplier;
2438  break;
2439  }
2440  default: {
2441  UNREACHABLE();
2442  break;
2443  }
2444  }
2445  } else {
2446  invalid_frame_bound_expr_type = true;
2447  }
2448  } else {
2449  invalid_time_unit_type = true;
2450  }
2451  if (invalid_frame_bound_expr_type) {
2452  throw std::runtime_error(
2453  "Invalid time unit is used to define window frame bound expression for " +
2454  order_key_ti.get_type_name() + " type");
2455  } else if (invalid_time_unit_type) {
2456  throw std::runtime_error(
2457  "Window frame bound expression has an invalid type for " +
2458  order_key_ti.get_type_name() + " type");
2459  }
2460  return std::make_shared<Analyzer::Constant>(kBIGINT, false, d);
2461  }
2462  case kDATE: {
2464  if (time_val_expr->get_type_info().is_integer()) {
2465  switch (time_unit) {
2466  case kDAY: {
2467  daField = to_dateadd_field("day");
2468  break;
2469  }
2470  case kMONTH: {
2471  daField = to_dateadd_field("month");
2472  break;
2473  }
2474  case kYEAR: {
2475  daField = to_dateadd_field("year");
2476  break;
2477  }
2478  default: {
2479  invalid_frame_bound_expr_type = true;
2480  break;
2481  }
2482  }
2483  } else {
2484  invalid_time_unit_type = true;
2485  }
2486  if (invalid_frame_bound_expr_type) {
2487  throw std::runtime_error(
2488  "Invalid time unit is used to define window frame bound expression for " +
2489  order_key_ti.get_type_name() + " type");
2490  } else if (invalid_time_unit_type) {
2491  throw std::runtime_error(
2492  "Window frame bound expression has an invalid type for " +
2493  order_key_ti.get_type_name() + " type");
2494  }
2496  prepare_time_value_datum(false);
2497  const auto cast_number_units = makeExpr<Analyzer::Constant>(kBIGINT, false, d);
2498  const int dim = order_key_ti.get_dimension();
2499  return makeExpr<Analyzer::DateaddExpr>(
2500  SQLTypeInfo(kTIMESTAMP, dim, 0, false), daField, cast_number_units, order_key);
2501  }
2502  case kTIMESTAMP: {
2504  switch (time_unit) {
2505  case kSECOND: {
2506  switch (time_val_expr->get_type_info().get_scale()) {
2507  case 0: {
2508  daField = to_dateadd_field("second");
2509  break;
2510  }
2511  case 3: {
2512  daField = to_dateadd_field("millisecond");
2513  break;
2514  }
2515  case 6: {
2516  daField = to_dateadd_field("microsecond");
2517  break;
2518  }
2519  case 9: {
2520  daField = to_dateadd_field("nanosecond");
2521  break;
2522  }
2523  default:
2524  UNREACHABLE();
2525  break;
2526  }
2527  prepare_time_value_datum(true);
2528  break;
2529  }
2530  case kMINUTE: {
2531  daField = to_dateadd_field("minute");
2532  prepare_time_value_datum(false);
2533  break;
2534  }
2535  case kHOUR: {
2536  daField = to_dateadd_field("hour");
2537  prepare_time_value_datum(false);
2538  break;
2539  }
2540  case kDAY: {
2541  daField = to_dateadd_field("day");
2542  prepare_time_value_datum(false);
2543  break;
2544  }
2545  case kMONTH: {
2546  daField = to_dateadd_field("month");
2547  prepare_time_value_datum(false);
2548  break;
2549  }
2550  case kYEAR: {
2551  daField = to_dateadd_field("year");
2552  prepare_time_value_datum(false);
2553  break;
2554  }
2555  default: {
2556  invalid_time_unit_type = true;
2557  break;
2558  }
2559  }
2560  if (!invalid_time_unit_type) {
2562  const auto cast_number_units = makeExpr<Analyzer::Constant>(kBIGINT, false, d);
2563  const int dim = order_key_ti.get_dimension();
2564  return makeExpr<Analyzer::DateaddExpr>(SQLTypeInfo(kTIMESTAMP, dim, 0, false),
2565  daField,
2566  cast_number_units,
2567  order_key);
2568  }
2569  return nullptr;
2570  }
2571  default: {
2572  UNREACHABLE();
2573  break;
2574  }
2575  }
2576  if (invalid_frame_bound_expr_type) {
2577  throw std::runtime_error(
2578  "Invalid time unit is used to define window frame bound expression for " +
2579  order_key_ti.get_type_name() + " type");
2580  } else if (invalid_time_unit_type) {
2581  throw std::runtime_error("Window frame bound expression has an invalid type for " +
2582  order_key_ti.get_type_name() + " type");
2583  }
2584  return nullptr;
2585 }
Definition: sqltypes.h:64
const Expr * get_right_operand() const
Definition: Analyzer.h:452
#define UNREACHABLE()
Definition: Logger.h:333
DateaddField
Definition: DateAdd.h:42
#define CHECK_NE(x, y)
Definition: Logger.h:298
size_t determineTimeValMultiplierForTimeType(const SQLTypes &window_frame_bound_type, const Analyzer::Constant *const_expr)
int64_t bigintval
Definition: Datum.h:72
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:83
Definition: sqltypes.h:68
ExtractField
const Expr * get_left_operand() const
Definition: Analyzer.h:451
Definition: sqltypes.h:60
Definition: Datum.h:67
DateaddField to_dateadd_field(const std::string &field)
ExtractField determineTimeUnit(const SQLTypes &window_frame_bound_type, const Analyzer::Constant *const_expr)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateItem ( const RexFunctionOperator rex_function) const
private

Definition at line 1501 of file RelAlgTranslator.cpp.

References CHECK_EQ, RexOperator::getOperand(), kARRAY_AT, kONE, RexOperator::size(), and translateScalarRex().

Referenced by translateFunction().

1502  {
1503  CHECK_EQ(size_t(2), rex_function->size());
1504  const auto base = translateScalarRex(rex_function->getOperand(0));
1505  const auto index = translateScalarRex(rex_function->getOperand(1));
1506  return makeExpr<Analyzer::BinOper>(
1507  base->get_type_info().get_elem_type(), false, kARRAY_AT, kONE, base, index);
1508 }
#define CHECK_EQ(x, y)
Definition: Logger.h:297
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
size_t size() const
Definition: RelAlgDag.h:270
const RexScalar * getOperand(const size_t idx) const
Definition: RelAlgDag.h:272
Definition: sqldefs.h:71

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateKeyForString ( const RexFunctionOperator rex_function) const
private

Definition at line 1373 of file RelAlgTranslator.cpp.

References run_benchmark_import::args, CHECK_EQ, RexFunctionOperator::getName(), kUNNEST, and translateFunctionArgs().

Referenced by translateFunction().

1374  {
1375  const auto& args = translateFunctionArgs(rex_function);
1376  CHECK_EQ(size_t(1), args.size());
1377  const auto expr = dynamic_cast<Analyzer::Expr*>(args[0].get());
1378  if (nullptr == expr || !expr->get_type_info().is_string() ||
1379  expr->get_type_info().is_varlen()) {
1380  throw std::runtime_error(rex_function->getName() +
1381  " expects a dictionary encoded text column.");
1382  }
1383  auto unnest_arg = dynamic_cast<Analyzer::UOper*>(expr);
1384  if (unnest_arg && unnest_arg->get_optype() == SQLOps::kUNNEST) {
1385  throw std::runtime_error(
1386  rex_function->getName() +
1387  " does not support unnest operator as its input expression.");
1388  }
1389  return makeExpr<Analyzer::KeyForStringExpr>(args[0]);
1390 }
#define CHECK_EQ(x, y)
Definition: Logger.h:297
Analyzer::ExpressionPtrVector translateFunctionArgs(const RexFunctionOperator *) const
const std::string & getName() const
Definition: RelAlgDag.h:500

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateLength ( const RexFunctionOperator rex_function) const
private

Definition at line 1365 of file RelAlgTranslator.cpp.

References CHECK_EQ, RexFunctionOperator::getName(), RexOperator::getOperand(), RexOperator::size(), and translateScalarRex().

Referenced by translateFunction().

1366  {
1367  CHECK_EQ(size_t(1), rex_function->size());
1368  const auto str_arg = translateScalarRex(rex_function->getOperand(0));
1369  return makeExpr<Analyzer::CharLengthExpr>(str_arg->decompress(),
1370  rex_function->getName() == "CHAR_LENGTH"sv);
1371 }
#define CHECK_EQ(x, y)
Definition: Logger.h:297
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
size_t size() const
Definition: RelAlgDag.h:270
const RexScalar * getOperand(const size_t idx) const
Definition: RelAlgDag.h:272
const std::string & getName() const
Definition: RelAlgDag.h:500

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateLike ( const RexFunctionOperator rex_function) const
private

Definition at line 1114 of file RelAlgTranslator.cpp.

References CHECK, Parser::LikeExpr::get(), RexFunctionOperator::getName(), RexOperator::getOperand(), RexOperator::size(), and translateScalarRex().

Referenced by translateFunction().

1115  {
1116  CHECK(rex_function->size() == 2 || rex_function->size() == 3);
1117  const auto arg = translateScalarRex(rex_function->getOperand(0));
1118  const auto like = translateScalarRex(rex_function->getOperand(1));
1119  if (!std::dynamic_pointer_cast<const Analyzer::Constant>(like)) {
1120  throw std::runtime_error("The matching pattern must be a literal.");
1121  }
1122  const auto escape = (rex_function->size() == 3)
1123  ? translateScalarRex(rex_function->getOperand(2))
1124  : nullptr;
1125  const bool is_ilike = rex_function->getName() == "PG_ILIKE"sv;
1126  return Parser::LikeExpr::get(arg, like, escape, is_ilike, false);
1127 }
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
size_t size() const
Definition: RelAlgDag.h:270
const RexScalar * getOperand(const size_t idx) const
Definition: RelAlgDag.h:272
static std::shared_ptr< Analyzer::Expr > get(std::shared_ptr< Analyzer::Expr > arg_expr, std::shared_ptr< Analyzer::Expr > like_expr, std::shared_ptr< Analyzer::Expr > escape_expr, const bool is_ilike, const bool is_not)
Definition: ParserNode.cpp:691
#define CHECK(condition)
Definition: Logger.h:289
const std::string & getName() const
Definition: RelAlgDag.h:500

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateLikely ( const RexFunctionOperator rex_function) const
private

Definition at line 1143 of file RelAlgTranslator.cpp.

References CHECK, RexOperator::getOperand(), RexOperator::size(), and translateScalarRex().

Referenced by translateFunction().

1144  {
1145  CHECK(rex_function->size() == 1);
1146  const auto arg = translateScalarRex(rex_function->getOperand(0));
1147  return makeExpr<Analyzer::LikelihoodExpr>(arg, 0.9375);
1148 }
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
size_t size() const
Definition: RelAlgDag.h:270
const RexScalar * getOperand(const size_t idx) const
Definition: RelAlgDag.h:272
#define CHECK(condition)
Definition: Logger.h:289

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateLiteral ( const RexLiteral rex_literal)
static

Definition at line 359 of file RelAlgTranslator.cpp.

References Parser::StringLiteral::analyzeValue(), Parser::IntLiteral::analyzeValue(), Parser::FixedPtLiteral::analyzeValue(), run_benchmark_import::args, Datum::bigintval, Datum::boolval, anonymous_namespace{RelAlgTranslator.cpp}::build_type_info(), Datum::doubleval, logger::FATAL, kBIGINT, kBOOLEAN, kDATE, kDECIMAL, kDOUBLE, kINT, kINTERVAL_DAY_TIME, kINTERVAL_YEAR_MONTH, kNULLT, kTEXT, kTIME, kTIMESTAMP, LOG, and make_fp_constant().

Referenced by ResultSetLogicalValuesBuilder::build(), translateGeoFunctionArg(), and translateGeoLiteral().

360  {
361  auto lit_ti = build_type_info(
362  rex_literal->getType(), rex_literal->getScale(), rex_literal->getPrecision());
363  auto target_ti = build_type_info(rex_literal->getTargetType(),
364  rex_literal->getTargetScale(),
365  rex_literal->getTargetPrecision());
366  switch (rex_literal->getType()) {
367  case kINT:
368  case kBIGINT: {
369  Datum d;
370  d.bigintval = rex_literal->getVal<int64_t>();
371  return makeExpr<Analyzer::Constant>(rex_literal->getType(), false, d);
372  }
373  case kDECIMAL: {
374  const auto val = rex_literal->getVal<int64_t>();
375  const int precision = rex_literal->getPrecision();
376  const int scale = rex_literal->getScale();
377  if (target_ti.is_fp() && !scale) {
378  return make_fp_constant(val, target_ti);
379  }
380  auto lit_expr = scale ? Parser::FixedPtLiteral::analyzeValue(val, scale, precision)
381  : Parser::IntLiteral::analyzeValue(val);
382  return lit_ti != target_ti ? lit_expr->add_cast(target_ti) : lit_expr;
383  }
384  case kTEXT: {
385  return Parser::StringLiteral::analyzeValue(rex_literal->getVal<std::string>(),
386  false);
387  }
388  case kBOOLEAN: {
389  Datum d;
390  d.boolval = rex_literal->getVal<bool>();
391  return makeExpr<Analyzer::Constant>(kBOOLEAN, false, d);
392  }
393  case kDOUBLE: {
394  Datum d;
395  d.doubleval = rex_literal->getVal<double>();
396  auto lit_expr =
397  makeExpr<Analyzer::Constant>(SQLTypeInfo(rex_literal->getType(),
398  rex_literal->getPrecision(),
399  rex_literal->getScale(),
400  false),
401  false,
402  d);
403  return lit_ti != target_ti ? lit_expr->add_cast(target_ti) : lit_expr;
404  }
405  case kINTERVAL_DAY_TIME:
406  case kINTERVAL_YEAR_MONTH: {
407  Datum d;
408  d.bigintval = rex_literal->getVal<int64_t>();
409  return makeExpr<Analyzer::Constant>(rex_literal->getType(), false, d);
410  }
411  case kTIME:
412  case kTIMESTAMP: {
413  Datum d;
414  d.bigintval =
415  rex_literal->getType() == kTIMESTAMP && rex_literal->getPrecision() > 0
416  ? rex_literal->getVal<int64_t>()
417  : rex_literal->getVal<int64_t>() / 1000;
418  return makeExpr<Analyzer::Constant>(
419  SQLTypeInfo(rex_literal->getType(), rex_literal->getPrecision(), 0, false),
420  false,
421  d);
422  }
423  case kDATE: {
424  Datum d;
425  d.bigintval = rex_literal->getVal<int64_t>() * 24 * 3600;
426  return makeExpr<Analyzer::Constant>(rex_literal->getType(), false, d);
427  }
428  case kNULLT: {
429  if (target_ti.is_array()) {
431  // defaulting to valid sub-type for convenience
432  target_ti.set_subtype(kBOOLEAN);
433  return makeExpr<Analyzer::ArrayExpr>(target_ti, args, true);
434  }
435  return makeExpr<Analyzer::Constant>(rex_literal->getTargetType(), true, Datum{0});
436  }
437  default: {
438  LOG(FATAL) << "Unexpected literal type " << lit_ti.get_type_name();
439  }
440  }
441  return nullptr;
442 }
Definition: sqltypes.h:64
#define LOG(tag)
Definition: Logger.h:283
int8_t boolval
Definition: Datum.h:68
int64_t bigintval
Definition: Datum.h:72
SQLTypeInfo build_type_info(const SQLTypes sql_type, const int scale, const int precision)
Definition: sqltypes.h:67
Definition: sqltypes.h:68
static std::shared_ptr< Analyzer::Expr > analyzeValue(const std::string &stringval, const bool is_null)
Definition: ParserNode.cpp:142
static std::shared_ptr< Analyzer::Expr > analyzeValue(const int64_t numericval, const int scale, const int precision)
Definition: ParserNode.cpp:186
std::shared_ptr< Analyzer::Constant > make_fp_constant(const int64_t val, const SQLTypeInfo &ti)
std::vector< ExpressionPtr > ExpressionPtrVector
Definition: Analyzer.h:190
Definition: sqltypes.h:60
Definition: Datum.h:67
double doubleval
Definition: Datum.h:74

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateOffsetInFragment ( ) const
private

Definition at line 1582 of file RelAlgTranslator.cpp.

Referenced by translateFunction().

1582  {
1583  return makeExpr<Analyzer::OffsetInFragment>();
1584 }

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateOper ( const RexOperator rex_operator) const
private

Definition at line 996 of file RelAlgTranslator.cpp.

References CHECK, CHECK_GT, executor_, RexOperator::getOperand(), RexOperator::getOperator(), getQuantifiedRhs(), IS_COMPARISON, kIN, kMINUS, kONE, kOVERLAPS, kPLUS, Parser::OperExpr::normalize(), RexOperator::size(), translateDatePlusMinus(), translateGeoComparison(), translateInOper(), translateOverlapsOper(), translateScalarRex(), and translateUoper().

997  {
998  CHECK_GT(rex_operator->size(), size_t(0));
999  if (rex_operator->size() == 1) {
1000  return translateUoper(rex_operator);
1001  }
1002  const auto sql_op = rex_operator->getOperator();
1003  if (sql_op == kIN) {
1004  return translateInOper(rex_operator);
1005  }
1006  if (sql_op == kMINUS || sql_op == kPLUS) {
1007  auto date_plus_minus = translateDatePlusMinus(rex_operator);
1008  if (date_plus_minus) {
1009  return date_plus_minus;
1010  }
1011  }
1012  if (sql_op == kOVERLAPS) {
1013  return translateOverlapsOper(rex_operator);
1014  } else if (IS_COMPARISON(sql_op)) {
1015  auto geo_comp = translateGeoComparison(rex_operator);
1016  if (geo_comp) {
1017  return geo_comp;
1018  }
1019  }
1020  auto lhs = translateScalarRex(rex_operator->getOperand(0));
1021  for (size_t i = 1; i < rex_operator->size(); ++i) {
1022  std::shared_ptr<Analyzer::Expr> rhs;
1023  SQLQualifier sql_qual{kONE};
1024  const auto rhs_op = rex_operator->getOperand(i);
1025  std::tie(rhs, sql_qual) = getQuantifiedRhs(rhs_op);
1026  if (!rhs) {
1027  rhs = translateScalarRex(rhs_op);
1028  }
1029  CHECK(rhs);
1030 
1031  // Pass in executor to get string proxy info if cast needed between
1032  // string columns
1033  lhs = Parser::OperExpr::normalize(sql_op, sql_qual, lhs, rhs, executor_);
1034  }
1035  return lhs;
1036 }
const Executor * executor_
SQLQualifier
Definition: sqldefs.h:71
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
size_t size() const
Definition: RelAlgDag.h:270
const RexScalar * getOperand(const size_t idx) const
Definition: RelAlgDag.h:272
std::shared_ptr< Analyzer::Expr > translateUoper(const RexOperator *) const
static std::shared_ptr< Analyzer::Expr > normalize(const SQLOps optype, const SQLQualifier qual, std::shared_ptr< Analyzer::Expr > left_expr, std::shared_ptr< Analyzer::Expr > right_expr, const Executor *executor=nullptr)
Definition: ParserNode.cpp:370
Definition: sqldefs.h:40
#define CHECK_GT(x, y)
Definition: Logger.h:301
SQLOps getOperator() const
Definition: RelAlgDag.h:282
std::pair< std::shared_ptr< Analyzer::Expr >, SQLQualifier > getQuantifiedRhs(const RexScalar *) const
Definition: sqldefs.h:39
Definition: sqldefs.h:71
std::shared_ptr< Analyzer::Expr > translateOverlapsOper(const RexOperator *) const
Definition: sqldefs.h:52
#define CHECK(condition)
Definition: Logger.h:289
std::shared_ptr< Analyzer::Expr > translateInOper(const RexOperator *) const
std::shared_ptr< Analyzer::Expr > translateGeoComparison(const RexOperator *) const
std::shared_ptr< Analyzer::Expr > translateDatePlusMinus(const RexOperator *) const
#define IS_COMPARISON(X)
Definition: sqldefs.h:58

+ Here is the call graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateOverlapsOper ( const RexOperator rex_operator) const
private

Definition at line 1038 of file RelAlgTranslator.cpp.

References CHECK, RexOperator::getOperand(), RexOperator::getOperator(), kOVERLAPS, translateGeoOverlapsOper(), and translateScalarRex().

Referenced by translateOper().

1039  {
1040  const auto sql_op = rex_operator->getOperator();
1041  CHECK(sql_op == kOVERLAPS);
1042 
1043  const auto lhs = translateScalarRex(rex_operator->getOperand(0));
1044  const auto lhs_ti = lhs->get_type_info();
1045  if (lhs_ti.is_geometry()) {
1046  return translateGeoOverlapsOper(rex_operator);
1047  } else {
1048  throw std::runtime_error(
1049  "Overlaps equivalence is currently only supported for geospatial types");
1050  }
1051 }
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const RexScalar * getOperand(const size_t idx) const
Definition: RelAlgDag.h:272
std::shared_ptr< Analyzer::Expr > translateGeoOverlapsOper(const RexOperator *) const
SQLOps getOperator() const
Definition: RelAlgDag.h:282
#define CHECK(condition)
Definition: Logger.h:289

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateRegexp ( const RexFunctionOperator rex_function) const
private

Definition at line 1129 of file RelAlgTranslator.cpp.

References CHECK, Parser::RegexpExpr::get(), RexOperator::getOperand(), RexOperator::size(), and translateScalarRex().

Referenced by translateFunction().

1130  {
1131  CHECK(rex_function->size() == 2 || rex_function->size() == 3);
1132  const auto arg = translateScalarRex(rex_function->getOperand(0));
1133  const auto pattern = translateScalarRex(rex_function->getOperand(1));
1134  if (!std::dynamic_pointer_cast<const Analyzer::Constant>(pattern)) {
1135  throw std::runtime_error("The matching pattern must be a literal.");
1136  }
1137  const auto escape = (rex_function->size() == 3)
1138  ? translateScalarRex(rex_function->getOperand(2))
1139  : nullptr;
1140  return Parser::RegexpExpr::get(arg, pattern, escape, false);
1141 }
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
size_t size() const
Definition: RelAlgDag.h:270
const RexScalar * getOperand(const size_t idx) const
Definition: RelAlgDag.h:272
static std::shared_ptr< Analyzer::Expr > get(std::shared_ptr< Analyzer::Expr > arg_expr, std::shared_ptr< Analyzer::Expr > pattern_expr, std::shared_ptr< Analyzer::Expr > escape_expr, const bool is_not)
Definition: ParserNode.cpp:786
#define CHECK(condition)
Definition: Logger.h:289

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<>
std::shared_ptr<Analyzer::Expr> RelAlgTranslator::translateRexScalar ( RexScalar const *  rex) const

Definition at line 216 of file RelAlgTranslator.cpp.

217  {
218  return translateFunction(static_cast<RexFunctionOperator const*>(rex));
219 }
std::shared_ptr< Analyzer::Expr > translateFunction(const RexFunctionOperator *) const
template<>
std::shared_ptr<Analyzer::Expr> RelAlgTranslator::translateRexScalar ( RexScalar const *  rex) const

Definition at line 226 of file RelAlgTranslator.cpp.

227  {
228  return translateCase(static_cast<RexCase const*>(rex));
229 }
std::shared_ptr< Analyzer::Expr > translateCase(const RexCase *) const
template<>
std::shared_ptr<Analyzer::Expr> RelAlgTranslator::translateRexScalar ( RexScalar const *  rex) const

Definition at line 221 of file RelAlgTranslator.cpp.

222  {
223  return translateOper(static_cast<RexOperator const*>(rex));
224 }
std::shared_ptr< Analyzer::Expr > translateOper(const RexOperator *) const
template<>
std::shared_ptr<Analyzer::Expr> RelAlgTranslator::translateRexScalar ( RexScalar const *  rex) const

Definition at line 205 of file RelAlgTranslator.cpp.

206  {
207  return translateLiteral(static_cast<RexLiteral const*>(rex));
208 }
static std::shared_ptr< Analyzer::Expr > translateLiteral(const RexLiteral *)
template<>
std::shared_ptr<Analyzer::Expr> RelAlgTranslator::translateRexScalar ( RexScalar const *  rex) const

Definition at line 211 of file RelAlgTranslator.cpp.

212  {
213  return translateWindowFunction(static_cast<RexWindowFunctionOperator const*>(rex));
214 }
std::shared_ptr< Analyzer::Expr > translateWindowFunction(const RexWindowFunctionOperator *) const
template<>
std::shared_ptr<Analyzer::Expr> RelAlgTranslator::translateRexScalar ( RexScalar const *  rex) const

Definition at line 231 of file RelAlgTranslator.cpp.

232  {
233  return translateScalarSubquery(static_cast<RexSubQuery const*>(rex));
234 }
std::shared_ptr< Analyzer::Expr > translateScalarSubquery(const RexSubQuery *) const
template<>
std::shared_ptr<Analyzer::Expr> RelAlgTranslator::translateRexScalar ( RexScalar const *  rex) const