OmniSciDB  04ee39c94c
RelAlgVisitor.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, 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 #ifndef QUERYENGINE_RELALGVISITOR_H
18 #define QUERYENGINE_RELALGVISITOR_H
19 
21 
22 template <class T>
24  public:
25  T visit(const RelAlgNode* rel_alg) const {
26  auto result = defaultResult();
27  for (size_t i = 0; i < rel_alg->inputCount(); ++i) {
28  result = aggregateResult(result, visit(rel_alg->getInput(i)));
29  }
30  const auto aggregate = dynamic_cast<const RelAggregate*>(rel_alg);
31  if (aggregate) {
32  return aggregateResult(result, visitAggregate(aggregate));
33  }
34  const auto compound = dynamic_cast<const RelCompound*>(rel_alg);
35  if (compound) {
36  return aggregateResult(result, visitCompound(compound));
37  }
38  const auto filter = dynamic_cast<const RelFilter*>(rel_alg);
39  if (filter) {
40  return aggregateResult(result, visitFilter(filter));
41  }
42  const auto join = dynamic_cast<const RelJoin*>(rel_alg);
43  if (join) {
45  }
46  const auto left_deep_inner_join = dynamic_cast<const RelLeftDeepInnerJoin*>(rel_alg);
47  if (left_deep_inner_join) {
48  return aggregateResult(result, visitLeftDeepInnerJoin(left_deep_inner_join));
49  }
50  const auto project = dynamic_cast<const RelProject*>(rel_alg);
51  if (project) {
52  return aggregateResult(result, visitProject(project));
53  }
54  const auto scan = dynamic_cast<const RelScan*>(rel_alg);
55  if (scan) {
56  return aggregateResult(result, visitScan(scan));
57  }
58  const auto sort = dynamic_cast<const RelSort*>(rel_alg);
59  if (sort) {
60  return aggregateResult(result, visitSort(sort));
61  }
62  const auto logical_values = dynamic_cast<const RelLogicalValues*>(rel_alg);
63  if (logical_values) {
64  return aggregateResult(result, visitLogicalValues(logical_values));
65  }
66  const auto modify = dynamic_cast<const RelModify*>(rel_alg);
67  if (modify) {
68  return aggregateResult(result, visitModify(modify));
69  }
70  CHECK(false);
71  return defaultResult();
72  }
73 
74  virtual T visitAggregate(const RelAggregate*) const { return defaultResult(); }
75 
76  virtual T visitCompound(const RelCompound*) const { return defaultResult(); }
77 
78  virtual T visitFilter(const RelFilter*) const { return defaultResult(); }
79 
80  virtual T visitJoin(const RelJoin*) const { return defaultResult(); }
81 
82  virtual T visitLeftDeepInnerJoin(const RelLeftDeepInnerJoin*) const {
83  return defaultResult();
84  }
85 
86  virtual T visitProject(const RelProject*) const { return defaultResult(); }
87 
88  virtual T visitScan(const RelScan*) const { return defaultResult(); }
89 
90  virtual T visitSort(const RelSort*) const { return defaultResult(); }
91 
92  virtual T visitLogicalValues(const RelLogicalValues*) const { return defaultResult(); }
93 
94  virtual T visitModify(const RelModify*) const { return defaultResult(); }
95 
96  protected:
97  virtual T aggregateResult(const T& aggregate, const T& next_result) const {
98  return next_result;
99  }
100 
101  virtual T defaultResult() const { return T{}; }
102 };
103 
104 #endif // QUERYENGINE_RELALGVISITOR_H
virtual T visitScan(const RelScan *) const
Definition: RelAlgVisitor.h:88
T visit(const RelAlgNode *rel_alg) const
Definition: RelAlgVisitor.h:25
std::string join(T const &container, std::string const &delim)
virtual T aggregateResult(const T &aggregate, const T &next_result) const
Definition: RelAlgVisitor.h:97
virtual T visitProject(const RelProject *) const
Definition: RelAlgVisitor.h:86
virtual T visitJoin(const RelJoin *) const
Definition: RelAlgVisitor.h:80
virtual T visitModify(const RelModify *) const
Definition: RelAlgVisitor.h:94
virtual T visitCompound(const RelCompound *) const
Definition: RelAlgVisitor.h:76
virtual T visitAggregate(const RelAggregate *) const
Definition: RelAlgVisitor.h:74
virtual T visitLeftDeepInnerJoin(const RelLeftDeepInnerJoin *) const
Definition: RelAlgVisitor.h:82
virtual T visitFilter(const RelFilter *) const
Definition: RelAlgVisitor.h:78
const size_t inputCount() const
virtual T defaultResult() const
virtual T visitLogicalValues(const RelLogicalValues *) const
Definition: RelAlgVisitor.h:92
#define CHECK(condition)
Definition: Logger.h:187
const RelAlgNode * getInput(const size_t idx) const
virtual T visitSort(const RelSort *) const
Definition: RelAlgVisitor.h:90