OmniSciDB  29e35f4d58
RelAlgTranslator Class Reference

#include <RelAlgTranslator.h>

+ Collaboration diagram for RelAlgTranslator:

Public Member Functions

 RelAlgTranslator (const Catalog_Namespace::Catalog &cat, 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, QueryFeatureDescriptor &feature_stash)
 
std::shared_ptr< Analyzer::ExprtranslateScalarRex (const RexScalar *rex) const
 

Static Public Member Functions

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

Private Member Functions

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::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::ExprtranslateLower (const RexFunctionOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateCardinality (const RexFunctionOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateItem (const RexFunctionOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateNow () 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
 
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::ExprtranslateGeoConstructor (const RexFunctionOperator *) const
 
std::shared_ptr< Analyzer::ExprtranslateGeoOverlapsOper (const RexOperator *) const
 
std::vector< std::shared_ptr< Analyzer::Expr > > translateGeoFunctionArg (const RexScalar *rex_scalar, SQLTypeInfo &arg_ti, int32_t &lindex, const bool with_bounds, const bool with_render_group, const bool expand_geo_col, const bool is_projection=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
 

Static Private Member Functions

static std::shared_ptr< Analyzer::ExprtranslateLiteral (const RexLiteral *)
 

Private Attributes

const Catalog_Namespace::Catalogcat_
 
const Executorexecutor_
 
const std::unordered_map< const RelAlgNode *, int > input_to_nest_level_
 
const std::vector< JoinTypejoin_types_
 
time_t now_
 
const bool just_explain_
 
QueryFeatureDescriptorfeature_stash_
 

Detailed Description

Definition at line 41 of file RelAlgTranslator.h.

Constructor & Destructor Documentation

◆ RelAlgTranslator()

RelAlgTranslator::RelAlgTranslator ( const Catalog_Namespace::Catalog cat,
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,
QueryFeatureDescriptor feature_stash 
)
inline

Definition at line 43 of file RelAlgTranslator.h.

50  : cat_(cat)
51  , executor_(executor)
52  , input_to_nest_level_(input_to_nest_level)
53  , join_types_(join_types)
54  , now_(now)
55  , just_explain_(just_explain)
56  , feature_stash_(feature_stash) {}
const std::vector< JoinType > join_types_
const Executor * executor_
QueryFeatureDescriptor & feature_stash_
const std::unordered_map< const RelAlgNode *, int > input_to_nest_level_
const bool just_explain_
const Catalog_Namespace::Catalog & cat_

Member Function Documentation

◆ getInIntegerSetExpr()

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::getInIntegerSetExpr ( std::shared_ptr< Analyzer::Expr arg,
const ResultSet val_set 
) const
private

Definition at line 735 of file RelAlgTranslator.cpp.

References can_use_parallel_algorithms(), CHECK, CHECK_EQ, CHECK_GE, cpu_threads(), anonymous_namespace{RelAlgTranslator.cpp}::fill_dictionary_encoded_in_vals(), anonymous_namespace{RelAlgTranslator.cpp}::fill_integer_in_vals(), g_cluster, inline_int_null_val(), and kENCODING_DICT.

737  {
738  if (!can_use_parallel_algorithms(val_set)) {
739  return nullptr;
740  }
741  std::vector<int64_t> value_exprs;
742  const size_t fetcher_count = cpu_threads();
743  std::vector<std::vector<int64_t>> expr_set(fetcher_count);
744  std::vector<std::future<void>> fetcher_threads;
745  const auto& arg_type = arg->get_type_info();
746  const auto entry_count = val_set.entryCount();
747  CHECK_EQ(size_t(1), val_set.colCount());
748  const auto& col_type = val_set.getColType(0);
749  if (g_cluster && arg_type.is_string() &&
750  (col_type.get_comp_param() <= 0 || arg_type.get_comp_param() <= 0)) {
751  // Skip this case for now, see comment for fill_dictionary_encoded_in_vals.
752  return nullptr;
753  }
754  std::atomic<size_t> total_in_vals_count{0};
755  for (size_t i = 0,
756  start_entry = 0,
757  stride = (entry_count + fetcher_count - 1) / fetcher_count;
758  i < fetcher_count && start_entry < entry_count;
759  ++i, start_entry += stride) {
760  expr_set[i].reserve(entry_count / fetcher_count);
761  const auto end_entry = std::min(start_entry + stride, entry_count);
762  if (arg_type.is_string()) {
763  CHECK_EQ(kENCODING_DICT, arg_type.get_compression());
764  // const int32_t dest_dict_id = arg_type.get_comp_param();
765  // const int32_t source_dict_id = col_type.get_comp_param();
766  const DictRef dest_dict_ref(arg_type.get_comp_param(), cat_.getDatabaseId());
767  const DictRef source_dict_ref(col_type.get_comp_param(), cat_.getDatabaseId());
768  const auto dd = executor_->getStringDictionaryProxy(
769  arg_type.get_comp_param(), val_set.getRowSetMemOwner(), true);
770  const auto sd = executor_->getStringDictionaryProxy(
771  col_type.get_comp_param(), val_set.getRowSetMemOwner(), true);
772  CHECK(sd);
773  const auto needle_null_val = inline_int_null_val(arg_type);
774  fetcher_threads.push_back(std::async(
775  std::launch::async,
776  [this,
777  &val_set,
778  &total_in_vals_count,
779  sd,
780  dd,
781  source_dict_ref,
782  dest_dict_ref,
783  needle_null_val](
784  std::vector<int64_t>& in_vals, const size_t start, const size_t end) {
785  if (g_cluster) {
786  CHECK_GE(dd->getGeneration(), 0);
788  total_in_vals_count,
789  &val_set,
790  {start, end},
792  source_dict_ref,
793  dest_dict_ref,
794  dd->getGeneration(),
795  needle_null_val);
796  } else {
798  total_in_vals_count,
799  &val_set,
800  {start, end},
801  sd,
802  dd,
803  needle_null_val);
804  }
805  },
806  std::ref(expr_set[i]),
807  start_entry,
808  end_entry));
809  } else {
810  CHECK(arg_type.is_integer());
811  fetcher_threads.push_back(std::async(
812  std::launch::async,
813  [&val_set, &total_in_vals_count](
814  std::vector<int64_t>& in_vals, const size_t start, const size_t end) {
815  fill_integer_in_vals(in_vals, total_in_vals_count, &val_set, {start, end});
816  },
817  std::ref(expr_set[i]),
818  start_entry,
819  end_entry));
820  }
821  }
822  for (auto& child : fetcher_threads) {
823  child.get();
824  }
825 
826  val_set.moveToBegin();
827  value_exprs.reserve(entry_count);
828  for (auto& exprs : expr_set) {
829  value_exprs.insert(value_exprs.end(), exprs.begin(), exprs.end());
830  }
831  return makeExpr<Analyzer::InIntegerSet>(
832  arg, value_exprs, arg_type.get_notnull() && col_type.get_notnull());
833 }
#define CHECK_EQ(x, y)
Definition: Logger.h:201
const std::vector< LeafHostInfo > & getStringDictionaryHosts() const
Definition: Catalog.cpp:1413
bool g_cluster
const Executor * executor_
#define CHECK_GE(x, y)
Definition: Logger.h:206
int getDatabaseId() const
Definition: Catalog.h:192
bool can_use_parallel_algorithms(const ResultSet &rows)
Definition: ResultSet.cpp:869
#define CHECK(condition)
Definition: Logger.h:193
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)
int cpu_threads()
Definition: thread_count.h:25
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 std::vector< LeafHostInfo > &leaf_hosts, const DictRef source_dict_ref, const DictRef dest_dict_ref, const int32_t dest_generation, const int64_t needle_null_val)
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:

◆ translateAbs()

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

Definition at line 1271 of file RelAlgTranslator.cpp.

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

1272  {
1273  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
1274  expr_list;
1275  CHECK_EQ(size_t(1), rex_function->size());
1276  const auto operand = translateScalarRex(rex_function->getOperand(0));
1277  const auto& operand_ti = operand->get_type_info();
1278  CHECK(operand_ti.is_number());
1279  const auto zero = makeNumericConstant(operand_ti, 0);
1280  const auto lt_zero = makeExpr<Analyzer::BinOper>(kBOOLEAN, kLT, kONE, operand, zero);
1281  const auto uminus_operand =
1282  makeExpr<Analyzer::UOper>(operand_ti.get_type(), kUMINUS, operand);
1283  expr_list.emplace_back(lt_zero, uminus_operand);
1284  return makeExpr<Analyzer::CaseExpr>(operand_ti, false, expr_list, operand);
1285 }
#define CHECK_EQ(x, y)
Definition: Logger.h:201
std::shared_ptr< Analyzer::Constant > makeNumericConstant(const SQLTypeInfo &ti, const long val)
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const RexScalar * getOperand(const size_t idx) const
Definition: sqldefs.h:69
#define CHECK(condition)
Definition: Logger.h:193
Definition: sqldefs.h:33
size_t size() const
+ Here is the call graph for this function:

◆ translateAggregateRex()

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

Definition at line 216 of file RelAlgTranslator.cpp.

References CHECK_LE, CHECK_LT, 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, kINT, RexAgg::size(), and ThriftSerializers::takes_arg().

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

218  {
219  const auto agg_kind = rex->getKind();
220  const bool is_distinct = rex->isDistinct();
221  const bool takes_arg{rex->size() > 0};
222  std::shared_ptr<Analyzer::Expr> arg_expr;
223  std::shared_ptr<Analyzer::Constant> err_rate;
224  if (takes_arg) {
225  const auto operand = rex->getOperand(0);
226  CHECK_LT(operand, scalar_sources.size());
227  CHECK_LE(rex->size(), 2u);
228  arg_expr = scalar_sources[operand];
229  if (agg_kind == kAPPROX_COUNT_DISTINCT && rex->size() == 2) {
230  err_rate = std::dynamic_pointer_cast<Analyzer::Constant>(
231  scalar_sources[rex->getOperand(1)]);
232  if (!err_rate || err_rate->get_type_info().get_type() != kINT ||
233  err_rate->get_constval().intval < 1 || err_rate->get_constval().intval > 100) {
234  throw std::runtime_error(
235  "APPROX_COUNT_DISTINCT's second parameter should be SMALLINT literal between "
236  "1 and 100");
237  }
238  }
239  const auto& arg_ti = arg_expr->get_type_info();
240  if (!is_agg_supported_for_type(agg_kind, arg_ti)) {
241  throw std::runtime_error("Aggregate on " + arg_ti.get_type_name() +
242  " is not supported yet.");
243  }
244  }
245  const auto agg_ti = get_agg_type(agg_kind, arg_expr.get());
246  return makeExpr<Analyzer::AggExpr>(agg_ti, agg_kind, arg_expr, is_distinct, err_rate);
247 }
size_t size() const
size_t getOperand(size_t idx) const
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:203
#define CHECK_LE(x, y)
Definition: Logger.h:204
SQLAgg getKind() const
bool takes_arg(const TargetInfo &target_info)
Definition: sqltypes.h:48
bool is_distinct(const size_t input_idx, const RelAlgNode *node)
bool isDistinct() const
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ translateArrayFunction()

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

Definition at line 1313 of file RelAlgTranslator.cpp.

References CHECK, get_nullable_logical_type_info(), SQLTypeInfoCore< TYPE_FACET_PACK >::get_subtype(), RexOperator::getType(), kARRAY, kBOOLEAN, kENCODING_FIXED, kNULLT, and TRANSIENT_DICT_ID.

1314  {
1315  if (rex_function->getType().get_subtype() == kNULLT) {
1316  auto sql_type = rex_function->getType();
1317  CHECK(sql_type.get_type() == kARRAY);
1318 
1319  // FIX-ME: Deal with NULL arrays
1320  auto translated_function_args(translateFunctionArgs(rex_function));
1321  if (translated_function_args.size() > 0) {
1322  auto const& first_element_logical_type(
1323  get_nullable_logical_type_info(translated_function_args[0]->get_type_info()));
1324 
1325  on_member_of_typeset<kCHAR, kVARCHAR, kTEXT>(
1326  first_element_logical_type,
1327  [&] {
1328  bool same_type_status = true;
1329  for (auto const& expr_ptr : translated_function_args) {
1330  same_type_status =
1331  same_type_status && (expr_ptr->get_type_info().is_string());
1332  }
1333 
1334  if (same_type_status == false) {
1335  throw std::runtime_error(
1336  "All elements of the array are not of the same logical subtype; "
1337  "consider casting to force this condition.");
1338  }
1339 
1340  sql_type.set_subtype(first_element_logical_type.get_type());
1341  sql_type.set_compression(kENCODING_FIXED);
1342  sql_type.set_comp_param(TRANSIENT_DICT_ID);
1343  },
1344  [&] {
1345  // Non string types
1346  bool same_type_status = true;
1347  for (auto const& expr_ptr : translated_function_args) {
1348  same_type_status =
1349  same_type_status &&
1350  (first_element_logical_type ==
1351  get_nullable_logical_type_info(expr_ptr->get_type_info()));
1352  }
1353 
1354  if (same_type_status == false) {
1355  throw std::runtime_error(
1356  "All elements of the array are not of the same logical subtype; "
1357  "consider casting to force this condition.");
1358  }
1359  sql_type.set_subtype(first_element_logical_type.get_type());
1360  sql_type.set_scale(first_element_logical_type.get_scale());
1361  sql_type.set_precision(first_element_logical_type.get_precision());
1362  });
1363 
1364  feature_stash_.setCPUOnlyExecutionRequired();
1365  return makeExpr<Analyzer::ArrayExpr>(
1366  sql_type, translated_function_args, feature_stash_.getAndBumpArrayExprCount());
1367  } else {
1368  // defaulting to valid sub-type for convenience
1369  sql_type.set_subtype(kBOOLEAN);
1370  return makeExpr<Analyzer::ArrayExpr>(
1371  sql_type, translated_function_args, feature_stash_.getAndBumpArrayExprCount());
1372  }
1373  } else {
1374  feature_stash_.setCPUOnlyExecutionRequired();
1375  return makeExpr<Analyzer::ArrayExpr>(rex_function->getType(),
1376  translateFunctionArgs(rex_function),
1377  feature_stash_.getAndBumpArrayExprCount());
1378  }
1379 }
SQLTypeInfo get_nullable_logical_type_info(const SQLTypeInfo &type_info)
Definition: sqltypes.h:884
Analyzer::ExpressionPtrVector translateFunctionArgs(const RexFunctionOperator *) const
QueryFeatureDescriptor & feature_stash_
HOST DEVICE SQLTypes get_subtype() const
Definition: sqltypes.h:327
#define TRANSIENT_DICT_ID
Definition: sqltypes.h:189
#define CHECK(condition)
Definition: Logger.h:193
const SQLTypeInfo & getType() const
+ Here is the call graph for this function:

◆ translateBinaryGeoFunction()

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

Definition at line 758 of file RelAlgTranslatorGeo.cpp.

References CHECK_EQ, func_resolve, SQLTypeInfoCore< TYPE_FACET_PACK >::get_comp_param(), SQLTypeInfoCore< TYPE_FACET_PACK >::get_compression(), SQLTypeInfoCore< TYPE_FACET_PACK >::get_input_srid(), SQLTypeInfoCore< TYPE_FACET_PACK >::get_output_srid(), SQLTypeInfoCore< TYPE_FACET_PACK >::get_subtype(), SQLTypeInfoCore< TYPE_FACET_PACK >::get_type(), RexFunctionOperator::getName(), RexOperator::getOperand(), RexOperator::getType(), Datum::intval, kBOOLEAN, kDOUBLE, kENCODING_GEOINT, kGEOGRAPHY, kINT, kLINESTRING, kNOT, kNULLT, kPOINT, run_benchmark_import::result, RexOperator::size(), anonymous_namespace{RelAlgTranslatorGeo.cpp}::suffix(), and translateGeoFunctionArg().

Referenced by translateTernaryGeoFunction().

759  {
760  auto function_name = rex_function->getName();
761  auto return_type = rex_function->getType();
762  bool swap_args = false;
763  bool with_bounds = false;
764  bool negate_result = false;
765  if (function_name == "ST_DWithin"sv) {
766  CHECK_EQ(size_t(3), rex_function->size());
767  function_name = "ST_Distance";
768  return_type = SQLTypeInfo(kDOUBLE, false);
769  } else if (function_name == "ST_DFullyWithin"sv) {
770  CHECK_EQ(size_t(3), rex_function->size());
771  function_name = "ST_MaxDistance";
772  return_type = SQLTypeInfo(kDOUBLE, false);
773  } else {
774  CHECK_EQ(size_t(2), rex_function->size());
775  }
776  if (function_name == "ST_Within"sv) {
777  function_name = "ST_Contains";
778  swap_args = true;
779  } else if (function_name == "ST_Disjoint"sv) {
780  function_name = "ST_Intersects";
781  negate_result = true;
782  }
783  if (func_resolve(function_name, "ST_Contains"sv, "ST_Intersects"sv)) {
784  with_bounds = true;
785  }
786 
787  std::vector<std::shared_ptr<Analyzer::Expr>> geoargs;
788  SQLTypeInfo arg0_ti;
789  SQLTypeInfo arg1_ti;
790  int32_t lindex0 = 0;
791  int32_t lindex1 = 0;
792 
793  auto geoargs0 = translateGeoFunctionArg(rex_function->getOperand(swap_args ? 1 : 0),
794  arg0_ti,
795  lindex0,
796  with_bounds,
797  false,
798  false);
799  if (arg0_ti.get_type() == kLINESTRING) {
800  Datum index;
801  index.intval = lindex0;
802  geoargs0.push_back(makeExpr<Analyzer::Constant>(kINT, false, index));
803  }
804  geoargs.insert(geoargs.end(), geoargs0.begin(), geoargs0.end());
805  auto geoargs1 = translateGeoFunctionArg(rex_function->getOperand(swap_args ? 0 : 1),
806  arg1_ti,
807  lindex1,
808  with_bounds,
809  false,
810  false);
811  if (arg1_ti.get_type() == kLINESTRING) {
812  Datum index;
813  index.intval = lindex1;
814  geoargs1.push_back(makeExpr<Analyzer::Constant>(kINT, false, index));
815  }
816  geoargs.insert(geoargs.end(), geoargs1.begin(), geoargs1.end());
817 
818  if (arg0_ti.get_subtype() != kNULLT && arg0_ti.get_subtype() != arg1_ti.get_subtype()) {
819  throw QueryNotSupported(rex_function->getName() +
820  " accepts either two GEOGRAPHY or two GEOMETRY arguments");
821  }
822  // Check SRID match if at least one is set/valid
823  if ((arg0_ti.get_output_srid() > 0 || arg1_ti.get_output_srid() > 0) &&
824  arg0_ti.get_output_srid() != arg1_ti.get_output_srid()) {
825  throw QueryNotSupported(rex_function->getName() + " cannot accept different SRIDs");
826  }
827 
828  std::string specialized_geofunc{function_name + suffix(arg0_ti.get_type()) +
829  suffix(arg1_ti.get_type())};
830 
831  if (arg0_ti.get_subtype() == kGEOGRAPHY && arg0_ti.get_output_srid() == 4326) {
832  // Need to call geodesic runtime functions
833  if (function_name == "ST_Distance"sv) {
834  if ((arg0_ti.get_type() == kPOINT ||
835  (arg0_ti.get_type() == kLINESTRING && lindex0 != 0)) &&
836  (arg1_ti.get_type() == kPOINT ||
837  (arg1_ti.get_type() == kLINESTRING && lindex1 != 0))) {
838  // Geodesic distance between points (or indexed linestrings)
839  specialized_geofunc += "_Geodesic"s;
840  } else {
841  throw QueryNotSupported(function_name +
842  " currently doesn't accept non-POINT geographies");
843  }
844  } else if (rex_function->getName() == "ST_Contains"sv) {
845  // We currently don't have a geodesic implementation of ST_Contains,
846  // allowing calls to a [less precise] cartesian implementation.
847  } else {
848  throw QueryNotSupported(function_name + " doesn't accept geographies");
849  }
850  } else if (function_name == "ST_Distance"sv && rex_function->size() == 3) {
851  if (arg0_ti.get_type() == kPOINT && arg1_ti.get_type() == kPOINT) {
852  // Cartesian distance between points used by ST_DWithin - switch to faster Squared
853  specialized_geofunc += "_Squared"s;
854  }
855  }
856 
857  // Add first input's compression mode and SRID args to enable on-the-fly
858  // decompression/transforms
859  Datum input_compression0;
860  input_compression0.intval =
861  (arg0_ti.get_compression() == kENCODING_GEOINT && arg0_ti.get_comp_param() == 32)
862  ? 1
863  : 0;
864  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_compression0));
865  Datum input_srid0;
866  input_srid0.intval = arg0_ti.get_input_srid();
867  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_srid0));
868 
869  // Add second input's compression mode and SRID args to enable on-the-fly
870  // decompression/transforms
871  Datum input_compression1;
872  input_compression1.intval =
873  (arg1_ti.get_compression() == kENCODING_GEOINT && arg1_ti.get_comp_param() == 32)
874  ? 1
875  : 0;
876  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_compression1));
877  Datum input_srid1;
878  input_srid1.intval = arg1_ti.get_input_srid();
879  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_srid1));
880 
881  // Add output SRID arg to enable on-the-fly transforms
882  Datum output_srid;
883  output_srid.intval = arg0_ti.get_output_srid();
884  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, output_srid));
885 
886  auto result =
887  makeExpr<Analyzer::FunctionOper>(return_type, specialized_geofunc, geoargs);
888  if (negate_result) {
889  return makeExpr<Analyzer::UOper>(kBOOLEAN, kNOT, result);
890  }
891  return result;
892 }
#define CHECK_EQ(x, y)
Definition: Logger.h:201
auto func_resolve
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:326
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:334
int32_t intval
Definition: sqltypes.h:128
HOST DEVICE SQLTypes get_subtype() const
Definition: sqltypes.h:327
HOST DEVICE int get_input_srid() const
Definition: sqltypes.h:330
std::vector< std::shared_ptr< Analyzer::Expr > > translateGeoFunctionArg(const RexScalar *rex_scalar, SQLTypeInfo &arg_ti, int32_t &lindex, const bool with_bounds, const bool with_render_group, const bool expand_geo_col, const bool is_projection=false) const
const RexScalar * getOperand(const size_t idx) const
SQLTypeInfoCore< ArrayContextTypeSizer, ExecutorTypePackaging, DateTimeFacilities > SQLTypeInfo
Definition: sqltypes.h:852
HOST DEVICE int get_output_srid() const
Definition: sqltypes.h:332
HOST DEVICE int get_comp_param() const
Definition: sqltypes.h:335
const SQLTypeInfo & getType() const
Definition: sqltypes.h:48
const std::string & getName() const
Definition: sqldefs.h:39
size_t size() const
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ translateCardinality()

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

Definition at line 1215 of file RelAlgTranslator.cpp.

References SQLTypeInfoCore< TYPE_FACET_PACK >::get_size(), RexFunctionOperator::getName(), RexOperator::getOperand(), RexOperator::getType(), kARRAY, and anonymous_namespace{RelAlgTranslator.cpp}::makeNumericConstant().

1216  {
1217  const auto ret_ti = rex_function->getType();
1218  const auto arg = translateScalarRex(rex_function->getOperand(0));
1219  const auto arg_ti = arg->get_type_info();
1220  if (!arg_ti.is_array()) {
1221  throw std::runtime_error(rex_function->getName() + " expects an array expression.");
1222  }
1223  if (arg_ti.get_subtype() == kARRAY) {
1224  throw std::runtime_error(rex_function->getName() +
1225  " expects one-dimension array expression.");
1226  }
1227  const auto array_size = arg_ti.get_size();
1228  const auto array_elem_size = arg_ti.get_elem_type().get_array_context_logical_size();
1229 
1230  if (array_size > 0) {
1231  if (array_elem_size <= 0) {
1232  throw std::runtime_error(rex_function->getName() +
1233  ": unexpected array element type.");
1234  }
1235  // Return cardinality of a fixed length array
1236  return makeNumericConstant(ret_ti, array_size / array_elem_size);
1237  }
1238  // Variable length array cardinality will be calculated at runtime
1239  return makeExpr<Analyzer::CardinalityExpr>(arg);
1240 }
std::shared_ptr< Analyzer::Constant > makeNumericConstant(const SQLTypeInfo &ti, const long val)
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const RexScalar * getOperand(const size_t idx) const
const SQLTypeInfo & getType() const
const std::string & getName() const
+ Here is the call graph for this function:

◆ translateCase()

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

Definition at line 889 of file RelAlgTranslator.cpp.

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

890  {
891  std::shared_ptr<Analyzer::Expr> else_expr;
892  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
893  expr_list;
894  for (size_t i = 0; i < rex_case->branchCount(); ++i) {
895  const auto when_expr = translateScalarRex(rex_case->getWhen(i));
896  const auto then_expr = translateScalarRex(rex_case->getThen(i));
897  expr_list.emplace_back(when_expr, then_expr);
898  }
899  if (rex_case->getElse()) {
900  else_expr = translateScalarRex(rex_case->getElse());
901  }
902  return Parser::CaseExpr::normalize(expr_list, else_expr);
903 }
size_t branchCount() const
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 >)
Definition: ParserNode.cpp:905
const RexScalar * getWhen(const size_t idx) const
const RexScalar * getThen(const size_t idx) const
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const RexScalar * getElse() const
+ Here is the call graph for this function:

◆ translateDateadd()

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

Definition at line 1009 of file RelAlgTranslator.cpp.

References CHECK_EQ, field(), fold_expr(), DateTimeUtils::get_dateadd_high_precision_adjusted_scale(), DateTimeUtils::get_dateadd_timestamp_precision_scale(), RexOperator::getOperand(), DateTimeUtils::is_subsecond_dateadd_field(), kBIGINT, kDIVIDE, kONE, kTIME, kTIMESTAMP, anonymous_namespace{RelAlgTranslator.cpp}::makeNumericConstant(), RexOperator::size(), and to_dateadd_field().

1010  {
1011  CHECK_EQ(size_t(3), rex_function->size());
1012  const auto timeunit = translateScalarRex(rex_function->getOperand(0));
1013  const auto timeunit_lit = std::dynamic_pointer_cast<Analyzer::Constant>(timeunit);
1014  if (!timeunit_lit || timeunit_lit->get_is_null()) {
1015  throw std::runtime_error("The time unit parameter must be a literal.");
1016  }
1017 
1018  const auto number_units = translateScalarRex(rex_function->getOperand(1));
1019  auto cast_number_units = number_units->add_cast(SQLTypeInfo(kBIGINT, false));
1020  const auto datetime = translateScalarRex(rex_function->getOperand(2));
1021  const auto& datetime_ti = datetime->get_type_info();
1022  if (datetime_ti.get_type() == kTIME) {
1023  throw std::runtime_error("DateAdd operation not supported for TIME.");
1024  }
1025  const auto& field = to_dateadd_field(*timeunit_lit->get_constval().stringval);
1026  if (!datetime_ti.is_high_precision_timestamp() &&
1028  // Scale the number to get value in seconds
1029  const auto bigint_ti = SQLTypeInfo(kBIGINT, false);
1030  cast_number_units = makeExpr<Analyzer::BinOper>(
1031  bigint_ti.get_type(),
1032  kDIVIDE,
1033  kONE,
1034  cast_number_units,
1035  makeNumericConstant(bigint_ti,
1037  cast_number_units = fold_expr(cast_number_units.get());
1038  }
1039  if (datetime_ti.is_high_precision_timestamp() &&
1042  field, datetime_ti.get_dimension());
1043  if (oper_scale.first) {
1044  // scale number to desired precision
1045  const auto bigint_ti = SQLTypeInfo(kBIGINT, false);
1046  cast_number_units =
1047  makeExpr<Analyzer::BinOper>(bigint_ti.get_type(),
1048  oper_scale.first,
1049  kONE,
1050  cast_number_units,
1051  makeNumericConstant(bigint_ti, oper_scale.second));
1052  cast_number_units = fold_expr(cast_number_units.get());
1053  }
1054  }
1055  return makeExpr<Analyzer::DateaddExpr>(
1056  SQLTypeInfo(kTIMESTAMP, datetime_ti.get_dimension(), 0, false),
1057  to_dateadd_field(*timeunit_lit->get_constval().stringval),
1058  cast_number_units,
1059  datetime);
1060 }
#define CHECK_EQ(x, y)
Definition: Logger.h:201
Definition: sqltypes.h:52
constexpr int64_t get_dateadd_timestamp_precision_scale(const DateaddField field)
Definition: DateTimeUtils.h:67
std::shared_ptr< Analyzer::Constant > makeNumericConstant(const SQLTypeInfo &ti, const long val)
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
const std::pair< SQLOps, int64_t > get_dateadd_high_precision_adjusted_scale(const DateaddField field, int32_t dimen)
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const RexScalar * getOperand(const size_t idx) const
SQLTypeInfoCore< ArrayContextTypeSizer, ExecutorTypePackaging, DateTimeFacilities > SQLTypeInfo
Definition: sqltypes.h:852
Definition: sqldefs.h:69
constexpr bool is_subsecond_dateadd_field(const DateaddField field)
Definition: DateTimeUtils.h:99
size_t size() const
DateaddField to_dateadd_field(const std::string &field)
std::shared_ptr< Analyzer::Expr > fold_expr(const Analyzer::Expr *expr)
+ Here is the call graph for this function:

◆ translateDatediff()

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

Definition at line 1152 of file RelAlgTranslator.cpp.

References CHECK_EQ, field(), RexOperator::getOperand(), kBIGINT, RexOperator::size(), and to_datediff_field().

1153  {
1154  CHECK_EQ(size_t(3), rex_function->size());
1155  const auto timeunit = translateScalarRex(rex_function->getOperand(0));
1156  const auto timeunit_lit = std::dynamic_pointer_cast<Analyzer::Constant>(timeunit);
1157  if (!timeunit_lit || timeunit_lit->get_is_null()) {
1158  throw std::runtime_error("The time unit parameter must be a literal.");
1159  }
1160  const auto start = translateScalarRex(rex_function->getOperand(1));
1161  const auto end = translateScalarRex(rex_function->getOperand(2));
1162  const auto field = to_datediff_field(*timeunit_lit->get_constval().stringval);
1163  return makeExpr<Analyzer::DatediffExpr>(SQLTypeInfo(kBIGINT, false), field, start, end);
1164 }
#define CHECK_EQ(x, y)
Definition: Logger.h:201
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
DatetruncField to_datediff_field(const std::string &field)
const RexScalar * getOperand(const size_t idx) const
SQLTypeInfoCore< ArrayContextTypeSizer, ExecutorTypePackaging, DateTimeFacilities > SQLTypeInfo
Definition: sqltypes.h:852
size_t size() const
+ Here is the call graph for this function:

◆ translateDatepart()

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

Definition at line 1166 of file RelAlgTranslator.cpp.

References CHECK_EQ, ExtractExpr::generate(), RexOperator::getOperand(), RexOperator::size(), and to_datepart_field().

1167  {
1168  CHECK_EQ(size_t(2), rex_function->size());
1169  const auto timeunit = translateScalarRex(rex_function->getOperand(0));
1170  const auto timeunit_lit = std::dynamic_pointer_cast<Analyzer::Constant>(timeunit);
1171  if (!timeunit_lit || timeunit_lit->get_is_null()) {
1172  throw std::runtime_error("The time unit parameter must be a literal.");
1173  }
1174  const auto from_expr = translateScalarRex(rex_function->getOperand(1));
1175  return ExtractExpr::generate(
1176  from_expr, to_datepart_field(*timeunit_lit->get_constval().stringval));
1177 }
#define CHECK_EQ(x, y)
Definition: Logger.h:201
ExtractField to_datepart_field(const std::string &field)
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const RexScalar * getOperand(const size_t idx) const
size_t size() const
const std::shared_ptr< Analyzer::Expr > generate() const
+ Here is the call graph for this function:

◆ translateDatePlusMinus()

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

Definition at line 1071 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(), and RexOperator::size().

1072  {
1073  if (rex_operator->size() != 2) {
1074  return nullptr;
1075  }
1076  const auto datetime = translateScalarRex(rex_operator->getOperand(0));
1077  const auto datetime_ti = datetime->get_type_info();
1078  if (!datetime_ti.is_timestamp() && !datetime_ti.is_date()) {
1079  if (datetime_ti.get_type() == kTIME) {
1080  throw std::runtime_error("DateTime addition/subtraction not supported for TIME.");
1081  }
1082  return nullptr;
1083  }
1084  const auto rhs = translateScalarRex(rex_operator->getOperand(1));
1085  const auto rhs_ti = rhs->get_type_info();
1086  if (rhs_ti.get_type() == kTIMESTAMP || rhs_ti.get_type() == kDATE) {
1087  if (datetime_ti.is_high_precision_timestamp() ||
1088  rhs_ti.is_high_precision_timestamp()) {
1089  throw std::runtime_error(
1090  "High Precision timestamps are not supported for TIMESTAMPDIFF operation. Use "
1091  "DATEDIFF.");
1092  }
1093  auto bigint_ti = SQLTypeInfo(kBIGINT, false);
1094  const auto& rex_operator_ti = rex_operator->getType();
1095  const auto datediff_field =
1096  (rex_operator_ti.get_type() == kINTERVAL_DAY_TIME) ? dtSECOND : dtMONTH;
1097  auto result =
1098  makeExpr<Analyzer::DatediffExpr>(bigint_ti, datediff_field, rhs, datetime);
1099  // multiply 1000 to result since expected result should be in millisecond precision.
1100  if (rex_operator_ti.get_type() == kINTERVAL_DAY_TIME) {
1101  return makeExpr<Analyzer::BinOper>(bigint_ti.get_type(),
1102  kMULTIPLY,
1103  kONE,
1104  result,
1105  makeNumericConstant(bigint_ti, 1000));
1106  } else {
1107  return result;
1108  }
1109  }
1110  const auto op = rex_operator->getOperator();
1111  if (op == kPLUS) {
1112  std::vector<std::shared_ptr<Analyzer::Expr>> args = {datetime, rhs};
1113  auto dt_plus = makeExpr<Analyzer::FunctionOper>(
1114  datetime_ti, get_datetimeplus_rewrite_funcname(op), args);
1115  const auto date_trunc = rewrite_to_date_trunc(dt_plus.get());
1116  if (date_trunc) {
1117  return date_trunc;
1118  }
1119  }
1120  const auto interval = fold_expr(rhs.get());
1121  auto interval_ti = interval->get_type_info();
1122  auto bigint_ti = SQLTypeInfo(kBIGINT, false);
1123  const auto interval_lit = std::dynamic_pointer_cast<Analyzer::Constant>(interval);
1124  if (interval_ti.get_type() == kINTERVAL_DAY_TIME) {
1125  std::shared_ptr<Analyzer::Expr> interval_sec;
1126  if (interval_lit) {
1127  interval_sec =
1128  makeNumericConstant(bigint_ti,
1129  (op == kMINUS ? -interval_lit->get_constval().bigintval
1130  : interval_lit->get_constval().bigintval) /
1131  1000);
1132  } else {
1133  interval_sec = makeExpr<Analyzer::BinOper>(bigint_ti.get_type(),
1134  kDIVIDE,
1135  kONE,
1136  interval,
1137  makeNumericConstant(bigint_ti, 1000));
1138  if (op == kMINUS) {
1139  interval_sec =
1140  std::make_shared<Analyzer::UOper>(bigint_ti, false, kUMINUS, interval_sec);
1141  }
1142  }
1143  return makeExpr<Analyzer::DateaddExpr>(datetime_ti, daSECOND, interval_sec, datetime);
1144  }
1145  CHECK(interval_ti.get_type() == kINTERVAL_YEAR_MONTH);
1146  const auto interval_months = op == kMINUS ? std::make_shared<Analyzer::UOper>(
1147  bigint_ti, false, kUMINUS, interval)
1148  : interval;
1149  return makeExpr<Analyzer::DateaddExpr>(datetime_ti, daMONTH, interval_months, datetime);
1150 }
SQLOps getOperator() const
Definition: sqltypes.h:52
Definition: sqldefs.h:41
std::shared_ptr< Analyzer::Constant > makeNumericConstant(const SQLTypeInfo &ti, const long val)
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const RexScalar * getOperand(const size_t idx) const
SQLTypeInfoCore< ArrayContextTypeSizer, ExecutorTypePackaging, DateTimeFacilities > SQLTypeInfo
Definition: sqltypes.h:852
Definition: sqltypes.h:56
Definition: sqldefs.h:40
Definition: sqldefs.h:69
#define CHECK(condition)
Definition: Logger.h:193
const SQLTypeInfo & getType() const
std::string get_datetimeplus_rewrite_funcname(const SQLOps &op)
std::shared_ptr< Analyzer::Expr > rewrite_to_date_trunc(const Analyzer::FunctionOper *dt_plus)
size_t size() const
std::shared_ptr< Analyzer::Expr > fold_expr(const Analyzer::Expr *expr)
+ Here is the call graph for this function:

◆ translateDatetime()

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

Definition at line 1255 of file RelAlgTranslator.cpp.

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

1256  {
1257  CHECK_EQ(size_t(1), rex_function->size());
1258  const auto arg = translateScalarRex(rex_function->getOperand(0));
1259  const auto arg_lit = std::dynamic_pointer_cast<Analyzer::Constant>(arg);
1260  const std::string datetime_err{R"(Only DATETIME('NOW') supported for now.)"};
1261  if (!arg_lit || arg_lit->get_is_null()) {
1262  throw std::runtime_error(datetime_err);
1263  }
1264  CHECK(arg_lit->get_type_info().is_string());
1265  if (*arg_lit->get_constval().stringval != "NOW"sv) {
1266  throw std::runtime_error(datetime_err);
1267  }
1268  return translateNow();
1269 }
#define CHECK_EQ(x, y)
Definition: Logger.h:201
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const RexScalar * getOperand(const size_t idx) const
#define CHECK(condition)
Definition: Logger.h:193
std::shared_ptr< Analyzer::Expr > translateNow() const
size_t size() const
+ Here is the call graph for this function:

◆ translateExtract()

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

Definition at line 948 of file RelAlgTranslator.cpp.

References CHECK_EQ, ExtractExpr::generate(), DateTruncExpr::generate(), RexFunctionOperator::getName(), RexOperator::getOperand(), and RexOperator::size().

949  {
950  CHECK_EQ(size_t(2), rex_function->size());
951  const auto timeunit = translateScalarRex(rex_function->getOperand(0));
952  const auto timeunit_lit = std::dynamic_pointer_cast<Analyzer::Constant>(timeunit);
953  if (!timeunit_lit || timeunit_lit->get_is_null()) {
954  throw std::runtime_error("The time unit parameter must be a literal.");
955  }
956  const auto from_expr = translateScalarRex(rex_function->getOperand(1));
957  const bool is_date_trunc = rex_function->getName() == "PG_DATE_TRUNC"sv;
958  if (is_date_trunc) {
959  return DateTruncExpr::generate(from_expr, *timeunit_lit->get_constval().stringval);
960  } else {
961  return ExtractExpr::generate(from_expr, *timeunit_lit->get_constval().stringval);
962  }
963 }
#define CHECK_EQ(x, y)
Definition: Logger.h:201
const std::shared_ptr< Analyzer::Expr > generate() const
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const RexScalar * getOperand(const size_t idx) const
const std::string & getName() const
size_t size() const
const std::shared_ptr< Analyzer::Expr > generate() const
+ Here is the call graph for this function:

◆ translateFunction()

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

Definition at line 1381 of file RelAlgTranslator.cpp.

References run_benchmark_import::args, bind_function(), CHECK, CHECK_EQ, ext_arg_type_to_type_info(), func_resolve, g_enable_experimental_string_functions, RexFunctionOperator::getName(), RexOperator::getOperand(), RexOperator::getType(), SQLTypeInfoCore< TYPE_FACET_PACK >::is_decimal(), kDIVIDE, kONE, kSMALLINT, Parser::OperExpr::normalize(), rewrite_to_date_trunc(), SQLTypeInfoCore< TYPE_FACET_PACK >::set_notnull(), RexOperator::size(), and Datum::smallintval.

1382  {
1383  if (func_resolve(rex_function->getName(), "LIKE"sv, "PG_ILIKE"sv)) {
1384  return translateLike(rex_function);
1385  }
1386  if (rex_function->getName() == "REGEXP_LIKE"sv) {
1387  return translateRegexp(rex_function);
1388  }
1389  if (rex_function->getName() == "LIKELY"sv) {
1390  return translateLikely(rex_function);
1391  }
1392  if (rex_function->getName() == "UNLIKELY"sv) {
1393  return translateUnlikely(rex_function);
1394  }
1395  if (func_resolve(rex_function->getName(), "PG_EXTRACT"sv, "PG_DATE_TRUNC"sv)) {
1396  return translateExtract(rex_function);
1397  }
1398  if (rex_function->getName() == "DATEADD"sv) {
1399  return translateDateadd(rex_function);
1400  }
1401  if (rex_function->getName() == "DATEDIFF"sv) {
1402  return translateDatediff(rex_function);
1403  }
1404  if (rex_function->getName() == "DATEPART"sv) {
1405  return translateDatepart(rex_function);
1406  }
1407  if (func_resolve(rex_function->getName(), "LENGTH"sv, "CHAR_LENGTH"sv)) {
1408  return translateLength(rex_function);
1409  }
1410  if (rex_function->getName() == "KEY_FOR_STRING"sv) {
1411  return translateKeyForString(rex_function);
1412  }
1413  if (g_enable_experimental_string_functions && rex_function->getName() == "LOWER"sv) {
1414  return translateLower(rex_function);
1415  }
1416  if (func_resolve(rex_function->getName(), "CARDINALITY"sv, "ARRAY_LENGTH"sv)) {
1417  return translateCardinality(rex_function);
1418  }
1419  if (rex_function->getName() == "ITEM"sv) {
1420  return translateItem(rex_function);
1421  }
1422  if (rex_function->getName() == "NOW"sv) {
1423  return translateNow();
1424  }
1425  if (rex_function->getName() == "DATETIME"sv) {
1426  return translateDatetime(rex_function);
1427  }
1428  if (func_resolve(rex_function->getName(), "usTIMESTAMP"sv, "nsTIMESTAMP"sv)) {
1429  return translateHPTLiteral(rex_function);
1430  }
1431  if (rex_function->getName() == "ABS"sv) {
1432  return translateAbs(rex_function);
1433  }
1434  if (rex_function->getName() == "SIGN"sv) {
1435  return translateSign(rex_function);
1436  }
1437  if (func_resolve(rex_function->getName(), "CEIL"sv, "FLOOR"sv)) {
1438  return makeExpr<Analyzer::FunctionOperWithCustomTypeHandling>(
1439  rex_function->getType(),
1440  rex_function->getName(),
1441  translateFunctionArgs(rex_function));
1442  } else if (rex_function->getName() == "ROUND"sv) {
1443  std::vector<std::shared_ptr<Analyzer::Expr>> args =
1444  translateFunctionArgs(rex_function);
1445 
1446  if (rex_function->size() == 1) {
1447  // push a 0 constant if 2nd operand is missing.
1448  // this needs to be done as calcite returns
1449  // only the 1st operand without defaulting the 2nd one
1450  // when the user did not specify the 2nd operand.
1451  SQLTypes t = kSMALLINT;
1452  Datum d;
1453  d.smallintval = 0;
1454  args.push_back(makeExpr<Analyzer::Constant>(t, false, d));
1455  }
1456 
1457  // make sure we have only 2 operands
1458  CHECK(args.size() == 2);
1459 
1460  if (!args[0]->get_type_info().is_number()) {
1461  throw std::runtime_error("Only numeric 1st operands are supported");
1462  }
1463 
1464  // the 2nd operand does not need to be a constant
1465  // it can happily reference another integer column
1466  if (!args[1]->get_type_info().is_integer()) {
1467  throw std::runtime_error("Only integer 2nd operands are supported");
1468  }
1469 
1470  // Calcite may upcast decimals in a way that is
1471  // incompatible with the extension function input. Play it safe and stick with the
1472  // argument type instead.
1473  const SQLTypeInfo ret_ti = args[0]->get_type_info().is_decimal()
1474  ? args[0]->get_type_info()
1475  : rex_function->getType();
1476 
1477  return makeExpr<Analyzer::FunctionOperWithCustomTypeHandling>(
1478  ret_ti, rex_function->getName(), args);
1479  }
1480  if (rex_function->getName() == "DATETIME_PLUS"sv) {
1481  auto dt_plus = makeExpr<Analyzer::FunctionOper>(rex_function->getType(),
1482  rex_function->getName(),
1483  translateFunctionArgs(rex_function));
1484  const auto date_trunc = rewrite_to_date_trunc(dt_plus.get());
1485  if (date_trunc) {
1486  return date_trunc;
1487  }
1488  return translateDateadd(rex_function);
1489  }
1490  if (rex_function->getName() == "/INT"sv) {
1491  CHECK_EQ(size_t(2), rex_function->size());
1492  std::shared_ptr<Analyzer::Expr> lhs = translateScalarRex(rex_function->getOperand(0));
1493  std::shared_ptr<Analyzer::Expr> rhs = translateScalarRex(rex_function->getOperand(1));
1494  const auto rhs_lit = std::dynamic_pointer_cast<Analyzer::Constant>(rhs);
1495  return Parser::OperExpr::normalize(kDIVIDE, kONE, lhs, rhs);
1496  }
1497  if (rex_function->getName() == "Reinterpret"sv) {
1498  CHECK_EQ(size_t(1), rex_function->size());
1499  return translateScalarRex(rex_function->getOperand(0));
1500  }
1501  if (func_resolve(rex_function->getName(),
1502  "ST_X"sv,
1503  "ST_Y"sv,
1504  "ST_XMin"sv,
1505  "ST_YMin"sv,
1506  "ST_XMax"sv,
1507  "ST_YMax"sv,
1508  "ST_NRings"sv,
1509  "ST_NPoints"sv,
1510  "ST_Length"sv,
1511  "ST_Perimeter"sv,
1512  "ST_Area"sv,
1513  "ST_SRID"sv,
1514  "MapD_GeoPolyBoundsPtr"sv /* deprecated */,
1515  "MapD_GeoPolyBoundsPtr"sv /* deprecated */,
1516  "OmniSci_Geo_PolyBoundsPtr"sv,
1517  "OmniSci_Geo_PolyRenderGroup"sv)) {
1518  CHECK_EQ(rex_function->size(), size_t(1));
1519  return translateUnaryGeoFunction(rex_function);
1520  }
1521  if (func_resolve(rex_function->getName(),
1522  "convert_meters_to_pixel_width"sv,
1523  "convert_meters_to_pixel_height"sv,
1524  "is_point_in_view"sv,
1525  "is_point_size_in_view"sv)) {
1526  return translateFunctionWithGeoArg(rex_function);
1527  }
1528  if (func_resolve(rex_function->getName(),
1529  "ST_Distance"sv,
1530  "ST_MaxDistance"sv,
1531  "ST_Intersects"sv,
1532  "ST_Disjoint"sv,
1533  "ST_Contains"sv,
1534  "ST_Within"sv)) {
1535  CHECK_EQ(rex_function->size(), size_t(2));
1536  return translateBinaryGeoFunction(rex_function);
1537  }
1538  if (func_resolve(rex_function->getName(), "ST_DWithin"sv, "ST_DFullyWithin"sv)) {
1539  CHECK_EQ(rex_function->size(), size_t(3));
1540  return translateTernaryGeoFunction(rex_function);
1541  }
1542  if (rex_function->getName() == "OFFSET_IN_FRAGMENT"sv) {
1543  CHECK_EQ(size_t(0), rex_function->size());
1544  return translateOffsetInFragment();
1545  }
1546  if (rex_function->getName() == "ARRAY"sv) {
1547  // Var args; currently no check. Possible fix-me -- can array have 0 elements?
1548  return translateArrayFunction(rex_function);
1549  }
1550  if (func_resolve(rex_function->getName(),
1551  "ST_GeomFromText"sv,
1552  "ST_GeogFromText"sv,
1553  "ST_Point"sv,
1554  "ST_SetSRID"sv)) {
1555  return translateGeoConstructor(rex_function);
1556  }
1557 
1558  auto arg_expr_list = translateFunctionArgs(rex_function);
1559  // Reset possibly wrong return type of rex_function to the return
1560  // type of the optimal valid implementation. The return type can be
1561  // wrong in the case of multiple implementations of UDF functions
1562  // that have different return types but Calcite specifies the return
1563  // type according to the first implementation.
1564  auto ext_func_sig = bind_function(rex_function->getName(), arg_expr_list);
1565  auto ret_ti = ext_arg_type_to_type_info(ext_func_sig.getRet());
1566  // By defualt, the extension function type will not allow nulls. If one of the arguments
1567  // is nullable, the extension function must also explicitly allow nulls.
1568  bool arguments_not_null = true;
1569  for (const auto& arg_expr : arg_expr_list) {
1570  if (!arg_expr->get_type_info().get_notnull()) {
1571  arguments_not_null = false;
1572  break;
1573  }
1574  }
1575  ret_ti.set_notnull(arguments_not_null);
1576  return makeExpr<Analyzer::FunctionOper>(ret_ti, rex_function->getName(), arg_expr_list);
1577 }
#define CHECK_EQ(x, y)
Definition: Logger.h:201
auto func_resolve
std::shared_ptr< Analyzer::Expr > translateDatediff(const RexFunctionOperator *) const
SQLTypes
Definition: sqltypes.h:41
std::shared_ptr< Analyzer::Expr > translateCardinality(const RexFunctionOperator *) const
ExtensionFunction bind_function(std::string name, Analyzer::ExpressionPtrVector func_args, const std::vector< ExtensionFunction > &ext_funcs)
std::shared_ptr< Analyzer::Expr > translateKeyForString(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateAbs(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateItem(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateFunctionWithGeoArg(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateSign(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateLength(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateUnaryGeoFunction(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateUnlikely(const RexFunctionOperator *) const
Analyzer::ExpressionPtrVector translateFunctionArgs(const RexFunctionOperator *) const
void set_notnull(bool n)
Definition: sqltypes.h:423
bool g_enable_experimental_string_functions
std::shared_ptr< Analyzer::Expr > translateOffsetInFragment() const
std::shared_ptr< Analyzer::Expr > translateBinaryGeoFunction(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateLower(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateDateadd(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
bool is_decimal() const
Definition: sqltypes.h:480
int16_t smallintval
Definition: sqltypes.h:127
std::shared_ptr< Analyzer::Expr > translateGeoConstructor(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateArrayFunction(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateLike(const RexFunctionOperator *) const
const RexScalar * getOperand(const size_t idx) 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)
Definition: ParserNode.cpp:261
Definition: sqldefs.h:69
std::shared_ptr< Analyzer::Expr > translateExtract(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateLikely(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateRegexp(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateHPTLiteral(const RexFunctionOperator *) const
#define CHECK(condition)
Definition: Logger.h:193
const SQLTypeInfo & getType() const
const std::string & getName() const
std::shared_ptr< Analyzer::Expr > translateTernaryGeoFunction(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateNow() const
std::shared_ptr< Analyzer::Expr > translateDatetime(const RexFunctionOperator *) 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 > translateDatepart(const RexFunctionOperator *) const
size_t size() const
+ Here is the call graph for this function:

◆ translateFunctionArgs()

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

Definition at line 1658 of file RelAlgTranslator.cpp.

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

1659  {
1660  std::vector<std::shared_ptr<Analyzer::Expr>> args;
1661  for (size_t i = 0; i < rex_function->size(); ++i) {
1662  args.push_back(translateScalarRex(rex_function->getOperand(i)));
1663  }
1664  return args;
1665 }
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const RexScalar * getOperand(const size_t idx) const
size_t size() const
+ Here is the call graph for this function:

◆ translateFunctionWithGeoArg()

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

Definition at line 959 of file RelAlgTranslatorGeo.cpp.

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

960  {
961  int32_t lindex = 0;
962  std::string specialized_geofunc{rex_function->getName()};
963  if (func_resolve(rex_function->getName(),
964  "convert_meters_to_pixel_width"sv,
965  "convert_meters_to_pixel_height"sv)) {
966  CHECK_EQ(rex_function->size(), 6u);
967  SQLTypeInfo arg_ti;
968  std::vector<std::shared_ptr<Analyzer::Expr>> args;
969  args.push_back(translateScalarRex(rex_function->getOperand(0)));
970  auto geoargs = translateGeoFunctionArg(
971  rex_function->getOperand(1), arg_ti, lindex, false, true, false);
972  // only works on points
973  if (arg_ti.get_type() != kPOINT) {
974  throw QueryNotSupported(rex_function->getName() +
975  " expects a point for the second argument");
976  }
977 
978  args.insert(args.end(), geoargs.begin(), geoargs.begin() + 1);
979 
980  // Add compression information
981  Datum input_compression;
982  input_compression.intval =
983  (arg_ti.get_compression() == kENCODING_GEOINT && arg_ti.get_comp_param() == 32)
984  ? 1
985  : 0;
986  args.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_compression));
987  if (arg_ti.get_input_srid() != 4326) {
988  throw QueryNotSupported(
989  rex_function->getName() +
990  " currently only supports points of with SRID WGS84/EPSG:4326");
991  }
992  Datum input_srid;
993  input_srid.intval = arg_ti.get_input_srid();
994  args.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_srid));
995  Datum output_srid;
996  // Forcing web-mercator projection for now
997  // TODO(croot): check that the input-to-output conversion routines exist?
998  output_srid.intval =
999  arg_ti.get_output_srid() != 900913 ? 900913 : arg_ti.get_output_srid();
1000  args.push_back(makeExpr<Analyzer::Constant>(kINT, false, output_srid));
1001 
1002  args.push_back(translateScalarRex(rex_function->getOperand(2)));
1003  args.push_back(translateScalarRex(rex_function->getOperand(3)));
1004  args.push_back(translateScalarRex(rex_function->getOperand(4)));
1005  args.push_back(translateScalarRex(rex_function->getOperand(5)));
1006  return makeExpr<Analyzer::FunctionOper>(
1007  rex_function->getType(), specialized_geofunc, args);
1008  } else if (rex_function->getName() == "is_point_in_view"sv) {
1009  CHECK_EQ(rex_function->size(), 5u);
1010  SQLTypeInfo arg_ti;
1011  std::vector<std::shared_ptr<Analyzer::Expr>> args;
1012  auto geoargs = translateGeoFunctionArg(
1013  rex_function->getOperand(0), arg_ti, lindex, false, true, false);
1014  // only works on points
1015  if (arg_ti.get_type() != kPOINT) {
1016  throw QueryNotSupported(rex_function->getName() +
1017  " expects a point for the second argument");
1018  }
1019 
1020  args.insert(args.end(), geoargs.begin(), geoargs.begin() + 1);
1021 
1022  // Add compression information
1023  Datum input_compression;
1024  input_compression.intval =
1025  (arg_ti.get_compression() == kENCODING_GEOINT && arg_ti.get_comp_param() == 32)
1026  ? 1
1027  : 0;
1028  args.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_compression));
1029  if (arg_ti.get_input_srid() != 4326) {
1030  throw QueryNotSupported(
1031  rex_function->getName() +
1032  " currently only supports points of with SRID WGS84/EPSG:4326");
1033  }
1034  args.push_back(translateScalarRex(rex_function->getOperand(1)));
1035  args.push_back(translateScalarRex(rex_function->getOperand(2)));
1036  args.push_back(translateScalarRex(rex_function->getOperand(3)));
1037  args.push_back(translateScalarRex(rex_function->getOperand(4)));
1038  return makeExpr<Analyzer::FunctionOper>(
1039  rex_function->getType(), specialized_geofunc, args);
1040  } else if (rex_function->getName() == "is_point_size_in_view"sv) {
1041  CHECK_EQ(rex_function->size(), 6u);
1042  SQLTypeInfo arg_ti;
1043  std::vector<std::shared_ptr<Analyzer::Expr>> args;
1044  auto geoargs = translateGeoFunctionArg(
1045  rex_function->getOperand(0), arg_ti, lindex, false, true, false);
1046  // only works on points
1047  if (arg_ti.get_type() != kPOINT) {
1048  throw QueryNotSupported(rex_function->getName() +
1049  " expects a point for the second argument");
1050  }
1051 
1052  args.insert(args.end(), geoargs.begin(), geoargs.begin() + 1);
1053 
1054  // Add compression information
1055  Datum input_compression;
1056  input_compression.intval =
1057  (arg_ti.get_compression() == kENCODING_GEOINT && arg_ti.get_comp_param() == 32)
1058  ? 1
1059  : 0;
1060  args.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_compression));
1061  if (arg_ti.get_input_srid() != 4326) {
1062  throw QueryNotSupported(
1063  rex_function->getName() +
1064  " currently only supports points of with SRID WGS84/EPSG:4326");
1065  }
1066  args.push_back(translateScalarRex(rex_function->getOperand(1)));
1067  args.push_back(translateScalarRex(rex_function->getOperand(2)));
1068  args.push_back(translateScalarRex(rex_function->getOperand(3)));
1069  args.push_back(translateScalarRex(rex_function->getOperand(4)));
1070  args.push_back(translateScalarRex(rex_function->getOperand(5)));
1071  return makeExpr<Analyzer::FunctionOper>(
1072  rex_function->getType(), specialized_geofunc, args);
1073  }
1074  CHECK(false);
1075  return nullptr;
1076 }
#define CHECK_EQ(x, y)
Definition: Logger.h:201
auto func_resolve
int32_t intval
Definition: sqltypes.h:128
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
std::vector< std::shared_ptr< Analyzer::Expr > > translateGeoFunctionArg(const RexScalar *rex_scalar, SQLTypeInfo &arg_ti, int32_t &lindex, const bool with_bounds, const bool with_render_group, const bool expand_geo_col, const bool is_projection=false) const
const RexScalar * getOperand(const size_t idx) const
#define CHECK(condition)
Definition: Logger.h:193
const SQLTypeInfo & getType() const
Definition: sqltypes.h:48
const std::string & getName() const
size_t size() const
+ Here is the call graph for this function:

◆ translateGeoColumn()

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 21 of file RelAlgTranslatorGeo.cpp.

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

Referenced by translateGeoFunctionArg(), and translateGeoOverlapsOper().

26  {
27  std::vector<std::shared_ptr<Analyzer::Expr>> args;
28  const auto source = rex_input->getSourceNode();
29  const auto it_rte_idx = input_to_nest_level_.find(source);
30  CHECK(it_rte_idx != input_to_nest_level_.end());
31  const int rte_idx = it_rte_idx->second;
32  const auto& in_metainfo = source->getOutputMetainfo();
33 
34  int32_t table_id{0};
35  int column_id{-1};
36  const auto scan_source = dynamic_cast<const RelScan*>(source);
37  if (scan_source) {
38  // We're at leaf (scan) level and not supposed to have input metadata,
39  // the name and type information come directly from the catalog.
40  CHECK(in_metainfo.empty());
41 
42  const auto td = scan_source->getTableDescriptor();
43  table_id = td->tableId;
44 
45  const auto gcd = cat_.getMetadataForColumnBySpi(table_id, rex_input->getIndex() + 1);
46  CHECK(gcd);
47  ti = gcd->columnType;
48  column_id = gcd->columnId;
49 
50  } else {
51  // Likely backed by a temp table. Read the table ID from the source node and negate it
52  // (see RelAlgTranslator::translateInput)
53  table_id = -source->getId();
54 
55  CHECK(!expand_geo_col);
56 
57  CHECK(!in_metainfo.empty());
58  CHECK_GE(rte_idx, 0);
59  column_id = rex_input->getIndex();
60  CHECK_LT(static_cast<size_t>(column_id), in_metainfo.size());
61  ti = in_metainfo[column_id].get_type_info();
62  }
63  CHECK(IS_GEO(ti.get_type()));
64 
65  // Return geo column reference. The geo column may be expanded if required for extension
66  // function arguments. Otherwise, the geo column reference will be translated into
67  // physical columns as required. Bounds column will be added if present and requested.
68  if (expand_geo_col) {
69  for (auto i = 0; i < ti.get_physical_coord_cols(); i++) {
70  const auto pcd = cat_.getMetadataForColumnBySpi(
71  table_id, SPIMAP_GEO_PHYSICAL_INPUT(rex_input->getIndex(), i + 1));
72  auto pcol_ti = pcd->columnType;
73  args.push_back(std::make_shared<Analyzer::ColumnVar>(
74  pcol_ti, table_id, pcd->columnId, rte_idx));
75  }
76  } else {
77  args.push_back(
78  std::make_shared<Analyzer::ColumnVar>(ti, table_id, column_id, rte_idx));
79  }
80  if (with_bounds && ti.has_bounds()) {
81  const auto bounds_cd = cat_.getMetadataForColumnBySpi(
82  table_id,
84  ti.get_physical_coord_cols() + 1));
85  auto bounds_ti = bounds_cd->columnType;
86  args.push_back(std::make_shared<Analyzer::ColumnVar>(
87  bounds_ti, table_id, bounds_cd->columnId, rte_idx));
88  }
89  if (with_render_group && ti.has_render_group()) {
90  const auto render_group_cd = cat_.getMetadataForColumnBySpi(
91  table_id,
93  ti.get_physical_coord_cols() + 2));
94  auto render_group_ti = render_group_cd->columnType;
95  args.push_back(std::make_shared<Analyzer::ColumnVar>(
96  render_group_ti, table_id, render_group_cd->columnId, rte_idx));
97  }
98  return args;
99 }
#define SPIMAP_GEO_PHYSICAL_INPUT(c, i)
Definition: Catalog.h:70
#define CHECK_GE(x, y)
Definition: Logger.h:206
const RelAlgNode * getSourceNode() const
const std::unordered_map< const RelAlgNode *, int > input_to_nest_level_
#define CHECK_LT(x, y)
Definition: Logger.h:203
unsigned getIndex() const
const ColumnDescriptor * getMetadataForColumnBySpi(const int tableId, const size_t spi) const
Definition: Catalog.cpp:1464
#define CHECK(condition)
Definition: Logger.h:193
SQLTypeInfo columnType
#define IS_GEO(T)
Definition: sqltypes.h:167
const Catalog_Namespace::Catalog & cat_
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ translateGeoComparison()

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

Definition at line 926 of file RelAlgTranslatorGeo.cpp.

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

927  {
928  if (rex_operator->size() != size_t(2)) {
929  return nullptr;
930  }
931 
932  auto geo_distance_expr = translateScalarRex(rex_operator->getOperand(0));
933  auto func_oper = dynamic_cast<Analyzer::FunctionOper*>(geo_distance_expr.get());
934  if (func_oper && func_oper->getName() == "ST_Distance_Point_Point"sv) {
935  const auto& distance_ti = SQLTypeInfo(kDOUBLE, false);
936  std::vector<std::shared_ptr<Analyzer::Expr>> geoargs;
937  for (size_t i = 0; i < func_oper->getArity(); i++) {
938  geoargs.push_back(func_oper->getOwnArg(i));
939  }
940  geo_distance_expr = makeExpr<Analyzer::FunctionOper>(
941  distance_ti, "ST_Distance_Point_Point_Squared"s, geoargs);
942  auto distance_expr = translateScalarRex(rex_operator->getOperand(1));
943  if (distance_expr->get_type_info().get_type() != kDOUBLE) {
944  distance_expr->add_cast(distance_ti);
945  }
946  distance_expr = makeExpr<Analyzer::BinOper>(distance_ti,
947  distance_expr->get_contains_agg(),
948  kMULTIPLY,
949  kONE,
950  distance_expr,
951  distance_expr);
952  distance_expr = fold_expr(distance_expr.get());
953  return makeExpr<Analyzer::BinOper>(
954  kBOOLEAN, rex_operator->getOperator(), kONE, geo_distance_expr, distance_expr);
955  }
956  return nullptr;
957 }
SQLOps getOperator() const
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const RexScalar * getOperand(const size_t idx) const
SQLTypeInfoCore< ArrayContextTypeSizer, ExecutorTypePackaging, DateTimeFacilities > SQLTypeInfo
Definition: sqltypes.h:852
Definition: sqldefs.h:69
size_t size() const
std::shared_ptr< Analyzer::Expr > fold_expr(const Analyzer::Expr *expr)
+ Here is the call graph for this function:

◆ translateGeoConstructor()

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

Definition at line 548 of file RelAlgTranslatorGeo.cpp.

References func_resolve, RexFunctionOperator::getName(), and translateGeoFunctionArg().

549  {
550  if (func_resolve(rex_function->getName(),
551  "ST_GeomFromText"sv,
552  "ST_GeogFromText"sv,
553  "ST_Point"sv,
554  "ST_SetSRID"sv)) {
555  SQLTypeInfo arg_ti;
556  int32_t lindex = 0;
557  auto geoargs =
558  translateGeoFunctionArg(rex_function, arg_ti, lindex, false, false, true, true);
559  return makeExpr<Analyzer::GeoExpr>(arg_ti, geoargs);
560  }
561  throw QueryNotSupported(rex_function->getName() +
562  " geo constructor is not supported in this context");
563 }
auto func_resolve
std::vector< std::shared_ptr< Analyzer::Expr > > translateGeoFunctionArg(const RexScalar *rex_scalar, SQLTypeInfo &arg_ti, int32_t &lindex, const bool with_bounds, const bool with_render_group, const bool expand_geo_col, const bool is_projection=false) const
const std::string & getName() const
+ Here is the call graph for this function:

◆ translateGeoFunctionArg()

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

Definition at line 202 of file RelAlgTranslatorGeo.cpp.

References run_benchmark_import::args, CHECK, CHECK_EQ, fold_expr(), func_resolve, SQLTypeInfoCore< TYPE_FACET_PACK >::get_input_srid(), SQLTypeInfoCore< TYPE_FACET_PACK >::get_output_srid(), SQLTypeInfoCore< TYPE_FACET_PACK >::get_type(), IS_GEO, kARRAY, kCAST, kDOUBLE, kENCODING_NONE, kGEOGRAPHY, kGEOMETRY, kINT, kLINESTRING, kNULLT, kPOINT, kSMALLINT, kTEXT, kTINYINT, SQLTypeInfoCore< TYPE_FACET_PACK >::set_compression(), SQLTypeInfoCore< TYPE_FACET_PACK >::set_input_srid(), SQLTypeInfoCore< TYPE_FACET_PACK >::set_output_srid(), SQLTypeInfoCore< TYPE_FACET_PACK >::set_subtype(), SQLTypeInfoCore< TYPE_FACET_PACK >::set_type(), to_string(), translateGeoColumn(), translateGeoLiteral(), translateInput(), translateLiteral(), and translateScalarRex().

Referenced by translateBinaryGeoFunction(), translateFunctionWithGeoArg(), translateGeoConstructor(), and translateUnaryGeoFunction().

209  {
210  std::vector<std::shared_ptr<Analyzer::Expr>> geoargs;
211 
212  const auto rex_input = dynamic_cast<const RexInput*>(rex_scalar);
213  if (rex_input) {
214  const auto input = translateInput(rex_input);
215  const auto column = dynamic_cast<const Analyzer::ColumnVar*>(input.get());
216  if (!column || !column->get_type_info().is_geometry()) {
217  throw QueryNotSupported("Geo function is expecting a geo column argument");
218  }
219  return translateGeoColumn(
220  rex_input, arg_ti, with_bounds, with_render_group, expand_geo_col);
221  }
222  const auto rex_function = dynamic_cast<const RexFunctionOperator*>(rex_scalar);
223  if (rex_function) {
224  if (rex_function->getName() == "ST_Transform"sv) {
225  CHECK_EQ(size_t(2), rex_function->size());
226  const auto rex_scalar0 =
227  dynamic_cast<const RexScalar*>(rex_function->getOperand(0));
228  if (!rex_scalar0) {
229  throw QueryNotSupported(rex_function->getName() + ": unexpected first argument");
230  }
231  auto arg0 = translateGeoFunctionArg(
232  rex_scalar0, arg_ti, lindex, with_bounds, with_render_group, expand_geo_col);
233 
234  const auto rex_literal =
235  dynamic_cast<const RexLiteral*>(rex_function->getOperand(1));
236  if (!rex_literal) {
237  throw QueryNotSupported(rex_function->getName() +
238  ": second argument is expected to be a literal");
239  }
240  const auto e = translateLiteral(rex_literal);
241  auto ce = std::dynamic_pointer_cast<Analyzer::Constant>(e);
242  if (!ce || !e->get_type_info().is_integer()) {
243  throw QueryNotSupported(rex_function->getName() + ": expecting integer SRID");
244  }
245  int32_t srid = 0;
246  if (e->get_type_info().get_type() == kSMALLINT) {
247  srid = static_cast<int32_t>(ce->get_constval().smallintval);
248  } else if (e->get_type_info().get_type() == kTINYINT) {
249  srid = static_cast<int32_t>(ce->get_constval().tinyintval);
250  } else if (e->get_type_info().get_type() == kINT) {
251  srid = static_cast<int32_t>(ce->get_constval().intval);
252  } else {
253  throw QueryNotSupported(rex_function->getName() + ": expecting integer SRID");
254  }
255  if (srid != 900913) {
256  throw QueryNotSupported(rex_function->getName() + ": unsupported output SRID " +
257  std::to_string(srid));
258  }
259  if (arg_ti.get_input_srid() > 0) {
260  if (arg_ti.get_input_srid() != 4326) {
261  throw QueryNotSupported(rex_function->getName() + ": unsupported input SRID " +
262  std::to_string(arg_ti.get_input_srid()));
263  }
264  arg_ti.set_output_srid(
265  srid); // We have a valid input SRID, register the output SRID for transform
266  } else {
267  throw QueryNotSupported(rex_function->getName() +
268  ": unexpected input SRID, unable to transform");
269  }
270  return arg0;
271  } else if (func_resolve(
272  rex_function->getName(), "ST_GeomFromText"sv, "ST_GeogFromText"sv)) {
273  CHECK(rex_function->size() == size_t(1) || rex_function->size() == size_t(2));
274  // First - register srid, then send it to geo literal translation
275  int32_t srid = 0;
276  if (rex_function->size() == 2) {
277  const auto rex_literal =
278  dynamic_cast<const RexLiteral*>(rex_function->getOperand(1));
279  if (!rex_literal) {
280  throw QueryNotSupported(rex_function->getName() +
281  ": second argument is expected to be a literal");
282  }
283  const auto e = translateLiteral(rex_literal);
284  auto ce = std::dynamic_pointer_cast<Analyzer::Constant>(e);
285  if (!ce || !e->get_type_info().is_integer()) {
286  throw QueryNotSupported(rex_function->getName() + ": expecting integer SRID");
287  }
288  if (e->get_type_info().get_type() == kSMALLINT) {
289  srid = static_cast<int32_t>(ce->get_constval().smallintval);
290  } else if (e->get_type_info().get_type() == kTINYINT) {
291  srid = static_cast<int32_t>(ce->get_constval().tinyintval);
292  } else if (e->get_type_info().get_type() == kINT) {
293  srid = static_cast<int32_t>(ce->get_constval().intval);
294  } else {
295  throw QueryNotSupported(rex_function->getName() + " expecting integer SRID");
296  }
297  if (srid != 0 && srid != 4326 && srid != 900913) {
298  throw QueryNotSupported(rex_function->getName() + ": unsupported SRID " +
299  std::to_string(srid));
300  }
301  }
302  arg_ti.set_input_srid(srid); // Input SRID
303  arg_ti.set_output_srid(srid); // Output SRID is the same - no transform
304 
305  const auto rex_literal =
306  dynamic_cast<const RexLiteral*>(rex_function->getOperand(0));
307  if (!rex_literal) {
308  throw QueryNotSupported(rex_function->getName() +
309  " expects a string literal as first argument");
310  }
311  auto arg0 = translateGeoLiteral(rex_literal, arg_ti, with_bounds);
312  arg_ti.set_subtype((rex_function->getName() == "ST_GeogFromText"sv) ? kGEOGRAPHY
313  : kGEOMETRY);
314  return arg0;
315  } else if (rex_function->getName() == "ST_PointN"sv) {
316  CHECK_EQ(size_t(2), rex_function->size());
317  const auto rex_scalar0 =
318  dynamic_cast<const RexScalar*>(rex_function->getOperand(0));
319  if (!rex_scalar0) {
320  throw QueryNotSupported(rex_function->getName() +
321  ": expects scalar as first argument");
322  }
323  auto arg0 = translateGeoFunctionArg(
324  rex_scalar0, arg_ti, lindex, with_bounds, with_render_group, expand_geo_col);
325  if (arg_ti.get_type() != kLINESTRING) {
326  throw QueryNotSupported(rex_function->getName() +
327  " expects LINESTRING as first argument");
328  }
329  const auto rex_literal =
330  dynamic_cast<const RexLiteral*>(rex_function->getOperand(1));
331  if (!rex_literal) {
332  throw QueryNotSupported(rex_function->getName() +
333  ": second argument is expected to be a literal");
334  }
335  const auto e = translateLiteral(rex_literal);
336  auto ce = std::dynamic_pointer_cast<Analyzer::Constant>(e);
337  if (!ce || !e->get_type_info().is_integer()) {
338  throw QueryNotSupported(rex_function->getName() +
339  ": expecting integer index as second argument");
340  }
341  int32_t index = 0;
342  if (e->get_type_info().get_type() == kSMALLINT) {
343  index = static_cast<int32_t>(ce->get_constval().smallintval);
344  } else if (e->get_type_info().get_type() == kTINYINT) {
345  index = static_cast<int32_t>(ce->get_constval().tinyintval);
346  } else if (e->get_type_info().get_type() == kINT) {
347  index = static_cast<int32_t>(ce->get_constval().intval);
348  } else {
349  throw QueryNotSupported(rex_function->getName() + " expecting integer index");
350  }
351  if (lindex != 0) {
352  throw QueryNotSupported(rex_function->getName() +
353  ": LINESTRING is already indexed");
354  }
355  if (index == 0) {
356  throw QueryNotSupported(rex_function->getName() + ": invalid index");
357  }
358  lindex = index;
359  return arg0;
360  } else if (rex_function->getName() == "ST_StartPoint"sv) {
361  CHECK_EQ(size_t(1), rex_function->size());
362  const auto rex_scalar0 =
363  dynamic_cast<const RexScalar*>(rex_function->getOperand(0));
364  if (!rex_scalar0) {
365  throw QueryNotSupported(rex_function->getName() +
366  ": expects scalar as first argument");
367  }
368  auto arg0 = translateGeoFunctionArg(
369  rex_scalar0, arg_ti, lindex, with_bounds, with_render_group, expand_geo_col);
370  if (arg_ti.get_type() != kLINESTRING) {
371  throw QueryNotSupported(rex_function->getName() +
372  " expects LINESTRING as first argument");
373  }
374  if (lindex != 0) {
375  throw QueryNotSupported(rex_function->getName() +
376  ": LINESTRING is already indexed");
377  }
378  lindex = 1;
379  return arg0;
380  } else if (rex_function->getName() == "ST_EndPoint"sv) {
381  CHECK_EQ(size_t(1), rex_function->size());
382  const auto rex_scalar0 =
383  dynamic_cast<const RexScalar*>(rex_function->getOperand(0));
384  if (!rex_scalar0) {
385  throw QueryNotSupported(rex_function->getName() +
386  ": expects scalar as first argument");
387  }
388  auto arg0 = translateGeoFunctionArg(
389  rex_scalar0, arg_ti, lindex, with_bounds, with_render_group, expand_geo_col);
390  if (arg_ti.get_type() != kLINESTRING) {
391  throw QueryNotSupported(rex_function->getName() +
392  " expects LINESTRING as first argument");
393  }
394  if (lindex != 0) {
395  throw QueryNotSupported(rex_function->getName() +
396  ": LINESTRING is already indexed");
397  }
398  lindex = -1;
399  return arg0;
400  } else if (rex_function->getName() == "ST_SRID"sv) {
401  CHECK_EQ(size_t(1), rex_function->size());
402  const auto rex_scalar0 =
403  dynamic_cast<const RexScalar*>(rex_function->getOperand(0));
404  if (!rex_scalar0) {
405  throw QueryNotSupported(rex_function->getName() +
406  ": expects scalar as first argument");
407  }
408  auto arg0 = translateGeoFunctionArg(
409  rex_scalar0, arg_ti, lindex, with_bounds, with_render_group, expand_geo_col);
410  if (!IS_GEO(arg_ti.get_type())) {
411  throw QueryNotSupported(rex_function->getName() + " expects geometry argument");
412  }
413  return arg0;
414  } else if (rex_function->getName() == "ST_SetSRID"sv) {
415  CHECK_EQ(size_t(2), rex_function->size());
416  const auto rex_scalar0 =
417  dynamic_cast<const RexScalar*>(rex_function->getOperand(0));
418  if (!rex_scalar0) {
419  throw QueryNotSupported(rex_function->getName() +
420  ": expects scalar as first argument");
421  }
422  auto arg0 = translateGeoFunctionArg(rex_scalar0,
423  arg_ti,
424  lindex,
425  with_bounds,
426  with_render_group,
427  expand_geo_col,
428  is_projection);
429  if (!IS_GEO(arg_ti.get_type())) {
430  throw QueryNotSupported(rex_function->getName() + " expects geometry argument");
431  }
432  const auto rex_literal =
433  dynamic_cast<const RexLiteral*>(rex_function->getOperand(1));
434  if (!rex_literal) {
435  throw QueryNotSupported(rex_function->getName() +
436  ": second argument is expected to be a literal");
437  }
438  const auto e = translateLiteral(rex_literal);
439  auto ce = std::dynamic_pointer_cast<Analyzer::Constant>(e);
440  if (!ce || !e->get_type_info().is_integer()) {
441  throw QueryNotSupported(rex_function->getName() + ": expecting integer SRID");
442  }
443  int32_t srid = 0;
444  if (e->get_type_info().get_type() == kSMALLINT) {
445  srid = static_cast<int32_t>(ce->get_constval().smallintval);
446  } else if (e->get_type_info().get_type() == kTINYINT) {
447  srid = static_cast<int32_t>(ce->get_constval().tinyintval);
448  } else if (e->get_type_info().get_type() == kINT) {
449  srid = static_cast<int32_t>(ce->get_constval().intval);
450  } else {
451  throw QueryNotSupported(rex_function->getName() + ": expecting integer SRID");
452  }
453  arg_ti.set_input_srid(srid); // Input SRID
454  arg_ti.set_output_srid(srid); // Output SRID is the same - no transform
455  return arg0;
456  } else if (rex_function->getName() == "CastToGeography"sv) {
457  CHECK_EQ(size_t(1), rex_function->size());
458  const auto rex_scalar0 =
459  dynamic_cast<const RexScalar*>(rex_function->getOperand(0));
460  if (!rex_scalar0) {
461  throw QueryNotSupported(rex_function->getName() +
462  ": expects scalar as first argument");
463  }
464  auto arg0 = translateGeoFunctionArg(
465  rex_scalar0, arg_ti, lindex, with_bounds, with_render_group, expand_geo_col);
466  if (!IS_GEO(arg_ti.get_type())) {
467  throw QueryNotSupported(rex_function->getName() + " expects geometry argument");
468  }
469  if (arg_ti.get_output_srid() != 4326) {
470  throw QueryNotSupported(rex_function->getName() +
471  " expects geometry with SRID=4326");
472  }
473  arg_ti.set_subtype(kGEOGRAPHY);
474  return arg0;
475  } else if (rex_function->getName() == "ST_Point"sv) {
476  CHECK_EQ(size_t(2), rex_function->size());
477  arg_ti.set_type(kPOINT);
478  arg_ti.set_subtype(kGEOMETRY);
479  arg_ti.set_input_srid(0);
480  arg_ti.set_output_srid(0);
482 
483  auto coord1 = translateScalarRex(rex_function->getOperand(0));
484  auto coord2 = translateScalarRex(rex_function->getOperand(1));
485  auto d_ti = SQLTypeInfo(kDOUBLE, true);
486  auto cast_coord1 = coord1->add_cast(d_ti);
487  auto cast_coord2 = coord2->add_cast(d_ti);
488  // First try to fold to geo literal
489  auto fold1 = fold_expr(cast_coord1.get());
490  auto fold2 = fold_expr(cast_coord2.get());
491  auto const_coord1 = std::dynamic_pointer_cast<Analyzer::Constant>(fold1);
492  auto const_coord2 = std::dynamic_pointer_cast<Analyzer::Constant>(fold2);
493  if (const_coord1 && const_coord2) {
494  CHECK(const_coord1->get_type_info().get_type() == kDOUBLE);
495  CHECK(const_coord2->get_type_info().get_type() == kDOUBLE);
496  std::string wkt = "POINT(" +
497  std::to_string(const_coord1->get_constval().doubleval) + " " +
498  std::to_string(const_coord2->get_constval().doubleval) + ")";
499  RexLiteral rex_literal{wkt, kTEXT, kNULLT, 0, 0, 0, 0};
500  auto args = translateGeoLiteral(&rex_literal, arg_ti, false);
501  CHECK(arg_ti.get_type() == kPOINT);
502  return args;
503  }
504  // Couldn't fold to geo literal, construct on the fly
505  auto da_ti = SQLTypeInfo(kARRAY, true);
506  da_ti.set_subtype(kDOUBLE);
507  da_ti.set_size(16);
508  auto cast_coords = {cast_coord1, cast_coord2};
509  auto is_local_alloca = !is_projection;
510  auto ae =
511  makeExpr<Analyzer::ArrayExpr>(da_ti, cast_coords, 0, false, is_local_alloca);
512  // cast it to tinyint[16]
513  SQLTypeInfo tia_ti = SQLTypeInfo(kARRAY, true);
514  tia_ti.set_subtype(kTINYINT);
515  tia_ti.set_size(16);
516  return {makeExpr<Analyzer::UOper>(tia_ti, false, kCAST, ae)};
517  } else {
518  throw QueryNotSupported("Unsupported argument: " + rex_function->getName());
519  }
520  }
521  const auto rex_literal = dynamic_cast<const RexLiteral*>(rex_scalar);
522  if (rex_literal) {
523  return translateGeoLiteral(rex_literal, arg_ti, with_bounds);
524  }
525  throw QueryNotSupported("Geo function argument not supported");
526 }
#define CHECK_EQ(x, y)
Definition: Logger.h:201
auto func_resolve
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:326
void set_input_srid(int d)
Definition: sqltypes.h:420
Definition: sqldefs.h:49
HOST DEVICE void set_type(SQLTypes t)
Definition: sqltypes.h:416
void set_compression(EncodingType c)
Definition: sqltypes.h:426
std::string to_string(char const *&&v)
void set_output_srid(int s)
Definition: sqltypes.h:422
HOST DEVICE void set_subtype(SQLTypes st)
Definition: sqltypes.h:417
static std::shared_ptr< Analyzer::Expr > translateLiteral(const RexLiteral *)
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
HOST DEVICE int get_input_srid() const
Definition: sqltypes.h:330
std::vector< std::shared_ptr< Analyzer::Expr > > translateGeoFunctionArg(const RexScalar *rex_scalar, SQLTypeInfo &arg_ti, int32_t &lindex, const bool with_bounds, const bool with_render_group, const bool expand_geo_col, const bool is_projection=false) const
std::vector< std::shared_ptr< Analyzer::Expr > > translateGeoLiteral(const RexLiteral *, SQLTypeInfo &, bool) const
SQLTypeInfoCore< ArrayContextTypeSizer, ExecutorTypePackaging, DateTimeFacilities > SQLTypeInfo
Definition: sqltypes.h:852
Definition: sqltypes.h:55
HOST DEVICE int get_output_srid() const
Definition: sqltypes.h:332
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 CHECK(condition)
Definition: Logger.h:193
Definition: sqltypes.h:48
std::shared_ptr< Analyzer::Expr > translateInput(const RexInput *) const
#define IS_GEO(T)
Definition: sqltypes.h:167
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:

◆ translateGeoLiteral()

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

Definition at line 107 of file RelAlgTranslatorGeo.cpp.

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

Referenced by translateGeoFunctionArg().

110  {
111  CHECK(rex_literal);
112  if (rex_literal->getType() != kTEXT) {
113  throw std::runtime_error("Geo literals must be strings");
114  }
115  const auto e = translateLiteral(rex_literal);
116  auto wkt = std::dynamic_pointer_cast<Analyzer::Constant>(e);
117  CHECK(wkt);
118  std::vector<double> coords;
119  std::vector<double> bounds;
120  std::vector<int> ring_sizes;
121  std::vector<int> poly_rings;
122  int32_t srid = ti.get_output_srid();
124  *wkt->get_constval().stringval, ti, coords, bounds, ring_sizes, poly_rings)) {
125  throw QueryNotSupported("Could not read geometry from text");
126  }
128  ti.set_input_srid(srid);
129  ti.set_output_srid(srid);
130  // Compress geo literals by default
131  if (srid == 4326) {
133  ti.set_comp_param(32);
134  }
135 
136  std::vector<std::shared_ptr<Analyzer::Expr>> args;
137 
138  std::vector<uint8_t> compressed_coords = Importer_NS::compress_coords(coords, ti);
139  std::list<std::shared_ptr<Analyzer::Expr>> compressed_coords_exprs;
140  for (auto cc : compressed_coords) {
141  Datum d;
142  d.tinyintval = cc;
143  auto e = makeExpr<Analyzer::Constant>(kTINYINT, false, d);
144  compressed_coords_exprs.push_back(e);
145  }
146  SQLTypeInfo arr_ti = SQLTypeInfo(kARRAY, true);
147  arr_ti.set_subtype(kTINYINT);
148  arr_ti.set_size(compressed_coords.size() * sizeof(int8_t));
149  arr_ti.set_compression(ti.get_compression());
150  arr_ti.set_comp_param((ti.get_compression() == kENCODING_GEOINT) ? 32 : 64);
151  args.push_back(makeExpr<Analyzer::Constant>(arr_ti, false, compressed_coords_exprs));
152 
153  auto lit_type = ti.get_type();
154  if (lit_type == kPOLYGON || lit_type == kMULTIPOLYGON) {
155  // ring sizes
156  std::list<std::shared_ptr<Analyzer::Expr>> ring_size_exprs;
157  for (auto c : ring_sizes) {
158  Datum d;
159  d.intval = c;
160  auto e = makeExpr<Analyzer::Constant>(kINT, false, d);
161  ring_size_exprs.push_back(e);
162  }
163  SQLTypeInfo arr_ti = SQLTypeInfo(kARRAY, true);
164  arr_ti.set_subtype(kINT);
165  arr_ti.set_size(ring_sizes.size() * sizeof(int32_t));
166  args.push_back(makeExpr<Analyzer::Constant>(arr_ti, false, ring_size_exprs));
167 
168  // poly rings
169  if (lit_type == kMULTIPOLYGON) {
170  std::list<std::shared_ptr<Analyzer::Expr>> poly_rings_exprs;
171  for (auto c : poly_rings) {
172  Datum d;
173  d.intval = c;
174  auto e = makeExpr<Analyzer::Constant>(kINT, false, d);
175  poly_rings_exprs.push_back(e);
176  }
177  SQLTypeInfo arr_ti = SQLTypeInfo(kARRAY, true);
178  arr_ti.set_subtype(kINT);
179  arr_ti.set_size(poly_rings.size() * sizeof(int32_t));
180  args.push_back(makeExpr<Analyzer::Constant>(arr_ti, false, poly_rings_exprs));
181  }
182  }
183 
184  if (with_bounds && ti.has_bounds()) {
185  // bounds
186  std::list<std::shared_ptr<Analyzer::Expr>> bounds_exprs;
187  for (auto b : bounds) {
188  Datum d;
189  d.doubleval = b;
190  auto e = makeExpr<Analyzer::Constant>(kDOUBLE, false, d);
191  bounds_exprs.push_back(e);
192  }
193  SQLTypeInfo arr_ti = SQLTypeInfo(kARRAY, true);
194  arr_ti.set_subtype(kDOUBLE);
195  arr_ti.set_size(bounds.size() * sizeof(double));
196  args.push_back(makeExpr<Analyzer::Constant>(arr_ti, false, bounds_exprs));
197  }
198 
199  return args;
200 }
int8_t tinyintval
Definition: sqltypes.h:126
void set_size(int s)
Definition: sqltypes.h:424
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:326
std::vector< uint8_t > compress_coords(std::vector< double > &coords, const SQLTypeInfo &ti)
Definition: Importer.cpp:1422
void set_input_srid(int d)
Definition: sqltypes.h:420
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:334
void set_compression(EncodingType c)
Definition: sqltypes.h:426
int32_t intval
Definition: sqltypes.h:128
void set_output_srid(int s)
Definition: sqltypes.h:422
HOST DEVICE void set_subtype(SQLTypes st)
Definition: sqltypes.h:417
static std::shared_ptr< Analyzer::Expr > translateLiteral(const RexLiteral *)
SQLTypeInfoCore< ArrayContextTypeSizer, ExecutorTypePackaging, DateTimeFacilities > SQLTypeInfo
Definition: sqltypes.h:852
Definition: sqltypes.h:55
HOST DEVICE int get_output_srid() const
Definition: sqltypes.h:332
static bool getGeoColumns(const std::string &wkt, 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: geo_types.cpp:459
void set_comp_param(int p)
Definition: sqltypes.h:427
#define CHECK(condition)
Definition: Logger.h:193
Definition: sqltypes.h:48
bool has_bounds() const
Definition: sqltypes.h:395
double doubleval
Definition: sqltypes.h:131
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ translateGeoOverlapsOper()

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

Definition at line 1078 of file RelAlgTranslatorGeo.cpp.

References CHECK, CHECK_EQ, CHECK_GT, SQLTypeInfoCore< TYPE_FACET_PACK >::get_type(), RexOperator::getOperand(), kBOOLEAN, kONE, kOVERLAPS, kPOINT, RexOperator::size(), and translateGeoColumn().

1079  {
1080  CHECK_EQ(rex_operator->size(), 2u);
1081 
1082  auto translate_input =
1083  [&](const RexScalar* operand) -> std::shared_ptr<Analyzer::Expr> {
1084  const auto input = dynamic_cast<const RexInput*>(operand);
1085  CHECK(input);
1086 
1087  SQLTypeInfo ti;
1088  const auto exprs = translateGeoColumn(input, ti, true, false, false);
1089  CHECK_GT(exprs.size(), 0u);
1090  if (ti.get_type() == kPOINT) {
1091  return exprs.front();
1092  } else {
1093  return exprs.back();
1094  }
1095  };
1096 
1097  SQLQualifier sql_qual{kONE};
1098  SQLOps sql_op{kOVERLAPS};
1099  return makeExpr<Analyzer::BinOper>(SQLTypeInfo(kBOOLEAN, false),
1100  false,
1101  sql_op,
1102  sql_qual,
1103  translate_input(rex_operator->getOperand(1)),
1104  translate_input(rex_operator->getOperand(0)));
1105 }
#define CHECK_EQ(x, y)
Definition: Logger.h:201
SQLQualifier
Definition: sqldefs.h:69
SQLOps
Definition: sqldefs.h:29
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:326
#define CHECK_GT(x, y)
Definition: Logger.h:205
const RexScalar * getOperand(const size_t idx) const
SQLTypeInfoCore< ArrayContextTypeSizer, ExecutorTypePackaging, DateTimeFacilities > SQLTypeInfo
Definition: sqltypes.h:852
Definition: sqldefs.h:69
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 CHECK(condition)
Definition: Logger.h:193
size_t size() const
+ Here is the call graph for this function:

◆ translateHPTLiteral()

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

Definition at line 1710 of file RelAlgTranslator.cpp.

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

1711  {
1712  /* since calcite uses Avatica package called DateTimeUtils to parse timestamp strings.
1713  Therefore any string having fractional seconds more 3 places after the decimal
1714  (milliseconds) will get truncated to 3 decimal places, therefore we lose precision
1715  (us|ns). Issue: [BE-2461] Here we are hijacking literal cast to Timestamp(6|9) from
1716  calcite and translating them to generate our own casts.
1717  */
1718  CHECK_EQ(size_t(1), rex_function->size());
1719  const auto operand = translateScalarRex(rex_function->getOperand(0));
1720  const auto& operand_ti = operand->get_type_info();
1721  const auto& target_ti = rex_function->getType();
1722  if (!operand_ti.is_string()) {
1723  throw std::runtime_error(
1724  "High precision timestamp cast argument must be a string. Input type is: " +
1725  operand_ti.get_type_name());
1726  } else if (!target_ti.is_high_precision_timestamp()) {
1727  throw std::runtime_error(
1728  "Cast target type should be high precision timestamp. Input type is: " +
1729  target_ti.get_type_name());
1730  } else if (target_ti.get_dimension() != 6 && target_ti.get_dimension() != 9) {
1731  throw std::runtime_error(
1732  "Cast target type should be TIMESTAMP(6|9). Input type is: TIMESTAMP(" +
1733  std::to_string(target_ti.get_dimension()) + ")");
1734  } else {
1735  return operand->add_cast(target_ti);
1736  }
1737 }
#define CHECK_EQ(x, y)
Definition: Logger.h:201
std::string to_string(char const *&&v)
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const RexScalar * getOperand(const size_t idx) const
const SQLTypeInfo & getType() const
size_t size() const
+ Here is the call graph for this function:

◆ translateInOper()

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

Definition at line 516 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(), RexOperator::getOperand(), kCAST, kENCODING_DICT, kENCODING_NONE, run_benchmark_import::result, RexOperator::size(), and RelAlgNode::size().

517  {
518  if (just_explain_) {
519  throw std::runtime_error("EXPLAIN is not supported with sub-queries");
520  }
521  CHECK(rex_operator->size() == 2);
522  const auto lhs = translateScalarRex(rex_operator->getOperand(0));
523  const auto rhs = rex_operator->getOperand(1);
524  const auto rex_subquery = dynamic_cast<const RexSubQuery*>(rhs);
525  CHECK(rex_subquery);
526  auto ti = lhs->get_type_info();
527  auto result = rex_subquery->getExecutionResult();
528  auto& row_set = result->getRows();
529  CHECK_EQ(size_t(1), row_set->colCount());
530  const auto& rhs_ti = row_set->getColType(0);
531  if (rhs_ti.get_type() != ti.get_type()) {
532  throw std::runtime_error(
533  "The two sides of the IN operator must have the same type; found " +
534  ti.get_type_name() + " and " + rhs_ti.get_type_name());
535  }
536  row_set->moveToBegin();
537  if (row_set->entryCount() > 10000) {
538  std::shared_ptr<Analyzer::Expr> expr;
539  if ((ti.is_integer() || (ti.is_string() && ti.get_compression() == kENCODING_DICT)) &&
540  !row_set->getQueryMemDesc().didOutputColumnar()) {
541  expr = getInIntegerSetExpr(lhs, *row_set);
542  // Handle the highly unlikely case when the InIntegerSet ended up being tiny.
543  // Just let it fall through the usual InValues path at the end of this method,
544  // its codegen knows to use inline comparisons for few values.
545  if (expr && std::static_pointer_cast<Analyzer::InIntegerSet>(expr)
546  ->get_value_list()
547  .size() <= 100) {
548  expr = nullptr;
549  }
550  } else {
551  expr = get_in_values_expr(lhs, *row_set);
552  }
553  if (expr) {
554  return expr;
555  }
556  }
557  std::list<std::shared_ptr<Analyzer::Expr>> value_exprs;
558  while (true) {
559  auto row = row_set->getNextRow(true, false);
560  if (row.empty()) {
561  break;
562  }
563  if (g_enable_watchdog && value_exprs.size() >= 10000) {
564  throw std::runtime_error(
565  "Unable to handle 'expr IN (subquery)', subquery returned 10000+ rows.");
566  }
567  auto scalar_tv = boost::get<ScalarTargetValue>(&row[0]);
568  Datum d{0};
569  bool is_null_const{false};
570  std::tie(d, is_null_const) = datum_from_scalar_tv(scalar_tv, ti);
571  if (ti.is_string() && ti.get_compression() != kENCODING_NONE) {
572  auto ti_none_encoded = ti;
573  ti_none_encoded.set_compression(kENCODING_NONE);
574  auto none_encoded_string = makeExpr<Analyzer::Constant>(ti, is_null_const, d);
575  auto dict_encoded_string =
576  std::make_shared<Analyzer::UOper>(ti, false, kCAST, none_encoded_string);
577  value_exprs.push_back(dict_encoded_string);
578  } else {
579  value_exprs.push_back(makeExpr<Analyzer::Constant>(ti, is_null_const, d));
580  }
581  }
582  return makeExpr<Analyzer::InValues>(lhs, value_exprs);
583 }
#define CHECK_EQ(x, y)
Definition: Logger.h:201
bool g_enable_watchdog
Definition: Execute.cpp:71
std::shared_ptr< Analyzer::Expr > getInIntegerSetExpr(std::shared_ptr< Analyzer::Expr > arg, const ResultSet &val_set) const
Definition: sqldefs.h:49
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const RexScalar * getOperand(const size_t idx) const
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:193
std::pair< Datum, bool > datum_from_scalar_tv(const ScalarTargetValue *scalar_tv, const SQLTypeInfo &ti) noexcept
const bool just_explain_
size_t size() const
+ Here is the call graph for this function:

◆ translateInput()

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

Definition at line 348 of file RelAlgTranslator.cpp.

References CHECK, CHECK_EQ, CHECK_GE, CHECK_LE, CHECK_LT, RexAbstractInput::getIndex(), RelAlgNode::getOutputMetainfo(), RexInput::getSourceNode(), kTEXT, and LEFT.

Referenced by translateGeoFunctionArg().

349  {
350  const auto source = rex_input->getSourceNode();
351  const auto it_rte_idx = input_to_nest_level_.find(source);
352  CHECK(it_rte_idx != input_to_nest_level_.end());
353  const int rte_idx = it_rte_idx->second;
354  const auto scan_source = dynamic_cast<const RelScan*>(source);
355  const auto& in_metainfo = source->getOutputMetainfo();
356  if (scan_source) {
357  // We're at leaf (scan) level and not supposed to have input metadata,
358  // the name and type information come directly from the catalog.
359  CHECK(in_metainfo.empty());
360  const auto table_desc = scan_source->getTableDescriptor();
361  const auto cd =
362  cat_.getMetadataForColumnBySpi(table_desc->tableId, rex_input->getIndex() + 1);
363  CHECK(cd);
364  auto col_ti = cd->columnType;
365  if (col_ti.is_string()) {
366  col_ti.set_type(kTEXT);
367  }
368  if (cd->isVirtualCol) {
369  // TODO(alex): remove at some point, we only need this fixup for backwards
370  // compatibility with old imported data
371  CHECK_EQ("rowid", cd->columnName);
372  col_ti.set_size(8);
373  }
374  CHECK_LE(static_cast<size_t>(rte_idx), join_types_.size());
375  if (rte_idx > 0 && join_types_[rte_idx - 1] == JoinType::LEFT) {
376  col_ti.set_notnull(false);
377  }
378  return std::make_shared<Analyzer::ColumnVar>(
379  col_ti, table_desc->tableId, cd->columnId, rte_idx);
380  }
381  CHECK(!in_metainfo.empty());
382  CHECK_GE(rte_idx, 0);
383  const size_t col_id = rex_input->getIndex();
384  CHECK_LT(col_id, in_metainfo.size());
385  auto col_ti = in_metainfo[col_id].get_type_info();
386  CHECK_LE(static_cast<size_t>(rte_idx), join_types_.size());
387  if (rte_idx > 0 && join_types_[rte_idx - 1] == JoinType::LEFT) {
388  col_ti.set_notnull(false);
389  }
390  return std::make_shared<Analyzer::ColumnVar>(col_ti, -source->getId(), col_id, rte_idx);
391 }
const std::vector< JoinType > join_types_
#define CHECK_EQ(x, y)
Definition: Logger.h:201
#define CHECK_GE(x, y)
Definition: Logger.h:206
const RelAlgNode * getSourceNode() const
const std::unordered_map< const RelAlgNode *, int > input_to_nest_level_
#define CHECK_LT(x, y)
Definition: Logger.h:203
Definition: sqltypes.h:55
const std::vector< TargetMetaInfo > & getOutputMetainfo() const
#define CHECK_LE(x, y)
Definition: Logger.h:204
unsigned getIndex() const
const ColumnDescriptor * getMetadataForColumnBySpi(const int tableId, const size_t spi) const
Definition: Catalog.cpp:1464
#define CHECK(condition)
Definition: Logger.h:193
const Catalog_Namespace::Catalog & cat_
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ translateItem()

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

Definition at line 1242 of file RelAlgTranslator.cpp.

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

1243  {
1244  CHECK_EQ(size_t(2), rex_function->size());
1245  const auto base = translateScalarRex(rex_function->getOperand(0));
1246  const auto index = translateScalarRex(rex_function->getOperand(1));
1247  return makeExpr<Analyzer::BinOper>(
1248  base->get_type_info().get_elem_type(), false, kARRAY_AT, kONE, base, index);
1249 }
#define CHECK_EQ(x, y)
Definition: Logger.h:201
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const RexScalar * getOperand(const size_t idx) const
Definition: sqldefs.h:69
size_t size() const
+ Here is the call graph for this function:

◆ translateKeyForString()

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

Definition at line 1187 of file RelAlgTranslator.cpp.

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

1188  {
1189  const auto& args = translateFunctionArgs(rex_function);
1190  CHECK_EQ(size_t(1), args.size());
1191  const auto expr = dynamic_cast<Analyzer::Expr*>(args[0].get());
1192  if (nullptr == expr || !expr->get_type_info().is_string() ||
1193  expr->get_type_info().is_varlen()) {
1194  throw std::runtime_error(rex_function->getName() +
1195  " expects a dictionary encoded text column.");
1196  }
1197  return makeExpr<Analyzer::KeyForStringExpr>(args[0]);
1198 }
#define CHECK_EQ(x, y)
Definition: Logger.h:201
Analyzer::ExpressionPtrVector translateFunctionArgs(const RexFunctionOperator *) const
const std::string & getName() const
+ Here is the call graph for this function:

◆ translateLength()

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

Definition at line 1179 of file RelAlgTranslator.cpp.

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

1180  {
1181  CHECK_EQ(size_t(1), rex_function->size());
1182  const auto str_arg = translateScalarRex(rex_function->getOperand(0));
1183  return makeExpr<Analyzer::CharLengthExpr>(str_arg->decompress(),
1184  rex_function->getName() == "CHAR_LENGTH"sv);
1185 }
#define CHECK_EQ(x, y)
Definition: Logger.h:201
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const RexScalar * getOperand(const size_t idx) const
const std::string & getName() const
size_t size() const
+ Here is the call graph for this function:

◆ translateLike()

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

Definition at line 905 of file RelAlgTranslator.cpp.

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

906  {
907  CHECK(rex_function->size() == 2 || rex_function->size() == 3);
908  const auto arg = translateScalarRex(rex_function->getOperand(0));
909  const auto like = translateScalarRex(rex_function->getOperand(1));
910  if (!std::dynamic_pointer_cast<const Analyzer::Constant>(like)) {
911  throw std::runtime_error("The matching pattern must be a literal.");
912  }
913  const auto escape = (rex_function->size() == 3)
914  ? translateScalarRex(rex_function->getOperand(2))
915  : nullptr;
916  const bool is_ilike = rex_function->getName() == "PG_ILIKE"sv;
917  return Parser::LikeExpr::get(arg, like, escape, is_ilike, false);
918 }
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const RexScalar * getOperand(const size_t idx) const
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:530
#define CHECK(condition)
Definition: Logger.h:193
const std::string & getName() const
size_t size() const
+ Here is the call graph for this function:

◆ translateLikely()

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

Definition at line 934 of file RelAlgTranslator.cpp.

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

935  {
936  CHECK(rex_function->size() == 1);
937  const auto arg = translateScalarRex(rex_function->getOperand(0));
938  return makeExpr<Analyzer::LikelihoodExpr>(arg, 0.9375);
939 }
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const RexScalar * getOperand(const size_t idx) const
#define CHECK(condition)
Definition: Logger.h:193
size_t size() const
+ Here is the call graph for this function:

◆ translateLiteral()

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

Definition at line 249 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, kBOOLEAN, kDATE, kDECIMAL, kDOUBLE, kINTERVAL_DAY_TIME, kINTERVAL_YEAR_MONTH, kNULLT, kTEXT, kTIME, kTIMESTAMP, LOG, and make_fp_constant().

Referenced by translateGeoFunctionArg(), and translateGeoLiteral().

250  {
251  auto lit_ti = build_type_info(
252  rex_literal->getType(), rex_literal->getScale(), rex_literal->getPrecision());
253  auto target_ti = build_type_info(rex_literal->getTargetType(),
254  rex_literal->getTypeScale(),
255  rex_literal->getTypePrecision());
256  switch (rex_literal->getType()) {
257  case kDECIMAL: {
258  const auto val = rex_literal->getVal<int64_t>();
259  const int precision = rex_literal->getPrecision();
260  const int scale = rex_literal->getScale();
261  if (target_ti.is_fp() && !scale) {
262  return make_fp_constant(val, target_ti);
263  }
264  auto lit_expr = scale ? Parser::FixedPtLiteral::analyzeValue(val, scale, precision)
265  : Parser::IntLiteral::analyzeValue(val);
266  return lit_ti != target_ti ? lit_expr->add_cast(target_ti) : lit_expr;
267  }
268  case kTEXT: {
269  return Parser::StringLiteral::analyzeValue(rex_literal->getVal<std::string>());
270  }
271  case kBOOLEAN: {
272  Datum d;
273  d.boolval = rex_literal->getVal<bool>();
274  return makeExpr<Analyzer::Constant>(kBOOLEAN, false, d);
275  }
276  case kDOUBLE: {
277  Datum d;
278  d.doubleval = rex_literal->getVal<double>();
279  auto lit_expr = makeExpr<Analyzer::Constant>(kDOUBLE, false, d);
280  return lit_ti != target_ti ? lit_expr->add_cast(target_ti) : lit_expr;
281  }
282  case kINTERVAL_DAY_TIME:
283  case kINTERVAL_YEAR_MONTH: {
284  Datum d;
285  d.bigintval = rex_literal->getVal<int64_t>();
286  return makeExpr<Analyzer::Constant>(rex_literal->getType(), false, d);
287  }
288  case kTIME:
289  case kTIMESTAMP: {
290  Datum d;
291  d.bigintval =
292  rex_literal->getType() == kTIMESTAMP && rex_literal->getPrecision() > 0
293  ? rex_literal->getVal<int64_t>()
294  : rex_literal->getVal<int64_t>() / 1000;
295  return makeExpr<Analyzer::Constant>(
296  SQLTypeInfo(rex_literal->getType(), rex_literal->getPrecision(), 0, false),
297  false,
298  d);
299  }
300  case kDATE: {
301  Datum d;
302  d.bigintval = rex_literal->getVal<int64_t>() * 24 * 3600;
303  return makeExpr<Analyzer::Constant>(rex_literal->getType(), false, d);
304  }
305  case kNULLT: {
306  if (target_ti.is_array()) {
308  // defaulting to valid sub-type for convenience
309  target_ti.set_subtype(kBOOLEAN);
310  return makeExpr<Analyzer::ArrayExpr>(target_ti, args, -1, true);
311  }
312  return makeExpr<Analyzer::Constant>(rex_literal->getTargetType(), true, Datum{0});
313  }
314  default: {
315  LOG(FATAL) << "Unexpected literal type " << lit_ti.get_type_name();
316  }
317  }
318  return nullptr;
319 }
Definition: sqltypes.h:52
#define LOG(tag)
Definition: Logger.h:188
bool boolval
Definition: sqltypes.h:125
int64_t bigintval
Definition: sqltypes.h:129
SQLTypeInfo build_type_info(const SQLTypes sql_type, const int scale, const int precision)
static std::shared_ptr< Analyzer::Expr > analyzeValue(const std::string &)
Definition: ParserNode.cpp:97
SQLTypeInfoCore< ArrayContextTypeSizer, ExecutorTypePackaging, DateTimeFacilities > SQLTypeInfo
Definition: sqltypes.h:852
Definition: sqltypes.h:55
Definition: sqltypes.h:56
Definition: Catalog.h:60
static std::shared_ptr< Analyzer::Expr > analyzeValue(const int64_t numericval, const int scale, const int precision)
Definition: ParserNode.cpp:137
std::shared_ptr< Analyzer::Constant > make_fp_constant(const int64_t val, const SQLTypeInfo &ti)
std::vector< ExpressionPtr > ExpressionPtrVector
Definition: Analyzer.h:182
double doubleval
Definition: sqltypes.h:131
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ translateLower()

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

Definition at line 1200 of file RelAlgTranslator.cpp.

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

1201  {
1202  const auto& args = translateFunctionArgs(rex_function);
1203  CHECK_EQ(size_t(1), args.size());
1204  CHECK(args[0]);
1205 
1206  if (args[0]->get_type_info().is_dict_encoded_string() ||
1207  dynamic_cast<Analyzer::Constant*>(args[0].get())) {
1208  return makeExpr<Analyzer::LowerExpr>(args[0]);
1209  }
1210 
1211  throw std::runtime_error(rex_function->getName() +
1212  " expects a dictionary encoded text column or a literal.");
1213 }
#define CHECK_EQ(x, y)
Definition: Logger.h:201
Analyzer::ExpressionPtrVector translateFunctionArgs(const RexFunctionOperator *) const
#define CHECK(condition)
Definition: Logger.h:193
const std::string & getName() const
+ Here is the call graph for this function:

◆ translateNow()

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

Definition at line 1251 of file RelAlgTranslator.cpp.

References Parser::TimestampLiteral::get().

1251  {
1253 }
static std::shared_ptr< Analyzer::Expr > get(const int64_t)
Definition: ParserNode.cpp:173
+ Here is the call graph for this function:

◆ translateOffsetInFragment()

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

Definition at line 1309 of file RelAlgTranslator.cpp.

1309  {
1310  return makeExpr<Analyzer::OffsetInFragment>();
1311 }

◆ translateOper()

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

Definition at line 835 of file RelAlgTranslator.cpp.

References CHECK, CHECK_GT, anonymous_namespace{RelAlgTranslator.cpp}::get_quantified_rhs(), RexOperator::getOperand(), RexOperator::getOperator(), IS_COMPARISON, kIN, kMINUS, kONE, kOVERLAPS, kPLUS, Parser::OperExpr::normalize(), and RexOperator::size().

836  {
837  CHECK_GT(rex_operator->size(), size_t(0));
838  if (rex_operator->size() == 1) {
839  return translateUoper(rex_operator);
840  }
841  const auto sql_op = rex_operator->getOperator();
842  if (sql_op == kIN) {
843  return translateInOper(rex_operator);
844  }
845  if (sql_op == kMINUS || sql_op == kPLUS) {
846  auto date_plus_minus = translateDatePlusMinus(rex_operator);
847  if (date_plus_minus) {
848  return date_plus_minus;
849  }
850  }
851  if (sql_op == kOVERLAPS) {
852  return translateOverlapsOper(rex_operator);
853  } else if (IS_COMPARISON(sql_op)) {
854  auto geo_comp = translateGeoComparison(rex_operator);
855  if (geo_comp) {
856  return geo_comp;
857  }
858  }
859  auto lhs = translateScalarRex(rex_operator->getOperand(0));
860  for (size_t i = 1; i < rex_operator->size(); ++i) {
861  std::shared_ptr<Analyzer::Expr> rhs;
862  SQLQualifier sql_qual{kONE};
863  const auto rhs_op = rex_operator->getOperand(i);
864  std::tie(rhs, sql_qual) = get_quantified_rhs(rhs_op, *this);
865  if (!rhs) {
866  rhs = translateScalarRex(rhs_op);
867  }
868  CHECK(rhs);
869  lhs = Parser::OperExpr::normalize(sql_op, sql_qual, lhs, rhs);
870  }
871  return lhs;
872 }
std::shared_ptr< Analyzer::Expr > translateGeoComparison(const RexOperator *) const
SQLOps getOperator() const
SQLQualifier
Definition: sqldefs.h:69
Definition: sqldefs.h:41
#define CHECK_GT(x, y)
Definition: Logger.h:205
std::shared_ptr< Analyzer::Expr > translateOverlapsOper(const RexOperator *) const
std::shared_ptr< Analyzer::Expr > translateDatePlusMinus(const RexOperator *) const
std::shared_ptr< Analyzer::Expr > translateUoper(const RexOperator *) const
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const RexScalar * getOperand(const size_t idx) 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)
Definition: ParserNode.cpp:261
std::pair< std::shared_ptr< Analyzer::Expr >, SQLQualifier > get_quantified_rhs(const RexScalar *rex_scalar, const RelAlgTranslator &translator)
Definition: sqldefs.h:40
Definition: sqldefs.h:69
Definition: sqldefs.h:53
std::shared_ptr< Analyzer::Expr > translateInOper(const RexOperator *) const
#define CHECK(condition)
Definition: Logger.h:193
size_t size() const
#define IS_COMPARISON(X)
Definition: sqldefs.h:57
+ Here is the call graph for this function:

◆ translateOverlapsOper()

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

Definition at line 874 of file RelAlgTranslator.cpp.

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

875  {
876  const auto sql_op = rex_operator->getOperator();
877  CHECK(sql_op == kOVERLAPS);
878 
879  const auto lhs = translateScalarRex(rex_operator->getOperand(0));
880  const auto lhs_ti = lhs->get_type_info();
881  if (lhs_ti.is_geometry()) {
882  return translateGeoOverlapsOper(rex_operator);
883  } else {
884  throw std::runtime_error(
885  "Overlaps equivalence is currently only supported for geospatial types");
886  }
887 }
SQLOps getOperator() const
std::shared_ptr< Analyzer::Expr > translateGeoOverlapsOper(const RexOperator *) const
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const RexScalar * getOperand(const size_t idx) const
#define CHECK(condition)
Definition: Logger.h:193
+ Here is the call graph for this function:

◆ translateRegexp()

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

Definition at line 920 of file RelAlgTranslator.cpp.

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

921  {
922  CHECK(rex_function->size() == 2 || rex_function->size() == 3);
923  const auto arg = translateScalarRex(rex_function->getOperand(0));
924  const auto pattern = translateScalarRex(rex_function->getOperand(1));
925  if (!std::dynamic_pointer_cast<const Analyzer::Constant>(pattern)) {
926  throw std::runtime_error("The matching pattern must be a literal.");
927  }
928  const auto escape = (rex_function->size() == 3)
929  ? translateScalarRex(rex_function->getOperand(2))
930  : nullptr;
931  return Parser::RegexpExpr::get(arg, pattern, escape, false);
932 }
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:625
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const RexScalar * getOperand(const size_t idx) const
#define CHECK(condition)
Definition: Logger.h:193
size_t size() const
+ Here is the call graph for this function:

◆ translateScalarRex()

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

Definition at line 169 of file RelAlgTranslator.cpp.

References CHECK.

Referenced by anonymous_namespace{RelAlgExecutor.cpp}::get_inputs_meta(), anonymous_namespace{RelAlgTranslator.cpp}::get_quantified_rhs(), RelAlgExecutor::makeJoinQuals(), anonymous_namespace{RelAlgExecutor.cpp}::translate_quals(), anonymous_namespace{RelAlgExecutor.cpp}::translate_scalar_sources(), anonymous_namespace{RelAlgExecutor.cpp}::translate_scalar_sources_for_update(), anonymous_namespace{RelAlgExecutor.cpp}::translate_targets(), translateFunctionWithGeoArg(), translateGeoComparison(), translateGeoFunctionArg(), and translateTernaryGeoFunction().

170  {
171  const auto rex_input = dynamic_cast<const RexInput*>(rex);
172  if (rex_input) {
173  return translateInput(rex_input);
174  }
175  const auto rex_literal = dynamic_cast<const RexLiteral*>(rex);
176  if (rex_literal) {
177  return translateLiteral(rex_literal);
178  }
179  const auto rex_window_function = dynamic_cast<const RexWindowFunctionOperator*>(rex);
180  if (rex_window_function) {
181  return translateWindowFunction(rex_window_function);
182  }
183  const auto rex_function = dynamic_cast<const RexFunctionOperator*>(rex);
184  if (rex_function) {
185  return translateFunction(rex_function);
186  }
187  const auto rex_operator = dynamic_cast<const RexOperator*>(rex);
188  if (rex_operator) {
189  return translateOper(rex_operator);
190  }
191  const auto rex_case = dynamic_cast<const RexCase*>(rex);
192  if (rex_case) {
193  return translateCase(rex_case);
194  }
195  const auto rex_subquery = dynamic_cast<const RexSubQuery*>(rex);
196  if (rex_subquery) {
197  return translateScalarSubquery(rex_subquery);
198  }
199  CHECK(false);
200  return nullptr;
201 }
std::shared_ptr< Analyzer::Expr > translateWindowFunction(const RexWindowFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateOper(const RexOperator *) const
static std::shared_ptr< Analyzer::Expr > translateLiteral(const RexLiteral *)
std::shared_ptr< Analyzer::Expr > translateScalarSubquery(const RexSubQuery *) const
std::shared_ptr< Analyzer::Expr > translateFunction(const RexFunctionOperator *) const
#define CHECK(condition)
Definition: Logger.h:193
std::shared_ptr< Analyzer::Expr > translateCase(const RexCase *) const
std::shared_ptr< Analyzer::Expr > translateInput(const RexInput *) const
+ Here is the caller graph for this function:

◆ translateScalarSubquery()

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateScalarSubquery ( const RexSubQuery rex_subquery) const
private

Definition at line 321 of file RelAlgTranslator.cpp.

References CHECK, CHECK_EQ, anonymous_namespace{RelAlgTranslator.cpp}::datum_from_scalar_tv(), and run_benchmark_import::result.

322  {
323  if (just_explain_) {
324  throw std::runtime_error("EXPLAIN is not supported with sub-queries");
325  }
326  CHECK(rex_subquery);
327  auto result = rex_subquery->getExecutionResult();
328  auto row_set = result->getRows();
329  if (row_set->rowCount() > size_t(1)) {
330  throw std::runtime_error("Scalar sub-query returned multiple rows");
331  }
332  if (row_set->rowCount() < size_t(1)) {
333  CHECK_EQ(row_set->rowCount(), size_t(0));
334  throw std::runtime_error("Scalar sub-query returned no results");
335  }
336  auto first_row = row_set->getNextRow(false, false);
337  auto scalar_tv = boost::get<ScalarTargetValue>(&first_row[0]);
338  auto ti = rex_subquery->getType();
339  if (ti.is_string()) {
340  throw std::runtime_error("Scalar sub-queries which return strings not supported");
341  }
342  Datum d{0};
343  bool is_null_const{false};
344  std::tie(d, is_null_const) = datum_from_scalar_tv(scalar_tv, ti);
345  return makeExpr<Analyzer::Constant>(ti, is_null_const, d);
346 }
#define CHECK_EQ(x, y)
Definition: Logger.h:201
#define CHECK(condition)
Definition: Logger.h:193
std::pair< Datum, bool > datum_from_scalar_tv(const ScalarTargetValue *scalar_tv, const SQLTypeInfo &ti) noexcept
const bool just_explain_
+ Here is the call graph for this function:

◆ translateSign()

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

Definition at line 1287 of file RelAlgTranslator.cpp.

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

1288  {
1289  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
1290  expr_list;
1291  CHECK_EQ(size_t(1), rex_function->size());
1292  const auto operand = translateScalarRex(rex_function->getOperand(0));
1293  const auto& operand_ti = operand->get_type_info();
1294  CHECK(operand_ti.is_number());
1295  const auto zero = makeNumericConstant(operand_ti, 0);
1296  const auto lt_zero = makeExpr<Analyzer::BinOper>(kBOOLEAN, kLT, kONE, operand, zero);
1297  expr_list.emplace_back(lt_zero, makeNumericConstant(operand_ti, -1));
1298  const auto eq_zero = makeExpr<Analyzer::BinOper>(kBOOLEAN, kEQ, kONE, operand, zero);
1299  expr_list.emplace_back(eq_zero, makeNumericConstant(operand_ti, 0));
1300  const auto gt_zero = makeExpr<Analyzer::BinOper>(kBOOLEAN, kGT, kONE, operand, zero);
1301  expr_list.emplace_back(gt_zero, makeNumericConstant(operand_ti, 1));
1302  return makeExpr<Analyzer::CaseExpr>(
1303  operand_ti,
1304  false,
1305  expr_list,
1306  makeExpr<Analyzer::Constant>(operand_ti, true, Datum{0}));
1307 }
#define CHECK_EQ(x, y)
Definition: Logger.h:201
Definition: sqldefs.h:30
std::shared_ptr< Analyzer::Constant > makeNumericConstant(const SQLTypeInfo &ti, const long val)
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const RexScalar * getOperand(const size_t idx) const
Definition: sqldefs.h:34
Definition: sqldefs.h:69
#define CHECK(condition)
Definition: Logger.h:193
Definition: sqldefs.h:33
size_t size() const
+ Here is the call graph for this function:

◆ translateTernaryGeoFunction()

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

Definition at line 894 of file RelAlgTranslatorGeo.cpp.

References CHECK_EQ, fold_expr(), RexFunctionOperator::getName(), RexOperator::getOperand(), kBOOLEAN, kDOUBLE, kLE, kMULTIPLY, kONE, RexOperator::size(), translateBinaryGeoFunction(), and translateScalarRex().

895  {
896  CHECK_EQ(size_t(3), rex_function->size());
897 
898  auto distance_expr = translateScalarRex(rex_function->getOperand(2));
899  const auto& distance_ti = SQLTypeInfo(kDOUBLE, false);
900  if (distance_expr->get_type_info().get_type() != kDOUBLE) {
901  distance_expr->add_cast(distance_ti);
902  }
903 
904  // Translate the geo distance function call portion
905  const auto geo_distance_expr = translateBinaryGeoFunction(rex_function);
906 
907  if (rex_function->getName() == "ST_DWithin") {
908  auto func_oper = dynamic_cast<Analyzer::FunctionOper*>(geo_distance_expr.get());
909  if (func_oper && func_oper->getName() == "ST_Distance_Point_Point_Squared"sv) {
910  // Point_Point combination will yield geo_distance squared which is faster,
911  // need to compare it with distance squared
912  distance_expr = makeExpr<Analyzer::BinOper>(distance_ti,
913  distance_expr->get_contains_agg(),
914  kMULTIPLY,
915  kONE,
916  distance_expr,
917  distance_expr);
918  distance_expr = fold_expr(distance_expr.get());
919  }
920  }
921 
922  return makeExpr<Analyzer::BinOper>(
923  kBOOLEAN, kLE, kONE, geo_distance_expr, distance_expr);
924 }
#define CHECK_EQ(x, y)
Definition: Logger.h:201
Definition: sqldefs.h:35
std::shared_ptr< Analyzer::Expr > translateBinaryGeoFunction(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const RexScalar * getOperand(const size_t idx) const
SQLTypeInfoCore< ArrayContextTypeSizer, ExecutorTypePackaging, DateTimeFacilities > SQLTypeInfo
Definition: sqltypes.h:852
Definition: sqldefs.h:69
const std::string & getName() const
size_t size() const
std::shared_ptr< Analyzer::Expr > fold_expr(const Analyzer::Expr *expr)
+ Here is the call graph for this function:

◆ translateUnaryGeoFunction()

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

Definition at line 565 of file RelAlgTranslatorGeo.cpp.

References CHECK_EQ, func_resolve, SQLTypeInfoCore< TYPE_FACET_PACK >::get_comp_param(), SQLTypeInfoCore< TYPE_FACET_PACK >::get_compression(), SQLTypeInfoCore< TYPE_FACET_PACK >::get_input_srid(), SQLTypeInfoCore< TYPE_FACET_PACK >::get_output_srid(), SQLTypeInfoCore< TYPE_FACET_PACK >::get_subtype(), SQLTypeInfoCore< TYPE_FACET_PACK >::get_type(), RexFunctionOperator::getName(), RexOperator::getOperand(), RexOperator::getType(), Datum::intval, IS_GEO_POLY, kENCODING_GEOINT, kGEOGRAPHY, kINT, kLINESTRING, kMULTIPOLYGON, kPOINT, kPOLYGON, RexOperator::size(), anonymous_namespace{RelAlgTranslatorGeo.cpp}::suffix(), and translateGeoFunctionArg().

566  {
567  CHECK_EQ(size_t(1), rex_function->size());
568 
569  int32_t lindex = 0;
570  std::string specialized_geofunc{rex_function->getName()};
571 
572  // Geo function calls which do not need the coords col but do need cols associated with
573  // physical coords (e.g. ring_sizes / poly_rings)
574  if (rex_function->getName() == "ST_NRings"sv) {
575  SQLTypeInfo arg_ti;
576  auto geoargs = translateGeoFunctionArg(
577  rex_function->getOperand(0), arg_ti, lindex, false, false, true);
578  if (arg_ti.get_type() == kPOLYGON) {
579  CHECK_EQ(geoargs.size(), 2u);
580  geoargs.erase(geoargs.begin(), geoargs.begin() + 1); // remove the coords
581  return makeExpr<Analyzer::FunctionOper>(
582  rex_function->getType(), specialized_geofunc, geoargs);
583  } else if (arg_ti.get_type() == kMULTIPOLYGON) {
584  CHECK_EQ(geoargs.size(), 3u);
585  geoargs.erase(geoargs.begin(), geoargs.begin() + 1); // remove the coords
586  geoargs.erase(geoargs.begin() + 1, geoargs.end()); // remove the poly_rings
587  return makeExpr<Analyzer::FunctionOper>(
588  rex_function->getType(), specialized_geofunc, geoargs);
589  } else {
590  throw QueryNotSupported(rex_function->getName() +
591  " expects a POLYGON or MULTIPOLYGON");
592  }
593  } else if (rex_function->getName() == "ST_NPoints"sv) {
594  SQLTypeInfo arg_ti;
595  auto geoargs = translateGeoFunctionArg(
596  rex_function->getOperand(0), arg_ti, lindex, false, false, true);
597  geoargs.erase(geoargs.begin() + 1, geoargs.end()); // remove all but coords
598  // Add compression information
599  Datum input_compression;
600  input_compression.intval =
601  (arg_ti.get_compression() == kENCODING_GEOINT && arg_ti.get_comp_param() == 32)
602  ? 1
603  : 0;
604  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_compression));
605  return makeExpr<Analyzer::FunctionOper>(
606  rex_function->getType(), specialized_geofunc, geoargs);
607  } else if (func_resolve(rex_function->getName(), "ST_Perimeter"sv, "ST_Area"sv)) {
608  SQLTypeInfo arg_ti;
609  auto geoargs = translateGeoFunctionArg(
610  rex_function->getOperand(0), arg_ti, lindex, false, false, true);
611  if (arg_ti.get_type() != kPOLYGON && arg_ti.get_type() != kMULTIPOLYGON) {
612  throw QueryNotSupported(rex_function->getName() +
613  " expects a POLYGON or MULTIPOLYGON");
614  }
615  specialized_geofunc += suffix(arg_ti.get_type());
616  if (arg_ti.get_subtype() == kGEOGRAPHY && arg_ti.get_output_srid() == 4326) {
617  specialized_geofunc += "_Geodesic"s;
618  }
619  // Add compression information
620  Datum input_compression;
621  input_compression.intval =
622  (arg_ti.get_compression() == kENCODING_GEOINT && arg_ti.get_comp_param() == 32)
623  ? 1
624  : 0;
625  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_compression));
626  Datum input_srid;
627  input_srid.intval = arg_ti.get_input_srid();
628  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_srid));
629  Datum output_srid;
630  output_srid.intval = arg_ti.get_output_srid();
631  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, output_srid));
632  return makeExpr<Analyzer::FunctionOper>(
633  rex_function->getType(), specialized_geofunc, geoargs);
634  }
635 
636  // Accessors for poly bounds and render group for in-situ poly render queries
637  if (func_resolve(rex_function->getName(),
638  "MapD_GeoPolyBoundsPtr"sv /* deprecated */,
639  "OmniSci_Geo_PolyBoundsPtr"sv)) {
640  SQLTypeInfo arg_ti;
641  // get geo column plus bounds only (not expanded)
642  auto geoargs = translateGeoFunctionArg(
643  rex_function->getOperand(0), arg_ti, lindex, true, false, false);
644  // this function only works on polys
645  if (!IS_GEO_POLY(arg_ti.get_type())) {
646  throw QueryNotSupported(rex_function->getName() +
647  " expects a POLYGON or MULTIPOLYGON");
648  }
649  // only need the bounds argument (last), discard the rest
650  geoargs.erase(geoargs.begin(), geoargs.end() - 1);
651  // done
652  return makeExpr<Analyzer::FunctionOper>(
653  rex_function->getType(), specialized_geofunc, geoargs);
654  } else if (func_resolve(rex_function->getName(),
655  "MapD_GeoPolyRenderGroup"sv /* deprecated */,
656  "OmniSci_Geo_PolyRenderGroup"sv)) {
657  SQLTypeInfo arg_ti;
658  // get geo column plus render_group only (not expanded)
659  auto geoargs = translateGeoFunctionArg(
660  rex_function->getOperand(0), arg_ti, lindex, false, true, false);
661  // this function only works on polys
662  if (!IS_GEO_POLY(arg_ti.get_type())) {
663  throw QueryNotSupported(rex_function->getName() +
664  " expects a POLYGON or MULTIPOLYGON");
665  }
666  // only need the render_group argument (last), discard the rest
667  geoargs.erase(geoargs.begin(), geoargs.end() - 1);
668  // done
669  return makeExpr<Analyzer::FunctionOper>(
670  rex_function->getType(), specialized_geofunc, geoargs);
671  }
672 
673  // All functions below use geo col as reference and expand it as necessary
674  SQLTypeInfo arg_ti;
675  bool with_bounds = true;
676  auto geoargs = translateGeoFunctionArg(
677  rex_function->getOperand(0), arg_ti, lindex, with_bounds, false, false);
678 
679  if (rex_function->getName() == "ST_SRID"sv) {
680  Datum output_srid;
681  output_srid.intval = arg_ti.get_output_srid();
682  return makeExpr<Analyzer::Constant>(kINT, false, output_srid);
683  }
684 
685  if (func_resolve(
686  rex_function->getName(), "ST_XMin"sv, "ST_YMin"sv, "ST_XMax"sv, "ST_YMax"sv)) {
687  // If type has bounds - use them, otherwise look at coords
688  if (arg_ti.has_bounds()) {
689  if (lindex != 0) {
690  throw QueryNotSupported(rex_function->getName() +
691  " doesn't support indexed LINESTRINGs");
692  }
693  // Only need the bounds argument, discard the rest
694  geoargs.erase(geoargs.begin(), geoargs.end() - 1);
695 
696  // Supply srids too - transformed geo would have a transformed bounding box
697  Datum input_srid;
698  input_srid.intval = arg_ti.get_input_srid();
699  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_srid));
700  Datum output_srid;
701  output_srid.intval = arg_ti.get_output_srid();
702  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, output_srid));
703 
704  specialized_geofunc += "_Bounds"s;
705  return makeExpr<Analyzer::FunctionOper>(
706  rex_function->getType(), specialized_geofunc, geoargs);
707  }
708  }
709 
710  // All geo function calls translated below only need the coords, extras e.g. ring_sizes
711  // are dropped. Specialize for other/new functions if needed.
712  geoargs.erase(geoargs.begin() + 1, geoargs.end());
713 
714  if (func_resolve(rex_function->getName(), "ST_X"sv, "ST_Y"sv)) {
715  if (arg_ti.get_type() == kLINESTRING) {
716  if (lindex == 0) {
717  throw QueryNotSupported(
718  rex_function->getName() +
719  " expects a POINT, use LINESTRING accessor, e.g. ST_POINTN");
720  }
721  Datum index;
722  index.intval = lindex;
723  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, index));
724  } else if (arg_ti.get_type() != kPOINT) {
725  throw QueryNotSupported(rex_function->getName() + " expects a POINT");
726  }
727  specialized_geofunc += suffix(arg_ti.get_type());
728  } else if (rex_function->getName() == "ST_Length"sv) {
729  if (arg_ti.get_type() != kLINESTRING || lindex != 0) {
730  throw QueryNotSupported(rex_function->getName() + " expects unindexed LINESTRING");
731  }
732  specialized_geofunc += suffix(arg_ti.get_type());
733  if (arg_ti.get_subtype() == kGEOGRAPHY && arg_ti.get_output_srid() == 4326) {
734  specialized_geofunc += "_Geodesic"s;
735  }
736  }
737 
738  // Add input compression mode and SRID args to enable on-the-fly
739  // decompression/transforms
740  Datum input_compression;
741  input_compression.intval =
742  (arg_ti.get_compression() == kENCODING_GEOINT && arg_ti.get_comp_param() == 32) ? 1
743  : 0;
744  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_compression));
745  Datum input_srid;
746  input_srid.intval = arg_ti.get_input_srid();
747  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_srid));
748 
749  // Add output SRID arg to enable on-the-fly transforms
750  Datum output_srid;
751  output_srid.intval = arg_ti.get_output_srid();
752  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, output_srid));
753 
754  return makeExpr<Analyzer::FunctionOper>(
755  rex_function->getType(), specialized_geofunc, geoargs);
756 }
#define CHECK_EQ(x, y)
Definition: Logger.h:201
auto func_resolve
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:326
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:334
int32_t intval
Definition: sqltypes.h:128
HOST DEVICE SQLTypes get_subtype() const
Definition: sqltypes.h:327
HOST DEVICE int get_input_srid() const
Definition: sqltypes.h:330
std::vector< std::shared_ptr< Analyzer::Expr > > translateGeoFunctionArg(const RexScalar *rex_scalar, SQLTypeInfo &arg_ti, int32_t &lindex, const bool with_bounds, const bool with_render_group, const bool expand_geo_col, const bool is_projection=false) const
const RexScalar * getOperand(const size_t idx) const
HOST DEVICE int get_output_srid() const
Definition: sqltypes.h:332
HOST DEVICE int get_comp_param() const
Definition: sqltypes.h:335
const SQLTypeInfo & getType() const
Definition: sqltypes.h:48
const std::string & getName() const
size_t size() const
#define IS_GEO_POLY(T)
Definition: sqltypes.h:171
+ Here is the call graph for this function:

◆ translateUnlikely()

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

Definition at line 941 of file RelAlgTranslator.cpp.

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

942  {
943  CHECK(rex_function->size() == 1);
944  const auto arg = translateScalarRex(rex_function->getOperand(0));
945  return makeExpr<Analyzer::LikelihoodExpr>(arg, 0.0625);
946 }
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const RexScalar * getOperand(const size_t idx) const
#define CHECK(condition)
Definition: Logger.h:193
size_t size() const
+ Here is the call graph for this function:

◆ translateUoper()

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

Definition at line 393 of file RelAlgTranslator.cpp.

References CHECK, CHECK_EQ, CHECK_NE, RexOperator::getOperand(), RexOperator::getOperator(), RexOperator::getType(), anonymous_namespace{TypedDataAccessors.h}::is_null(), kBOOLEAN, kCAST, kDATE, kISNOTNULL, kISNULL, kMINUS, kNOT, kNULLT, kUMINUS, kUNNEST, and RexOperator::size().

394  {
395  CHECK_EQ(size_t(1), rex_operator->size());
396  const auto operand_expr = translateScalarRex(rex_operator->getOperand(0));
397  const auto sql_op = rex_operator->getOperator();
398  switch (sql_op) {
399  case kCAST: {
400  const auto& target_ti = rex_operator->getType();
401  CHECK_NE(kNULLT, target_ti.get_type());
402  const auto& operand_ti = operand_expr->get_type_info();
403  if (operand_ti.is_string() && target_ti.is_string()) {
404  return operand_expr;
405  }
406  if (target_ti.is_time() ||
407  operand_ti
408  .is_string()) { // TODO(alex): check and unify with the rest of the cases
409  // Do not propogate encoding on small dates
410  return target_ti.is_date_in_days()
411  ? operand_expr->add_cast(SQLTypeInfo(kDATE, false))
412  : operand_expr->add_cast(target_ti);
413  }
414  if (!operand_ti.is_string() && target_ti.is_string()) {
415  return operand_expr->add_cast(target_ti);
416  }
417 
418  return std::make_shared<Analyzer::UOper>(target_ti, false, sql_op, operand_expr);
419  }
420  case kNOT:
421  case kISNULL: {
422  return std::make_shared<Analyzer::UOper>(kBOOLEAN, sql_op, operand_expr);
423  }
424  case kISNOTNULL: {
425  auto is_null = std::make_shared<Analyzer::UOper>(kBOOLEAN, kISNULL, operand_expr);
426  return std::make_shared<Analyzer::UOper>(kBOOLEAN, kNOT, is_null);
427  }
428  case kMINUS: {
429  const auto& ti = operand_expr->get_type_info();
430  return std::make_shared<Analyzer::UOper>(ti, false, kUMINUS, operand_expr);
431  }
432  case kUNNEST: {
433  const auto& ti = operand_expr->get_type_info();
434  CHECK(ti.is_array());
435  return makeExpr<Analyzer::UOper>(ti.get_elem_type(), false, kUNNEST, operand_expr);
436  }
437  default:
438  CHECK(false);
439  }
440  return nullptr;
441 }
SQLOps getOperator() const
#define CHECK_EQ(x, y)
Definition: Logger.h:201
Definition: sqldefs.h:49
#define CHECK_NE(x, y)
Definition: Logger.h:202
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const RexScalar * getOperand(const size_t idx) const
SQLTypeInfoCore< ArrayContextTypeSizer, ExecutorTypePackaging, DateTimeFacilities > SQLTypeInfo
Definition: sqltypes.h:852
Definition: sqltypes.h:56
Definition: sqldefs.h:40
bool is_null(const T &v, const SQLTypeInfo &t)
#define CHECK(condition)
Definition: Logger.h:193
const SQLTypeInfo & getType() const
Definition: sqldefs.h:39
size_t size() const
+ Here is the call graph for this function:

◆ translateWindowFunction()

std::shared_ptr< Analyzer::Expr > RelAlgTranslator::translateWindowFunction ( const RexWindowFunctionOperator rex_window_function) const
private

Definition at line 1624 of file RelAlgTranslator.cpp.

References run_benchmark_import::args, CHECK_GE, RexWindowFunctionOperator::getCollation(), RexWindowFunctionOperator::getKind(), RexWindowFunctionOperator::getLowerBound(), RexOperator::getOperand(), RexWindowFunctionOperator::getOrderKeys(), RexWindowFunctionOperator::getPartitionKeys(), RexOperator::getType(), RexWindowFunctionOperator::isRows(), ROW_NUMBER, RexOperator::size(), anonymous_namespace{RelAlgTranslator.cpp}::supported_lower_bound(), anonymous_namespace{RelAlgTranslator.cpp}::supported_upper_bound(), anonymous_namespace{RelAlgTranslator.cpp}::translate_collation(), and window_function_is_value().

1625  {
1626  if (!supported_lower_bound(rex_window_function->getLowerBound()) ||
1627  !supported_upper_bound(rex_window_function) ||
1628  ((rex_window_function->getKind() == SqlWindowFunctionKind::ROW_NUMBER) !=
1629  rex_window_function->isRows())) {
1630  throw std::runtime_error("Frame specification not supported");
1631  }
1632  std::vector<std::shared_ptr<Analyzer::Expr>> args;
1633  for (size_t i = 0; i < rex_window_function->size(); ++i) {
1634  args.push_back(translateScalarRex(rex_window_function->getOperand(i)));
1635  }
1636  std::vector<std::shared_ptr<Analyzer::Expr>> partition_keys;
1637  for (const auto& partition_key : rex_window_function->getPartitionKeys()) {
1638  partition_keys.push_back(translateScalarRex(partition_key.get()));
1639  }
1640  std::vector<std::shared_ptr<Analyzer::Expr>> order_keys;
1641  for (const auto& order_key : rex_window_function->getOrderKeys()) {
1642  order_keys.push_back(translateScalarRex(order_key.get()));
1643  }
1644  auto ti = rex_window_function->getType();
1645  if (window_function_is_value(rex_window_function->getKind())) {
1646  CHECK_GE(args.size(), 1u);
1647  ti = args.front()->get_type_info();
1648  }
1649  return makeExpr<Analyzer::WindowFunction>(
1650  ti,
1651  rex_window_function->getKind(),
1652  args,
1653  partition_keys,
1654  order_keys,
1655  translate_collation(rex_window_function->getCollation()));
1656 }
const std::vector< SortField > & getCollation() const
const ConstRexScalarPtrVector & getPartitionKeys() const
SqlWindowFunctionKind getKind() const
bool supported_lower_bound(const RexWindowFunctionOperator::RexWindowBound &window_bound)
const RexWindowBound & getLowerBound() const
const ConstRexScalarPtrVector & getOrderKeys() const
#define CHECK_GE(x, y)
Definition: Logger.h:206
bool window_function_is_value(const SqlWindowFunctionKind kind)
Definition: WindowContext.h:27
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
std::vector< Analyzer::OrderEntry > translate_collation(const std::vector< SortField > &sort_fields)
const RexScalar * getOperand(const size_t idx) const
bool supported_upper_bound(const RexWindowFunctionOperator *rex_window_function)
const SQLTypeInfo & getType() const
size_t size() const
+ Here is the call graph for this function:

Member Data Documentation

◆ cat_

const Catalog_Namespace::Catalog& RelAlgTranslator::cat_
private

Definition at line 173 of file RelAlgTranslator.h.

Referenced by translateGeoColumn().

◆ executor_

const Executor* RelAlgTranslator::executor_
private

Definition at line 174 of file RelAlgTranslator.h.

◆ feature_stash_

QueryFeatureDescriptor& RelAlgTranslator::feature_stash_
private

Definition at line 179 of file RelAlgTranslator.h.

◆ input_to_nest_level_

const std::unordered_map<const RelAlgNode*, int> RelAlgTranslator::input_to_nest_level_
private

Definition at line 175 of file RelAlgTranslator.h.

Referenced by translateGeoColumn().

◆ join_types_

const std::vector<JoinType> RelAlgTranslator::join_types_
private

Definition at line 176 of file RelAlgTranslator.h.

◆ just_explain_

const bool RelAlgTranslator::just_explain_
private

Definition at line 178 of file RelAlgTranslator.h.

◆ now_

time_t RelAlgTranslator::now_
private

Definition at line 177 of file RelAlgTranslator.h.


The documentation for this class was generated from the following files: