1 | #include "duckdb/planner/logical_operator_visitor.hpp" |
2 | |
3 | #include "duckdb/planner/expression/list.hpp" |
4 | #include "duckdb/planner/expression_iterator.hpp" |
5 | #include "duckdb/planner/operator/list.hpp" |
6 | |
7 | using namespace duckdb; |
8 | using namespace std; |
9 | |
10 | void LogicalOperatorVisitor::VisitOperator(LogicalOperator &op) { |
11 | VisitOperatorChildren(op); |
12 | VisitOperatorExpressions(op); |
13 | } |
14 | |
15 | void LogicalOperatorVisitor::VisitOperatorChildren(LogicalOperator &op) { |
16 | for (auto &child : op.children) { |
17 | VisitOperator(*child); |
18 | } |
19 | } |
20 | |
21 | void LogicalOperatorVisitor::VisitOperatorExpressions(LogicalOperator &op) { |
22 | switch (op.type) { |
23 | case LogicalOperatorType::EXPRESSION_GET: { |
24 | auto &get = (LogicalExpressionGet &)op; |
25 | for (auto &expr_list : get.expressions) { |
26 | for (auto &expr : expr_list) { |
27 | VisitExpression(&expr); |
28 | } |
29 | } |
30 | break; |
31 | } |
32 | case LogicalOperatorType::ORDER_BY: { |
33 | auto &order = (LogicalOrder &)op; |
34 | for (auto &node : order.orders) { |
35 | VisitExpression(&node.expression); |
36 | } |
37 | break; |
38 | } |
39 | case LogicalOperatorType::TOP_N: { |
40 | auto &order = (LogicalTopN &)op; |
41 | for (auto &node : order.orders) { |
42 | VisitExpression(&node.expression); |
43 | } |
44 | break; |
45 | } |
46 | case LogicalOperatorType::DISTINCT: { |
47 | auto &distinct = (LogicalDistinct &)op; |
48 | for (auto &target : distinct.distinct_targets) { |
49 | VisitExpression(&target); |
50 | } |
51 | break; |
52 | } |
53 | case LogicalOperatorType::DELIM_JOIN: |
54 | case LogicalOperatorType::COMPARISON_JOIN: { |
55 | auto &join = (LogicalComparisonJoin &)op; |
56 | for (auto &cond : join.conditions) { |
57 | VisitExpression(&cond.left); |
58 | VisitExpression(&cond.right); |
59 | } |
60 | break; |
61 | } |
62 | case LogicalOperatorType::ANY_JOIN: { |
63 | auto &join = (LogicalAnyJoin &)op; |
64 | VisitExpression(&join.condition); |
65 | break; |
66 | } |
67 | case LogicalOperatorType::AGGREGATE_AND_GROUP_BY: { |
68 | auto &aggr = (LogicalAggregate &)op; |
69 | for (idx_t i = 0; i < aggr.groups.size(); i++) { |
70 | VisitExpression(&aggr.groups[i]); |
71 | } |
72 | break; |
73 | } |
74 | default: |
75 | break; |
76 | } |
77 | for (idx_t i = 0; i < op.expressions.size(); i++) { |
78 | VisitExpression(&op.expressions[i]); |
79 | } |
80 | } |
81 | |
82 | void LogicalOperatorVisitor::VisitExpression(unique_ptr<Expression> *expression) { |
83 | auto &expr = **expression; |
84 | unique_ptr<Expression> result; |
85 | switch (expr.GetExpressionClass()) { |
86 | case ExpressionClass::BOUND_AGGREGATE: |
87 | result = VisitReplace((BoundAggregateExpression &)expr, expression); |
88 | break; |
89 | case ExpressionClass::BOUND_BETWEEN: |
90 | result = VisitReplace((BoundBetweenExpression &)expr, expression); |
91 | break; |
92 | case ExpressionClass::BOUND_CASE: |
93 | result = VisitReplace((BoundCaseExpression &)expr, expression); |
94 | break; |
95 | case ExpressionClass::BOUND_CAST: |
96 | result = VisitReplace((BoundCastExpression &)expr, expression); |
97 | break; |
98 | case ExpressionClass::BOUND_COLUMN_REF: |
99 | result = VisitReplace((BoundColumnRefExpression &)expr, expression); |
100 | break; |
101 | case ExpressionClass::BOUND_COMPARISON: |
102 | result = VisitReplace((BoundComparisonExpression &)expr, expression); |
103 | break; |
104 | case ExpressionClass::BOUND_CONJUNCTION: |
105 | result = VisitReplace((BoundConjunctionExpression &)expr, expression); |
106 | break; |
107 | case ExpressionClass::BOUND_CONSTANT: |
108 | result = VisitReplace((BoundConstantExpression &)expr, expression); |
109 | break; |
110 | case ExpressionClass::BOUND_FUNCTION: |
111 | result = VisitReplace((BoundFunctionExpression &)expr, expression); |
112 | break; |
113 | case ExpressionClass::BOUND_SUBQUERY: |
114 | result = VisitReplace((BoundSubqueryExpression &)expr, expression); |
115 | break; |
116 | case ExpressionClass::BOUND_OPERATOR: |
117 | result = VisitReplace((BoundOperatorExpression &)expr, expression); |
118 | break; |
119 | case ExpressionClass::BOUND_PARAMETER: |
120 | result = VisitReplace((BoundParameterExpression &)expr, expression); |
121 | break; |
122 | case ExpressionClass::BOUND_REF: |
123 | result = VisitReplace((BoundReferenceExpression &)expr, expression); |
124 | break; |
125 | case ExpressionClass::BOUND_DEFAULT: |
126 | result = VisitReplace((BoundDefaultExpression &)expr, expression); |
127 | break; |
128 | case ExpressionClass::COMMON_SUBEXPRESSION: |
129 | result = VisitReplace((CommonSubExpression &)expr, expression); |
130 | break; |
131 | case ExpressionClass::BOUND_WINDOW: |
132 | result = VisitReplace((BoundWindowExpression &)expr, expression); |
133 | break; |
134 | case ExpressionClass::BOUND_UNNEST: |
135 | result = VisitReplace((BoundUnnestExpression &)expr, expression); |
136 | break; |
137 | default: |
138 | assert(0); |
139 | } |
140 | if (result) { |
141 | *expression = move(result); |
142 | } else { |
143 | // visit the children of this node |
144 | VisitExpressionChildren(expr); |
145 | } |
146 | } |
147 | |
148 | void LogicalOperatorVisitor::VisitExpressionChildren(Expression &expr) { |
149 | ExpressionIterator::EnumerateChildren(expr, [&](unique_ptr<Expression> expr) -> unique_ptr<Expression> { |
150 | VisitExpression(&expr); |
151 | return move(expr); |
152 | }); |
153 | } |
154 | |
155 | unique_ptr<Expression> LogicalOperatorVisitor::VisitReplace(BoundAggregateExpression &expr, |
156 | unique_ptr<Expression> *expr_ptr) { |
157 | return nullptr; |
158 | } |
159 | |
160 | unique_ptr<Expression> LogicalOperatorVisitor::VisitReplace(BoundBetweenExpression &expr, |
161 | unique_ptr<Expression> *expr_ptr) { |
162 | return nullptr; |
163 | } |
164 | |
165 | unique_ptr<Expression> LogicalOperatorVisitor::VisitReplace(BoundCaseExpression &expr, |
166 | unique_ptr<Expression> *expr_ptr) { |
167 | return nullptr; |
168 | } |
169 | |
170 | unique_ptr<Expression> LogicalOperatorVisitor::VisitReplace(BoundCastExpression &expr, |
171 | unique_ptr<Expression> *expr_ptr) { |
172 | return nullptr; |
173 | } |
174 | |
175 | unique_ptr<Expression> LogicalOperatorVisitor::VisitReplace(BoundColumnRefExpression &expr, |
176 | unique_ptr<Expression> *expr_ptr) { |
177 | return nullptr; |
178 | } |
179 | |
180 | unique_ptr<Expression> LogicalOperatorVisitor::VisitReplace(BoundComparisonExpression &expr, |
181 | unique_ptr<Expression> *expr_ptr) { |
182 | return nullptr; |
183 | } |
184 | |
185 | unique_ptr<Expression> LogicalOperatorVisitor::VisitReplace(BoundConjunctionExpression &expr, |
186 | unique_ptr<Expression> *expr_ptr) { |
187 | return nullptr; |
188 | } |
189 | |
190 | unique_ptr<Expression> LogicalOperatorVisitor::VisitReplace(BoundConstantExpression &expr, |
191 | unique_ptr<Expression> *expr_ptr) { |
192 | return nullptr; |
193 | } |
194 | |
195 | unique_ptr<Expression> LogicalOperatorVisitor::VisitReplace(BoundDefaultExpression &expr, |
196 | unique_ptr<Expression> *expr_ptr) { |
197 | return nullptr; |
198 | } |
199 | |
200 | unique_ptr<Expression> LogicalOperatorVisitor::VisitReplace(BoundFunctionExpression &expr, |
201 | unique_ptr<Expression> *expr_ptr) { |
202 | return nullptr; |
203 | } |
204 | |
205 | unique_ptr<Expression> LogicalOperatorVisitor::VisitReplace(BoundOperatorExpression &expr, |
206 | unique_ptr<Expression> *expr_ptr) { |
207 | return nullptr; |
208 | } |
209 | |
210 | unique_ptr<Expression> LogicalOperatorVisitor::VisitReplace(BoundParameterExpression &expr, |
211 | unique_ptr<Expression> *expr_ptr) { |
212 | return nullptr; |
213 | } |
214 | |
215 | unique_ptr<Expression> LogicalOperatorVisitor::VisitReplace(BoundReferenceExpression &expr, |
216 | unique_ptr<Expression> *expr_ptr) { |
217 | return nullptr; |
218 | } |
219 | |
220 | unique_ptr<Expression> LogicalOperatorVisitor::VisitReplace(BoundSubqueryExpression &expr, |
221 | unique_ptr<Expression> *expr_ptr) { |
222 | return nullptr; |
223 | } |
224 | |
225 | unique_ptr<Expression> LogicalOperatorVisitor::VisitReplace(BoundWindowExpression &expr, |
226 | unique_ptr<Expression> *expr_ptr) { |
227 | return nullptr; |
228 | } |
229 | |
230 | unique_ptr<Expression> LogicalOperatorVisitor::VisitReplace(BoundUnnestExpression &expr, |
231 | unique_ptr<Expression> *expr_ptr) { |
232 | return nullptr; |
233 | } |
234 | |
235 | unique_ptr<Expression> LogicalOperatorVisitor::VisitReplace(CommonSubExpression &expr, |
236 | unique_ptr<Expression> *expr_ptr) { |
237 | return nullptr; |
238 | } |
239 | |