OmniSciDB  2b310ab3b2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ExpressionRewrite.cpp File Reference
#include "ExpressionRewrite.h"
#include "../Analyzer/Analyzer.h"
#include "../Parser/ParserNode.h"
#include "../Shared/sqldefs.h"
#include "DeepCopyVisitor.h"
#include "Execute.h"
#include "Logger/Logger.h"
#include "RelAlgTranslator.h"
#include "ScalarExprVisitor.h"
#include "WindowExpressionRewrite.h"
#include <boost/locale/conversion.hpp>
#include <unordered_set>
+ Include dependency graph for ExpressionRewrite.cpp:

Go to the source code of this file.

Classes

class  anonymous_namespace{ExpressionRewrite.cpp}::OrToInVisitor
 
class  anonymous_namespace{ExpressionRewrite.cpp}::RecursiveOrToInVisitor
 
class  anonymous_namespace{ExpressionRewrite.cpp}::ArrayElementStringLiteralEncodingVisitor
 
class  anonymous_namespace{ExpressionRewrite.cpp}::ConstantFoldingVisitor
 
class  JoinCoveredQualVisitor
 

Namespaces

 anonymous_namespace{ExpressionRewrite.cpp}
 

Functions

const Analyzer::Expranonymous_namespace{ExpressionRewrite.cpp}::strip_likelihood (const Analyzer::Expr *expr)
 
Analyzer::ExpressionPtr rewrite_array_elements (Analyzer::Expr const *expr)
 
Analyzer::ExpressionPtr rewrite_expr (const Analyzer::Expr *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 *expr)
 

Variables

static const
std::unordered_set
< std::string > 
anonymous_namespace{ExpressionRewrite.cpp}::overlaps_supported_functions
 
static const
std::unordered_set
< std::string > 
anonymous_namespace{ExpressionRewrite.cpp}::requires_many_to_many
 

Function Documentation

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

Definition at line 938 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::translateDatePlusMinus(), RelAlgTranslator::translateGeoComparison(), RelAlgTranslator::translateGeoFunctionArg(), and RelAlgTranslator::translateTernaryGeoFunction().

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

Analyzer::ExpressionPtr rewrite_array_elements ( Analyzer::Expr const *  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:

Analyzer::ExpressionPtr rewrite_expr ( const Analyzer::Expr expr)

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

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

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

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

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

Referenced by create_count_all_execution_unit().

916  {
918  return quals;
919  }
920 
921  if (join_quals.empty()) {
922  return quals;
923  }
924 
925  std::list<std::shared_ptr<Analyzer::Expr>> quals_to_return;
926 
927  JoinCoveredQualVisitor visitor(join_quals);
928  for (const auto& qual : quals) {
929  if (!visitor.visit(qual.get())) {
930  // Not a covered qual, don't elide it from the filtered count
931  quals_to_return.push_back(qual);
932  }
933  }
934 
935  return quals_to_return;
936 }
bool g_strip_join_covered_quals
Definition: Execute.cpp:95

+ Here is the call graph for this function:

+ Here is the caller graph for this function: