OmniSciDB  a5dc49c757
 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)
 
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 visitStringOper (const Analyzer::StringOper *string_oper) 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 visitMLPredict (const Analyzer::MLPredictExpr *ml_predict_expr) const
 
virtual std::string visitPCAProject (const Analyzer::PCAProjectExpr *pca_project_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
 
virtual void visitBegin () const
 

Static Private Member Functions

static std::string binOpTypeToString (const SQLOps op_type)
 

Private Attributes

const RelAlgExecutionUnitra_exe_unit_
 

Detailed Description

Definition at line 23 of file SerializeToSql.h.

Constructor & Destructor Documentation

ScalarExprToSql::ScalarExprToSql ( const RelAlgExecutionUnit ra_exe_unit)

Definition at line 20 of file SerializeToSql.cpp.

21  : ra_exe_unit_(ra_exe_unit) {}
const RelAlgExecutionUnit * ra_exe_unit_

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 54 of file SerializeToSql.h.

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

Definition at line 186 of file SerializeToSql.cpp.

References logger::FATAL, kAND, kARRAY_AT, kBBOX_INTERSECT, kDIVIDE, kEQ, kGE, kGT, kLE, kLT, kMINUS, kMODULO, kMULTIPLY, kNE, kOR, 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 kBBOX_INTERSECT:
217  return "BBOX_INTERSECT";
218  default:
219  LOG(FATAL) << "Invalid operator type: " << op_type;
220  return "";
221  }
222 }
#define LOG(tag)
Definition: Logger.h:285
Definition: sqldefs.h:37
Definition: sqldefs.h:38
Definition: sqldefs.h:40
Definition: sqldefs.h:32
Definition: sqldefs.h:43
Definition: sqldefs.h:39
Definition: sqldefs.h:36
Definition: sqldefs.h:42
Definition: sqldefs.h:34
Definition: sqldefs.h:35

+ Here is the caller graph for this function:

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

Reimplemented from ScalarExprVisitor< std::string >.

Definition at line 59 of file SerializeToSql.h.

59  {
60  throw std::runtime_error("Expression not supported yet");
61  }
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(), and ra_exe_unit_.

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

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

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

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

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

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

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

References Catalog_Namespace::SysCatalog::getCatalog(), Analyzer::ColumnVar::getColumnKey(), Catalog_Namespace::SysCatalog::instance(), serialize_column_ref(), and serialize_table_ref().

29  {
30  const auto& column_key = col_var->getColumnKey();
31  const auto catalog =
33  return serialize_table_ref(column_key.table_id, catalog.get()) + "." +
34  serialize_column_ref(column_key.table_id, column_key.column_id, catalog.get());
35 }
std::string serialize_column_ref(const int table_id, const int column_id, const Catalog_Namespace::Catalog *catalog)
static SysCatalog & instance()
Definition: SysCatalog.h:343
std::shared_ptr< Catalog > getCatalog(const std::string &dbName)
const shared::ColumnKey & getColumnKey() const
Definition: Analyzer.h:198
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 37 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.

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

+ 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(), 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:301
size_t getArity() const
Definition: Analyzer.h:2746
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:2748
std::string getName() const
Definition: Analyzer.h:2744

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

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

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

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

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

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

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

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

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

23  {
24  auto it = ra_exe_unit_->groupby_exprs.begin();
25  std::advance(it, var->get_varno() - 1);
26  return visit(it->get());
27 }
std::string visit(const Analyzer::Expr *expr) const
const std::list< std::shared_ptr< Analyzer::Expr > > groupby_exprs
int32_t get_varno() const
Definition: Analyzer.h:288
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(), 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:301
SqlWindowFunctionKind getKind() const
Definition: Analyzer.h:2925
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:2933
const std::vector< OrderEntry > & getCollation() const
Definition: Analyzer.h:2951
std::vector< std::string > visitList(const List &expressions) const
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs() const
Definition: Analyzer.h:2927
std::string toString(const Executor::ExtModuleKinds &kind)
Definition: Execute.h:1703
const std::vector< std::shared_ptr< Analyzer::Expr > > & getPartitionKeys() const
Definition: Analyzer.h:2929

+ Here is the call graph for this function:

Member Data Documentation

const RelAlgExecutionUnit* ScalarExprToSql::ra_exe_unit_
private

Definition at line 66 of file SerializeToSql.h.

Referenced by visitAggExpr(), and visitVar().


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