OmniSciDB  cde582ebc3
 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 2022 HEAVY.AI, 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 void RelRexDagVisitor::castAndVisit(RelAlgNode const* rel_alg_node) {
41  // Array that pairs std::type_index(typeid(*rel_alg_node)) -> method pointer.
42  static auto const handlers = make_handlers<RelAlgNode,
45  RelFilter,
46  RelJoin,
50  RelModify,
51  RelProject,
52  RelScan,
53  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: "
64  << rel_alg_node->toString(RelRexToStringConfig::defaults());
65  }
66 }
67 
68 void RelRexDagVisitor::visit(RelAlgNode const* rel_alg_node) {
69  castAndVisit(rel_alg_node);
70  for (size_t i = 0; i < rel_alg_node->inputCount(); ++i) {
71  visit(rel_alg_node->getInput(i));
72  }
73 }
74 
75 void RelRexDagVisitor::visit(RelCompound const* rel_compound) {
76  if (rel_compound->getFilterExpr()) {
77  visit(rel_compound->getFilterExpr());
78  }
79  for (size_t i = 0; i < rel_compound->getScalarSourcesSize(); ++i) {
80  visit(rel_compound->getScalarSource(i));
81  }
82 }
83 
84 void RelRexDagVisitor::visit(RelFilter const* rel_filter) {
85  visit(rel_filter->getCondition());
86 }
87 
88 void RelRexDagVisitor::visit(RelJoin const* rel_join) {
89  visit(rel_join->getCondition());
90 }
91 
92 void RelRexDagVisitor::visit(RelLeftDeepInnerJoin const* rel_left_deep_inner_join) {
93  visit(rel_left_deep_inner_join->getInnerCondition());
94  for (size_t level = 1; level < rel_left_deep_inner_join->inputCount(); ++level) {
95  if (auto* outer_condition = rel_left_deep_inner_join->getOuterCondition(level)) {
96  visit(outer_condition);
97  }
98  }
99 }
100 
101 void RelRexDagVisitor::visit(RelLogicalValues const* rel_logical_values) {
102  for (size_t row_idx = 0; row_idx < rel_logical_values->getNumRows(); ++row_idx) {
103  for (size_t col_idx = 0; col_idx < rel_logical_values->getRowsSize(); ++col_idx) {
104  visit(rel_logical_values->getValueAt(row_idx, col_idx));
105  }
106  }
107 }
108 
109 void RelRexDagVisitor::visit(RelProject const* rel_projection) {
110  for (size_t i = 0; i < rel_projection->size(); ++i) {
111  visit(rel_projection->getProjectAt(i));
112  }
113 }
114 
115 void RelRexDagVisitor::visit(RelTableFunction const* rel_table_function) {
116  for (size_t i = 0; i < rel_table_function->getTableFuncInputsSize(); ++i) {
117  visit(rel_table_function->getTableFuncInputAt(i));
118  }
119 }
120 
121 void RelRexDagVisitor::visit(RelTranslatedJoin const* rel_translated_join) {
122  visit(rel_translated_join->getLHS());
123  visit(rel_translated_join->getRHS());
124  if (auto* outer_join_condition = rel_translated_join->getOuterJoinCond()) {
125  visit(outer_join_condition);
126  }
127 }
128 
129 // RexScalar types
130 void RelRexDagVisitor::visit(RexScalar const* rex_scalar) {
131  // Array that pairs std::type_index(typeid(*rex_scalar)) -> method pointer.
132  static auto const handlers = make_handlers<RexScalar,
134  RexCase,
136  RexInput,
137  RexLiteral,
138  RexOperator,
139  RexRef,
140  RexSubQuery,
142  static_assert(std::is_trivially_destructible_v<decltype(handlers)>);
143  // Will throw std::bad_typeid if rex_scalar == nullptr.
144  auto const& type_index = std::type_index(typeid(*rex_scalar));
145  auto const itr = std::lower_bound(handlers.cbegin(), handlers.cend(), type_index);
146  if (itr != handlers.cend() && itr->type_index == type_index) {
147  (this->*itr->handler)(rex_scalar);
148  } else {
149  LOG(FATAL) << "Unhandled RexScalar type: "
150  << rex_scalar->toString(RelRexToStringConfig::defaults());
151  }
152 }
153 
155  RexWindowFunctionOperator const* rex_window_function_operator) {
156  for (const auto& partition_key : rex_window_function_operator->getPartitionKeys()) {
157  visit(partition_key.get());
158  }
159  for (const auto& order_key : rex_window_function_operator->getOrderKeys()) {
160  visit(order_key.get());
161  }
162 }
163 
164 void RelRexDagVisitor::visit(RexCase const* rex_case) {
165  for (size_t i = 0; i < rex_case->branchCount(); ++i) {
166  visit(rex_case->getWhen(i));
167  visit(rex_case->getThen(i));
168  }
169  if (rex_case->getElse()) {
170  visit(rex_case->getElse());
171  }
172 }
173 
174 void RelRexDagVisitor::visit(RexFunctionOperator const* rex_function_operator) {
175  for (size_t i = 0; i < rex_function_operator->size(); ++i) {
176  visit(rex_function_operator->getOperand(i));
177  }
178 }
179 
180 void RelRexDagVisitor::visit(RexOperator const* rex_operator) {
181  for (size_t i = 0; i < rex_operator->size(); ++i) {
182  visit(rex_operator->getOperand(i));
183  }
184 }
185 
186 void RelRexDagVisitor::visit(RexSubQuery const* rex_sub_query) {
187  visit(rex_sub_query->getRelAlg());
188 }
189 
190 void RelRexDagVisitor::visit(RexInput const* rex_input) {
191  visit(rex_input->getSourceNode());
192 }
virtual std::string toString(RelRexToStringConfig config) const =0
const RexScalar * getThen(const size_t idx) const
Definition: RelAlgDag.h:400
virtual void visit(RelAlgNode const *)
size_t size() const override
Definition: RelAlgDag.h:1068
const RexScalar * getFilterExpr() const
Definition: RelAlgDag.h:1673
const RexScalar * getElse() const
Definition: RelAlgDag.h:405
const RexScalar * getOuterCondition(const size_t nesting_level) const
#define LOG(tag)
Definition: Logger.h:216
size_t size() const
Definition: RelAlgDag.h:245
const RexScalar * getOperand(const size_t idx) const
Definition: RelAlgDag.h:247
size_t getNumRows() const
Definition: RelAlgDag.h:2223
const RexScalar * getCondition() const
Definition: RelAlgDag.h:1539
DEVICE void sort(ARGS &&...args)
Definition: gpu_enabled.h:105
const RexScalar * getWhen(const size_t idx) const
Definition: RelAlgDag.h:395
const RexScalar * getCondition() const
Definition: RelAlgDag.h:1320
std::array< TypeHandler< RelRexDagVisitor, T >, N > Handlers
size_t getRowsSize() const
Definition: RelAlgDag.h:2215
const size_t getScalarSourcesSize() const
Definition: RelAlgDag.h:1687
const RelAlgNode * getRHS() const
Definition: RelAlgDag.h:1472
virtual std::string toString(RelRexToStringConfig config) const =0
static Handlers< T, sizeof...(Ts)> make_handlers()
size_t branchCount() const
Definition: RelAlgDag.h:393
const RelAlgNode * getInput(const size_t idx) const
Definition: RelAlgDag.h:826
size_t getTableFuncInputsSize() const
Definition: RelAlgDag.h:2079
const RexScalar * getProjectAt(const size_t idx) const
Definition: RelAlgDag.h:1070
const ConstRexScalarPtrVector & getPartitionKeys() const
Definition: RelAlgDag.h:573
const RelAlgNode * getLHS() const
Definition: RelAlgDag.h:1471
DEVICE auto lower_bound(ARGS &&...args)
Definition: gpu_enabled.h:78
const RexScalar * getOuterJoinCond() const
Definition: RelAlgDag.h:1477
static RelRexToStringConfig defaults()
Definition: RelAlgDag.h:49
const RelAlgNode * getSourceNode() const
Definition: RelAlgDag.h:351
void castAndVisit(RelAlgNode const *)
const RexScalar * getTableFuncInputAt(const size_t idx) const
Definition: RelAlgDag.h:2085
const RexScalar * getValueAt(const size_t row_idx, const size_t col_idx) const
Definition: RelAlgDag.h:2208
const RexScalar * getInnerCondition() const
const ConstRexScalarPtrVector & getOrderKeys() const
Definition: RelAlgDag.h:583
const size_t inputCount() const
Definition: RelAlgDag.h:824
const RexScalar * getScalarSource(const size_t i) const
Definition: RelAlgDag.h:1689