OmniSciDB  d2f719934e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ScalarExprToSql Class Reference

#include <SerializeToSql.h>

+ Inheritance diagram for ScalarExprToSql:
+ Collaboration diagram for ScalarExprToSql:

Public Member Functions

 ScalarExprToSql (const RelAlgExecutionUnit *ra_exe_unit, const Catalog_Namespace::Catalog *catalog)
 
std::string visitVar (const Analyzer::Var *) const override
 
std::string visitColumnVar (const Analyzer::ColumnVar *col_var) const override
 
std::string visitConstant (const Analyzer::Constant *constant) const override
 
std::string visitUOper (const Analyzer::UOper *uoper) const override
 
std::string visitBinOper (const Analyzer::BinOper *bin_oper) const override
 
std::string visitInValues (const Analyzer::InValues *in_values) const override
 
std::string visitLikeExpr (const Analyzer::LikeExpr *like) const override
 
std::string visitCaseExpr (const Analyzer::CaseExpr *case_) const override
 
std::string visitFunctionOper (const Analyzer::FunctionOper *func_oper) const override
 
std::string visitWindowFunction (const Analyzer::WindowFunction *window_func) const override
 
std::string visitAggExpr (const Analyzer::AggExpr *agg) const override
 
template<typename List >
std::vector< std::string > visitList (const List &expressions) const
 
- Public Member Functions inherited from ScalarExprVisitor< std::string >
std::string visit (const Analyzer::Expr *expr) const
 

Protected Member Functions

std::string aggregateResult (const std::string &aggregate, const std::string &next_result) const override
 
std::string defaultResult () const override
 
- Protected Member Functions inherited from ScalarExprVisitor< std::string >
virtual std::string visitColumnVarTuple (const Analyzer::ExpressionTuple *) const
 
virtual std::string visitGeoExpr (const Analyzer::GeoExpr *geo_expr) const
 
virtual std::string visitInIntegerSet (const Analyzer::InIntegerSet *in_integer_set) const
 
virtual std::string visitCharLength (const Analyzer::CharLengthExpr *char_length) const
 
virtual std::string visitKeyForString (const Analyzer::KeyForStringExpr *key_for_string) const
 
virtual std::string visitSampleRatio (const Analyzer::SampleRatioExpr *sample_ratio) const
 
virtual std::string visitLower (const Analyzer::LowerExpr *lower_expr) const
 
virtual std::string visitCardinality (const Analyzer::CardinalityExpr *cardinality) const
 
virtual std::string visitRegexpExpr (const Analyzer::RegexpExpr *regexp) const
 
virtual std::string visitWidthBucket (const Analyzer::WidthBucketExpr *width_bucket_expr) const
 
virtual std::string visitDatetruncExpr (const Analyzer::DatetruncExpr *datetrunc) const
 
virtual std::string visitExtractExpr (const Analyzer::ExtractExpr *extract) const
 
virtual std::string visitFunctionOperWithCustomTypeHandling (const Analyzer::FunctionOperWithCustomTypeHandling *func_oper) const
 
virtual std::string visitArrayOper (Analyzer::ArrayExpr const *array_expr) const
 
virtual std::string visitGeoUOper (const Analyzer::GeoUOper *geo_expr) const
 
virtual std::string visitGeoBinOper (const Analyzer::GeoBinOper *geo_expr) const
 
virtual std::string visitDatediffExpr (const Analyzer::DatediffExpr *datediff) const
 
virtual std::string visitDateaddExpr (const Analyzer::DateaddExpr *dateadd) const
 
virtual std::string visitLikelihood (const Analyzer::LikelihoodExpr *likelihood) const
 
virtual std::string visitOffsetInFragment (const Analyzer::OffsetInFragment *) const
 
virtual std::string visitRangeJoinOper (const Analyzer::RangeOper *range_oper) const
 

Static Private Member Functions

static std::string binOpTypeToString (const SQLOps op_type)
 

Private Attributes

const RelAlgExecutionUnitra_exe_unit_
 
const Catalog_Namespace::Catalogcatalog_
 

Detailed Description

Definition at line 23 of file SerializeToSql.h.

Constructor & Destructor Documentation

ScalarExprToSql::ScalarExprToSql ( const RelAlgExecutionUnit ra_exe_unit,
const Catalog_Namespace::Catalog catalog 
)

Definition at line 20 of file SerializeToSql.cpp.

22  : ra_exe_unit_(ra_exe_unit), catalog_(catalog) {}
const RelAlgExecutionUnit * ra_exe_unit_
const Catalog_Namespace::Catalog * catalog_

Member Function Documentation

std::string ScalarExprToSql::aggregateResult ( const std::string &  aggregate,
const std::string &  next_result 
) const
inlineoverrideprotectedvirtual

Reimplemented from ScalarExprVisitor< std::string >.

Definition at line 55 of file SerializeToSql.h.

56  {
57  throw std::runtime_error("Expression not supported yet");
58  }
std::string ScalarExprToSql::binOpTypeToString ( const SQLOps  op_type)
staticprivate

Definition at line 186 of file SerializeToSql.cpp.

References logger::FATAL, kAND, kARRAY_AT, kDIVIDE, kEQ, kGE, kGT, kLE, kLT, kMINUS, kMODULO, kMULTIPLY, kNE, kOR, kOVERLAPS, kPLUS, and LOG.

Referenced by visitBinOper().

186  {
187  switch (op_type) {
188  case kEQ:
189  return "=";
190  case kNE:
191  return "<>";
192  case kLT:
193  return "<";
194  case kLE:
195  return "<=";
196  case kGT:
197  return ">";
198  case kGE:
199  return ">=";
200  case kAND:
201  return "AND";
202  case kOR:
203  return "OR";
204  case kMINUS:
205  return "-";
206  case kPLUS:
207  return "+";
208  case kMULTIPLY:
209  return "*";
210  case kDIVIDE:
211  return "/";
212  case kMODULO:
213  return "%";
214  case kARRAY_AT:
215  return "[]";
216  case kOVERLAPS:
217  return "OVERLAPS";
218  default:
219  LOG(FATAL) << "Invalid operator type: " << op_type;
220  return "";
221  }
222 }
#define LOG(tag)
Definition: Logger.h:205
Definition: sqldefs.h:35
Definition: sqldefs.h:36
Definition: sqldefs.h:38
Definition: sqldefs.h:30
Definition: sqldefs.h:41
Definition: sqldefs.h:37
Definition: sqldefs.h:34
Definition: sqldefs.h:40
Definition: sqldefs.h:32
Definition: sqldefs.h:33

+ Here is the caller graph for this function:

std::string ScalarExprToSql::defaultResult ( ) const
inlineoverrideprotectedvirtual

Reimplemented from ScalarExprVisitor< std::string >.

Definition at line 60 of file SerializeToSql.h.

60  {
61  throw std::runtime_error("Expression not supported yet");
62  }
std::string ScalarExprToSql::visitAggExpr ( const Analyzer::AggExpr agg) const
overridevirtual

Reimplemented from ScalarExprVisitor< std::string >.

Definition at line 182 of file SerializeToSql.cpp.

References anonymous_namespace{SerializeToSql.cpp}::agg_to_string(), catalog_, and ra_exe_unit_.

182  {
183  return agg_to_string(agg, ra_exe_unit_, catalog_);
184 }
std::string agg_to_string(const Analyzer::AggExpr *agg_expr, const RelAlgExecutionUnit *ra_exe_unit, const Catalog_Namespace::Catalog *catalog)
const RelAlgExecutionUnit * ra_exe_unit_
const Catalog_Namespace::Catalog * catalog_

+ Here is the call graph for this function:

std::string ScalarExprToSql::visitBinOper ( const Analyzer::BinOper bin_oper) const
overridevirtual

Reimplemented from ScalarExprVisitor< std::string >.

Definition at line 83 of file SerializeToSql.cpp.

References binOpTypeToString(), Analyzer::BinOper::get_left_operand(), Analyzer::BinOper::get_optype(), Analyzer::BinOper::get_right_operand(), and ScalarExprVisitor< std::string >::visit().

83  {
84  return visit(bin_oper->get_left_operand()) + " " +
85  binOpTypeToString(bin_oper->get_optype()) + " " +
86  visit(bin_oper->get_right_operand());
87 }
const Expr * get_right_operand() const
Definition: Analyzer.h:442
std::string visit(const Analyzer::Expr *expr) const
SQLOps get_optype() const
Definition: Analyzer.h:438
static std::string binOpTypeToString(const SQLOps op_type)
const Expr * get_left_operand() const
Definition: Analyzer.h:441

+ Here is the call graph for this function:

std::string ScalarExprToSql::visitCaseExpr ( const Analyzer::CaseExpr case_) const
overridevirtual

Reimplemented from ScalarExprVisitor< std::string >.

Definition at line 106 of file SerializeToSql.cpp.

References Analyzer::CaseExpr::get_else_expr(), Analyzer::CaseExpr::get_expr_pair_list(), and ScalarExprVisitor< std::string >::visit().

106  {
107  std::string case_str = "CASE ";
108  const auto& expr_pair_list = case_->get_expr_pair_list();
109  for (const auto& expr_pair : expr_pair_list) {
110  const auto when = "WHEN " + visit(expr_pair.first.get());
111  const auto then = " THEN " + visit(expr_pair.second.get());
112  case_str += when + then;
113  }
114  return case_str + " ELSE " + visit(case_->get_else_expr()) + " END";
115 }
const Expr * get_else_expr() const
Definition: Analyzer.h:1307
std::string visit(const Analyzer::Expr *expr) const
const std::list< std::pair< std::shared_ptr< Analyzer::Expr >, std::shared_ptr< Analyzer::Expr > > > & get_expr_pair_list() const
Definition: Analyzer.h:1304

+ Here is the call graph for this function:

std::string ScalarExprToSql::visitColumnVar ( const Analyzer::ColumnVar col_var) const
overridevirtual

Reimplemented from ScalarExprVisitor< std::string >.

Definition at line 30 of file SerializeToSql.cpp.

References catalog_, Analyzer::ColumnVar::get_column_id(), Analyzer::ColumnVar::get_table_id(), serialize_column_ref(), and serialize_table_ref().

30  {
31  return serialize_table_ref(col_var->get_table_id(), catalog_) + "." +
33  col_var->get_table_id(), col_var->get_column_id(), catalog_);
34 }
int get_table_id() const
Definition: Analyzer.h:193
std::string serialize_column_ref(const int table_id, const int column_id, const Catalog_Namespace::Catalog *catalog)
const Catalog_Namespace::Catalog * catalog_
int get_column_id() const
Definition: Analyzer.h:194
std::string serialize_table_ref(const int table_id, const Catalog_Namespace::Catalog *catalog)

+ Here is the call graph for this function:

std::string ScalarExprToSql::visitConstant ( const Analyzer::Constant constant) const
overridevirtual

Reimplemented from ScalarExprVisitor< std::string >.

Definition at line 36 of file SerializeToSql.cpp.

References DatumToString(), Analyzer::Constant::get_constval(), Analyzer::Constant::get_is_null(), Analyzer::Expr::get_type_info(), and run_benchmark_import::result.

36  {
37  if (constant->get_is_null()) {
38  return "NULL";
39  }
40  const auto& constant_ti = constant->get_type_info();
41  const auto result = DatumToString(constant->get_constval(), constant_ti);
42  if (constant_ti.is_string()) {
43  return "'" + result + "'";
44  } else {
45  return result;
46  }
47 }
std::string DatumToString(Datum d, const SQLTypeInfo &ti)
Definition: Datum.cpp:392
bool get_is_null() const
Definition: Analyzer.h:333
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:77
Datum get_constval() const
Definition: Analyzer.h:334

+ Here is the call graph for this function:

std::string ScalarExprToSql::visitFunctionOper ( const Analyzer::FunctionOper func_oper) const
overridevirtual

Reimplemented from ScalarExprVisitor< std::string >.

Definition at line 132 of file SerializeToSql.cpp.

References CHECK_EQ, Analyzer::FunctionOper::getArg(), Analyzer::FunctionOper::getArity(), Analyzer::FunctionOper::getName(), i, join(), run_benchmark_import::result, and ScalarExprVisitor< std::string >::visit().

133  {
134  std::string result = func_oper->getName();
135  if (result == "||") {
136  CHECK_EQ(func_oper->getArity(), size_t(2));
137  return visit(func_oper->getArg(0)) + "||" + visit(func_oper->getArg(1));
138  }
139  if (result == "SUBSTRING") {
140  result = "SUBSTR";
141  }
142  std::vector<std::string> arg_strs;
143  for (size_t i = 0; i < func_oper->getArity(); ++i) {
144  arg_strs.push_back(visit(func_oper->getArg(i)));
145  }
146  return result + "(" + boost::algorithm::join(arg_strs, ",") + ")";
147 }
#define CHECK_EQ(x, y)
Definition: Logger.h:219
size_t getArity() const
Definition: Analyzer.h:1515
std::string join(T const &container, std::string const &delim)
std::string visit(const Analyzer::Expr *expr) const
const Analyzer::Expr * getArg(const size_t i) const
Definition: Analyzer.h:1517
std::string getName() const
Definition: Analyzer.h:1513

+ Here is the call graph for this function:

std::string ScalarExprToSql::visitInValues ( const Analyzer::InValues in_values) const
overridevirtual

Reimplemented from ScalarExprVisitor< std::string >.

Definition at line 89 of file SerializeToSql.cpp.

References Analyzer::InValues::get_arg(), Analyzer::InValues::get_value_list(), join(), ScalarExprVisitor< std::string >::visit(), and visitList().

89  {
90  const auto needle = visit(in_values->get_arg());
91  const auto haystack = visitList(in_values->get_value_list());
92  return needle + " IN (" + boost::algorithm::join(haystack, ", ") + ")";
93 }
std::string join(T const &container, std::string const &delim)
std::string visit(const Analyzer::Expr *expr) const
std::vector< std::string > visitList(const List &expressions) const
const std::list< std::shared_ptr< Analyzer::Expr > > & get_value_list() const
Definition: Analyzer.h:632
const Expr * get_arg() const
Definition: Analyzer.h:630

+ Here is the call graph for this function:

std::string ScalarExprToSql::visitLikeExpr ( const Analyzer::LikeExpr like) const
overridevirtual

Reimplemented from ScalarExprVisitor< std::string >.

Definition at line 95 of file SerializeToSql.cpp.

References Analyzer::LikeExpr::get_arg(), Analyzer::LikeExpr::get_escape_expr(), Analyzer::LikeExpr::get_like_expr(), run_benchmark_import::result, and ScalarExprVisitor< std::string >::visit().

95  {
96  const auto str = visit(like->get_arg());
97  const auto pattern = visit(like->get_like_expr());
98  const auto result = str + " LIKE " + pattern;
99  if (like->get_escape_expr()) {
100  const auto escape = visit(like->get_escape_expr());
101  return result + " ESCAPE " + escape;
102  }
103  return result;
104 }
const Expr * get_escape_expr() const
Definition: Analyzer.h:954
std::string visit(const Analyzer::Expr *expr) const
const Expr * get_arg() const
Definition: Analyzer.h:951
const Expr * get_like_expr() const
Definition: Analyzer.h:953

+ Here is the call graph for this function:

template<typename List >
std::vector< std::string > ScalarExprToSql::visitList ( const List &  expressions) const

Definition at line 225 of file SerializeToSql.cpp.

References run_benchmark_import::result, and ScalarExprVisitor< std::string >::visit().

Referenced by anonymous_namespace{SerializeToSql.cpp}::group_by_to_string(), anonymous_namespace{SerializeToSql.cpp}::join_condition_to_string(), visitInValues(), visitWindowFunction(), and anonymous_namespace{SerializeToSql.cpp}::where_to_string().

225  {
226  std::vector<std::string> result;
227  for (const auto& expression : expressions) {
228  result.push_back(visit(expression.get()));
229  }
230  return result;
231 }
std::string visit(const Analyzer::Expr *expr) const

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::string ScalarExprToSql::visitUOper ( const Analyzer::UOper uoper) const
overridevirtual

Reimplemented from ScalarExprVisitor< std::string >.

Definition at line 49 of file SerializeToSql.cpp.

References Analyzer::UOper::get_operand(), Analyzer::UOper::get_optype(), Analyzer::Expr::get_type_info(), SQLTypeInfo::get_type_name(), is_supported_type_for_extern_execution(), kCAST, kISNULL, kNOT, kUMINUS, to_string(), and ScalarExprVisitor< std::string >::visit().

49  {
50  const auto operand = uoper->get_operand();
51  const auto operand_str = visit(operand);
52  const auto optype = uoper->get_optype();
53  switch (optype) {
54  case kNOT: {
55  return "NOT (" + operand_str + ")";
56  }
57  case kUMINUS: {
58  return "-" + operand_str;
59  }
60  case kISNULL: {
61  return operand_str + " IS NULL";
62  }
63  case kCAST: {
64  const auto& operand_ti = operand->get_type_info();
65  const auto& target_ti = uoper->get_type_info();
67  throw std::runtime_error("Type not supported yet for extern execution: " +
68  target_ti.get_type_name());
69  }
70  if ((operand_ti.get_type() == target_ti.get_type()) ||
71  ((operand_ti.is_string() && target_ti.is_string()))) {
72  return operand_str;
73  }
74  return "CAST(" + operand_str + " AS " + target_ti.get_type_name() + ")";
75  }
76  default: {
77  throw std::runtime_error("Unary operator type: " + std::to_string(optype) +
78  " not supported");
79  }
80  }
81 }
bool is_supported_type_for_extern_execution(const SQLTypeInfo &ti)
Definition: sqldefs.h:49
std::string visit(const Analyzer::Expr *expr) const
std::string to_string(char const *&&v)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:77
const Expr * get_operand() const
Definition: Analyzer.h:370
std::string get_type_name() const
Definition: sqltypes.h:442
Definition: sqldefs.h:39
SQLOps get_optype() const
Definition: Analyzer.h:369

+ Here is the call graph for this function:

std::string ScalarExprToSql::visitVar ( const Analyzer::Var var) const
overridevirtual

Reimplemented from ScalarExprVisitor< std::string >.

Definition at line 24 of file SerializeToSql.cpp.

References Analyzer::Var::get_varno(), RelAlgExecutionUnit::groupby_exprs, ra_exe_unit_, and ScalarExprVisitor< std::string >::visit().

24  {
25  auto it = ra_exe_unit_->groupby_exprs.begin();
26  std::advance(it, var->get_varno() - 1);
27  return visit(it->get());
28 }
std::string visit(const Analyzer::Expr *expr) const
const std::list< std::shared_ptr< Analyzer::Expr > > groupby_exprs
int get_varno() const
Definition: Analyzer.h:274
const RelAlgExecutionUnit * ra_exe_unit_

+ Here is the call graph for this function:

std::string ScalarExprToSql::visitWindowFunction ( const Analyzer::WindowFunction window_func) const
overridevirtual

Reimplemented from ScalarExprVisitor< std::string >.

Definition at line 149 of file SerializeToSql.cpp.

References CHECK_EQ, Analyzer::WindowFunction::getArgs(), Analyzer::WindowFunction::getCollation(), Analyzer::WindowFunction::getKind(), Analyzer::WindowFunction::getOrderKeys(), Analyzer::WindowFunction::getPartitionKeys(), i, join(), run_benchmark_import::result, toString(), ScalarExprVisitor< std::string >::visit(), and visitList().

150  {
151  std::string result = ::toString(window_func->getKind());
152  {
153  const auto arg_strs = visitList(window_func->getArgs());
154  result += "(" + boost::algorithm::join(arg_strs, ",") + ")";
155  }
156  result += " OVER (";
157  {
158  const auto partition_strs = visitList(window_func->getPartitionKeys());
159  if (!partition_strs.empty()) {
160  result += "PARTITION BY " + boost::algorithm::join(partition_strs, ",");
161  }
162  }
163  {
164  std::vector<std::string> order_strs;
165  const auto& order_keys = window_func->getOrderKeys();
166  const auto& collation = window_func->getCollation();
167  CHECK_EQ(order_keys.size(), collation.size());
168  for (size_t i = 0; i < order_keys.size(); ++i) {
169  std::string order_str = visit(order_keys[i].get());
170  order_str += collation[i].is_desc ? " DESC" : " ASC";
171  // TODO: handle nulls first / last
172  order_strs.push_back(order_str);
173  }
174  if (!order_strs.empty()) {
175  result += " ORDER BY " + boost::algorithm::join(order_strs, ",");
176  }
177  }
178  result += ")";
179  return result;
180 }
#define CHECK_EQ(x, y)
Definition: Logger.h:219
SqlWindowFunctionKind getKind() const
Definition: Analyzer.h:1607
std::string toString(const ExtArgumentType &sig_type)
std::string join(T const &container, std::string const &delim)
std::string visit(const Analyzer::Expr *expr) const
const std::vector< std::shared_ptr< Analyzer::Expr > > & getOrderKeys() const
Definition: Analyzer.h:1615
const std::vector< OrderEntry > & getCollation() const
Definition: Analyzer.h:1619
std::vector< std::string > visitList(const List &expressions) const
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs() const
Definition: Analyzer.h:1609
const std::vector< std::shared_ptr< Analyzer::Expr > > & getPartitionKeys() const
Definition: Analyzer.h:1611

+ Here is the call graph for this function:

Member Data Documentation

const Catalog_Namespace::Catalog* ScalarExprToSql::catalog_
private

Definition at line 68 of file SerializeToSql.h.

Referenced by visitAggExpr(), and visitColumnVar().

const RelAlgExecutionUnit* ScalarExprToSql::ra_exe_unit_
private

Definition at line 67 of file SerializeToSql.h.

Referenced by visitAggExpr(), and visitVar().


The documentation for this class was generated from the following files: