OmniSciDB  0fdbebe030
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 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 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 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 visitGeoExpr (const Analyzer::GeoExpr *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
 

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 22 of file SerializeToSql.cpp.

24  : 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 210 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().

210  {
211  switch (op_type) {
212  case kEQ:
213  return "=";
214  case kNE:
215  return "<>";
216  case kLT:
217  return "<";
218  case kLE:
219  return "<=";
220  case kGT:
221  return ">";
222  case kGE:
223  return ">=";
224  case kAND:
225  return "AND";
226  case kOR:
227  return "OR";
228  case kMINUS:
229  return "-";
230  case kPLUS:
231  return "+";
232  case kMULTIPLY:
233  return "*";
234  case kDIVIDE:
235  return "/";
236  case kMODULO:
237  return "%";
238  case kARRAY_AT:
239  return "[]";
240  case kOVERLAPS:
241  return "OVERLAPS";
242  default:
243  LOG(FATAL) << "Invalid operator type: " << op_type;
244  return "";
245  }
246 }
#define LOG(tag)
Definition: Logger.h:188
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 206 of file SerializeToSql.cpp.

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

206  {
207  return agg_to_string(agg, ra_exe_unit_, catalog_);
208 }
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 85 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().

85  {
86  return visit(bin_oper->get_left_operand()) + " " +
87  binOpTypeToString(bin_oper->get_optype()) + " " +
88  visit(bin_oper->get_right_operand());
89 }
const Expr * get_right_operand() const
Definition: Analyzer.h:437
std::string visit(const Analyzer::Expr *expr) const
SQLOps get_optype() const
Definition: Analyzer.h:433
static std::string binOpTypeToString(const SQLOps op_type)
const Expr * get_left_operand() const
Definition: Analyzer.h:436

+ 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 108 of file SerializeToSql.cpp.

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

108  {
109  std::string case_str = "CASE ";
110  const auto& expr_pair_list = case_->get_expr_pair_list();
111  for (const auto& expr_pair : expr_pair_list) {
112  const auto when = "WHEN " + visit(expr_pair.first.get());
113  const auto then = " THEN " + visit(expr_pair.second.get());
114  case_str += when + then;
115  }
116  return case_str + " ELSE " + visit(case_->get_else_expr()) + " END";
117 }
const Expr * get_else_expr() const
Definition: Analyzer.h:1101
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:1098

+ 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 32 of file SerializeToSql.cpp.

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

32  {
33  return serialize_table_ref(col_var->get_table_id(), catalog_) + "." +
35  col_var->get_table_id(), col_var->get_column_id(), catalog_);
36 }
int get_table_id() const
Definition: Analyzer.h:194
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:195
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 38 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.

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

+ 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 156 of file SerializeToSql.cpp.

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

157  {
158  std::string result = func_oper->getName();
159  if (result == "||") {
160  CHECK_EQ(func_oper->getArity(), size_t(2));
161  return visit(func_oper->getArg(0)) + "||" + visit(func_oper->getArg(1));
162  }
163  if (result == "SUBSTRING") {
164  result = "SUBSTR";
165  }
166  std::vector<std::string> arg_strs;
167  for (size_t i = 0; i < func_oper->getArity(); ++i) {
168  arg_strs.push_back(visit(func_oper->getArg(i)));
169  }
170  return result + "(" + boost::algorithm::join(arg_strs, ",") + ")";
171 }
#define CHECK_EQ(x, y)
Definition: Logger.h:205
size_t getArity() const
Definition: Analyzer.h:1309
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:1311
std::string getName() const
Definition: Analyzer.h:1307

+ 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 91 of file SerializeToSql.cpp.

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

91  {
92  const auto needle = visit(in_values->get_arg());
93  const auto haystack = visitList(in_values->get_value_list());
94  return needle + " IN (" + boost::algorithm::join(haystack, ", ") + ")";
95 }
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:580
const Expr * get_arg() const
Definition: Analyzer.h:578

+ 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 97 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().

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

+ Here is the call graph for this function:

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

Definition at line 249 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().

249  {
250  std::vector<std::string> result;
251  for (const auto& expression : expressions) {
252  result.push_back(visit(expression.get()));
253  }
254  return result;
255 }
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 51 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().

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

+ 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 26 of file SerializeToSql.cpp.

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

26  {
27  auto it = ra_exe_unit_->groupby_exprs.begin();
28  std::advance(it, var->get_varno() - 1);
29  return visit(it->get());
30 }
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:275
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 173 of file SerializeToSql.cpp.

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

174  {
175  std::string result = sql_window_function_to_str(window_func->getKind());
176  {
177  const auto arg_strs = visitList(window_func->getArgs());
178  result += "(" + boost::algorithm::join(arg_strs, ",") + ")";
179  }
180  result += " OVER (";
181  {
182  const auto partition_strs = visitList(window_func->getPartitionKeys());
183  if (!partition_strs.empty()) {
184  result += "PARTITION BY " + boost::algorithm::join(partition_strs, ",");
185  }
186  }
187  {
188  std::vector<std::string> order_strs;
189  const auto& order_keys = window_func->getOrderKeys();
190  const auto& collation = window_func->getCollation();
191  CHECK_EQ(order_keys.size(), collation.size());
192  for (size_t i = 0; i < order_keys.size(); ++i) {
193  std::string order_str = visit(order_keys[i].get());
194  order_str += collation[i].is_desc ? " DESC" : " ASC";
195  // TODO: handle nulls first / last
196  order_strs.push_back(order_str);
197  }
198  if (!order_strs.empty()) {
199  result += " ORDER BY " + boost::algorithm::join(order_strs, ",");
200  }
201  }
202  result += ")";
203  return result;
204 }
#define CHECK_EQ(x, y)
Definition: Logger.h:205
SqlWindowFunctionKind getKind() const
Definition: Analyzer.h:1396
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:1404
const std::vector< OrderEntry > & getCollation() const
Definition: Analyzer.h:1408
std::vector< std::string > visitList(const List &expressions) const
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs() const
Definition: Analyzer.h:1398
std::string sql_window_function_to_str(const SqlWindowFunctionKind kind)
const std::vector< std::shared_ptr< Analyzer::Expr > > & getPartitionKeys() const
Definition: Analyzer.h:1400

+ 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: