OmniSciDB  29e35f4d58
ExpressionRewrite.h File Reference
#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< OverlapsJoinConjunctionrewrite_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 *)
 

Function Documentation

◆ fold_expr()

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

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

839  {
840  if (!expr) {
841  return nullptr;
842  }
843  const auto expr_no_likelihood = strip_likelihood(expr);
844  ConstantFoldingVisitor visitor;
845  auto rewritten_expr = visitor.visit(expr_no_likelihood);
846  if (visitor.get_num_overflows() > 0 && rewritten_expr->get_type_info().is_integer() &&
847  rewritten_expr->get_type_info().get_type() != kBIGINT) {
848  auto rewritten_expr_const =
849  std::dynamic_pointer_cast<const Analyzer::Constant>(rewritten_expr);
850  if (!rewritten_expr_const) {
851  // Integer expression didn't fold completely the first time due to
852  // overflows in smaller type subexpressions, trying again with a cast
853  const auto& ti = SQLTypeInfo(kBIGINT, false);
854  auto bigint_expr_no_likelihood = expr_no_likelihood->deep_copy()->add_cast(ti);
855  auto rewritten_expr_take2 = visitor.visit(bigint_expr_no_likelihood.get());
856  auto rewritten_expr_take2_const =
857  std::dynamic_pointer_cast<Analyzer::Constant>(rewritten_expr_take2);
858  if (rewritten_expr_take2_const) {
859  // Managed to fold, switch to the new constant
860  rewritten_expr = rewritten_expr_take2_const;
861  }
862  }
863  }
864  const auto expr_with_likelihood = dynamic_cast<const Analyzer::LikelihoodExpr*>(expr);
865  if (expr_with_likelihood) {
866  // Add back likelihood
867  return std::make_shared<Analyzer::LikelihoodExpr>(
868  rewritten_expr, expr_with_likelihood->get_likelihood());
869  }
870  return rewritten_expr;
871 }
SQLTypeInfoCore< ArrayContextTypeSizer, ExecutorTypePackaging, DateTimeFacilities > SQLTypeInfo
Definition: sqltypes.h:852
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:

◆ rewrite_array_elements()

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

Definition at line 692 of file ExpressionRewrite.cpp.

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

692  {
693  return ArrayElementStringLiteralEncodingVisitor().visit(expr);
694 }
+ Here is the caller graph for this function:

◆ rewrite_expr()

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

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

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

◆ rewrite_overlaps_conjunction()

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

Definition at line 729 of file ExpressionRewrite.cpp.

References CHECK, CHECK_GE, kBOOLEAN, kONE, kOVERLAPS, LOG, anonymous_namespace{ExpressionRewrite.cpp}::overlaps_supported_functions, ScalarExprVisitor< T >::visit(), and logger::WARNING.

Referenced by QueryRewriter::rewriteOverlapsJoin().

730  {
731  auto func_oper = dynamic_cast<Analyzer::FunctionOper*>(expr.get());
732  if (func_oper) {
733  // TODO(adb): consider converting unordered set to an unordered map, potentially
734  // storing the rewrite function we want to apply in the map
735  if (overlaps_supported_functions.find(func_oper->getName()) !=
737  CHECK_GE(func_oper->getArity(), size_t(3));
738 
739  DeepCopyVisitor deep_copy_visitor;
740  auto lhs = func_oper->getOwnArg(2);
741  auto rewritten_lhs = deep_copy_visitor.visit(lhs.get());
742  CHECK(rewritten_lhs);
743  const auto& lhs_ti = rewritten_lhs->get_type_info();
744  if (!lhs_ti.is_geometry()) {
745  // TODO(adb): If ST_Contains is passed geospatial literals instead of columns, the
746  // function will be expanded during translation rather than during code
747  // generation. While this scenario does not make sense for the overlaps join, we
748  // need to detect and abort the overlaps rewrite. Adding a GeospatialConstant
749  // dervied class to the Analyzer may prove to be a better way to handle geo
750  // literals, but for now we ensure the LHS type is a geospatial type, which would
751  // mean the function has not been expanded to the physical types, yet.
752  LOG(WARNING) << "Failed to rewrite " << func_oper->getName()
753  << " to overlaps conjunction. LHS input type is not a geospatial "
754  "type. Are both inputs geospatial columns?";
755  return boost::none;
756  }
757 
758  // Read the bounds arg from the ST_Contains FuncOper (second argument)instead of the
759  // poly column (first argument)
760  auto rhs = func_oper->getOwnArg(1);
761  auto rewritten_rhs = deep_copy_visitor.visit(rhs.get());
762  CHECK(rewritten_rhs);
763 
764  auto overlaps_oper = makeExpr<Analyzer::BinOper>(
765  kBOOLEAN, kOVERLAPS, kONE, rewritten_lhs, rewritten_rhs);
766 
767  return OverlapsJoinConjunction{{expr}, {overlaps_oper}};
768  }
769  }
770  return boost::none;
771 }
#define LOG(tag)
Definition: Logger.h:188
#define CHECK_GE(x, y)
Definition: Logger.h:206
static const std::unordered_set< std::string > overlaps_supported_functions
Definition: sqldefs.h:69
T visit(const Analyzer::Expr *expr) const
#define CHECK(condition)
Definition: Logger.h:193
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ strip_join_covered_filter_quals()

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

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

Referenced by create_count_all_execution_unit().

817  {
819  return quals;
820  }
821 
822  if (join_quals.empty()) {
823  return quals;
824  }
825 
826  std::list<std::shared_ptr<Analyzer::Expr>> quals_to_return;
827 
828  JoinCoveredQualVisitor visitor(join_quals);
829  for (const auto& qual : quals) {
830  if (!visitor.visit(qual.get())) {
831  // Not a covered qual, don't elide it from the filtered count
832  quals_to_return.push_back(qual);
833  }
834  }
835 
836  return quals_to_return;
837 }
bool g_strip_join_covered_quals
Definition: Execute.cpp:90
+ Here is the call graph for this function:
+ Here is the caller graph for this function: