OmniSciDB  16c4e035a1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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,
57  static_assert(std::is_trivially_destructible_v<decltype(handlers)>);
58  // Will throw std::bad_typeid if rel_alg_node == nullptr.
59  auto const& type_index = std::type_index(typeid(*rel_alg_node));
60  auto const itr = std::lower_bound(handlers.cbegin(), handlers.cend(), type_index);
61  if (itr != handlers.cend() && itr->type_index == type_index) {
62  (this->*itr->handler)(rel_alg_node);
63  } else {
64  LOG(FATAL) << "Unhandled RelAlgNode type: " << rel_alg_node->toString();
65  }
66  for (size_t i = 0; i < rel_alg_node->inputCount(); ++i) {
67  visit(rel_alg_node->getInput(i));
68  }
69 }
70 
71 void RelRexDagVisitor::visit(RelCompound const* rel_compound) {
72  if (rel_compound->getFilterExpr()) {
73  visit(rel_compound->getFilterExpr());
74  }
75  for (size_t i = 0; i < rel_compound->getScalarSourcesSize(); ++i) {
76  visit(rel_compound->getScalarSource(i));
77  }
78 }
79 
80 void RelRexDagVisitor::visit(RelFilter const* rel_filter) {
81  visit(rel_filter->getCondition());
82 }
83 
84 void RelRexDagVisitor::visit(RelJoin const* rel_join) {
85  visit(rel_join->getCondition());
86 }
87 
88 void RelRexDagVisitor::visit(RelLeftDeepInnerJoin const* rel_left_deep_inner_join) {
89  visit(rel_left_deep_inner_join->getInnerCondition());
90  for (size_t level = 1; level < rel_left_deep_inner_join->inputCount(); ++level) {
91  if (auto* outer_condition = rel_left_deep_inner_join->getOuterCondition(level)) {
92  visit(outer_condition);
93  }
94  }
95 }
96 
97 void RelRexDagVisitor::visit(RelLogicalValues const* rel_logical_values) {
98  for (size_t row_idx = 0; row_idx < rel_logical_values->getNumRows(); ++row_idx) {
99  for (size_t col_idx = 0; col_idx < rel_logical_values->getRowsSize(); ++col_idx) {
100  visit(rel_logical_values->getValueAt(row_idx, col_idx));
101  }
102  }
103 }
104 
105 void RelRexDagVisitor::visit(RelProject const* rel_projection) {
106  for (size_t i = 0; i < rel_projection->size(); ++i) {
107  visit(rel_projection->getProjectAt(i));
108  }
109 }
110 
111 void RelRexDagVisitor::visit(RelTableFunction const* rel_table_function) {
112  for (size_t i = 0; i < rel_table_function->getTableFuncInputsSize(); ++i) {
113  visit(rel_table_function->getTableFuncInputAt(i));
114  }
115 }
116 
117 void RelRexDagVisitor::visit(RelTranslatedJoin const* rel_translated_join) {
118  visit(rel_translated_join->getLHS());
119  visit(rel_translated_join->getRHS());
120  if (auto* outer_join_condition = rel_translated_join->getOuterJoinCond()) {
121  visit(outer_join_condition);
122  }
123 }
124 
125 // RexScalar types
126 
127 void RelRexDagVisitor::visit(RexScalar const* rex_scalar) {
128  // Array that pairs std::type_index(typeid(*rex_scalar)) -> method pointer.
129  static auto const handlers = make_handlers<RexScalar,
131  RexCase,
133  RexInput,
134  RexLiteral,
135  RexOperator,
136  RexRef,
137  RexSubQuery,
139  static_assert(std::is_trivially_destructible_v<decltype(handlers)>);
140  // Will throw std::bad_typeid if rex_scalar == nullptr.
141  auto const& type_index = std::type_index(typeid(*rex_scalar));
142  auto const itr = std::lower_bound(handlers.cbegin(), handlers.cend(), type_index);
143  if (itr != handlers.cend() && itr->type_index == type_index) {
144  (this->*itr->handler)(rex_scalar);
145  } else {
146  LOG(FATAL) << "Unhandled RexScalar type: " << rex_scalar->toString();
147  }
148 }
149 
151  RexWindowFunctionOperator const* rex_window_function_operator) {
152  for (const auto& partition_key : rex_window_function_operator->getPartitionKeys()) {
153  visit(partition_key.get());
154  }
155  for (const auto& order_key : rex_window_function_operator->getOrderKeys()) {
156  visit(order_key.get());
157  }
158 }
159 
160 void RelRexDagVisitor::visit(RexCase const* rex_case) {
161  for (size_t i = 0; i < rex_case->branchCount(); ++i) {
162  visit(rex_case->getWhen(i));
163  visit(rex_case->getThen(i));
164  }
165  if (rex_case->getElse()) {
166  visit(rex_case->getElse());
167  }
168 }
169 
170 void RelRexDagVisitor::visit(RexFunctionOperator const* rex_function_operator) {
171  for (size_t i = 0; i < rex_function_operator->size(); ++i) {
172  visit(rex_function_operator->getOperand(i));
173  }
174 }
175 
176 void RelRexDagVisitor::visit(RexOperator const* rex_operator) {
177  for (size_t i = 0; i < rex_operator->size(); ++i) {
178  visit(rex_operator->getOperand(i));
179  }
180 }
181 
182 void RelRexDagVisitor::visit(RexSubQuery const* rex_sub_query) {
183  visit(rex_sub_query->getRelAlg());
184 }
185 
186 void RelRexDagVisitor::visit(RexInput const* rex_input) {
187  visit(rex_input->getSourceNode());
188 }
const RexScalar * getThen(const size_t idx) const
void visit(RelAlgNode const *)
size_t size() const override
const RexScalar * getFilterExpr() const
const RexScalar * getElse() const
const RexScalar * getOuterCondition(const size_t nesting_level) const
#define LOG(tag)
Definition: Logger.h:205
size_t size() const
const RexScalar * getOperand(const size_t idx) const
size_t getNumRows() const
const RexScalar * getCondition() const
DEVICE void sort(ARGS &&...args)
Definition: gpu_enabled.h:105
const RexScalar * getWhen(const size_t idx) const
const RexScalar * getCondition() const
std::array< TypeHandler< RelRexDagVisitor, T >, N > Handlers
size_t getRowsSize() const
const size_t getScalarSourcesSize() const
const RelAlgNode * getRHS() const
static Handlers< T, sizeof...(Ts)> make_handlers()
size_t branchCount() const
const RelAlgNode * getInput(const size_t idx) const
size_t getTableFuncInputsSize() const
const RexScalar * getProjectAt(const size_t idx) const
const ConstRexScalarPtrVector & getPartitionKeys() const
const RelAlgNode * getLHS() const
DEVICE auto lower_bound(ARGS &&...args)
Definition: gpu_enabled.h:78
const RexScalar * getOuterJoinCond() const
const RelAlgNode * getSourceNode() const
const RexScalar * getTableFuncInputAt(const size_t idx) const
virtual std::string toString() const =0
const RexScalar * getValueAt(const size_t row_idx, const size_t col_idx) const
const RexScalar * getInnerCondition() const
virtual std::string toString() const =0
const ConstRexScalarPtrVector & getOrderKeys() const
const size_t inputCount() const
const RexScalar * getScalarSource(const size_t i) const