OmniSciDB  a667adc9c8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 946 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(), and RelAlgTranslator::translateGeoFunctionArg().

946  {
947  if (!expr) {
948  return nullptr;
949  }
950  const auto expr_no_likelihood = strip_likelihood(expr);
951  ConstantFoldingVisitor visitor;
952  auto rewritten_expr = visitor.visit(expr_no_likelihood);
953  if (visitor.get_num_overflows() > 0 && rewritten_expr->get_type_info().is_integer() &&
954  rewritten_expr->get_type_info().get_type() != kBIGINT) {
955  auto rewritten_expr_const =
956  std::dynamic_pointer_cast<const Analyzer::Constant>(rewritten_expr);
957  if (!rewritten_expr_const) {
958  // Integer expression didn't fold completely the first time due to
959  // overflows in smaller type subexpressions, trying again with a cast
960  const auto& ti = SQLTypeInfo(kBIGINT, false);
961  auto bigint_expr_no_likelihood = expr_no_likelihood->deep_copy()->add_cast(ti);
962  auto rewritten_expr_take2 = visitor.visit(bigint_expr_no_likelihood.get());
963  auto rewritten_expr_take2_const =
964  std::dynamic_pointer_cast<Analyzer::Constant>(rewritten_expr_take2);
965  if (rewritten_expr_take2_const) {
966  // Managed to fold, switch to the new constant
967  rewritten_expr = rewritten_expr_take2_const;
968  }
969  }
970  }
971  const auto expr_with_likelihood = dynamic_cast<const Analyzer::LikelihoodExpr*>(expr);
972  if (expr_with_likelihood) {
973  // Add back likelihood
974  return std::make_shared<Analyzer::LikelihoodExpr>(
975  rewritten_expr, expr_with_likelihood->get_likelihood());
976  }
977  return rewritten_expr;
978 }
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 991 of file ExpressionRewrite.cpp.

References INT32_MIN.

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

992  {
993  int ret = INT32_MIN;
994  for (auto& kv : scan_idx_to_hash_pos) {
995  if (kv.first > ret) {
996  ret = kv.first;
997  }
998  }
999  return ret;
1000 }
#define INT32_MIN

+ Here is the caller graph for this function:

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

Definition at line 696 of file ExpressionRewrite.cpp.

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

696  {
697  return ArrayElementStringLiteralEncodingVisitor().visit(expr);
698 }

+ Here is the caller graph for this function:

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

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

References CHECK, CHECK_GE, g_enable_hashjoin_many_to_many, logger::INFO, is_constructed_point(), kBOOLEAN, 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().

758  {
759  auto func_oper = dynamic_cast<Analyzer::FunctionOper*>(expr.get());
760  if (func_oper) {
761  const auto needs_many_many = [func_oper]() {
762  return requires_many_to_many.find(func_oper->getName()) !=
763  requires_many_to_many.end();
764  };
765  // TODO(adb): consider converting unordered set to an unordered map, potentially
766  // storing the rewrite function we want to apply in the map
767  if (overlaps_supported_functions.find(func_oper->getName()) !=
769  if (!g_enable_hashjoin_many_to_many && needs_many_many()) {
770  LOG(WARNING) << "Many-to-many hashjoin support is disabled, unable to rewrite "
771  << func_oper->toString() << " to use accelerated geo join.";
772  return boost::none;
773  }
774 
775  DeepCopyVisitor deep_copy_visitor;
776  if (func_oper->getName() == "ST_Overlaps") {
777  CHECK_GE(func_oper->getArity(), size_t(2));
778  // return empty quals, overlaps join quals
779  // TODO(adb): we will likely want to actually check for true overlaps, but this
780  // works for now
781 
782  auto lhs = func_oper->getOwnArg(0);
783  auto rewritten_lhs = deep_copy_visitor.visit(lhs.get());
784  CHECK(rewritten_lhs);
785 
786  auto rhs = func_oper->getOwnArg(1);
787  auto rewritten_rhs = deep_copy_visitor.visit(rhs.get());
788  CHECK(rewritten_rhs);
789 
790  auto overlaps_oper = makeExpr<Analyzer::BinOper>(
791  kBOOLEAN, kOVERLAPS, kONE, rewritten_lhs, rewritten_rhs);
792  return OverlapsJoinConjunction{{}, {overlaps_oper}};
793  }
794 
795  // TODO(jclay): This will work for Poly_Poly,but needs to change for others.
796  CHECK_GE(func_oper->getArity(), size_t(4));
797  if (func_oper->getName() == "ST_Contains_Polygon_Polygon" ||
798  func_oper->getName() == "ST_Intersects_Polygon_Polygon" ||
799  func_oper->getName() == "ST_Intersects_MultiPolygon_MultiPolygon" ||
800  func_oper->getName() == "ST_Intersects_MultiPolygon_Polygon" ||
801  func_oper->getName() == "ST_Intersects_Polygon_MultiPolygon") {
802  auto lhs = func_oper->getOwnArg(3);
803  auto rewritten_lhs = deep_copy_visitor.visit(lhs.get());
804  CHECK(rewritten_lhs);
805  auto rhs = func_oper->getOwnArg(1);
806  auto rewritten_rhs = deep_copy_visitor.visit(rhs.get());
807  CHECK(rewritten_rhs);
808 
809  auto overlaps_oper = makeExpr<Analyzer::BinOper>(
810  kBOOLEAN, kOVERLAPS, kONE, rewritten_lhs, rewritten_rhs);
811 
812  VLOG(1) << "Successfully converted to overlaps join";
813  return OverlapsJoinConjunction{{expr}, {overlaps_oper}};
814  }
815 
816  auto lhs = func_oper->getOwnArg(2);
817  auto rewritten_lhs = deep_copy_visitor.visit(lhs.get());
818  CHECK(rewritten_lhs);
819  const auto& lhs_ti = rewritten_lhs->get_type_info();
820 
821  if (!lhs_ti.is_geometry() && !is_constructed_point(rewritten_lhs.get())) {
822  // TODO(adb): If ST_Contains is passed geospatial literals instead of columns, the
823  // function will be expanded during translation rather than during code
824  // generation. While this scenario does not make sense for the overlaps join, we
825  // need to detect and abort the overlaps rewrite. Adding a GeospatialConstant
826  // dervied class to the Analyzer may prove to be a better way to handle geo
827  // literals, but for now we ensure the LHS type is a geospatial type, which would
828  // mean the function has not been expanded to the physical types, yet.
829 
830  LOG(INFO) << "Unable to rewrite " << func_oper->getName()
831  << " to overlaps conjunction. LHS input type is neither a geospatial "
832  "column nor a constructed point\n"
833  << func_oper->toString();
834 
835  return boost::none;
836  }
837 
838  // Read the bounds arg from the ST_Contains FuncOper (second argument)instead of the
839  // poly column (first argument)
840  auto rhs = func_oper->getOwnArg(1);
841  auto rewritten_rhs = deep_copy_visitor.visit(rhs.get());
842  CHECK(rewritten_rhs);
843 
844  // Check for compatible join ordering. If the join ordering does not match expected
845  // ordering for overlaps, the join builder will fail.
846  std::set<int> lhs_rte_idx;
847  lhs->collect_rte_idx(lhs_rte_idx);
848  CHECK(!lhs_rte_idx.empty());
849  std::set<int> rhs_rte_idx;
850  rhs->collect_rte_idx(rhs_rte_idx);
851  CHECK(!rhs_rte_idx.empty());
852 
853  if (lhs_rte_idx.size() > 1 || rhs_rte_idx.size() > 1 || lhs_rte_idx > rhs_rte_idx) {
854  LOG(INFO) << "Unable to rewrite " << func_oper->getName()
855  << " to overlaps conjunction. Cannot build hash table over LHS type. "
856  "Check join order.\n"
857  << func_oper->toString();
858  return boost::none;
859  }
860 
861  VLOG(1) << "Rewritten to use overlaps join with lhs as "
862  << rewritten_lhs->toString() << " and rhs as " << rewritten_rhs->toString();
863 
864  auto overlaps_oper = makeExpr<Analyzer::BinOper>(
865  kBOOLEAN, kOVERLAPS, kONE, rewritten_lhs, rewritten_rhs);
866 
867  VLOG(1) << "Successfully converted to overlaps join";
868  if (func_oper->getName() == "ST_Approx_Overlaps_MultiPolygon_Point"sv) {
869  return OverlapsJoinConjunction{{}, {overlaps_oper}};
870  } else {
871  return OverlapsJoinConjunction{{expr}, {overlaps_oper}};
872  }
873  } else {
874  VLOG(1) << "Overlaps join not enabled for " << func_oper->getName();
875  }
876  }
877  return boost::none;
878 }
#define LOG(tag)
Definition: Logger.h:188
bool is_constructed_point(const Analyzer::Expr *expr)
Definition: Execute.h:1176
static const std::unordered_set< std::string > requires_many_to_many
#define CHECK_GE(x, y)
Definition: Logger.h:210
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:97
Definition: sqldefs.h:69
#define CHECK(condition)
Definition: Logger.h:197
#define VLOG(n)
Definition: Logger.h:291

+ 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 980 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().

982  {
983  if (key_side->get_table_id() == val_side->get_table_id() &&
984  key_side->get_rte_idx() == val_side->get_rte_idx() &&
985  key_side->get_rte_idx() > max_rte_covered) {
986  return true;
987  }
988  return false;
989 }

+ 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 922 of file ExpressionRewrite.cpp.

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

Referenced by create_count_all_execution_unit().

924  {
926  return quals;
927  }
928 
929  if (join_quals.empty()) {
930  return quals;
931  }
932 
933  std::list<std::shared_ptr<Analyzer::Expr>> quals_to_return;
934 
935  JoinCoveredQualVisitor visitor(join_quals);
936  for (const auto& qual : quals) {
937  if (!visitor.visit(qual.get())) {
938  // Not a covered qual, don't elide it from the filtered count
939  quals_to_return.push_back(qual);
940  }
941  }
942 
943  return quals_to_return;
944 }
bool g_strip_join_covered_quals
Definition: Execute.cpp:100

+ Here is the call graph for this function:

+ Here is the caller graph for this function: