OmniSciDB  1dac507f6e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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::Expr
translateAggregateRex (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::Expr
translateLiteral (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 ( 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

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

Definition at line 727 of file RelAlgTranslator.cpp.

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

Referenced by translateInOper().

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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 1263 of file RelAlgTranslator.cpp.

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

Referenced by translateFunction().

1264  {
1265  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
1266  expr_list;
1267  CHECK_EQ(size_t(1), rex_function->size());
1268  const auto operand = translateScalarRex(rex_function->getOperand(0));
1269  const auto& operand_ti = operand->get_type_info();
1270  CHECK(operand_ti.is_number());
1271  const auto zero = makeNumericConstant(operand_ti, 0);
1272  const auto lt_zero = makeExpr<Analyzer::BinOper>(kBOOLEAN, kLT, kONE, operand, zero);
1273  const auto uminus_operand =
1274  makeExpr<Analyzer::UOper>(operand_ti.get_type(), kUMINUS, operand);
1275  expr_list.emplace_back(lt_zero, uminus_operand);
1276  return makeExpr<Analyzer::CaseExpr>(operand_ti, false, expr_list, operand);
1277 }
#define CHECK_EQ(x, y)
Definition: Logger.h:198
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
size_t size() const
const RexScalar * getOperand(const size_t idx) const
std::shared_ptr< Analyzer::Constant > makeNumericConstant(const SQLTypeInfo &ti, const long val)
CHECK(cgen_state)
Definition: sqldefs.h:69
Definition: sqldefs.h:33

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 214 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(), and anonymous_namespace{RelAlgExecutor.cpp}::translate_targets_for_update().

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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 1305 of file RelAlgTranslator.cpp.

References CHECK(), feature_stash_, get_logical_type_info(), SQLTypeInfoCore< TYPE_FACET_PACK >::get_subtype(), RexOperator::getType(), kARRAY, kENCODING_FIXED, kNULLT, TRANSIENT_DICT_ID, and translateFunctionArgs().

Referenced by translateFunction().

1306  {
1307  if (rex_function->getType().get_subtype() == kNULLT) {
1308  auto sql_type = rex_function->getType();
1309  CHECK(sql_type.get_type() == kARRAY);
1310 
1311  // FIX-ME: Deal with NULL arrays
1312  auto translated_function_args(translateFunctionArgs(rex_function));
1313  if (translated_function_args.size() > 0) {
1314  auto const& first_element_logical_type(
1315  get_logical_type_info(translated_function_args[0]->get_type_info()));
1316 
1317  on_member_of_typeset<kCHAR, kVARCHAR, kTEXT>(
1318  first_element_logical_type,
1319  [&] {
1320  bool same_type_status = true;
1321  for (auto const& expr_ptr : translated_function_args) {
1322  same_type_status =
1323  same_type_status && (expr_ptr->get_type_info().is_string());
1324  }
1325 
1326  if (same_type_status == false) {
1327  throw std::runtime_error(
1328  "All elements of the array are not of the same logical subtype; "
1329  "consider casting to force this condition.");
1330  }
1331 
1332  sql_type.set_subtype(first_element_logical_type.get_type());
1333  sql_type.set_compression(kENCODING_FIXED);
1334  sql_type.set_comp_param(TRANSIENT_DICT_ID);
1335  },
1336  [&] {
1337  // Non string types
1338  bool same_type_status = true;
1339  for (auto const& expr_ptr : translated_function_args) {
1340  same_type_status =
1341  same_type_status && (first_element_logical_type ==
1342  get_logical_type_info(expr_ptr->get_type_info()));
1343  }
1344 
1345  if (same_type_status == false) {
1346  throw std::runtime_error(
1347  "All elements of the array are not of the same logical subtype; "
1348  "consider casting to force this condition.");
1349  }
1350  sql_type.set_subtype(first_element_logical_type.get_type());
1351  sql_type.set_scale(first_element_logical_type.get_scale());
1352  sql_type.set_precision(first_element_logical_type.get_precision());
1353  });
1354 
1355  feature_stash_.setCPUOnlyExecutionRequired();
1356  return makeExpr<Analyzer::ArrayExpr>(
1357  sql_type, translated_function_args, feature_stash_.getAndBumpArrayExprCount());
1358  } else {
1359  throw std::runtime_error("NULL ARRAY[] expressions not supported yet. FIX-ME.");
1360  }
1361  } else {
1362  feature_stash_.setCPUOnlyExecutionRequired();
1363  return makeExpr<Analyzer::ArrayExpr>(rex_function->getType(),
1364  translateFunctionArgs(rex_function),
1365  feature_stash_.getAndBumpArrayExprCount());
1366  }
1367 }
const SQLTypeInfo & getType() const
SQLTypeInfo get_logical_type_info(const SQLTypeInfo &type_info)
Definition: sqltypes.h:869
QueryFeatureDescriptor & feature_stash_
CHECK(cgen_state)
Analyzer::ExpressionPtrVector translateFunctionArgs(const RexFunctionOperator *) const
#define TRANSIENT_DICT_ID
Definition: sqltypes.h:189
HOST DEVICE SQLTypes get_subtype() const
Definition: sqltypes.h:327

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 757 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 translateFunction(), and translateTernaryGeoFunction().

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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 1207 of file RelAlgTranslator.cpp.

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

Referenced by translateFunction().

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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 881 of file RelAlgTranslator.cpp.

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

Referenced by translateScalarRex().

882  {
883  std::shared_ptr<Analyzer::Expr> else_expr;
884  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
885  expr_list;
886  for (size_t i = 0; i < rex_case->branchCount(); ++i) {
887  const auto when_expr = translateScalarRex(rex_case->getWhen(i));
888  const auto then_expr = translateScalarRex(rex_case->getThen(i));
889  expr_list.emplace_back(when_expr, then_expr);
890  }
891  if (rex_case->getElse()) {
892  else_expr = translateScalarRex(rex_case->getElse());
893  }
894  return Parser::CaseExpr::normalize(expr_list, else_expr);
895 }
const RexScalar * getThen(const size_t idx) const
const RexScalar * getElse() const
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const RexScalar * getWhen(const size_t idx) 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
size_t branchCount() const

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 1001 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(), to_dateadd_field(), and translateScalarRex().

Referenced by translateFunction().

1002  {
1003  CHECK_EQ(size_t(3), rex_function->size());
1004  const auto timeunit = translateScalarRex(rex_function->getOperand(0));
1005  const auto timeunit_lit = std::dynamic_pointer_cast<Analyzer::Constant>(timeunit);
1006  if (!timeunit_lit) {
1007  throw std::runtime_error("The time unit parameter must be a literal.");
1008  }
1009 
1010  const auto number_units = translateScalarRex(rex_function->getOperand(1));
1011  auto cast_number_units = number_units->add_cast(SQLTypeInfo(kBIGINT, false));
1012  const auto datetime = translateScalarRex(rex_function->getOperand(2));
1013  const auto& datetime_ti = datetime->get_type_info();
1014  if (datetime_ti.get_type() == kTIME) {
1015  throw std::runtime_error("DateAdd operation not supported for TIME.");
1016  }
1017  const auto& field = to_dateadd_field(*timeunit_lit->get_constval().stringval);
1018  if (!datetime_ti.is_high_precision_timestamp() &&
1020  // Scale the number to get value in seconds
1021  const auto bigint_ti = SQLTypeInfo(kBIGINT, false);
1022  cast_number_units = makeExpr<Analyzer::BinOper>(
1023  bigint_ti.get_type(),
1024  kDIVIDE,
1025  kONE,
1026  cast_number_units,
1027  makeNumericConstant(bigint_ti,
1029  cast_number_units = fold_expr(cast_number_units.get());
1030  }
1031  if (datetime_ti.is_high_precision_timestamp() &&
1034  field, datetime_ti.get_dimension());
1035  if (oper_scale.first) {
1036  // scale number to desired precision
1037  const auto bigint_ti = SQLTypeInfo(kBIGINT, false);
1038  cast_number_units =
1039  makeExpr<Analyzer::BinOper>(bigint_ti.get_type(),
1040  oper_scale.first,
1041  kONE,
1042  cast_number_units,
1043  makeNumericConstant(bigint_ti, oper_scale.second));
1044  cast_number_units = fold_expr(cast_number_units.get());
1045  }
1046  }
1047  return makeExpr<Analyzer::DateaddExpr>(
1048  SQLTypeInfo(kTIMESTAMP, datetime_ti.get_dimension(), 0, false),
1049  to_dateadd_field(*timeunit_lit->get_constval().stringval),
1050  cast_number_units,
1051  datetime);
1052 }
#define CHECK_EQ(x, y)
Definition: Logger.h:198
Definition: sqltypes.h:52
constexpr int64_t get_dateadd_timestamp_precision_scale(const DateaddField field)
Definition: DateTimeUtils.h:64
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
size_t size() const
const RexScalar * getOperand(const size_t idx) const
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)
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:96
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:

+ Here is the caller graph for this function:

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

Definition at line 1144 of file RelAlgTranslator.cpp.

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

Referenced by translateFunction().

1145  {
1146  CHECK_EQ(size_t(3), rex_function->size());
1147  const auto timeunit = translateScalarRex(rex_function->getOperand(0));
1148  const auto timeunit_lit = std::dynamic_pointer_cast<Analyzer::Constant>(timeunit);
1149  if (!timeunit_lit) {
1150  throw std::runtime_error("The time unit parameter must be a literal.");
1151  }
1152  const auto start = translateScalarRex(rex_function->getOperand(1));
1153  const auto end = translateScalarRex(rex_function->getOperand(2));
1154  const auto field = to_datediff_field(*timeunit_lit->get_constval().stringval);
1155  return makeExpr<Analyzer::DatediffExpr>(SQLTypeInfo(kBIGINT, false), field, start, end);
1156 }
#define CHECK_EQ(x, y)
Definition: Logger.h:198
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
size_t size() const
const RexScalar * getOperand(const size_t idx) const
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
DatetruncField to_datediff_field(const std::string &field)
SQLTypeInfoCore< ArrayContextTypeSizer, ExecutorTypePackaging, DateTimeFacilities > SQLTypeInfo
Definition: sqltypes.h:852

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 1158 of file RelAlgTranslator.cpp.

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

Referenced by translateFunction().

1159  {
1160  CHECK_EQ(size_t(2), rex_function->size());
1161  const auto timeunit = translateScalarRex(rex_function->getOperand(0));
1162  const auto timeunit_lit = std::dynamic_pointer_cast<Analyzer::Constant>(timeunit);
1163  if (!timeunit_lit) {
1164  throw std::runtime_error("The time unit parameter must be a literal.");
1165  }
1166  const auto from_expr = translateScalarRex(rex_function->getOperand(1));
1167  return ExtractExpr::generate(
1168  from_expr, to_datepart_field(*timeunit_lit->get_constval().stringval));
1169 }
#define CHECK_EQ(x, y)
Definition: Logger.h:198
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
size_t size() const
const RexScalar * getOperand(const size_t idx) const
ExtractField to_datepart_field(const std::string &field)
const std::shared_ptr< Analyzer::Expr > generate() const

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 1063 of file RelAlgTranslator.cpp.

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

Referenced by translateOper().

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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 1247 of file RelAlgTranslator.cpp.

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

Referenced by translateFunction().

1248  {
1249  CHECK_EQ(size_t(1), rex_function->size());
1250  const auto arg = translateScalarRex(rex_function->getOperand(0));
1251  const auto arg_lit = std::dynamic_pointer_cast<Analyzer::Constant>(arg);
1252  const std::string datetime_err{R"(Only DATETIME('NOW') supported for now.)"};
1253  if (!arg_lit) {
1254  throw std::runtime_error(datetime_err);
1255  }
1256  CHECK(arg_lit->get_type_info().is_string());
1257  if (*arg_lit->get_constval().stringval != "NOW"sv) {
1258  throw std::runtime_error(datetime_err);
1259  }
1260  return translateNow();
1261 }
#define CHECK_EQ(x, y)
Definition: Logger.h:198
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
size_t size() const
const RexScalar * getOperand(const size_t idx) const
std::shared_ptr< Analyzer::Expr > translateNow() const
CHECK(cgen_state)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 940 of file RelAlgTranslator.cpp.

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

Referenced by translateFunction().

941  {
942  CHECK_EQ(size_t(2), rex_function->size());
943  const auto timeunit = translateScalarRex(rex_function->getOperand(0));
944  const auto timeunit_lit = std::dynamic_pointer_cast<Analyzer::Constant>(timeunit);
945  if (!timeunit_lit) {
946  throw std::runtime_error("The time unit parameter must be a literal.");
947  }
948  const auto from_expr = translateScalarRex(rex_function->getOperand(1));
949  const bool is_date_trunc = rex_function->getName() == "PG_DATE_TRUNC"sv;
950  if (is_date_trunc) {
951  return DateTruncExpr::generate(from_expr, *timeunit_lit->get_constval().stringval);
952  } else {
953  return ExtractExpr::generate(from_expr, *timeunit_lit->get_constval().stringval);
954  }
955 }
#define CHECK_EQ(x, y)
Definition: Logger.h:198
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
size_t size() const
const RexScalar * getOperand(const size_t idx) const
const std::shared_ptr< Analyzer::Expr > generate() const
const std::string & getName() const
const std::shared_ptr< Analyzer::Expr > generate() const

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 1369 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(), Datum::smallintval, translateAbs(), translateArrayFunction(), translateBinaryGeoFunction(), translateCardinality(), translateDateadd(), translateDatediff(), translateDatepart(), translateDatetime(), translateExtract(), translateFunctionArgs(), translateFunctionWithGeoArg(), translateGeoConstructor(), translateHPTLiteral(), translateItem(), translateKeyForString(), translateLength(), translateLike(), translateLikely(), translateLower(), translateNow(), translateOffsetInFragment(), translateRegexp(), translateScalarRex(), translateSign(), translateTernaryGeoFunction(), translateUnaryGeoFunction(), and translateUnlikely().

Referenced by translateScalarRex().

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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 1646 of file RelAlgTranslator.cpp.

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

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

1647  {
1648  std::vector<std::shared_ptr<Analyzer::Expr>> args;
1649  for (size_t i = 0; i < rex_function->size(); ++i) {
1650  args.push_back(translateScalarRex(rex_function->getOperand(i)));
1651  }
1652  return args;
1653 }
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
size_t size() const
const RexScalar * getOperand(const size_t idx) const

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 957 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().

Referenced by translateFunction().

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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 21 of file RelAlgTranslatorGeo.cpp.

References run_benchmark_import::args, cat_, CHECK(), CHECK_GE, CHECK_LT, ColumnDescriptor::columnType, 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:203
unsigned getIndex() const
CHECK(cgen_state)
const ColumnDescriptor * getMetadataForColumnBySpi(const int tableId, const size_t spi) const
Definition: Catalog.cpp:1437
const std::unordered_map< const RelAlgNode *, int > input_to_nest_level_
#define CHECK_LT(x, y)
Definition: Logger.h:200
const RelAlgNode * getSourceNode() const
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:

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

Definition at line 924 of file RelAlgTranslatorGeo.cpp.

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

Referenced by translateOper().

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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 547 of file RelAlgTranslatorGeo.cpp.

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

Referenced by translateFunction().

548  {
549  if (func_resolve(rex_function->getName(),
550  "ST_GeomFromText"sv,
551  "ST_GeogFromText"sv,
552  "ST_Point"sv,
553  "ST_SetSRID"sv)) {
554  SQLTypeInfo arg_ti;
555  int32_t lindex = 0;
556  auto geoargs =
557  translateGeoFunctionArg(rex_function, arg_ti, lindex, false, false, true, true);
558  return makeExpr<Analyzer::GeoExpr>(arg_ti, geoargs);
559  }
560  throw QueryNotSupported(rex_function->getName() +
561  " geo constructor is not supported in this context");
562 }
auto func_resolve
const std::string & getName() 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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::vector< std::shared_ptr< Analyzer::Expr > > RelAlgTranslator::translateGeoFunctionArg ( const RexScalar rex_scalar,
SQLTypeInfo arg_ti,
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 = makeExpr<Analyzer::ArrayExpr>(da_ti, cast_coords, 0, is_local_alloca);
511  // cast it to tinyint[16]
512  SQLTypeInfo tia_ti = SQLTypeInfo(kARRAY, true);
513  tia_ti.set_subtype(kTINYINT);
514  tia_ti.set_size(16);
515  return {makeExpr<Analyzer::UOper>(tia_ti, false, kCAST, ae)};
516  } else {
517  throw QueryNotSupported("Unsupported argument: " + rex_function->getName());
518  }
519  }
520  const auto rex_literal = dynamic_cast<const RexLiteral*>(rex_scalar);
521  if (rex_literal) {
522  return translateGeoLiteral(rex_literal, arg_ti, with_bounds);
523  }
524  throw QueryNotSupported("Geo function argument not supported");
525 }
#define CHECK_EQ(x, y)
Definition: Logger.h:198
auto func_resolve
HOST DEVICE int get_output_srid() const
Definition: sqltypes.h:332
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
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
std::shared_ptr< Analyzer::Expr > translateInput(const RexInput *) const
std::vector< std::shared_ptr< Analyzer::Expr > > translateGeoLiteral(const RexLiteral *, SQLTypeInfo &, bool) const
HOST DEVICE void set_subtype(SQLTypes st)
Definition: sqltypes.h:417
static std::shared_ptr< Analyzer::Expr > translateLiteral(const RexLiteral *)
CHECK(cgen_state)
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:326
SQLTypeInfoCore< ArrayContextTypeSizer, ExecutorTypePackaging, DateTimeFacilities > SQLTypeInfo
Definition: sqltypes.h:852
Definition: sqltypes.h:55
Definition: sqltypes.h:48
HOST DEVICE int get_input_srid() const
Definition: sqltypes.h:330
std::vector< std::shared_ptr< Analyzer::Expr > > translateGeoColumn(const RexInput *, SQLTypeInfo &, const bool with_bounds, const bool with_render_group, const bool expand_geo_col) const
#define IS_GEO(T)
Definition: sqltypes.h:167
std::shared_ptr< Analyzer::Expr > fold_expr(const Analyzer::Expr *expr)
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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 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
HOST DEVICE int get_output_srid() const
Definition: sqltypes.h:332
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:334
bool has_bounds() const
Definition: sqltypes.h:395
void set_size(int s)
Definition: sqltypes.h:424
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
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 *)
CHECK(cgen_state)
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:326
SQLTypeInfoCore< ArrayContextTypeSizer, ExecutorTypePackaging, DateTimeFacilities > SQLTypeInfo
Definition: sqltypes.h:852
Definition: sqltypes.h:55
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
Definition: sqltypes.h:48
double doubleval
Definition: sqltypes.h:131

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 1076 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().

Referenced by translateOverlapsOper().

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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 1698 of file RelAlgTranslator.cpp.

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

Referenced by translateFunction().

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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 508 of file RelAlgTranslator.cpp.

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

Referenced by translateOper().

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

+ Here is the caller graph for this function:

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

Definition at line 340 of file RelAlgTranslator.cpp.

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

Referenced by translateGeoFunctionArg(), and translateScalarRex().

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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 1234 of file RelAlgTranslator.cpp.

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

Referenced by translateFunction().

1235  {
1236  CHECK_EQ(size_t(2), rex_function->size());
1237  const auto base = translateScalarRex(rex_function->getOperand(0));
1238  const auto index = translateScalarRex(rex_function->getOperand(1));
1239  return makeExpr<Analyzer::BinOper>(
1240  base->get_type_info().get_elem_type(), false, kARRAY_AT, kONE, base, index);
1241 }
#define CHECK_EQ(x, y)
Definition: Logger.h:198
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
size_t size() const
const RexScalar * getOperand(const size_t idx) const
Definition: sqldefs.h:69

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 1179 of file RelAlgTranslator.cpp.

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

Referenced by translateFunction().

1180  {
1181  const auto& args = translateFunctionArgs(rex_function);
1182  CHECK_EQ(size_t(1), args.size());
1183  const auto expr = dynamic_cast<Analyzer::Expr*>(args[0].get());
1184  if (nullptr == expr || !expr->get_type_info().is_string() ||
1185  expr->get_type_info().is_varlen()) {
1186  throw std::runtime_error(rex_function->getName() +
1187  " expects a dictionary encoded text column.");
1188  }
1189  return makeExpr<Analyzer::KeyForStringExpr>(args[0]);
1190 }
#define CHECK_EQ(x, y)
Definition: Logger.h:198
Analyzer::ExpressionPtrVector translateFunctionArgs(const RexFunctionOperator *) const
const std::string & getName() const

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 1171 of file RelAlgTranslator.cpp.

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

Referenced by translateFunction().

1172  {
1173  CHECK_EQ(size_t(1), rex_function->size());
1174  const auto str_arg = translateScalarRex(rex_function->getOperand(0));
1175  return makeExpr<Analyzer::CharLengthExpr>(str_arg->decompress(),
1176  rex_function->getName() == "CHAR_LENGTH"sv);
1177 }
#define CHECK_EQ(x, y)
Definition: Logger.h:198
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
size_t size() const
const RexScalar * getOperand(const size_t idx) const
const std::string & getName() const

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 897 of file RelAlgTranslator.cpp.

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

Referenced by translateFunction().

898  {
899  CHECK(rex_function->size() == 2 || rex_function->size() == 3);
900  const auto arg = translateScalarRex(rex_function->getOperand(0));
901  const auto like = translateScalarRex(rex_function->getOperand(1));
902  if (!std::dynamic_pointer_cast<const Analyzer::Constant>(like)) {
903  throw std::runtime_error("The matching pattern must be a literal.");
904  }
905  const auto escape = (rex_function->size() == 3)
906  ? translateScalarRex(rex_function->getOperand(2))
907  : nullptr;
908  const bool is_ilike = rex_function->getName() == "PG_ILIKE"sv;
909  return Parser::LikeExpr::get(arg, like, escape, is_ilike, false);
910 }
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
size_t size() const
const RexScalar * getOperand(const size_t idx) const
CHECK(cgen_state)
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
const std::string & getName() const

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 926 of file RelAlgTranslator.cpp.

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

Referenced by translateFunction().

927  {
928  CHECK(rex_function->size() == 1);
929  const auto arg = translateScalarRex(rex_function->getOperand(0));
930  return makeExpr<Analyzer::LikelihoodExpr>(arg, 0.9375);
931 }
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
size_t size() const
const RexScalar * getOperand(const size_t idx) const
CHECK(cgen_state)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 247 of file RelAlgTranslator.cpp.

References Parser::StringLiteral::analyzeValue(), Parser::IntLiteral::analyzeValue(), Parser::FixedPtLiteral::analyzeValue(), 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(), translateGeoLiteral(), and translateScalarRex().

248  {
249  const auto lit_ti = build_type_info(
250  rex_literal->getType(), rex_literal->getScale(), rex_literal->getPrecision());
251  const auto target_ti = build_type_info(rex_literal->getTargetType(),
252  rex_literal->getTypeScale(),
253  rex_literal->getTypePrecision());
254  switch (rex_literal->getType()) {
255  case kDECIMAL: {
256  const auto val = rex_literal->getVal<int64_t>();
257  const int precision = rex_literal->getPrecision();
258  const int scale = rex_literal->getScale();
259  if (target_ti.is_fp() && !scale) {
260  return make_fp_constant(val, target_ti);
261  }
262  auto lit_expr = scale ? Parser::FixedPtLiteral::analyzeValue(val, scale, precision)
263  : Parser::IntLiteral::analyzeValue(val);
264  return lit_ti != target_ti ? lit_expr->add_cast(target_ti) : lit_expr;
265  }
266  case kTEXT: {
267  return Parser::StringLiteral::analyzeValue(rex_literal->getVal<std::string>());
268  }
269  case kBOOLEAN: {
270  Datum d;
271  d.boolval = rex_literal->getVal<bool>();
272  return makeExpr<Analyzer::Constant>(kBOOLEAN, false, d);
273  }
274  case kDOUBLE: {
275  Datum d;
276  d.doubleval = rex_literal->getVal<double>();
277  auto lit_expr = makeExpr<Analyzer::Constant>(kDOUBLE, false, d);
278  return lit_ti != target_ti ? lit_expr->add_cast(target_ti) : lit_expr;
279  }
280  case kINTERVAL_DAY_TIME:
281  case kINTERVAL_YEAR_MONTH: {
282  Datum d;
283  d.bigintval = rex_literal->getVal<int64_t>();
284  return makeExpr<Analyzer::Constant>(rex_literal->getType(), false, d);
285  }
286  case kTIME:
287  case kTIMESTAMP: {
288  Datum d;
289  d.bigintval =
290  rex_literal->getType() == kTIMESTAMP && rex_literal->getPrecision() > 0
291  ? rex_literal->getVal<int64_t>()
292  : rex_literal->getVal<int64_t>() / 1000;
293  return makeExpr<Analyzer::Constant>(
294  SQLTypeInfo(rex_literal->getType(), rex_literal->getPrecision(), 0, false),
295  false,
296  d);
297  }
298  case kDATE: {
299  Datum d;
300  d.bigintval = rex_literal->getVal<int64_t>() * 24 * 3600;
301  return makeExpr<Analyzer::Constant>(rex_literal->getType(), false, d);
302  }
303  case kNULLT: {
304  return makeExpr<Analyzer::Constant>(rex_literal->getTargetType(), true, Datum{0});
305  }
306  default: {
307  LOG(FATAL) << "Unexpected literal type " << lit_ti.get_type_name();
308  }
309  }
310  return nullptr;
311 }
Definition: sqltypes.h:52
#define LOG(tag)
Definition: Logger.h:185
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
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)
double doubleval
Definition: sqltypes.h:131

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 1192 of file RelAlgTranslator.cpp.

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

Referenced by translateFunction().

1193  {
1194  const auto& args = translateFunctionArgs(rex_function);
1195  CHECK_EQ(size_t(1), args.size());
1196  CHECK(args[0]);
1197 
1198  if (args[0]->get_type_info().is_dict_encoded_string() ||
1199  dynamic_cast<Analyzer::Constant*>(args[0].get())) {
1200  return makeExpr<Analyzer::LowerExpr>(args[0]);
1201  }
1202 
1203  throw std::runtime_error(rex_function->getName() +
1204  " expects a dictionary encoded text column or a literal.");
1205 }
#define CHECK_EQ(x, y)
Definition: Logger.h:198
CHECK(cgen_state)
Analyzer::ExpressionPtrVector translateFunctionArgs(const RexFunctionOperator *) const
const std::string & getName() const

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 1243 of file RelAlgTranslator.cpp.

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

Referenced by translateDatetime(), and translateFunction().

1243  {
1245 }
static std::shared_ptr< Analyzer::Expr > get(const int64_t)
Definition: ParserNode.cpp:173

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 1301 of file RelAlgTranslator.cpp.

Referenced by translateFunction().

1301  {
1302  return makeExpr<Analyzer::OffsetInFragment>();
1303 }

+ Here is the caller graph for this function:

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

Definition at line 827 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(), RexOperator::size(), translateDatePlusMinus(), translateGeoComparison(), translateInOper(), translateOverlapsOper(), translateScalarRex(), and translateUoper().

Referenced by translateScalarRex().

828  {
829  CHECK_GT(rex_operator->size(), size_t(0));
830  if (rex_operator->size() == 1) {
831  return translateUoper(rex_operator);
832  }
833  const auto sql_op = rex_operator->getOperator();
834  if (sql_op == kIN) {
835  return translateInOper(rex_operator);
836  }
837  if (sql_op == kMINUS || sql_op == kPLUS) {
838  auto date_plus_minus = translateDatePlusMinus(rex_operator);
839  if (date_plus_minus) {
840  return date_plus_minus;
841  }
842  }
843  if (sql_op == kOVERLAPS) {
844  return translateOverlapsOper(rex_operator);
845  } else if (IS_COMPARISON(sql_op)) {
846  auto geo_comp = translateGeoComparison(rex_operator);
847  if (geo_comp) {
848  return geo_comp;
849  }
850  }
851  auto lhs = translateScalarRex(rex_operator->getOperand(0));
852  for (size_t i = 1; i < rex_operator->size(); ++i) {
853  std::shared_ptr<Analyzer::Expr> rhs;
854  SQLQualifier sql_qual{kONE};
855  const auto rhs_op = rex_operator->getOperand(i);
856  std::tie(rhs, sql_qual) = get_quantified_rhs(rhs_op, *this);
857  if (!rhs) {
858  rhs = translateScalarRex(rhs_op);
859  }
860  CHECK(rhs);
861  lhs = Parser::OperExpr::normalize(sql_op, sql_qual, lhs, rhs);
862  }
863  return lhs;
864 }
SQLQualifier
Definition: sqldefs.h:69
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
size_t size() const
const RexScalar * getOperand(const size_t idx) const
std::shared_ptr< Analyzer::Expr > translateUoper(const RexOperator *) const
Definition: sqldefs.h:41
#define CHECK_GT(x, y)
Definition: Logger.h:202
SQLOps getOperator() const
CHECK(cgen_state)
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
std::shared_ptr< Analyzer::Expr > translateOverlapsOper(const RexOperator *) const
Definition: sqldefs.h:53
std::shared_ptr< Analyzer::Expr > translateInOper(const RexOperator *) const
std::shared_ptr< Analyzer::Expr > translateGeoComparison(const RexOperator *) const
std::shared_ptr< Analyzer::Expr > translateDatePlusMinus(const RexOperator *) const
#define IS_COMPARISON(X)
Definition: sqldefs.h:57

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 866 of file RelAlgTranslator.cpp.

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

Referenced by translateOper().

867  {
868  const auto sql_op = rex_operator->getOperator();
869  CHECK(sql_op == kOVERLAPS);
870 
871  const auto lhs = translateScalarRex(rex_operator->getOperand(0));
872  const auto lhs_ti = lhs->get_type_info();
873  if (lhs_ti.is_geometry()) {
874  return translateGeoOverlapsOper(rex_operator);
875  } else {
876  throw std::runtime_error(
877  "Overlaps equivalence is currently only supported for geospatial types");
878  }
879 }
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const RexScalar * getOperand(const size_t idx) const
std::shared_ptr< Analyzer::Expr > translateGeoOverlapsOper(const RexOperator *) const
SQLOps getOperator() const
CHECK(cgen_state)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 912 of file RelAlgTranslator.cpp.

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

Referenced by translateFunction().

913  {
914  CHECK(rex_function->size() == 2 || rex_function->size() == 3);
915  const auto arg = translateScalarRex(rex_function->getOperand(0));
916  const auto pattern = translateScalarRex(rex_function->getOperand(1));
917  if (!std::dynamic_pointer_cast<const Analyzer::Constant>(pattern)) {
918  throw std::runtime_error("The matching pattern must be a literal.");
919  }
920  const auto escape = (rex_function->size() == 3)
921  ? translateScalarRex(rex_function->getOperand(2))
922  : nullptr;
923  return Parser::RegexpExpr::get(arg, pattern, escape, false);
924 }
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
size_t size() const
const RexScalar * getOperand(const size_t idx) const
CHECK(cgen_state)
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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 167 of file RelAlgTranslator.cpp.

References CHECK(), translateCase(), translateFunction(), translateInput(), translateLiteral(), translateOper(), translateScalarSubquery(), and translateWindowFunction().

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(), anonymous_namespace{RelAlgExecutor.cpp}::translate_targets_for_update(), translateAbs(), translateCardinality(), translateCase(), translateDateadd(), translateDatediff(), translateDatepart(), translateDatePlusMinus(), translateDatetime(), translateExtract(), translateFunction(), translateFunctionArgs(), translateFunctionWithGeoArg(), translateGeoComparison(), translateGeoFunctionArg(), translateHPTLiteral(), translateInOper(), translateItem(), translateLength(), translateLike(), translateLikely(), translateOper(), translateOverlapsOper(), translateRegexp(), translateSign(), translateTernaryGeoFunction(), translateUnlikely(), translateUoper(), and translateWindowFunction().

168  {
169  const auto rex_input = dynamic_cast<const RexInput*>(rex);
170  if (rex_input) {
171  return translateInput(rex_input);
172  }
173  const auto rex_literal = dynamic_cast<const RexLiteral*>(rex);
174  if (rex_literal) {
175  return translateLiteral(rex_literal);
176  }
177  const auto rex_window_function = dynamic_cast<const RexWindowFunctionOperator*>(rex);
178  if (rex_window_function) {
179  return translateWindowFunction(rex_window_function);
180  }
181  const auto rex_function = dynamic_cast<const RexFunctionOperator*>(rex);
182  if (rex_function) {
183  return translateFunction(rex_function);
184  }
185  const auto rex_operator = dynamic_cast<const RexOperator*>(rex);
186  if (rex_operator) {
187  return translateOper(rex_operator);
188  }
189  const auto rex_case = dynamic_cast<const RexCase*>(rex);
190  if (rex_case) {
191  return translateCase(rex_case);
192  }
193  const auto rex_subquery = dynamic_cast<const RexSubQuery*>(rex);
194  if (rex_subquery) {
195  return translateScalarSubquery(rex_subquery);
196  }
197  CHECK(false);
198  return nullptr;
199 }
std::shared_ptr< Analyzer::Expr > translateFunction(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateOper(const RexOperator *) const
std::shared_ptr< Analyzer::Expr > translateInput(const RexInput *) const
static std::shared_ptr< Analyzer::Expr > translateLiteral(const RexLiteral *)
CHECK(cgen_state)
std::shared_ptr< Analyzer::Expr > translateCase(const RexCase *) const
std::shared_ptr< Analyzer::Expr > translateWindowFunction(const RexWindowFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateScalarSubquery(const RexSubQuery *) const

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 313 of file RelAlgTranslator.cpp.

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

Referenced by translateScalarRex().

314  {
315  if (just_explain_) {
316  throw std::runtime_error("EXPLAIN is not supported with sub-queries");
317  }
318  CHECK(rex_subquery);
319  auto result = rex_subquery->getExecutionResult();
320  auto row_set = result->getRows();
321  if (row_set->rowCount() > size_t(1)) {
322  throw std::runtime_error("Scalar sub-query returned multiple rows");
323  }
324  if (row_set->rowCount() < size_t(1)) {
325  CHECK_EQ(row_set->rowCount(), size_t(0));
326  throw std::runtime_error("Scalar sub-query returned no results");
327  }
328  auto first_row = row_set->getNextRow(false, false);
329  auto scalar_tv = boost::get<ScalarTargetValue>(&first_row[0]);
330  auto ti = rex_subquery->getType();
331  if (ti.is_string()) {
332  throw std::runtime_error("Scalar sub-queries which return strings not supported");
333  }
334  Datum d{0};
335  bool is_null_const{false};
336  std::tie(d, is_null_const) = datum_from_scalar_tv(scalar_tv, ti);
337  return makeExpr<Analyzer::Constant>(ti, is_null_const, d);
338 }
#define CHECK_EQ(x, y)
Definition: Logger.h:198
CHECK(cgen_state)
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:

+ Here is the caller graph for this function:

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

Definition at line 1279 of file RelAlgTranslator.cpp.

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

Referenced by translateFunction().

1280  {
1281  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
1282  expr_list;
1283  CHECK_EQ(size_t(1), rex_function->size());
1284  const auto operand = translateScalarRex(rex_function->getOperand(0));
1285  const auto& operand_ti = operand->get_type_info();
1286  CHECK(operand_ti.is_number());
1287  const auto zero = makeNumericConstant(operand_ti, 0);
1288  const auto lt_zero = makeExpr<Analyzer::BinOper>(kBOOLEAN, kLT, kONE, operand, zero);
1289  expr_list.emplace_back(lt_zero, makeNumericConstant(operand_ti, -1));
1290  const auto eq_zero = makeExpr<Analyzer::BinOper>(kBOOLEAN, kEQ, kONE, operand, zero);
1291  expr_list.emplace_back(eq_zero, makeNumericConstant(operand_ti, 0));
1292  const auto gt_zero = makeExpr<Analyzer::BinOper>(kBOOLEAN, kGT, kONE, operand, zero);
1293  expr_list.emplace_back(gt_zero, makeNumericConstant(operand_ti, 1));
1294  return makeExpr<Analyzer::CaseExpr>(
1295  operand_ti,
1296  false,
1297  expr_list,
1298  makeExpr<Analyzer::Constant>(operand_ti, true, Datum{0}));
1299 }
#define CHECK_EQ(x, y)
Definition: Logger.h:198
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
size_t size() const
const RexScalar * getOperand(const size_t idx) const
Definition: sqldefs.h:30
std::shared_ptr< Analyzer::Constant > makeNumericConstant(const SQLTypeInfo &ti, const long val)
CHECK(cgen_state)
Definition: sqldefs.h:34
Definition: sqldefs.h:69
Definition: sqldefs.h:33

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 892 of file RelAlgTranslatorGeo.cpp.

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

Referenced by translateFunction().

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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 564 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(), SQLTypeInfoCore< TYPE_FACET_PACK >::has_bounds(), Datum::intval, IS_GEO_POLY, kENCODING_GEOINT, kGEOGRAPHY, kINT, kLINESTRING, kMULTIPOLYGON, kPOINT, kPOLYGON, RexOperator::size(), anonymous_namespace{RelAlgTranslatorGeo.cpp}::suffix(), and translateGeoFunctionArg().

Referenced by translateFunction().

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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 933 of file RelAlgTranslator.cpp.

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

Referenced by translateFunction().

934  {
935  CHECK(rex_function->size() == 1);
936  const auto arg = translateScalarRex(rex_function->getOperand(0));
937  return makeExpr<Analyzer::LikelihoodExpr>(arg, 0.0625);
938 }
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
size_t size() const
const RexScalar * getOperand(const size_t idx) const
CHECK(cgen_state)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

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

Referenced by translateOper().

386  {
387  CHECK_EQ(size_t(1), rex_operator->size());
388  const auto operand_expr = translateScalarRex(rex_operator->getOperand(0));
389  const auto sql_op = rex_operator->getOperator();
390  switch (sql_op) {
391  case kCAST: {
392  const auto& target_ti = rex_operator->getType();
393  CHECK_NE(kNULLT, target_ti.get_type());
394  const auto& operand_ti = operand_expr->get_type_info();
395  if (operand_ti.is_string() && target_ti.is_string()) {
396  return operand_expr;
397  }
398  if (target_ti.is_time() ||
399  operand_ti
400  .is_string()) { // TODO(alex): check and unify with the rest of the cases
401  // Do not propogate encoding on small dates
402  return target_ti.is_date_in_days()
403  ? operand_expr->add_cast(SQLTypeInfo(kDATE, false))
404  : operand_expr->add_cast(target_ti);
405  }
406  if (!operand_ti.is_string() && target_ti.is_string()) {
407  return operand_expr->add_cast(target_ti);
408  }
409 
410  return std::make_shared<Analyzer::UOper>(target_ti, false, sql_op, operand_expr);
411  }
412  case kNOT:
413  case kISNULL: {
414  return std::make_shared<Analyzer::UOper>(kBOOLEAN, sql_op, operand_expr);
415  }
416  case kISNOTNULL: {
417  auto is_null = std::make_shared<Analyzer::UOper>(kBOOLEAN, kISNULL, operand_expr);
418  return std::make_shared<Analyzer::UOper>(kBOOLEAN, kNOT, is_null);
419  }
420  case kMINUS: {
421  const auto& ti = operand_expr->get_type_info();
422  return std::make_shared<Analyzer::UOper>(ti, false, kUMINUS, operand_expr);
423  }
424  case kUNNEST: {
425  const auto& ti = operand_expr->get_type_info();
426  CHECK(ti.is_array());
427  return makeExpr<Analyzer::UOper>(ti.get_elem_type(), false, kUNNEST, operand_expr);
428  }
429  default:
430  CHECK(false);
431  }
432  return nullptr;
433 }
#define CHECK_EQ(x, y)
Definition: Logger.h:198
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const SQLTypeInfo & getType() const
size_t size() const
const RexScalar * getOperand(const size_t idx) const
Definition: sqldefs.h:49
SQLOps getOperator() const
CHECK(cgen_state)
#define CHECK_NE(x, y)
Definition: Logger.h:199
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)
Definition: sqldefs.h:39

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 1612 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(), translateScalarRex(), and window_function_is_value().

Referenced by translateScalarRex().

1613  {
1614  if (!supported_lower_bound(rex_window_function->getLowerBound()) ||
1615  !supported_upper_bound(rex_window_function) ||
1616  ((rex_window_function->getKind() == SqlWindowFunctionKind::ROW_NUMBER) !=
1617  rex_window_function->isRows())) {
1618  throw std::runtime_error("Frame specification not supported");
1619  }
1620  std::vector<std::shared_ptr<Analyzer::Expr>> args;
1621  for (size_t i = 0; i < rex_window_function->size(); ++i) {
1622  args.push_back(translateScalarRex(rex_window_function->getOperand(i)));
1623  }
1624  std::vector<std::shared_ptr<Analyzer::Expr>> partition_keys;
1625  for (const auto& partition_key : rex_window_function->getPartitionKeys()) {
1626  partition_keys.push_back(translateScalarRex(partition_key.get()));
1627  }
1628  std::vector<std::shared_ptr<Analyzer::Expr>> order_keys;
1629  for (const auto& order_key : rex_window_function->getOrderKeys()) {
1630  order_keys.push_back(translateScalarRex(order_key.get()));
1631  }
1632  auto ti = rex_window_function->getType();
1633  if (window_function_is_value(rex_window_function->getKind())) {
1634  CHECK_GE(args.size(), 1u);
1635  ti = args.front()->get_type_info();
1636  }
1637  return makeExpr<Analyzer::WindowFunction>(
1638  ti,
1639  rex_window_function->getKind(),
1640  args,
1641  partition_keys,
1642  order_keys,
1643  translate_collation(rex_window_function->getCollation()));
1644 }
bool supported_lower_bound(const RexWindowFunctionOperator::RexWindowBound &window_bound)
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const SQLTypeInfo & getType() const
size_t size() const
const RexScalar * getOperand(const size_t idx) const
const std::vector< SortField > & getCollation() const
#define CHECK_GE(x, y)
Definition: Logger.h:203
bool window_function_is_value(const SqlWindowFunctionKind kind)
Definition: WindowContext.h:27
std::vector< Analyzer::OrderEntry > translate_collation(const std::vector< SortField > &sort_fields)
const ConstRexScalarPtrVector & getPartitionKeys() const
const RexWindowBound & getLowerBound() const
SqlWindowFunctionKind getKind() const
bool supported_upper_bound(const RexWindowFunctionOperator *rex_window_function)
const ConstRexScalarPtrVector & getOrderKeys() const

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

const Catalog_Namespace::Catalog& RelAlgTranslator::cat_
private

Definition at line 173 of file RelAlgTranslator.h.

Referenced by getInIntegerSetExpr(), translateGeoColumn(), and translateInput().

const Executor* RelAlgTranslator::executor_
private

Definition at line 174 of file RelAlgTranslator.h.

Referenced by getInIntegerSetExpr().

QueryFeatureDescriptor& RelAlgTranslator::feature_stash_
private

Definition at line 179 of file RelAlgTranslator.h.

Referenced by translateArrayFunction().

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

Definition at line 175 of file RelAlgTranslator.h.

Referenced by translateGeoColumn(), and translateInput().

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

Definition at line 176 of file RelAlgTranslator.h.

Referenced by translateInput().

const bool RelAlgTranslator::just_explain_
private

Definition at line 178 of file RelAlgTranslator.h.

Referenced by translateInOper(), and translateScalarSubquery().

time_t RelAlgTranslator::now_
private

Definition at line 177 of file RelAlgTranslator.h.

Referenced by translateNow().


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