OmniSciDB  bf83d84833
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SerializeToSql.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019 OmniSci, 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 "SerializeToSql.h"
18 #include "ExternalExecutor.h"
19 
21  const Catalog_Namespace::Catalog* catalog)
22  : ra_exe_unit_(ra_exe_unit), catalog_(catalog) {}
23 
24 std::string ScalarExprToSql::visitVar(const Analyzer::Var* var) const {
25  auto it = ra_exe_unit_->groupby_exprs.begin();
26  std::advance(it, var->get_varno() - 1);
27  return visit(it->get());
28 }
29 
30 std::string ScalarExprToSql::visitColumnVar(const Analyzer::ColumnVar* col_var) const {
31  return serialize_table_ref(col_var->get_table_id(), catalog_) + "." +
33  col_var->get_table_id(), col_var->get_column_id(), catalog_);
34 }
35 
36 std::string ScalarExprToSql::visitConstant(const Analyzer::Constant* constant) const {
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 }
48 
49 std::string ScalarExprToSql::visitUOper(const Analyzer::UOper* uoper) const {
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 }
82 
83 std::string ScalarExprToSql::visitBinOper(const Analyzer::BinOper* bin_oper) const {
84  return visit(bin_oper->get_left_operand()) + " " +
85  binOpTypeToString(bin_oper->get_optype()) + " " +
86  visit(bin_oper->get_right_operand());
87 }
88 
89 std::string ScalarExprToSql::visitInValues(const Analyzer::InValues* in_values) const {
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 }
94 
95 std::string ScalarExprToSql::visitLikeExpr(const Analyzer::LikeExpr* like) const {
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 }
105 
106 std::string ScalarExprToSql::visitCaseExpr(const Analyzer::CaseExpr* case_) const {
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 }
116 
117 namespace {
118 
119 std::string agg_to_string(const Analyzer::AggExpr* agg_expr,
120  const RelAlgExecutionUnit* ra_exe_unit,
121  const Catalog_Namespace::Catalog* catalog) {
122  ScalarExprToSql scalar_expr_to_sql(ra_exe_unit, catalog);
123  const auto agg_type = ::toString(agg_expr->get_aggtype());
124  const auto arg =
125  agg_expr->get_arg() ? scalar_expr_to_sql.visit(agg_expr->get_arg()) : "*";
126  const auto distinct = agg_expr->get_is_distinct() ? "DISTINCT " : "";
127  return agg_type + "(" + distinct + arg + ")";
128 }
129 
130 } // namespace
131 
133  const Analyzer::FunctionOper* func_oper) const {
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 }
148 
150  const Analyzer::WindowFunction* window_func) const {
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 }
181 
182 std::string ScalarExprToSql::visitAggExpr(const Analyzer::AggExpr* agg) const {
183  return agg_to_string(agg, ra_exe_unit_, catalog_);
184 }
185 
186 std::string ScalarExprToSql::binOpTypeToString(const SQLOps op_type) {
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 }
223 
224 template <typename List>
225 std::vector<std::string> ScalarExprToSql::visitList(const List& expressions) const {
226  std::vector<std::string> result;
227  for (const auto& expression : expressions) {
228  result.push_back(visit(expression.get()));
229  }
230  return result;
231 }
232 
233 namespace {
234 
235 std::string where_to_string(const RelAlgExecutionUnit* ra_exe_unit,
236  const Catalog_Namespace::Catalog* catalog) {
237  ScalarExprToSql scalar_expr_to_sql(ra_exe_unit, catalog);
238  auto qual_strings = scalar_expr_to_sql.visitList(ra_exe_unit->quals);
239  const auto simple_qual_strings =
240  scalar_expr_to_sql.visitList(ra_exe_unit->simple_quals);
241  qual_strings.insert(
242  qual_strings.end(), simple_qual_strings.begin(), simple_qual_strings.end());
243  return boost::algorithm::join(qual_strings, " AND ");
244 }
245 
246 std::string join_condition_to_string(const RelAlgExecutionUnit* ra_exe_unit,
247  const Catalog_Namespace::Catalog* catalog) {
248  ScalarExprToSql scalar_expr_to_sql(ra_exe_unit, catalog);
249  std::vector<std::string> qual_strings;
250  for (const auto& join_level_quals : ra_exe_unit->join_quals) {
251  const auto level_qual_strings = scalar_expr_to_sql.visitList(join_level_quals.quals);
252  qual_strings.insert(
253  qual_strings.end(), level_qual_strings.begin(), level_qual_strings.end());
254  }
255  return boost::algorithm::join(qual_strings, " AND ");
256 }
257 
258 std::string targets_to_string(const RelAlgExecutionUnit* ra_exe_unit,
259  const Catalog_Namespace::Catalog* catalog) {
260  ScalarExprToSql scalar_expr_to_sql(ra_exe_unit, catalog);
261  std::vector<std::string> target_strings;
262  for (const auto target : ra_exe_unit->target_exprs) {
263  target_strings.push_back(scalar_expr_to_sql.visit(target));
264  }
265  return boost::algorithm::join(target_strings, ", ");
266 }
267 
268 std::string group_by_to_string(const RelAlgExecutionUnit* ra_exe_unit,
269  const Catalog_Namespace::Catalog* catalog) {
270  if (ra_exe_unit->groupby_exprs.size() == 1 || !ra_exe_unit->groupby_exprs.front()) {
271  return "";
272  }
273  ScalarExprToSql scalar_expr_to_sql(ra_exe_unit, catalog);
274  const auto group_by_strings = scalar_expr_to_sql.visitList(ra_exe_unit->groupby_exprs);
275  return boost::algorithm::join(group_by_strings, ", ");
276 }
277 
278 std::string from_to_string(const RelAlgExecutionUnit* ra_exe_unit,
279  const Catalog_Namespace::Catalog* catalog) {
280  std::vector<std::string> from_strings;
281  for (const auto& input_desc : ra_exe_unit->input_descs) {
282  const auto table_ref = serialize_table_ref(input_desc.getTableId(), catalog);
283  from_strings.push_back(table_ref);
284  }
285  return boost::algorithm::join(from_strings, ", ");
286 }
287 
288 std::string maybe(const std::string& prefix, const std::string& clause) {
289  return clause.empty() ? "" : " " + prefix + " " + clause;
290 }
291 
292 } // namespace
293 
294 std::string serialize_table_ref(const int table_id,
295  const Catalog_Namespace::Catalog* catalog) {
296  if (table_id >= 0) {
297  const auto td = catalog->getMetadataForTable(table_id);
298  CHECK(td);
299  return td->tableName;
300  }
301  return "\"#temp" + std::to_string(table_id) + "\"";
302 }
303 
304 std::string serialize_column_ref(const int table_id,
305  const int column_id,
306  const Catalog_Namespace::Catalog* catalog) {
307  if (table_id >= 0) {
308  const auto cd = catalog->getMetadataForColumn(table_id, column_id);
309  CHECK(cd);
310  return cd->columnName;
311  }
312  return "col" + std::to_string(column_id);
313 }
314 
316  const Catalog_Namespace::Catalog* catalog) {
317  const auto targets = targets_to_string(ra_exe_unit, catalog);
318  const auto from = from_to_string(ra_exe_unit, catalog);
319  const auto join_on = join_condition_to_string(ra_exe_unit, catalog);
320  const auto where = where_to_string(ra_exe_unit, catalog);
321  const auto group = group_by_to_string(ra_exe_unit, catalog);
322  return {"SELECT " + targets + " FROM " + from + maybe("ON", join_on) +
323  maybe("WHERE", where) + maybe("GROUP BY", group),
324  from};
325 }
int get_table_id() const
Definition: Analyzer.h:194
bool is_supported_type_for_extern_execution(const SQLTypeInfo &ti)
catalog_(nullptr)
std::vector< Analyzer::Expr * > target_exprs
#define CHECK_EQ(x, y)
Definition: Logger.h:205
SqlWindowFunctionKind getKind() const
Definition: Analyzer.h:1447
std::string visitColumnVar(const Analyzer::ColumnVar *col_var) const override
const Expr * get_else_expr() const
Definition: Analyzer.h:1152
std::string DatumToString(Datum d, const SQLTypeInfo &ti)
Definition: Datum.cpp:240
const Expr * get_escape_expr() const
Definition: Analyzer.h:908
std::string toString(const ExtArgumentType &sig_type)
size_t getArity() const
Definition: Analyzer.h:1360
class for a per-database catalog. also includes metadata for the current database and the current use...
Definition: Catalog.h:101
#define LOG(tag)
Definition: Logger.h:188
const Expr * get_right_operand() const
Definition: Analyzer.h:443
SQLOps
Definition: sqldefs.h:29
Definition: sqldefs.h:35
ScalarExprToSql(const RelAlgExecutionUnit *ra_exe_unit, const Catalog_Namespace::Catalog *catalog)
std::string maybe(const std::string &prefix, const std::string &clause)
Definition: sqldefs.h:36
std::string join(T const &container, std::string const &delim)
bool get_is_null() const
Definition: Analyzer.h:334
Definition: sqldefs.h:38
std::vector< InputDescriptor > input_descs
std::string visitWindowFunction(const Analyzer::WindowFunction *window_func) const override
Definition: sqldefs.h:49
Definition: sqldefs.h:30
Expr * get_arg() const
Definition: Analyzer.h:1096
Definition: sqldefs.h:41
std::string visit(const Analyzer::Expr *expr) const
const std::list< std::shared_ptr< Analyzer::Expr > > groupby_exprs
const std::vector< std::shared_ptr< Analyzer::Expr > > & getOrderKeys() const
Definition: Analyzer.h:1455
std::string visitBinOper(const Analyzer::BinOper *bin_oper) const override
const std::vector< OrderEntry > & getCollation() const
Definition: Analyzer.h:1459
const Expr * get_arg() const
Definition: Analyzer.h:905
std::vector< std::string > visitList(const List &expressions) const
std::string visitConstant(const Analyzer::Constant *constant) const override
std::string to_string(char const *&&v)
std::string from_to_string(const RelAlgExecutionUnit *ra_exe_unit, const Catalog_Namespace::Catalog *catalog)
std::string serialize_column_ref(const int table_id, const int column_id, const Catalog_Namespace::Catalog *catalog)
std::string visitAggExpr(const Analyzer::AggExpr *agg) const override
SQLOps get_optype() const
Definition: Analyzer.h:439
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs() const
Definition: Analyzer.h:1449
const JoinQualsPerNestingLevel join_quals
std::string visitCaseExpr(const Analyzer::CaseExpr *case_) const override
const ColumnDescriptor * getMetadataForColumn(int tableId, const std::string &colName) const
Definition: sqldefs.h:37
int get_varno() const
Definition: Analyzer.h:275
bool get_is_distinct() const
Definition: Analyzer.h:1098
std::string group_by_to_string(const RelAlgExecutionUnit *ra_exe_unit, const Catalog_Namespace::Catalog *catalog)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:78
std::string visitVar(const Analyzer::Var *) const override
Definition: sqldefs.h:34
std::string visitLikeExpr(const Analyzer::LikeExpr *like) const override
std::string agg_to_string(const Analyzer::AggExpr *agg_expr, const RelAlgExecutionUnit *ra_exe_unit, const Catalog_Namespace::Catalog *catalog)
Definition: sqldefs.h:40
static std::string binOpTypeToString(const SQLOps op_type)
const Expr * get_like_expr() const
Definition: Analyzer.h:907
const Analyzer::Expr * getArg(const size_t i) const
Definition: Analyzer.h:1362
const Expr * get_operand() const
Definition: Analyzer.h:371
Datum get_constval() const
Definition: Analyzer.h:335
Definition: sqldefs.h:32
const std::list< std::shared_ptr< Analyzer::Expr > > & get_value_list() const
Definition: Analyzer.h:586
std::string get_type_name() const
Definition: sqltypes.h:414
const RelAlgExecutionUnit * ra_exe_unit_
SQLAgg get_aggtype() const
Definition: Analyzer.h:1095
std::list< std::shared_ptr< Analyzer::Expr > > quals
std::string visitFunctionOper(const Analyzer::FunctionOper *func_oper) const override
#define CHECK(condition)
Definition: Logger.h:197
std::string visitUOper(const Analyzer::UOper *uoper) const override
const Catalog_Namespace::Catalog * catalog_
Definition: sqldefs.h:33
const Expr * get_left_operand() const
Definition: Analyzer.h:442
const TableDescriptor * getMetadataForTable(const std::string &tableName, const bool populateFragmenter=true) const
Returns a pointer to a const TableDescriptor struct matching the provided tableName.
int get_column_id() const
Definition: Analyzer.h:195
std::string getName() const
Definition: Analyzer.h:1358
std::string targets_to_string(const RelAlgExecutionUnit *ra_exe_unit, const Catalog_Namespace::Catalog *catalog)
std::string visitInValues(const Analyzer::InValues *in_values) const override
ExecutionUnitSql serialize_to_sql(const RelAlgExecutionUnit *ra_exe_unit, const Catalog_Namespace::Catalog *catalog)
const std::vector< std::shared_ptr< Analyzer::Expr > > & getPartitionKeys() const
Definition: Analyzer.h:1451
std::string join_condition_to_string(const RelAlgExecutionUnit *ra_exe_unit, const Catalog_Namespace::Catalog *catalog)
std::string serialize_table_ref(const int table_id, const Catalog_Namespace::Catalog *catalog)
Definition: sqldefs.h:39
const Expr * get_arg() const
Definition: Analyzer.h:584
SQLOps get_optype() const
Definition: Analyzer.h:370
const std::list< std::pair< std::shared_ptr< Analyzer::Expr >, std::shared_ptr< Analyzer::Expr > > > & get_expr_pair_list() const
Definition: Analyzer.h:1149
std::string where_to_string(const RelAlgExecutionUnit *ra_exe_unit, const Catalog_Namespace::Catalog *catalog)
std::list< std::shared_ptr< Analyzer::Expr > > simple_quals