OmniSciDB  04ee39c94c
WindowExpressionRewrite.cpp File Reference
+ Include dependency graph for WindowExpressionRewrite.cpp:

Go to the source code of this file.

Namespaces

 anonymous_namespace{WindowExpressionRewrite.cpp}
 

Functions

bool anonymous_namespace{WindowExpressionRewrite.cpp}::matches_else_null (const Analyzer::CaseExpr *case_expr)
 
bool anonymous_namespace{WindowExpressionRewrite.cpp}::matches_gt_bigint_zero (const Analyzer::BinOper *window_gt_zero)
 
bool anonymous_namespace{WindowExpressionRewrite.cpp}::window_sum_and_count_match (const Analyzer::WindowFunction *sum_window_expr, const Analyzer::WindowFunction *count_window_expr)
 
bool anonymous_namespace{WindowExpressionRewrite.cpp}::is_sum_kind (const SqlWindowFunctionKind kind)
 
std::shared_ptr< Analyzer::WindowFunctionrewrite_sum_window (const Analyzer::Expr *expr)
 
std::shared_ptr< Analyzer::WindowFunctionrewrite_avg_window (const Analyzer::Expr *expr)
 

Function Documentation

◆ rewrite_avg_window()

std::shared_ptr<Analyzer::WindowFunction> rewrite_avg_window ( const Analyzer::Expr expr)

Definition at line 97 of file WindowExpressionRewrite.cpp.

References AVG, CHECK_EQ, COUNT, expr_list_match(), kBIGINT, kCAST, kDIVIDE, kDOUBLE, and rewrite_sum_window().

Referenced by rewrite_expr().

97  {
98  const auto cast_expr = dynamic_cast<const Analyzer::UOper*>(expr);
99  const auto div_expr = dynamic_cast<const Analyzer::BinOper*>(
100  cast_expr && cast_expr->get_optype() == kCAST ? cast_expr->get_operand() : expr);
101  if (!div_expr || div_expr->get_optype() != kDIVIDE) {
102  return nullptr;
103  }
104  const auto sum_window_expr = rewrite_sum_window(div_expr->get_left_operand());
105  if (!sum_window_expr) {
106  return nullptr;
107  }
108  const auto cast_count_window =
109  dynamic_cast<const Analyzer::UOper*>(div_expr->get_right_operand());
110  if (cast_count_window && cast_count_window->get_optype() != kCAST) {
111  return nullptr;
112  }
113  const auto count_window = dynamic_cast<const Analyzer::WindowFunction*>(
114  cast_count_window ? cast_count_window->get_operand()
115  : div_expr->get_right_operand());
116  if (!count_window || count_window->getKind() != SqlWindowFunctionKind::COUNT) {
117  return nullptr;
118  }
119  CHECK_EQ(count_window->get_type_info().get_type(), kBIGINT);
120  if (cast_count_window && cast_count_window->get_type_info().get_type() !=
121  sum_window_expr->get_type_info().get_type()) {
122  return nullptr;
123  }
124  if (!expr_list_match(sum_window_expr.get()->getArgs(), count_window->getArgs())) {
125  return nullptr;
126  }
127  return makeExpr<Analyzer::WindowFunction>(SQLTypeInfo(kDOUBLE),
129  sum_window_expr->getArgs(),
130  sum_window_expr->getPartitionKeys(),
131  sum_window_expr->getOrderKeys(),
132  sum_window_expr->getCollation());
133 }
#define CHECK_EQ(x, y)
Definition: Logger.h:195
std::shared_ptr< Analyzer::WindowFunction > rewrite_sum_window(const Analyzer::Expr *expr)
Definition: sqldefs.h:49
bool expr_list_match(const std::vector< std::shared_ptr< Analyzer::Expr >> &lhs, const std::vector< std::shared_ptr< Analyzer::Expr >> &rhs)
Definition: Analyzer.cpp:2980
SQLTypeInfoCore< ArrayContextTypeSizer, ExecutorTypePackaging, DateTimeFacilities > SQLTypeInfo
Definition: sqltypes.h:823
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ rewrite_sum_window()

std::shared_ptr<Analyzer::WindowFunction> rewrite_sum_window ( const Analyzer::Expr expr)

Definition at line 54 of file WindowExpressionRewrite.cpp.

References CHECK, COUNT, Analyzer::CaseExpr::get_expr_pair_list(), anonymous_namespace{WindowExpressionRewrite.cpp}::is_sum_kind(), kBIGINT, anonymous_namespace{WindowExpressionRewrite.cpp}::matches_else_null(), anonymous_namespace{WindowExpressionRewrite.cpp}::matches_gt_bigint_zero(), remove_cast(), SUM, and anonymous_namespace{WindowExpressionRewrite.cpp}::window_sum_and_count_match().

Referenced by rewrite_avg_window(), and rewrite_expr().

54  {
55  const auto case_expr = dynamic_cast<const Analyzer::CaseExpr*>(expr);
56  if (!case_expr || !matches_else_null(case_expr)) {
57  return nullptr;
58  }
59  const auto& expr_pair_list = case_expr->get_expr_pair_list();
60  if (expr_pair_list.size() != 1) {
61  return nullptr;
62  }
63  const auto& expr_pair = expr_pair_list.front();
64  const auto window_gt_zero =
65  dynamic_cast<const Analyzer::BinOper*>(expr_pair.first.get());
66  if (!window_gt_zero || !matches_gt_bigint_zero(window_gt_zero)) {
67  return nullptr;
68  }
69  const auto sum_window_expr =
70  std::dynamic_pointer_cast<Analyzer::WindowFunction>(remove_cast(expr_pair.second));
71  if (!sum_window_expr || !is_sum_kind(sum_window_expr->getKind())) {
72  return nullptr;
73  }
74  const auto count_window_expr =
75  std::dynamic_pointer_cast<const Analyzer::WindowFunction>(
76  remove_cast(window_gt_zero->get_own_left_operand()));
77  if (!count_window_expr ||
78  count_window_expr->getKind() != SqlWindowFunctionKind::COUNT) {
79  return nullptr;
80  }
81  if (!window_sum_and_count_match(sum_window_expr.get(), count_window_expr.get())) {
82  return nullptr;
83  }
84  CHECK(sum_window_expr);
85  auto sum_ti = sum_window_expr->get_type_info();
86  if (sum_ti.is_integer()) {
87  sum_ti = SQLTypeInfo(kBIGINT, sum_ti.get_notnull());
88  }
89  return makeExpr<Analyzer::WindowFunction>(sum_ti,
91  sum_window_expr->getArgs(),
92  sum_window_expr->getPartitionKeys(),
93  sum_window_expr->getOrderKeys(),
94  sum_window_expr->getCollation());
95 }
bool matches_gt_bigint_zero(const Analyzer::BinOper *window_gt_zero)
std::shared_ptr< Analyzer::Expr > remove_cast(const std::shared_ptr< Analyzer::Expr > &expr)
Definition: Analyzer.cpp:2993
bool window_sum_and_count_match(const Analyzer::WindowFunction *sum_window_expr, const Analyzer::WindowFunction *count_window_expr)
bool matches_else_null(const Analyzer::CaseExpr *case_expr)
const std::list< std::pair< std::shared_ptr< Analyzer::Expr >, std::shared_ptr< Analyzer::Expr > > > & get_expr_pair_list() const
Definition: Analyzer.h:1041
bool is_sum_kind(const SqlWindowFunctionKind kind)
SQLTypeInfoCore< ArrayContextTypeSizer, ExecutorTypePackaging, DateTimeFacilities > SQLTypeInfo
Definition: sqltypes.h:823
#define CHECK(condition)
Definition: Logger.h:187
+ Here is the call graph for this function:
+ Here is the caller graph for this function: