OmniSciDB  fe05a0c208
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 visitGeoExpr(const Analyzer::GeoExpr* geo_expr) const override {
54  return geo_expr->deep_copy();
55  }
56 
57  RetType visitInValues(const Analyzer::InValues* in_values) const override {
58  const auto& value_list = in_values->get_value_list();
59  std::list<RetType> new_list;
60  for (const auto& in_value : value_list) {
61  new_list.push_back(visit(in_value.get()));
62  }
63  return makeExpr<Analyzer::InValues>(visit(in_values->get_arg()), new_list);
64  }
65 
66  RetType visitInIntegerSet(const Analyzer::InIntegerSet* in_integer_set) const override {
67  return makeExpr<Analyzer::InIntegerSet>(
68  visit(in_integer_set->get_arg()),
69  in_integer_set->get_value_list(),
70  in_integer_set->get_type_info().get_notnull());
71  }
72 
74  return makeExpr<Analyzer::CharLengthExpr>(visit(char_length->get_arg()),
75  char_length->get_calc_encoded_length());
76  }
77 
79  return makeExpr<Analyzer::KeyForStringExpr>(visit(expr->get_arg()));
80  }
81 
82  RetType visitSampleRatio(const Analyzer::SampleRatioExpr* expr) const override {
83  return makeExpr<Analyzer::SampleRatioExpr>(visit(expr->get_arg()));
84  }
85 
86  RetType visitLower(const Analyzer::LowerExpr* expr) const override {
87  return makeExpr<Analyzer::LowerExpr>(visit(expr->get_arg()));
88  }
89 
90  RetType visitCardinality(const Analyzer::CardinalityExpr* cardinality) const override {
91  return makeExpr<Analyzer::CardinalityExpr>(visit(cardinality->get_arg()));
92  }
93 
94  RetType visitLikeExpr(const Analyzer::LikeExpr* like) const override {
95  auto escape_expr = like->get_escape_expr();
96  return makeExpr<Analyzer::LikeExpr>(visit(like->get_arg()),
97  visit(like->get_like_expr()),
98  escape_expr ? visit(escape_expr) : nullptr,
99  like->get_is_ilike(),
100  like->get_is_simple());
101  }
102 
103  RetType visitRegexpExpr(const Analyzer::RegexpExpr* regexp) const override {
104  auto escape_expr = regexp->get_escape_expr();
105  return makeExpr<Analyzer::RegexpExpr>(visit(regexp->get_arg()),
106  visit(regexp->get_pattern_expr()),
107  escape_expr ? visit(escape_expr) : nullptr);
108  }
109 
110  RetType visitCaseExpr(const Analyzer::CaseExpr* case_expr) const override {
111  std::list<std::pair<RetType, RetType>> new_list;
112  for (auto p : case_expr->get_expr_pair_list()) {
113  new_list.emplace_back(visit(p.first.get()), visit(p.second.get()));
114  }
115  auto else_expr = case_expr->get_else_expr();
116  return makeExpr<Analyzer::CaseExpr>(
117  case_expr->get_type_info(),
118  case_expr->get_contains_agg(),
119  new_list,
120  else_expr == nullptr ? nullptr : visit(else_expr));
121  }
122 
123  RetType visitDatetruncExpr(const Analyzer::DatetruncExpr* datetrunc) const override {
124  return makeExpr<Analyzer::DatetruncExpr>(datetrunc->get_type_info(),
125  datetrunc->get_contains_agg(),
126  datetrunc->get_field(),
127  visit(datetrunc->get_from_expr()));
128  }
129 
130  RetType visitExtractExpr(const Analyzer::ExtractExpr* extract) const override {
131  return makeExpr<Analyzer::ExtractExpr>(extract->get_type_info(),
132  extract->get_contains_agg(),
133  extract->get_field(),
134  visit(extract->get_from_expr()));
135  }
136 
137  RetType visitArrayOper(const Analyzer::ArrayExpr* array_expr) const override {
138  std::vector<std::shared_ptr<Analyzer::Expr>> args_copy;
139  for (size_t i = 0; i < array_expr->getElementCount(); ++i) {
140  args_copy.push_back(visit(array_expr->getElement(i)));
141  }
142  const auto& type_info = array_expr->get_type_info();
143  return makeExpr<Analyzer::ArrayExpr>(
144  type_info, args_copy, array_expr->isNull(), array_expr->isLocalAlloc());
145  }
146 
147  RetType visitGeoUOper(const Analyzer::GeoUOper* geo_expr) const override {
148  std::vector<std::shared_ptr<Analyzer::Expr>> args0_copy;
149  for (const auto& arg : geo_expr->getArgs0()) {
150  args0_copy.push_back(visit(arg.get()));
151  }
152  const auto& ti0 = geo_expr->getTypeInfo0();
153  const auto& type_info = geo_expr->get_type_info();
154  return makeExpr<Analyzer::GeoUOper>(geo_expr->getOp(), type_info, ti0, args0_copy);
155  }
156 
157  RetType visitGeoBinOper(const Analyzer::GeoBinOper* geo_expr) const override {
158  std::vector<std::shared_ptr<Analyzer::Expr>> args0_copy;
159  for (const auto& arg : geo_expr->getArgs0()) {
160  args0_copy.push_back(visit(arg.get()));
161  }
162  std::vector<std::shared_ptr<Analyzer::Expr>> args1_copy;
163  for (const auto& arg : geo_expr->getArgs1()) {
164  args1_copy.push_back(visit(arg.get()));
165  }
166  const auto& ti0 = geo_expr->getTypeInfo0();
167  const auto& ti1 = geo_expr->getTypeInfo1();
168  const auto& type_info = geo_expr->get_type_info();
169  return makeExpr<Analyzer::GeoBinOper>(
170  geo_expr->getOp(), type_info, ti0, ti1, args0_copy, args1_copy);
171  }
172 
174  const Analyzer::WindowFunction* window_func) const override {
175  std::vector<std::shared_ptr<Analyzer::Expr>> args_copy;
176  for (const auto& arg : window_func->getArgs()) {
177  args_copy.push_back(visit(arg.get()));
178  }
179  std::vector<std::shared_ptr<Analyzer::Expr>> partition_keys_copy;
180  for (const auto& partition_key : window_func->getPartitionKeys()) {
181  partition_keys_copy.push_back(visit(partition_key.get()));
182  }
183  std::vector<std::shared_ptr<Analyzer::Expr>> order_keys_copy;
184  for (const auto& order_key : window_func->getOrderKeys()) {
185  order_keys_copy.push_back(visit(order_key.get()));
186  }
187  const auto& type_info = window_func->get_type_info();
188  return makeExpr<Analyzer::WindowFunction>(type_info,
189  window_func->getKind(),
190  args_copy,
191  partition_keys_copy,
192  order_keys_copy,
193  window_func->getCollation());
194  }
195 
196  RetType visitFunctionOper(const Analyzer::FunctionOper* func_oper) const override {
197  std::vector<std::shared_ptr<Analyzer::Expr>> args_copy;
198  for (size_t i = 0; i < func_oper->getArity(); ++i) {
199  args_copy.push_back(visit(func_oper->getArg(i)));
200  }
201  const auto& type_info = func_oper->get_type_info();
202  return makeExpr<Analyzer::FunctionOper>(type_info, func_oper->getName(), args_copy);
203  }
204 
205  RetType visitDatediffExpr(const Analyzer::DatediffExpr* datediff) const override {
206  return makeExpr<Analyzer::DatediffExpr>(datediff->get_type_info(),
207  datediff->get_field(),
208  visit(datediff->get_start_expr()),
209  visit(datediff->get_end_expr()));
210  }
211 
212  RetType visitDateaddExpr(const Analyzer::DateaddExpr* dateadd) const override {
213  return makeExpr<Analyzer::DateaddExpr>(dateadd->get_type_info(),
214  dateadd->get_field(),
215  visit(dateadd->get_number_expr()),
216  visit(dateadd->get_datetime_expr()));
217  }
218 
220  const Analyzer::FunctionOperWithCustomTypeHandling* func_oper) const override {
221  std::vector<std::shared_ptr<Analyzer::Expr>> args_copy;
222  for (size_t i = 0; i < func_oper->getArity(); ++i) {
223  args_copy.push_back(visit(func_oper->getArg(i)));
224  }
225  const auto& type_info = func_oper->get_type_info();
226  return makeExpr<Analyzer::FunctionOperWithCustomTypeHandling>(
227  type_info, func_oper->getName(), args_copy);
228  }
229 
230  RetType visitLikelihood(const Analyzer::LikelihoodExpr* likelihood) const override {
231  return makeExpr<Analyzer::LikelihoodExpr>(visit(likelihood->get_arg()),
232  likelihood->get_likelihood());
233  }
234 
235  RetType visitAggExpr(const Analyzer::AggExpr* agg) const override {
236  RetType arg = agg->get_arg() ? visit(agg->get_arg()) : nullptr;
237  return makeExpr<Analyzer::AggExpr>(agg->get_type_info(),
238  agg->get_aggtype(),
239  arg,
240  agg->get_is_distinct(),
241  agg->get_error_rate());
242  }
243 
245  return makeExpr<Analyzer::OffsetInFragment>();
246  }
247 };
RetType visitColumnVarTuple(const Analyzer::ExpressionTuple *col_var_tuple) const override
RetType visitInValues(const Analyzer::InValues *in_values) const override
float get_likelihood() const
Definition: Analyzer.h:1035
SqlWindowFunctionKind getKind() const
Definition: Analyzer.h:1447
const Expr * get_from_expr() const
Definition: Analyzer.h:1197
const Expr * get_else_expr() const
Definition: Analyzer.h:1152
const SQLTypeInfo getTypeInfo0() const
Definition: Analyzer.h:1556
RetType visitVar(const Analyzer::Var *var) const override
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs0() const
Definition: Analyzer.h:1558
const std::vector< int64_t > & get_value_list() const
Definition: Analyzer.h:635
const Expr * get_escape_expr() const
Definition: Analyzer.h:908
size_t getArity() const
Definition: Analyzer.h:1360
const SQLTypeInfo getTypeInfo0() const
Definition: Analyzer.h:1527
RetType visitFunctionOper(const Analyzer::FunctionOper *func_oper) const override
RetType visitConstant(const Analyzer::Constant *constant) const override
std::shared_ptr< Analyzer::Constant > get_error_rate() const
Definition: Analyzer.h:1099
ExtractField get_field() const
Definition: Analyzer.h:1196
const Expr * get_escape_expr() const
Definition: Analyzer.h:980
const Expr * get_right_operand() const
Definition: Analyzer.h:443
RetType visitDatetruncExpr(const Analyzer::DatetruncExpr *datetrunc) const override
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:81
RetType visitArrayOper(const Analyzer::ArrayExpr *array_expr) const override
bool get_contains_agg() const
Definition: Analyzer.h:80
const SQLTypeInfo getTypeInfo1() const
Definition: Analyzer.h:1557
const Expr * get_arg() const
Definition: Analyzer.h:977
Expr * get_arg() const
Definition: Analyzer.h:1096
DatetruncField get_field() const
Definition: Analyzer.h:1321
const Expr * get_arg() const
Definition: Analyzer.h:1033
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:60
std::shared_ptr< Analyzer::Expr > visit(const Analyzer::Expr *expr) const
bool isNull() const
Definition: Analyzer.h:1490
const std::vector< std::shared_ptr< Analyzer::Expr > > & getOrderKeys() const
Definition: Analyzer.h:1455
const std::vector< OrderEntry > & getCollation() const
Definition: Analyzer.h:1459
std::shared_ptr< Analyzer::Expr > RetType
const Expr * get_arg() const
Definition: Analyzer.h:905
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:70
bool get_calc_encoded_length() const
Definition: Analyzer.h:658
RetType visitGeoUOper(const Analyzer::GeoUOper *geo_expr) const override
SQLOps get_optype() const
Definition: Analyzer.h:439
RetType visitLikeExpr(const Analyzer::LikeExpr *like) const override
RetType visitOffsetInFragment(const Analyzer::OffsetInFragment *) const override
RetType visitExtractExpr(const Analyzer::ExtractExpr *extract) const override
ALWAYS_INLINE DEVICE int32_t char_length(const char *str, const int32_t str_len)
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs() const
Definition: Analyzer.h:1449
RetType visitCardinality(const Analyzer::CardinalityExpr *cardinality) const override
RetType visitLikelihood(const Analyzer::LikelihoodExpr *likelihood) const override
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs0() const
Definition: Analyzer.h:1528
RetType visitKeyForString(const Analyzer::KeyForStringExpr *expr) const override
RetType visitAggExpr(const Analyzer::AggExpr *agg) const override
RetType visitFunctionOperWithCustomTypeHandling(const Analyzer::FunctionOperWithCustomTypeHandling *func_oper) const override
bool get_is_simple() const
Definition: Analyzer.h:910
bool get_is_distinct() const
Definition: Analyzer.h:1098
const Expr * get_start_expr() const
Definition: Analyzer.h:1280
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:78
size_t getElementCount() const
Definition: Analyzer.h:1488
const Expr * get_pattern_expr() const
Definition: Analyzer.h:979
const Expr * get_from_expr() const
Definition: Analyzer.h:1322
const Expr * get_datetime_expr() const
Definition: Analyzer.h:1239
bool isLocalAlloc() const
Definition: Analyzer.h:1489
const Expr * get_like_expr() const
Definition: Analyzer.h:907
const Analyzer::Expr * getArg(const size_t i) const
Definition: Analyzer.h:1362
RetType visitUOper(const Analyzer::UOper *uoper) const override
const Expr * get_operand() const
Definition: Analyzer.h:371
const Expr * get_arg() const
Definition: Analyzer.h:750
RetType visitCharLength(const Analyzer::CharLengthExpr *char_length) const override
const Expr * get_arg() const
Definition: Analyzer.h:656
const std::list< std::shared_ptr< Analyzer::Expr > > & get_value_list() const
Definition: Analyzer.h:586
Expression class for the LOWER (lowercase) string function. The &quot;arg&quot; constructor parameter must be a...
Definition: Analyzer.h:791
virtual std::shared_ptr< Analyzer::Expr > deep_copy() const =0
DateaddField get_field() const
Definition: Analyzer.h:1237
SQLAgg get_aggtype() const
Definition: Analyzer.h:1095
const Expr * get_arg() const
Definition: Analyzer.h:705
const Expr * get_end_expr() const
Definition: Analyzer.h:1281
Geospatial::GeoBase::GeoOp getOp() const
Definition: Analyzer.h:1526
RetType visitCaseExpr(const Analyzer::CaseExpr *case_expr) const override
const Expr * get_left_operand() const
Definition: Analyzer.h:442
RetType visitInIntegerSet(const Analyzer::InIntegerSet *in_integer_set) const override
RetType visitSampleRatio(const Analyzer::SampleRatioExpr *expr) const override
RetType visitBinOper(const Analyzer::BinOper *bin_oper) const override
RetType visitGeoExpr(const Analyzer::GeoExpr *geo_expr) const override
RetType visitRegexpExpr(const Analyzer::RegexpExpr *regexp) const override
std::string getName() const
Definition: Analyzer.h:1358
RetType visitDatediffExpr(const Analyzer::DatediffExpr *datediff) const override
const Expr * get_arg() const
Definition: Analyzer.h:633
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:321
const std::vector< std::shared_ptr< Analyzer::Expr > > & getPartitionKeys() const
Definition: Analyzer.h:1451
const Expr * get_arg() const
Definition: Analyzer.h:851
RetType visitGeoBinOper(const Analyzer::GeoBinOper *geo_expr) const override
DatetruncField get_field() const
Definition: Analyzer.h:1279
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:85
Geospatial::GeoBase::GeoOp getOp() const
Definition: Analyzer.h:1555
const Expr * get_arg() const
Definition: Analyzer.h:584
RetType visitDateaddExpr(const Analyzer::DateaddExpr *dateadd) const override
SQLOps get_optype() const
Definition: Analyzer.h:370
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs1() const
Definition: Analyzer.h:1559
const std::list< std::pair< std::shared_ptr< Analyzer::Expr >, std::shared_ptr< Analyzer::Expr > > > & get_expr_pair_list() const
Definition: Analyzer.h:1149
const Expr * get_arg() const
Definition: Analyzer.h:795
RetType visitWindowFunction(const Analyzer::WindowFunction *window_func) const override
const Expr * get_number_expr() const
Definition: Analyzer.h:1238
bool get_is_ilike() const
Definition: Analyzer.h:909
RetType visitLower(const Analyzer::LowerExpr *expr) const override
const Analyzer::Expr * getElement(const size_t i) const
Definition: Analyzer.h:1492
SQLQualifier get_qualifier() const
Definition: Analyzer.h:441
RetType visitColumnVar(const Analyzer::ColumnVar *col_var) const override