OmniSciDB  8a228a1076
DeepCopyVisitor.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 #include "ScalarExprVisitor.h"
18 
19 class DeepCopyVisitor : public ScalarExprVisitor<std::shared_ptr<Analyzer::Expr>> {
20  protected:
21  using RetType = std::shared_ptr<Analyzer::Expr>;
22  RetType visitColumnVar(const Analyzer::ColumnVar* col_var) const override {
23  return col_var->deep_copy();
24  }
25 
27  const Analyzer::ExpressionTuple* col_var_tuple) const override {
28  return col_var_tuple->deep_copy();
29  }
30 
31  RetType visitVar(const Analyzer::Var* var) const override { return var->deep_copy(); }
32 
33  RetType visitConstant(const Analyzer::Constant* constant) const override {
34  return constant->deep_copy();
35  }
36 
37  RetType visitUOper(const Analyzer::UOper* uoper) const override {
38  return makeExpr<Analyzer::UOper>(uoper->get_type_info(),
39  uoper->get_contains_agg(),
40  uoper->get_optype(),
41  visit(uoper->get_operand()));
42  }
43 
44  RetType visitBinOper(const Analyzer::BinOper* bin_oper) const override {
45  return makeExpr<Analyzer::BinOper>(bin_oper->get_type_info(),
46  bin_oper->get_contains_agg(),
47  bin_oper->get_optype(),
48  bin_oper->get_qualifier(),
49  visit(bin_oper->get_left_operand()),
50  visit(bin_oper->get_right_operand()));
51  }
52 
53  RetType visitInValues(const Analyzer::InValues* in_values) const override {
54  const auto& value_list = in_values->get_value_list();
55  std::list<RetType> new_list;
56  for (const auto& in_value : value_list) {
57  new_list.push_back(visit(in_value.get()));
58  }
59  return makeExpr<Analyzer::InValues>(visit(in_values->get_arg()), new_list);
60  }
61 
62  RetType visitInIntegerSet(const Analyzer::InIntegerSet* in_integer_set) const override {
63  return makeExpr<Analyzer::InIntegerSet>(
64  visit(in_integer_set->get_arg()),
65  in_integer_set->get_value_list(),
66  in_integer_set->get_type_info().get_notnull());
67  }
68 
70  return makeExpr<Analyzer::CharLengthExpr>(visit(char_length->get_arg()),
71  char_length->get_calc_encoded_length());
72  }
73 
75  return makeExpr<Analyzer::KeyForStringExpr>(visit(expr->get_arg()));
76  }
77 
78  RetType visitSampleRatio(const Analyzer::SampleRatioExpr* expr) const override {
79  return makeExpr<Analyzer::SampleRatioExpr>(visit(expr->get_arg()));
80  }
81 
82  RetType visitLower(const Analyzer::LowerExpr* expr) const override {
83  return makeExpr<Analyzer::LowerExpr>(visit(expr->get_arg()));
84  }
85 
86  RetType visitCardinality(const Analyzer::CardinalityExpr* cardinality) const override {
87  return makeExpr<Analyzer::CardinalityExpr>(visit(cardinality->get_arg()));
88  }
89 
90  RetType visitLikeExpr(const Analyzer::LikeExpr* like) const override {
91  auto escape_expr = like->get_escape_expr();
92  return makeExpr<Analyzer::LikeExpr>(visit(like->get_arg()),
93  visit(like->get_like_expr()),
94  escape_expr ? visit(escape_expr) : nullptr,
95  like->get_is_ilike(),
96  like->get_is_simple());
97  }
98 
99  RetType visitRegexpExpr(const Analyzer::RegexpExpr* regexp) const override {
100  auto escape_expr = regexp->get_escape_expr();
101  return makeExpr<Analyzer::RegexpExpr>(visit(regexp->get_arg()),
102  visit(regexp->get_pattern_expr()),
103  escape_expr ? visit(escape_expr) : nullptr);
104  }
105 
106  RetType visitCaseExpr(const Analyzer::CaseExpr* case_expr) const override {
107  std::list<std::pair<RetType, RetType>> new_list;
108  for (auto p : case_expr->get_expr_pair_list()) {
109  new_list.emplace_back(visit(p.first.get()), visit(p.second.get()));
110  }
111  auto else_expr = case_expr->get_else_expr();
112  return makeExpr<Analyzer::CaseExpr>(
113  case_expr->get_type_info(),
114  case_expr->get_contains_agg(),
115  new_list,
116  else_expr == nullptr ? nullptr : visit(else_expr));
117  }
118 
119  RetType visitDatetruncExpr(const Analyzer::DatetruncExpr* datetrunc) const override {
120  return makeExpr<Analyzer::DatetruncExpr>(datetrunc->get_type_info(),
121  datetrunc->get_contains_agg(),
122  datetrunc->get_field(),
123  visit(datetrunc->get_from_expr()));
124  }
125 
126  RetType visitExtractExpr(const Analyzer::ExtractExpr* extract) const override {
127  return makeExpr<Analyzer::ExtractExpr>(extract->get_type_info(),
128  extract->get_contains_agg(),
129  extract->get_field(),
130  visit(extract->get_from_expr()));
131  }
132 
133  RetType visitArrayOper(const Analyzer::ArrayExpr* array_expr) const override {
134  std::vector<std::shared_ptr<Analyzer::Expr>> args_copy;
135  for (size_t i = 0; i < array_expr->getElementCount(); ++i) {
136  args_copy.push_back(visit(array_expr->getElement(i)));
137  }
138  const auto& type_info = array_expr->get_type_info();
139  return makeExpr<Analyzer::ArrayExpr>(
140  type_info, args_copy, array_expr->isNull(), array_expr->isLocalAlloc());
141  }
142 
143  RetType visitGeoUOper(const Analyzer::GeoUOper* geo_expr) const override {
144  std::vector<std::shared_ptr<Analyzer::Expr>> args0_copy;
145  for (const auto& arg : geo_expr->getArgs0()) {
146  args0_copy.push_back(visit(arg.get()));
147  }
148  const auto& ti0 = geo_expr->getTypeInfo0();
149  const auto& type_info = geo_expr->get_type_info();
150  return makeExpr<Analyzer::GeoUOper>(geo_expr->getOp(), type_info, ti0, args0_copy);
151  }
152 
153  RetType visitGeoBinOper(const Analyzer::GeoBinOper* geo_expr) const override {
154  std::vector<std::shared_ptr<Analyzer::Expr>> args0_copy;
155  for (const auto& arg : geo_expr->getArgs0()) {
156  args0_copy.push_back(visit(arg.get()));
157  }
158  std::vector<std::shared_ptr<Analyzer::Expr>> args1_copy;
159  for (const auto& arg : geo_expr->getArgs1()) {
160  args1_copy.push_back(visit(arg.get()));
161  }
162  const auto& ti0 = geo_expr->getTypeInfo0();
163  const auto& ti1 = geo_expr->getTypeInfo1();
164  const auto& type_info = geo_expr->get_type_info();
165  return makeExpr<Analyzer::GeoBinOper>(
166  geo_expr->getOp(), type_info, ti0, ti1, args0_copy, args1_copy);
167  }
168 
170  const Analyzer::WindowFunction* window_func) const override {
171  std::vector<std::shared_ptr<Analyzer::Expr>> args_copy;
172  for (const auto& arg : window_func->getArgs()) {
173  args_copy.push_back(visit(arg.get()));
174  }
175  std::vector<std::shared_ptr<Analyzer::Expr>> partition_keys_copy;
176  for (const auto& partition_key : window_func->getPartitionKeys()) {
177  partition_keys_copy.push_back(visit(partition_key.get()));
178  }
179  std::vector<std::shared_ptr<Analyzer::Expr>> order_keys_copy;
180  for (const auto& order_key : window_func->getOrderKeys()) {
181  order_keys_copy.push_back(visit(order_key.get()));
182  }
183  const auto& type_info = window_func->get_type_info();
184  return makeExpr<Analyzer::WindowFunction>(type_info,
185  window_func->getKind(),
186  args_copy,
187  partition_keys_copy,
188  order_keys_copy,
189  window_func->getCollation());
190  }
191 
192  RetType visitFunctionOper(const Analyzer::FunctionOper* func_oper) const override {
193  std::vector<std::shared_ptr<Analyzer::Expr>> args_copy;
194  for (size_t i = 0; i < func_oper->getArity(); ++i) {
195  args_copy.push_back(visit(func_oper->getArg(i)));
196  }
197  const auto& type_info = func_oper->get_type_info();
198  return makeExpr<Analyzer::FunctionOper>(type_info, func_oper->getName(), args_copy);
199  }
200 
201  RetType visitDatediffExpr(const Analyzer::DatediffExpr* datediff) const override {
202  return makeExpr<Analyzer::DatediffExpr>(datediff->get_type_info(),
203  datediff->get_field(),
204  visit(datediff->get_start_expr()),
205  visit(datediff->get_end_expr()));
206  }
207 
208  RetType visitDateaddExpr(const Analyzer::DateaddExpr* dateadd) const override {
209  return makeExpr<Analyzer::DateaddExpr>(dateadd->get_type_info(),
210  dateadd->get_field(),
211  visit(dateadd->get_number_expr()),
212  visit(dateadd->get_datetime_expr()));
213  }
214 
216  const Analyzer::FunctionOperWithCustomTypeHandling* func_oper) const override {
217  std::vector<std::shared_ptr<Analyzer::Expr>> args_copy;
218  for (size_t i = 0; i < func_oper->getArity(); ++i) {
219  args_copy.push_back(visit(func_oper->getArg(i)));
220  }
221  const auto& type_info = func_oper->get_type_info();
222  return makeExpr<Analyzer::FunctionOperWithCustomTypeHandling>(
223  type_info, func_oper->getName(), args_copy);
224  }
225 
226  RetType visitLikelihood(const Analyzer::LikelihoodExpr* likelihood) const override {
227  return makeExpr<Analyzer::LikelihoodExpr>(visit(likelihood->get_arg()),
228  likelihood->get_likelihood());
229  }
230 
231  RetType visitAggExpr(const Analyzer::AggExpr* agg) const override {
232  RetType arg = agg->get_arg() ? visit(agg->get_arg()) : nullptr;
233  return makeExpr<Analyzer::AggExpr>(agg->get_type_info(),
234  agg->get_aggtype(),
235  arg,
236  agg->get_is_distinct(),
237  agg->get_error_rate());
238  }
239 
241  return makeExpr<Analyzer::OffsetInFragment>();
242  }
243 };
RetType visitColumnVarTuple(const Analyzer::ExpressionTuple *col_var_tuple) const override
SQLQualifier get_qualifier() const
Definition: Analyzer.h:442
RetType visitInValues(const Analyzer::InValues *in_values) const override
const Expr * get_arg() const
Definition: Analyzer.h:751
const Expr * get_arg() const
Definition: Analyzer.h:585
bool get_is_simple() const
Definition: Analyzer.h:911
RetType visitVar(const Analyzer::Var *var) const override
bool get_calc_encoded_length() const
Definition: Analyzer.h:659
size_t getElementCount() const
Definition: Analyzer.h:1489
const Expr * get_datetime_expr() const
Definition: Analyzer.h:1240
std::string getName() const
Definition: Analyzer.h:1359
RetType visitFunctionOper(const Analyzer::FunctionOper *func_oper) const override
const Expr * get_arg() const
Definition: Analyzer.h:634
RetType visitConstant(const Analyzer::Constant *constant) const override
const Analyzer::Expr * getArg(const size_t i) const
Definition: Analyzer.h:1363
const std::vector< std::shared_ptr< Analyzer::Expr > > & getOrderKeys() const
Definition: Analyzer.h:1456
RetType visitDatetruncExpr(const Analyzer::DatetruncExpr *datetrunc) const override
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:80
RetType visitArrayOper(const Analyzer::ArrayExpr *array_expr) const override
bool isLocalAlloc() const
Definition: Analyzer.h:1490
const SQLTypeInfo getTypeInfo1() const
Definition: Analyzer.h:1552
const Expr * get_start_expr() const
Definition: Analyzer.h:1281
DateaddField get_field() const
Definition: Analyzer.h:1238
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:59
size_t getArity() const
Definition: Analyzer.h:1361
const Expr * get_from_expr() const
Definition: Analyzer.h:1198
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs0() const
Definition: Analyzer.h:1523
std::shared_ptr< Analyzer::Expr > RetType
const Expr * get_arg() const
Definition: Analyzer.h:852
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:69
SQLOps get_optype() const
Definition: Analyzer.h:440
Expr * get_arg() const
Definition: Analyzer.h:1097
DatetruncField get_field() const
Definition: Analyzer.h:1322
const Expr * get_arg() const
Definition: Analyzer.h:706
const Expr * get_arg() const
Definition: Analyzer.h:906
RetType visitGeoUOper(const Analyzer::GeoUOper *geo_expr) const override
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:266
const Expr * get_else_expr() const
Definition: Analyzer.h:1153
RetType visitLikeExpr(const Analyzer::LikeExpr *like) const override
RetType visitOffsetInFragment(const Analyzer::OffsetInFragment *) const override
const Analyzer::Expr * getElement(const size_t i) const
Definition: Analyzer.h:1493
RetType visitExtractExpr(const Analyzer::ExtractExpr *extract) const override
ALWAYS_INLINE DEVICE int32_t char_length(const char *str, const int32_t str_len)
RetType visitCardinality(const Analyzer::CardinalityExpr *cardinality) const override
RetType visitLikelihood(const Analyzer::LikelihoodExpr *likelihood) const override
const Expr * get_from_expr() const
Definition: Analyzer.h:1323
const Expr * get_escape_expr() const
Definition: Analyzer.h:909
RetType visitKeyForString(const Analyzer::KeyForStringExpr *expr) const override
const std::vector< OrderEntry > & getCollation() const
Definition: Analyzer.h:1460
float get_likelihood() const
Definition: Analyzer.h:1036
RetType visitAggExpr(const Analyzer::AggExpr *agg) const override
RetType visitFunctionOperWithCustomTypeHandling(const Analyzer::FunctionOperWithCustomTypeHandling *func_oper) const override
const std::list< std::pair< std::shared_ptr< Analyzer::Expr >, std::shared_ptr< Analyzer::Expr > > > & get_expr_pair_list() const
Definition: Analyzer.h:1150
const SQLTypeInfo getTypeInfo0() const
Definition: Analyzer.h:1522
DatetruncField get_field() const
Definition: Analyzer.h:1280
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs0() const
Definition: Analyzer.h:1553
const Expr * get_arg() const
Definition: Analyzer.h:978
const Expr * get_pattern_expr() const
Definition: Analyzer.h:980
const Expr * get_escape_expr() const
Definition: Analyzer.h:981
RetType visitUOper(const Analyzer::UOper *uoper) const override
const SQLTypeInfo getTypeInfo0() const
Definition: Analyzer.h:1551
SQLAgg get_aggtype() const
Definition: Analyzer.h:1096
const std::vector< std::shared_ptr< Analyzer::Expr > > & getPartitionKeys() const
Definition: Analyzer.h:1452
const Expr * get_arg() const
Definition: Analyzer.h:1034
RetType visitCharLength(const Analyzer::CharLengthExpr *char_length) const override
bool get_is_distinct() const
Definition: Analyzer.h:1099
Expression class for the LOWER (lowercase) string function. The "arg" constructor parameter must be a...
Definition: Analyzer.h:792
std::shared_ptr< Analyzer::Expr > visit(const Analyzer::Expr *expr) const
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs() const
Definition: Analyzer.h:1450
std::shared_ptr< Analyzer::Constant > get_error_rate() const
Definition: Analyzer.h:1100
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:79
const Expr * get_arg() const
Definition: Analyzer.h:796
const Expr * get_arg() const
Definition: Analyzer.h:657
RetType visitCaseExpr(const Analyzer::CaseExpr *case_expr) const override
RetType visitInIntegerSet(const Analyzer::InIntegerSet *in_integer_set) const override
const std::vector< int64_t > & get_value_list() const
Definition: Analyzer.h:636
RetType visitSampleRatio(const Analyzer::SampleRatioExpr *expr) const override
RetType visitBinOper(const Analyzer::BinOper *bin_oper) const override
Geo_namespace::GeoBase::GeoOp getOp() const
Definition: Analyzer.h:1521
bool get_contains_agg() const
Definition: Analyzer.h:81
RetType visitRegexpExpr(const Analyzer::RegexpExpr *regexp) const override
const Expr * get_like_expr() const
Definition: Analyzer.h:908
RetType visitDatediffExpr(const Analyzer::DatediffExpr *datediff) const override
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs1() const
Definition: Analyzer.h:1554
ExtractField get_field() const
Definition: Analyzer.h:1197
const std::list< std::shared_ptr< Analyzer::Expr > > & get_value_list() const
Definition: Analyzer.h:587
Geo_namespace::GeoBase::GeoOp getOp() const
Definition: Analyzer.h:1550
const Expr * get_end_expr() const
Definition: Analyzer.h:1282
RetType visitGeoBinOper(const Analyzer::GeoBinOper *geo_expr) const override
const Expr * get_number_expr() const
Definition: Analyzer.h:1239
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:84
SqlWindowFunctionKind getKind() const
Definition: Analyzer.h:1448
bool isNull() const
Definition: Analyzer.h:1491
RetType visitDateaddExpr(const Analyzer::DateaddExpr *dateadd) const override
bool get_is_ilike() const
Definition: Analyzer.h:910
SQLOps get_optype() const
Definition: Analyzer.h:371
RetType visitWindowFunction(const Analyzer::WindowFunction *window_func) const override
RetType visitLower(const Analyzer::LowerExpr *expr) const override
const Expr * get_right_operand() const
Definition: Analyzer.h:444
RetType visitColumnVar(const Analyzer::ColumnVar *col_var) const override
const Expr * get_left_operand() const
Definition: Analyzer.h:443
const Expr * get_operand() const
Definition: Analyzer.h:372