OmniSciDB  2e3a973ef4
RelRexDagVisitor.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 OmniSci, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  * Q: Why are std::arrays used, instead of std::unordered_maps to match type_index to
19  * their handlers?
20  *
21  * A: Since they are static variables, they should be trivially destructible. See
22  * https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables
23  */
24 
25 #include "RelRexDagVisitor.h"
26 #include "Logger/Logger.h"
27 
28 #include <algorithm>
29 #include <typeindex>
30 
31 template <typename T, typename... Ts>
33  RelRexDagVisitor::Handlers<T, sizeof...(Ts)> handlers{
34  {{std::type_index(typeid(Ts)), &RelRexDagVisitor::cast<T, Ts>}...}};
35  std::sort(handlers.begin(), handlers.end());
36  return handlers;
37 }
38 
39 // RelAlgNode types
40 
41 void RelRexDagVisitor::visit(RelAlgNode const* rel_alg_node) {
42  // Array that pairs std::type_index(typeid(*rel_alg_node)) -> method pointer.
43  static auto const handlers = make_handlers<RelAlgNode,
46  RelFilter,
47  RelJoin,
51  RelModify,
52  RelProject,
53  RelScan,
54  RelSort,
56  static_assert(std::is_trivially_destructible_v<decltype(handlers)>);
57  // Will throw std::bad_typeid if rel_alg_node == nullptr.
58  auto const& type_index = std::type_index(typeid(*rel_alg_node));
59  auto const itr = std::lower_bound(handlers.cbegin(), handlers.cend(), type_index);
60  if (itr != handlers.cend() && itr->type_index == type_index) {
61  (this->*itr->handler)(rel_alg_node);
62  } else {
63  LOG(FATAL) << "Unhandled RelAlgNode type: " << rel_alg_node->toString();
64  }
65  for (size_t i = 0; i < rel_alg_node->inputCount(); ++i) {
66  visit(rel_alg_node->getInput(i));
67  }
68 }
69 
70 void RelRexDagVisitor::visit(RelCompound const* rel_compound) {
71  if (rel_compound->getFilterExpr()) {
72  visit(rel_compound->getFilterExpr());
73  }
74 }
75 
76 void RelRexDagVisitor::visit(RelFilter const* rel_filter) {
77  visit(rel_filter->getCondition());
78 }
79 
80 void RelRexDagVisitor::visit(RelJoin const* rel_join) {
81  visit(rel_join->getCondition());
82 }
83 
84 void RelRexDagVisitor::visit(RelLeftDeepInnerJoin const* rel_left_deep_inner_join) {
85  visit(rel_left_deep_inner_join->getInnerCondition());
86  for (size_t level = 1; level < rel_left_deep_inner_join->inputCount(); ++level) {
87  if (auto* outer_condition = rel_left_deep_inner_join->getOuterCondition(level)) {
88  visit(outer_condition);
89  }
90  }
91 }
92 
93 void RelRexDagVisitor::visit(RelLogicalValues const* rel_logical_values) {
94  for (size_t row_idx = 0; row_idx < rel_logical_values->getNumRows(); ++row_idx) {
95  for (size_t col_idx = 0; col_idx < rel_logical_values->getRowsSize(); ++col_idx) {
96  visit(rel_logical_values->getValueAt(row_idx, col_idx));
97  }
98  }
99 }
100 
101 void RelRexDagVisitor::visit(RelProject const* rel_projection) {
102  for (size_t i = 0; i < rel_projection->size(); ++i) {
103  visit(rel_projection->getProjectAt(i));
104  }
105 }
106 
107 void RelRexDagVisitor::visit(RelTableFunction const* rel_table_function) {
108  for (size_t i = 0; i < rel_table_function->getTableFuncInputsSize(); ++i) {
109  visit(rel_table_function->getTableFuncInputAt(i));
110  }
111 }
112 
113 // RexScalar types
114 
115 void RelRexDagVisitor::visit(RexScalar const* rex_scalar) {
116  // Array that pairs std::type_index(typeid(*rex_scalar)) -> method pointer.
117  static auto const handlers = make_handlers<RexScalar,
119  RexCase,
121  RexInput,
122  RexLiteral,
123  RexOperator,
124  RexRef,
125  RexSubQuery,
127  static_assert(std::is_trivially_destructible_v<decltype(handlers)>);
128  // Will throw std::bad_typeid if rex_scalar == nullptr.
129  auto const& type_index = std::type_index(typeid(*rex_scalar));
130  auto const itr = std::lower_bound(handlers.cbegin(), handlers.cend(), type_index);
131  if (itr != handlers.cend() && itr->type_index == type_index) {
132  (this->*itr->handler)(rex_scalar);
133  } else {
134  LOG(FATAL) << "Unhandled RexScalar type: " << rex_scalar->toString();
135  }
136 }
137 
139  RexWindowFunctionOperator const* rex_window_function_operator) {
140  for (const auto& partition_key : rex_window_function_operator->getPartitionKeys()) {
141  visit(partition_key.get());
142  }
143  for (const auto& order_key : rex_window_function_operator->getOrderKeys()) {
144  visit(order_key.get());
145  }
146 }
147 
148 void RelRexDagVisitor::visit(RexCase const* rex_case) {
149  for (size_t i = 0; i < rex_case->branchCount(); ++i) {
150  visit(rex_case->getWhen(i));
151  visit(rex_case->getThen(i));
152  }
153  if (rex_case->getElse()) {
154  visit(rex_case->getElse());
155  }
156 }
157 
158 void RelRexDagVisitor::visit(RexFunctionOperator const* rex_function_operator) {
159  for (size_t i = 0; i < rex_function_operator->size(); ++i) {
160  visit(rex_function_operator->getOperand(i));
161  }
162 }
163 
164 void RelRexDagVisitor::visit(RexOperator const* rex_operator) {
165  for (size_t i = 0; i < rex_operator->size(); ++i) {
166  visit(rex_operator->getOperand(i));
167  }
168 }
169 
170 void RelRexDagVisitor::visit(RexSubQuery const* rex_sub_query) {
171  visit(rex_sub_query->getRelAlg());
172 }
const ConstRexScalarPtrVector & getPartitionKeys() const
void visit(RelAlgNode const *)
size_t size() const override
#define LOG(tag)
Definition: Logger.h:188
const RexScalar * getProjectAt(const size_t idx) const
const ConstRexScalarPtrVector & getOrderKeys() const
size_t branchCount() const
const RexScalar * getTableFuncInputAt(const size_t idx) const
std::array< TypeHandler< RelRexDagVisitor, T >, N > Handlers
const RexScalar * getCondition() const
const RexScalar * getWhen(const size_t idx) const
const RexScalar * getThen(const size_t idx) const
static Handlers< T, sizeof...(Ts)> make_handlers()
const RexScalar * getInnerCondition() const
const RexScalar * getOperand(const size_t idx) const
const RexScalar * getOuterCondition(const size_t nesting_level) const
const RexScalar * getCondition() const
const size_t inputCount() const
virtual std::string toString() const =0
const RexScalar * getValueAt(const size_t row_idx, const size_t col_idx) const
const RexScalar * getFilterExpr() const
virtual std::string toString() const =0
const RelAlgNode * getInput(const size_t idx) const
size_t getRowsSize() const
size_t getTableFuncInputsSize() const
size_t getNumRows() const
size_t size() const
const RexScalar * getElse() const