OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DeepCopyVisitor.h
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 #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 visitMLPredict(const Analyzer::MLPredictExpr* expr) const override {
87  return expr->deep_copy();
88  }
89 
90  RetType visitPCAProject(const Analyzer::PCAProjectExpr* expr) const override {
91  return expr->deep_copy();
92  }
93 
94  RetType visitCardinality(const Analyzer::CardinalityExpr* cardinality) const override {
95  return makeExpr<Analyzer::CardinalityExpr>(visit(cardinality->get_arg()));
96  }
97 
98  RetType visitLikeExpr(const Analyzer::LikeExpr* like) const override {
99  auto escape_expr = like->get_escape_expr();
100  return makeExpr<Analyzer::LikeExpr>(visit(like->get_arg()),
101  visit(like->get_like_expr()),
102  escape_expr ? visit(escape_expr) : nullptr,
103  like->get_is_ilike(),
104  like->get_is_simple());
105  }
106 
107  RetType visitRegexpExpr(const Analyzer::RegexpExpr* regexp) const override {
108  auto escape_expr = regexp->get_escape_expr();
109  return makeExpr<Analyzer::RegexpExpr>(visit(regexp->get_arg()),
110  visit(regexp->get_pattern_expr()),
111  escape_expr ? visit(escape_expr) : nullptr);
112  }
113 
115  const Analyzer::WidthBucketExpr* width_bucket_expr) const override {
116  return makeExpr<Analyzer::WidthBucketExpr>(
117  visit(width_bucket_expr->get_target_value()),
118  visit(width_bucket_expr->get_lower_bound()),
119  visit(width_bucket_expr->get_upper_bound()),
120  visit(width_bucket_expr->get_partition_count()));
121  }
122 
123  RetType visitCaseExpr(const Analyzer::CaseExpr* case_expr) const override {
124  std::list<std::pair<RetType, RetType>> new_list;
125  for (auto p : case_expr->get_expr_pair_list()) {
126  new_list.emplace_back(visit(p.first.get()), visit(p.second.get()));
127  }
128  auto else_expr = case_expr->get_else_expr();
129  return makeExpr<Analyzer::CaseExpr>(
130  case_expr->get_type_info(),
131  case_expr->get_contains_agg(),
132  new_list,
133  else_expr == nullptr ? nullptr : visit(else_expr));
134  }
135 
136  RetType visitDatetruncExpr(const Analyzer::DatetruncExpr* datetrunc) const override {
137  return makeExpr<Analyzer::DatetruncExpr>(datetrunc->get_type_info(),
138  datetrunc->get_contains_agg(),
139  datetrunc->get_field(),
140  visit(datetrunc->get_from_expr()));
141  }
142 
143  RetType visitExtractExpr(const Analyzer::ExtractExpr* extract) const override {
144  return makeExpr<Analyzer::ExtractExpr>(extract->get_type_info(),
145  extract->get_contains_agg(),
146  extract->get_field(),
147  visit(extract->get_from_expr()));
148  }
149 
150  RetType visitArrayOper(const Analyzer::ArrayExpr* array_expr) const override {
151  std::vector<std::shared_ptr<Analyzer::Expr>> args_copy;
152  for (size_t i = 0; i < array_expr->getElementCount(); ++i) {
153  args_copy.push_back(visit(array_expr->getElement(i)));
154  }
155  const auto& type_info = array_expr->get_type_info();
156  return makeExpr<Analyzer::ArrayExpr>(
157  type_info, args_copy, array_expr->isNull(), array_expr->isLocalAlloc());
158  }
159 
160  RetType visitGeoUOper(const Analyzer::GeoUOper* geo_expr) const override {
161  std::vector<std::shared_ptr<Analyzer::Expr>> args0_copy;
162  for (const auto& arg : geo_expr->getArgs0()) {
163  args0_copy.push_back(visit(arg.get()));
164  }
165  const auto& ti0 = geo_expr->getTypeInfo0();
166  const auto& type_info = geo_expr->get_type_info();
167  return makeExpr<Analyzer::GeoUOper>(geo_expr->getOp(), type_info, ti0, args0_copy);
168  }
169 
170  RetType visitGeoBinOper(const Analyzer::GeoBinOper* geo_expr) const override {
171  std::vector<std::shared_ptr<Analyzer::Expr>> args0_copy;
172  for (const auto& arg : geo_expr->getArgs0()) {
173  args0_copy.push_back(visit(arg.get()));
174  }
175  std::vector<std::shared_ptr<Analyzer::Expr>> args1_copy;
176  for (const auto& arg : geo_expr->getArgs1()) {
177  args1_copy.push_back(visit(arg.get()));
178  }
179  const auto& ti0 = geo_expr->getTypeInfo0();
180  const auto& ti1 = geo_expr->getTypeInfo1();
181  const auto& type_info = geo_expr->get_type_info();
182  return makeExpr<Analyzer::GeoBinOper>(
183  geo_expr->getOp(), type_info, ti0, ti1, args0_copy, args1_copy);
184  }
185 
187  const Analyzer::WindowFunction* window_func) const override {
188  std::vector<std::shared_ptr<Analyzer::Expr>> args_copy;
189  for (const auto& arg : window_func->getArgs()) {
190  args_copy.push_back(visit(arg.get()));
191  }
192  std::vector<std::shared_ptr<Analyzer::Expr>> partition_keys_copy;
193  for (const auto& partition_key : window_func->getPartitionKeys()) {
194  partition_keys_copy.push_back(visit(partition_key.get()));
195  }
196  std::vector<std::shared_ptr<Analyzer::Expr>> order_keys_copy;
197  for (const auto& order_key : window_func->getOrderKeys()) {
198  order_keys_copy.push_back(visit(order_key.get()));
199  }
200  const auto& type_info = window_func->get_type_info();
201  return makeExpr<Analyzer::WindowFunction>(
202  type_info,
203  window_func->getKind(),
204  args_copy,
205  partition_keys_copy,
206  order_keys_copy,
207  window_func->getFrameBoundType(),
208  window_func->getFrameStartBound()->deep_copy(),
209  window_func->getFrameEndBound()->deep_copy(),
210  window_func->getCollation());
211  }
212 
213  RetType visitStringOper(const Analyzer::StringOper* string_oper) const override {
214  return string_oper->deep_copy();
215  }
216 
217  RetType visitFunctionOper(const Analyzer::FunctionOper* func_oper) const override {
218  std::vector<std::shared_ptr<Analyzer::Expr>> args_copy;
219  for (size_t i = 0; i < func_oper->getArity(); ++i) {
220  args_copy.push_back(visit(func_oper->getArg(i)));
221  }
222  const auto& type_info = func_oper->get_type_info();
223  return makeExpr<Analyzer::FunctionOper>(type_info, func_oper->getName(), args_copy);
224  }
225 
226  RetType visitDatediffExpr(const Analyzer::DatediffExpr* datediff) const override {
227  return makeExpr<Analyzer::DatediffExpr>(datediff->get_type_info(),
228  datediff->get_field(),
229  visit(datediff->get_start_expr()),
230  visit(datediff->get_end_expr()));
231  }
232 
233  RetType visitDateaddExpr(const Analyzer::DateaddExpr* dateadd) const override {
234  return makeExpr<Analyzer::DateaddExpr>(dateadd->get_type_info(),
235  dateadd->get_field(),
236  visit(dateadd->get_number_expr()),
237  visit(dateadd->get_datetime_expr()));
238  }
239 
241  const Analyzer::FunctionOperWithCustomTypeHandling* func_oper) const override {
242  std::vector<std::shared_ptr<Analyzer::Expr>> args_copy;
243  for (size_t i = 0; i < func_oper->getArity(); ++i) {
244  args_copy.push_back(visit(func_oper->getArg(i)));
245  }
246  const auto& type_info = func_oper->get_type_info();
247  return makeExpr<Analyzer::FunctionOperWithCustomTypeHandling>(
248  type_info, func_oper->getName(), args_copy);
249  }
250 
251  RetType visitLikelihood(const Analyzer::LikelihoodExpr* likelihood) const override {
252  return makeExpr<Analyzer::LikelihoodExpr>(visit(likelihood->get_arg()),
253  likelihood->get_likelihood());
254  }
255 
256  RetType visitAggExpr(const Analyzer::AggExpr* agg) const override {
257  RetType arg = agg->get_arg() ? visit(agg->get_arg()) : nullptr;
258  return makeExpr<Analyzer::AggExpr>(agg->get_type_info(),
259  agg->get_aggtype(),
260  arg,
261  agg->get_is_distinct(),
262  agg->get_arg1());
263  }
264 
266  return makeExpr<Analyzer::OffsetInFragment>();
267  }
268 };
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:1269
SqlWindowFunctionKind getKind() const
Definition: Analyzer.h:2925
const Expr * get_from_expr() const
Definition: Analyzer.h:1432
const Expr * get_partition_count() const
Definition: Analyzer.h:1201
RetType visitPCAProject(const Analyzer::PCAProjectExpr *expr) const override
const Expr * get_else_expr() const
Definition: Analyzer.h:1387
const SQLTypeInfo getTypeInfo0() const
Definition: Analyzer.h:3091
RetType visitVar(const Analyzer::Var *var) const override
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs0() const
Definition: Analyzer.h:3093
const std::vector< int64_t > & get_value_list() const
Definition: Analyzer.h:695
const Expr * get_escape_expr() const
Definition: Analyzer.h:1064
size_t getArity() const
Definition: Analyzer.h:2746
const SQLTypeInfo getTypeInfo0() const
Definition: Analyzer.h:3062
RetType visitFunctionOper(const Analyzer::FunctionOper *func_oper) const override
RetType visitConstant(const Analyzer::Constant *constant) const override
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:3684
ExtractField get_field() const
Definition: Analyzer.h:1431
const Expr * get_escape_expr() const
Definition: Analyzer.h:1136
const Expr * get_right_operand() const
Definition: Analyzer.h:456
RetType visitDatetruncExpr(const Analyzer::DatetruncExpr *datetrunc) const override
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:87
RetType visitArrayOper(const Analyzer::ArrayExpr *array_expr) const override
bool get_contains_agg() const
Definition: Analyzer.h:81
RetType visitWidthBucket(const Analyzer::WidthBucketExpr *width_bucket_expr) const override
const SQLTypeInfo getTypeInfo1() const
Definition: Analyzer.h:3092
const Expr * get_arg() const
Definition: Analyzer.h:1133
Expr * get_arg() const
Definition: Analyzer.h:1330
DatetruncField get_field() const
Definition: Analyzer.h:1566
const Expr * get_arg() const
Definition: Analyzer.h:1267
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:66
const Analyzer::WindowFrame * getFrameStartBound() const
Definition: Analyzer.h:2937
std::shared_ptr< Analyzer::Expr > visit(const Analyzer::Expr *expr) const
bool isNull() const
Definition: Analyzer.h:3025
const Analyzer::WindowFrame * getFrameEndBound() const
Definition: Analyzer.h:2944
const std::vector< std::shared_ptr< Analyzer::Expr > > & getOrderKeys() const
Definition: Analyzer.h:2933
const std::vector< OrderEntry > & getCollation() const
Definition: Analyzer.h:2951
std::shared_ptr< Analyzer::Expr > RetType
const Expr * get_arg() const
Definition: Analyzer.h:1061
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:76
bool get_calc_encoded_length() const
Definition: Analyzer.h:870
RetType visitGeoUOper(const Analyzer::GeoUOper *geo_expr) const override
SQLOps get_optype() const
Definition: Analyzer.h:452
RetType visitLikeExpr(const Analyzer::LikeExpr *like) const override
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:234
RetType visitOffsetInFragment(const Analyzer::OffsetInFragment *) const override
RetType visitExtractExpr(const Analyzer::ExtractExpr *extract) const override
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs() const
Definition: Analyzer.h:2927
RetType visitCardinality(const Analyzer::CardinalityExpr *cardinality) const override
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:136
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:144
RetType visitLikelihood(const Analyzer::LikelihoodExpr *likelihood) const override
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs0() const
Definition: Analyzer.h:3063
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:1066
bool get_is_distinct() const
Definition: Analyzer.h:1332
const Expr * get_start_expr() const
Definition: Analyzer.h:1525
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:79
size_t getElementCount() const
Definition: Analyzer.h:3023
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int32_t char_length(const char *str, const int32_t str_len)
std::shared_ptr< Analyzer::Expr > get_arg1() const
Definition: Analyzer.h:1333
const Expr * get_pattern_expr() const
Definition: Analyzer.h:1135
Expression class for string functions The &quot;arg&quot; constructor parameter must be an expression that reso...
Definition: Analyzer.h:1601
const Expr * get_from_expr() const
Definition: Analyzer.h:1567
RetType visitMLPredict(const Analyzer::MLPredictExpr *expr) const override
const Expr * get_datetime_expr() const
Definition: Analyzer.h:1478
bool isLocalAlloc() const
Definition: Analyzer.h:3024
const Expr * get_like_expr() const
Definition: Analyzer.h:1063
const Analyzer::Expr * getArg(const size_t i) const
Definition: Analyzer.h:2748
RetType visitUOper(const Analyzer::UOper *uoper) const override
const Expr * get_operand() const
Definition: Analyzer.h:384
const Expr * get_arg() const
Definition: Analyzer.h:962
RetType visitCharLength(const Analyzer::CharLengthExpr *char_length) const override
const Expr * get_arg() const
Definition: Analyzer.h:868
const std::list< std::shared_ptr< Analyzer::Expr > > & get_value_list() const
Definition: Analyzer.h:646
virtual std::shared_ptr< Analyzer::Expr > deep_copy() const =0
DateaddField get_field() const
Definition: Analyzer.h:1476
SQLAgg get_aggtype() const
Definition: Analyzer.h:1329
const Expr * get_target_value() const
Definition: Analyzer.h:1198
const Expr * get_arg() const
Definition: Analyzer.h:917
const Expr * get_end_expr() const
Definition: Analyzer.h:1526
Geospatial::GeoBase::GeoOp getOp() const
Definition: Analyzer.h:3061
Analyzer::WindowFunction::FrameBoundType getFrameBoundType() const
Definition: Analyzer.h:2953
RetType visitStringOper(const Analyzer::StringOper *string_oper) const override
RetType visitCaseExpr(const Analyzer::CaseExpr *case_expr) const override
const Expr * get_left_operand() const
Definition: Analyzer.h:455
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
const Expr * get_lower_bound() const
Definition: Analyzer.h:1199
std::string getName() const
Definition: Analyzer.h:2744
RetType visitDatediffExpr(const Analyzer::DatediffExpr *datediff) const override
const Expr * get_arg() const
Definition: Analyzer.h:693
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:398
const Expr * get_upper_bound() const
Definition: Analyzer.h:1200
const std::vector< std::shared_ptr< Analyzer::Expr > > & getPartitionKeys() const
Definition: Analyzer.h:2929
const Expr * get_arg() const
Definition: Analyzer.h:1007
RetType visitGeoBinOper(const Analyzer::GeoBinOper *geo_expr) const override
DatetruncField get_field() const
Definition: Analyzer.h:1524
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:91
Geospatial::GeoBase::GeoOp getOp() const
Definition: Analyzer.h:3090
const Expr * get_arg() const
Definition: Analyzer.h:644
RetType visitDateaddExpr(const Analyzer::DateaddExpr *dateadd) const override
SQLOps get_optype() const
Definition: Analyzer.h:383
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs1() const
Definition: Analyzer.h:3094
const std::list< std::pair< std::shared_ptr< Analyzer::Expr >, std::shared_ptr< Analyzer::Expr > > > & get_expr_pair_list() const
Definition: Analyzer.h:1384
RetType visitWindowFunction(const Analyzer::WindowFunction *window_func) const override
const Expr * get_number_expr() const
Definition: Analyzer.h:1477
bool get_is_ilike() const
Definition: Analyzer.h:1065
const Analyzer::Expr * getElement(const size_t i) const
Definition: Analyzer.h:3027
SQLQualifier get_qualifier() const
Definition: Analyzer.h:454
RetType visitColumnVar(const Analyzer::ColumnVar *col_var) const override
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int32_t width_bucket_expr(const double target_value, const bool reversed, const double lower_bound, const double upper_bound, const int32_t partition_count)