OmniSciDB  6686921089
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ExpressionRewrite.h File Reference
#include <llvm/IR/Value.h>
#include <boost/optional.hpp>
#include <list>
#include <memory>
#include <vector>
#include "Analyzer/Analyzer.h"
#include "RelAlgExecutionUnit.h"
+ Include dependency graph for ExpressionRewrite.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  OverlapsJoinConjunction
 

Namespaces

 Analyzer
 

Functions

Analyzer::ExpressionPtr rewrite_expr (const Analyzer::Expr *)
 
Analyzer::ExpressionPtr rewrite_array_elements (const Analyzer::Expr *)
 
boost::optional
< OverlapsJoinConjunction
rewrite_overlaps_conjunction (const std::shared_ptr< Analyzer::Expr > expr)
 
std::list< std::shared_ptr
< Analyzer::Expr > > 
strip_join_covered_filter_quals (const std::list< std::shared_ptr< Analyzer::Expr >> &quals, const JoinQualsPerNestingLevel &join_quals)
 
std::shared_ptr< Analyzer::Exprfold_expr (const Analyzer::Expr *)
 
bool self_join_not_covered_by_left_deep_tree (const Analyzer::ColumnVar *lhs, const Analyzer::ColumnVar *rhs, const int max_rte_covered)
 
const int get_max_rte_scan_table (std::unordered_map< int, llvm::Value * > &scan_idx_to_hash_pos)
 

Function Documentation

std::shared_ptr<Analyzer::Expr> fold_expr ( const Analyzer::Expr )

Definition at line 992 of file ExpressionRewrite.cpp.

References kBIGINT, and anonymous_namespace{ExpressionRewrite.cpp}::strip_likelihood().

Referenced by RelAlgExecutor::createFilterWorkUnit(), anonymous_namespace{RelAlgExecutor.cpp}::set_transient_dict_maybe(), anonymous_namespace{RelAlgExecutor.cpp}::translate_quals(), anonymous_namespace{RelAlgExecutor.cpp}::translate_scalar_sources(), anonymous_namespace{RelAlgExecutor.cpp}::translate_targets(), RelAlgTranslator::translateBinaryGeoFunction(), RelAlgTranslator::translateDatePlusMinus(), RelAlgTranslator::translateGeoComparison(), and RelAlgTranslator::translateGeoFunctionArg().

992  {
993  if (!expr) {
994  return nullptr;
995  }
996  const auto expr_no_likelihood = strip_likelihood(expr);
997  ConstantFoldingVisitor visitor;
998  auto rewritten_expr = visitor.visit(expr_no_likelihood);
999  if (visitor.get_num_overflows() > 0 && rewritten_expr->get_type_info().is_integer() &&
1000  rewritten_expr->get_type_info().get_type() != kBIGINT) {
1001  auto rewritten_expr_const =
1002  std::dynamic_pointer_cast<const Analyzer::Constant>(rewritten_expr);
1003  if (!rewritten_expr_const) {
1004  // Integer expression didn't fold completely the first time due to
1005  // overflows in smaller type subexpressions, trying again with a cast
1006  const auto& ti = SQLTypeInfo(kBIGINT, false);
1007  auto bigint_expr_no_likelihood = expr_no_likelihood->deep_copy()->add_cast(ti);
1008  auto rewritten_expr_take2 = visitor.visit(bigint_expr_no_likelihood.get());
1009  auto rewritten_expr_take2_const =
1010  std::dynamic_pointer_cast<Analyzer::Constant>(rewritten_expr_take2);
1011  if (rewritten_expr_take2_const) {
1012  // Managed to fold, switch to the new constant
1013  rewritten_expr = rewritten_expr_take2_const;
1014  }
1015  }
1016  }
1017  const auto expr_with_likelihood = dynamic_cast<const Analyzer::LikelihoodExpr*>(expr);
1018  if (expr_with_likelihood) {
1019  // Add back likelihood
1020  return std::make_shared<Analyzer::LikelihoodExpr>(
1021  rewritten_expr, expr_with_likelihood->get_likelihood());
1022  }
1023  return rewritten_expr;
1024 }
const Analyzer::Expr * strip_likelihood(const Analyzer::Expr *expr)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

const int get_max_rte_scan_table ( std::unordered_map< int, llvm::Value * > &  scan_idx_to_hash_pos)

Definition at line 1037 of file ExpressionRewrite.cpp.

References INT32_MIN.

Referenced by BaselineJoinHashTable::codegenKey(), PerfectJoinHashTable::codegenMatchingSet(), and PerfectJoinHashTable::codegenSlot().

1038  {
1039  int ret = INT32_MIN;
1040  for (auto& kv : scan_idx_to_hash_pos) {
1041  if (kv.first > ret) {
1042  ret = kv.first;
1043  }
1044  }
1045  return ret;
1046 }
#define INT32_MIN

+ Here is the caller graph for this function:

Analyzer::ExpressionPtr rewrite_array_elements ( const Analyzer::Expr )

Definition at line 699 of file ExpressionRewrite.cpp.

Referenced by anonymous_namespace{RelAlgExecutor.cpp}::translate_scalar_sources(), and anonymous_namespace{RelAlgExecutor.cpp}::translate_scalar_sources_for_update().

699  {
700  return ArrayElementStringLiteralEncodingVisitor().visit(expr);
701 }

+ Here is the caller graph for this function:

Definition at line 703 of file ExpressionRewrite.cpp.

References rewrite_avg_window(), rewrite_sum_window(), and anonymous_namespace{ExpressionRewrite.cpp}::strip_likelihood().

Referenced by RelAlgExecutor::createFilterWorkUnit(), qual_to_conjunctive_form(), qual_to_disjunctive_form(), anonymous_namespace{RelAlgExecutor.cpp}::rewrite_quals(), QueryRewriter::rewriteConstrainedByIn(), anonymous_namespace{RelAlgExecutor.cpp}::translate_scalar_sources(), anonymous_namespace{RelAlgExecutor.cpp}::translate_scalar_sources_for_update(), and anonymous_namespace{RelAlgExecutor.cpp}::translate_targets().

703  {
704  const auto sum_window = rewrite_sum_window(expr);
705  if (sum_window) {
706  return sum_window;
707  }
708  const auto avg_window = rewrite_avg_window(expr);
709  if (avg_window) {
710  return avg_window;
711  }
712  const auto expr_no_likelihood = strip_likelihood(expr);
713  // The following check is not strictly needed, but seems silly to transform a
714  // simple string comparison to an IN just to codegen the same thing anyway.
715 
716  RecursiveOrToInVisitor visitor;
717  auto rewritten_expr = visitor.visit(expr_no_likelihood);
718  const auto expr_with_likelihood =
719  std::dynamic_pointer_cast<const Analyzer::LikelihoodExpr>(rewritten_expr);
720  if (expr_with_likelihood) {
721  // Add back likelihood
722  return std::make_shared<Analyzer::LikelihoodExpr>(
723  rewritten_expr, expr_with_likelihood->get_likelihood());
724  }
725  return rewritten_expr;
726 }
std::shared_ptr< Analyzer::WindowFunction > rewrite_avg_window(const Analyzer::Expr *expr)
std::shared_ptr< Analyzer::WindowFunction > rewrite_sum_window(const Analyzer::Expr *expr)
const Analyzer::Expr * strip_likelihood(const Analyzer::Expr *expr)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

boost::optional<OverlapsJoinConjunction> rewrite_overlaps_conjunction ( const std::shared_ptr< Analyzer::Expr expr)

Definition at line 760 of file ExpressionRewrite.cpp.

References CHECK, CHECK_EQ, CHECK_GE, g_enable_distance_rangejoin, g_enable_hashjoin_many_to_many, logger::INFO, is_constructed_point(), kBOOLEAN, kGEOGRAPHY, kLE, kLT, kONE, kOVERLAPS, LOG, anonymous_namespace{ExpressionRewrite.cpp}::overlaps_supported_functions, anonymous_namespace{ExpressionRewrite.cpp}::requires_many_to_many, ScalarExprVisitor< T >::visit(), VLOG, and logger::WARNING.

Referenced by QueryRewriter::rewriteOverlapsJoin().

761  {
762  auto func_oper = dynamic_cast<Analyzer::FunctionOper*>(expr.get());
763  if (func_oper) {
764  const auto needs_many_many = [func_oper]() {
765  return requires_many_to_many.find(func_oper->getName()) !=
766  requires_many_to_many.end();
767  };
768  // TODO(adb): consider converting unordered set to an unordered map, potentially
769  // storing the rewrite function we want to apply in the map
770  if (overlaps_supported_functions.find(func_oper->getName()) !=
772  if (!g_enable_hashjoin_many_to_many && needs_many_many()) {
773  LOG(WARNING) << "Many-to-many hashjoin support is disabled, unable to rewrite "
774  << func_oper->toString() << " to use accelerated geo join.";
775  return boost::none;
776  }
777 
778  DeepCopyVisitor deep_copy_visitor;
779  if (func_oper->getName() == "ST_Overlaps") {
780  CHECK_GE(func_oper->getArity(), size_t(2));
781  // return empty quals, overlaps join quals
782  // TODO(adb): we will likely want to actually check for true overlaps, but this
783  // works for now
784 
785  auto lhs = func_oper->getOwnArg(0);
786  auto rewritten_lhs = deep_copy_visitor.visit(lhs.get());
787  CHECK(rewritten_lhs);
788 
789  auto rhs = func_oper->getOwnArg(1);
790  auto rewritten_rhs = deep_copy_visitor.visit(rhs.get());
791  CHECK(rewritten_rhs);
792 
793  auto overlaps_oper = makeExpr<Analyzer::BinOper>(
794  kBOOLEAN, kOVERLAPS, kONE, rewritten_lhs, rewritten_rhs);
795  return OverlapsJoinConjunction{{}, {overlaps_oper}};
796  }
797 
798  // TODO(jclay): This will work for Poly_Poly,but needs to change for others.
799  CHECK_GE(func_oper->getArity(), size_t(4));
800  if (func_oper->getName() == "ST_Contains_Polygon_Polygon" ||
801  func_oper->getName() == "ST_Intersects_Polygon_Polygon" ||
802  func_oper->getName() == "ST_Intersects_MultiPolygon_MultiPolygon" ||
803  func_oper->getName() == "ST_Intersects_MultiPolygon_Polygon" ||
804  func_oper->getName() == "ST_Intersects_Polygon_MultiPolygon") {
805  auto lhs = func_oper->getOwnArg(3);
806  auto rewritten_lhs = deep_copy_visitor.visit(lhs.get());
807  CHECK(rewritten_lhs);
808  auto rhs = func_oper->getOwnArg(1);
809  auto rewritten_rhs = deep_copy_visitor.visit(rhs.get());
810  CHECK(rewritten_rhs);
811 
812  auto overlaps_oper = makeExpr<Analyzer::BinOper>(
813  kBOOLEAN, kOVERLAPS, kONE, rewritten_lhs, rewritten_rhs);
814 
815  VLOG(1) << "Successfully converted to overlaps join";
816  return OverlapsJoinConjunction{{expr}, {overlaps_oper}};
817  }
818 
819  auto lhs = func_oper->getOwnArg(2);
820  auto rewritten_lhs = deep_copy_visitor.visit(lhs.get());
821  CHECK(rewritten_lhs);
822  const auto& lhs_ti = rewritten_lhs->get_type_info();
823 
824  if (!lhs_ti.is_geometry() && !is_constructed_point(rewritten_lhs.get())) {
825  // TODO(adb): If ST_Contains is passed geospatial literals instead of columns, the
826  // function will be expanded during translation rather than during code
827  // generation. While this scenario does not make sense for the overlaps join, we
828  // need to detect and abort the overlaps rewrite. Adding a GeospatialConstant
829  // dervied class to the Analyzer may prove to be a better way to handle geo
830  // literals, but for now we ensure the LHS type is a geospatial type, which would
831  // mean the function has not been expanded to the physical types, yet.
832 
833  LOG(INFO) << "Unable to rewrite " << func_oper->getName()
834  << " to overlaps conjunction. LHS input type is neither a geospatial "
835  "column nor a constructed point\n"
836  << func_oper->toString();
837 
838  return boost::none;
839  }
840 
841  // Read the bounds arg from the ST_Contains FuncOper (second argument)instead of the
842  // poly column (first argument)
843  auto rhs = func_oper->getOwnArg(1);
844  auto rewritten_rhs = deep_copy_visitor.visit(rhs.get());
845  CHECK(rewritten_rhs);
846 
847  // Check for compatible join ordering. If the join ordering does not match expected
848  // ordering for overlaps, the join builder will fail.
849  std::set<int> lhs_rte_idx;
850  lhs->collect_rte_idx(lhs_rte_idx);
851  CHECK(!lhs_rte_idx.empty());
852  std::set<int> rhs_rte_idx;
853  rhs->collect_rte_idx(rhs_rte_idx);
854  CHECK(!rhs_rte_idx.empty());
855 
856  if (lhs_rte_idx.size() > 1 || rhs_rte_idx.size() > 1 || lhs_rte_idx > rhs_rte_idx) {
857  LOG(INFO) << "Unable to rewrite " << func_oper->getName()
858  << " to overlaps conjunction. Cannot build hash table over LHS type. "
859  "Check join order.\n"
860  << func_oper->toString();
861  return boost::none;
862  }
863 
864  VLOG(1) << "Rewritten to use overlaps join with lhs as "
865  << rewritten_lhs->toString() << " and rhs as " << rewritten_rhs->toString();
866 
867  auto overlaps_oper = makeExpr<Analyzer::BinOper>(
868  kBOOLEAN, kOVERLAPS, kONE, rewritten_lhs, rewritten_rhs);
869 
870  VLOG(1) << "Successfully converted to overlaps join";
871  if (func_oper->getName() == "ST_Approx_Overlaps_MultiPolygon_Point"sv) {
872  return OverlapsJoinConjunction{{}, {overlaps_oper}};
873  } else {
874  return OverlapsJoinConjunction{{expr}, {overlaps_oper}};
875  }
876  } else {
877  VLOG(1) << "Overlaps join not enabled for " << func_oper->getName();
878  }
879  return boost::none;
880  }
881  auto bin_oper = dynamic_cast<Analyzer::BinOper*>(expr.get());
882  if (g_enable_distance_rangejoin && bin_oper &&
883  (bin_oper->get_optype() == kLE || bin_oper->get_optype() == kLT)) {
884  auto lhs = dynamic_cast<const Analyzer::GeoOperator*>(bin_oper->get_left_operand());
885  auto rhs = dynamic_cast<const Analyzer::Constant*>(bin_oper->get_right_operand());
886  if (lhs && rhs && lhs->getName() == "ST_Distance") {
887  CHECK_EQ(lhs->size(), size_t(2));
888  auto l_arg = lhs->getOperand(0);
889  auto r_arg = lhs->getOperand(1);
890  const bool is_geography = l_arg->get_type_info().get_subtype() == kGEOGRAPHY ||
891  r_arg->get_type_info().get_subtype() == kGEOGRAPHY;
892  if (is_geography) {
893  VLOG(1) << "Range join not yet supported for geodesic distance "
894  << bin_oper->toString();
895  return boost::none;
896  }
897 
898  // Check for compatible join ordering. If the join ordering does not match expected
899  // ordering for overlaps, the join builder will fail.
900  std::set<int> lhs_rte_idx;
901  l_arg->collect_rte_idx(lhs_rte_idx);
902  CHECK(!lhs_rte_idx.empty());
903  std::set<int> rhs_rte_idx;
904  r_arg->collect_rte_idx(rhs_rte_idx);
905  CHECK(!rhs_rte_idx.empty());
906 
907  if (lhs_rte_idx.size() > 1 || rhs_rte_idx.size() > 1 || lhs_rte_idx > rhs_rte_idx) {
908  LOG(INFO) << "Unable to rewrite " << lhs->getName()
909  << " to overlaps conjunction. Cannot build hash table over LHS type. "
910  "Check join order.\n"
911  << bin_oper->toString();
912  return boost::none;
913  }
914 
915  const bool inclusive = bin_oper->get_optype() == kLE;
916  auto range_expr = makeExpr<Analyzer::RangeOper>(
917  inclusive, inclusive, r_arg->deep_copy(), rhs->deep_copy());
918  auto overlaps_oper = makeExpr<Analyzer::BinOper>(
919  kBOOLEAN, kOVERLAPS, kONE, l_arg->deep_copy(), range_expr);
920  return OverlapsJoinConjunction{{expr}, {overlaps_oper}};
921  }
922  }
923  return boost::none;
924 }
#define CHECK_EQ(x, y)
Definition: Logger.h:217
#define LOG(tag)
Definition: Logger.h:203
bool is_constructed_point(const Analyzer::Expr *expr)
Definition: Execute.h:1226
static const std::unordered_set< std::string > requires_many_to_many
Definition: sqldefs.h:35
#define CHECK_GE(x, y)
Definition: Logger.h:222
T visit(const Analyzer::Expr *expr) const
static const std::unordered_set< std::string > overlaps_supported_functions
bool g_enable_hashjoin_many_to_many
Definition: Execute.cpp:100
bool g_enable_distance_rangejoin
Definition: Execute.cpp:99
Definition: sqldefs.h:69
#define CHECK(condition)
Definition: Logger.h:209
Definition: sqldefs.h:33
#define VLOG(n)
Definition: Logger.h:303

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool self_join_not_covered_by_left_deep_tree ( const Analyzer::ColumnVar lhs,
const Analyzer::ColumnVar rhs,
const int  max_rte_covered 
)

Definition at line 1026 of file ExpressionRewrite.cpp.

References Analyzer::ColumnVar::get_rte_idx(), and Analyzer::ColumnVar::get_table_id().

Referenced by BaselineJoinHashTable::codegenKey(), PerfectJoinHashTable::codegenMatchingSet(), and PerfectJoinHashTable::codegenSlot().

1028  {
1029  if (key_side->get_table_id() == val_side->get_table_id() &&
1030  key_side->get_rte_idx() == val_side->get_rte_idx() &&
1031  key_side->get_rte_idx() > max_rte_covered) {
1032  return true;
1033  }
1034  return false;
1035 }

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::list<std::shared_ptr<Analyzer::Expr> > strip_join_covered_filter_quals ( const std::list< std::shared_ptr< Analyzer::Expr >> &  quals,
const JoinQualsPerNestingLevel join_quals 
)

Definition at line 968 of file ExpressionRewrite.cpp.

References g_strip_join_covered_quals, and ScalarExprVisitor< T >::visit().

Referenced by create_count_all_execution_unit().

970  {
972  return quals;
973  }
974 
975  if (join_quals.empty()) {
976  return quals;
977  }
978 
979  std::list<std::shared_ptr<Analyzer::Expr>> quals_to_return;
980 
981  JoinCoveredQualVisitor visitor(join_quals);
982  for (const auto& qual : quals) {
983  if (!visitor.visit(qual.get())) {
984  // Not a covered qual, don't elide it from the filtered count
985  quals_to_return.push_back(qual);
986  }
987  }
988 
989  return quals_to_return;
990 }
bool g_strip_join_covered_quals
Definition: Execute.cpp:103

+ Here is the call graph for this function:

+ Here is the caller graph for this function: