OmniSciDB  8a228a1076
Parser::QuerySpec Class Reference

#include <ParserNode.h>

+ Inheritance diagram for Parser::QuerySpec:
+ Collaboration diagram for Parser::QuerySpec:

Public Member Functions

 QuerySpec (bool d, std::list< SelectEntry *> *s, std::list< TableRef *> *f, Expr *w, std::list< Expr *> *g, Expr *h)
 
bool get_is_distinct () const
 
const std::list< std::unique_ptr< SelectEntry > > & get_select_clause () const
 
const std::list< std::unique_ptr< TableRef > > & get_from_clause () const
 
const Exprget_where_clause () const
 
const std::list< std::unique_ptr< Expr > > & get_groupby_clause () const
 
const Exprget_having_clause () const
 
void analyze (const Catalog_Namespace::Catalog &catalog, Analyzer::Query &query) const override
 
std::string to_string () const
 
- Public Member Functions inherited from Parser::Node
virtual ~Node ()
 

Private Member Functions

void analyze_from_clause (const Catalog_Namespace::Catalog &catalog, Analyzer::Query &query) const
 
void analyze_select_clause (const Catalog_Namespace::Catalog &catalog, Analyzer::Query &query) const
 
void analyze_where_clause (const Catalog_Namespace::Catalog &catalog, Analyzer::Query &query) const
 
void analyze_group_by (const Catalog_Namespace::Catalog &catalog, Analyzer::Query &query) const
 
void analyze_having_clause (const Catalog_Namespace::Catalog &catalog, Analyzer::Query &query) const
 

Private Attributes

bool is_distinct
 
std::list< std::unique_ptr< SelectEntry > > select_clause
 
std::list< std::unique_ptr< TableRef > > from_clause
 
std::unique_ptr< Exprwhere_clause
 
std::list< std::unique_ptr< Expr > > groupby_clause
 
std::unique_ptr< Exprhaving_clause
 

Detailed Description

Definition at line 1676 of file ParserNode.h.

Constructor & Destructor Documentation

◆ QuerySpec()

Parser::QuerySpec::QuerySpec ( bool  d,
std::list< SelectEntry *> *  s,
std::list< TableRef *> *  f,
Expr w,
std::list< Expr *> *  g,
Expr h 
)
inline

Definition at line 1678 of file ParserNode.h.

References CHECK.

1684  : is_distinct(d), where_clause(w), having_clause(h) {
1685  if (s) {
1686  for (const auto e : *s) {
1687  select_clause.emplace_back(e);
1688  }
1689  delete s;
1690  }
1691  CHECK(f);
1692  for (const auto e : *f) {
1693  from_clause.emplace_back(e);
1694  }
1695  delete f;
1696  if (g) {
1697  for (const auto e : *g) {
1698  groupby_clause.emplace_back(e);
1699  }
1700  delete g;
1701  }
1702  }
std::unique_ptr< Expr > where_clause
Definition: ParserNode.h:1723
std::list< std::unique_ptr< TableRef > > from_clause
Definition: ParserNode.h:1722
std::list< std::unique_ptr< SelectEntry > > select_clause
Definition: ParserNode.h:1721
std::list< std::unique_ptr< Expr > > groupby_clause
Definition: ParserNode.h:1724
#define CHECK(condition)
Definition: Logger.h:197
std::unique_ptr< Expr > having_clause
Definition: ParserNode.h:1725

Member Function Documentation

◆ analyze()

void Parser::QuerySpec::analyze ( const Catalog_Namespace::Catalog catalog,
Analyzer::Query query 
) const
overridevirtual

Implements Parser::QueryExpr.

Definition at line 1192 of file ParserNode.cpp.

References anonymous_namespace{RelAlgOptimizer.cpp}::is_distinct(), and Analyzer::Query::set_is_distinct().

1193  {
1195  analyze_from_clause(catalog, query);
1196  analyze_select_clause(catalog, query);
1197  analyze_where_clause(catalog, query);
1198  analyze_group_by(catalog, query);
1199  analyze_having_clause(catalog, query);
1200 }
void analyze_select_clause(const Catalog_Namespace::Catalog &catalog, Analyzer::Query &query) const
void analyze_group_by(const Catalog_Namespace::Catalog &catalog, Analyzer::Query &query) const
void analyze_where_clause(const Catalog_Namespace::Catalog &catalog, Analyzer::Query &query) const
void analyze_having_clause(const Catalog_Namespace::Catalog &catalog, Analyzer::Query &query) const
void analyze_from_clause(const Catalog_Namespace::Catalog &catalog, Analyzer::Query &query) const
void set_is_distinct(bool d)
Definition: Analyzer.h:1631
+ Here is the call graph for this function:

◆ analyze_from_clause()

void Parser::QuerySpec::analyze_from_clause ( const Catalog_Namespace::Catalog catalog,
Analyzer::Query query 
) const
private

Definition at line 1172 of file ParserNode.cpp.

References Analyzer::Query::add_rte(), and Catalog_Namespace::Catalog::getMetadataForTable().

1173  {
1175  for (auto& p : from_clause) {
1176  const TableDescriptor* table_desc;
1177  table_desc = catalog.getMetadataForTable(*p->get_table_name());
1178  if (table_desc == nullptr) {
1179  throw std::runtime_error("Table " + *p->get_table_name() + " does not exist.");
1180  }
1181  std::string range_var;
1182  if (p->get_range_var() == nullptr) {
1183  range_var = *p->get_table_name();
1184  } else {
1185  range_var = *p->get_range_var();
1186  }
1187  rte = new Analyzer::RangeTableEntry(range_var, table_desc, nullptr);
1188  query.add_rte(rte);
1189  }
1190 }
std::list< std::unique_ptr< TableRef > > from_clause
Definition: ParserNode.h:1722
void add_rte(RangeTableEntry *rte)
Definition: Analyzer.cpp:1349
const TableDescriptor * getMetadataForTable(const std::string &tableName, const bool populateFragmenter=true) const
Returns a pointer to a const TableDescriptor struct matching the provided tableName.
specifies the content in-memory of a row in the table metadata table
+ Here is the call graph for this function:

◆ analyze_group_by()

void Parser::QuerySpec::analyze_group_by ( const Catalog_Namespace::Catalog catalog,
Analyzer::Query query 
) const
private

Definition at line 1035 of file ParserNode.cpp.

References Analyzer::Expr::add_cast(), SQLTypeInfo::get_compression(), Parser::IntLiteral::get_intval(), Analyzer::Query::get_num_aggs(), Analyzer::Query::get_targetlist(), Analyzer::Var::get_varno(), SQLTypeInfo::is_string(), kENCODING_DICT, kENCODING_NONE, Analyzer::Var::kGROUPBY, Analyzer::Var::kOUTPUT, SQLTypeInfo::set_comp_param(), SQLTypeInfo::set_compression(), SQLTypeInfo::set_fixed_size(), Analyzer::Query::set_group_by(), and TRANSIENT_DICT_ID.

1036  {
1037  std::list<std::shared_ptr<Analyzer::Expr>> groupby;
1038  if (!groupby_clause.empty()) {
1039  int gexpr_no = 1;
1040  std::shared_ptr<Analyzer::Expr> gexpr;
1041  const std::vector<std::shared_ptr<Analyzer::TargetEntry>>& tlist =
1042  query.get_targetlist();
1043  for (auto& c : groupby_clause) {
1044  // special-case ordinal numbers in GROUP BY
1045  if (dynamic_cast<Literal*>(c.get())) {
1046  IntLiteral* i = dynamic_cast<IntLiteral*>(c.get());
1047  if (!i) {
1048  throw std::runtime_error("Invalid literal in GROUP BY clause.");
1049  }
1050  int varno = (int)i->get_intval();
1051  if (varno <= 0 || varno > static_cast<int>(tlist.size())) {
1052  throw std::runtime_error("Invalid ordinal number in GROUP BY clause.");
1053  }
1054  if (tlist[varno - 1]->get_expr()->get_contains_agg()) {
1055  throw std::runtime_error(
1056  "Ordinal number in GROUP BY cannot reference an expression containing "
1057  "aggregate "
1058  "functions.");
1059  }
1060  gexpr = makeExpr<Analyzer::Var>(
1061  tlist[varno - 1]->get_expr()->get_type_info(), Analyzer::Var::kOUTPUT, varno);
1062  } else {
1063  gexpr = c->analyze(catalog, query, Expr::TlistRefType::TLIST_REF);
1064  }
1065  const SQLTypeInfo gti = gexpr->get_type_info();
1066  bool set_new_type = false;
1067  SQLTypeInfo ti(gti);
1068  if (gti.is_string() && gti.get_compression() == kENCODING_NONE) {
1069  set_new_type = true;
1070  ti.set_compression(kENCODING_DICT);
1071  ti.set_comp_param(TRANSIENT_DICT_ID);
1072  ti.set_fixed_size();
1073  }
1074  std::shared_ptr<Analyzer::Var> v;
1075  if (std::dynamic_pointer_cast<Analyzer::Var>(gexpr)) {
1076  v = std::static_pointer_cast<Analyzer::Var>(gexpr);
1077  int n = v->get_varno();
1078  gexpr = tlist[n - 1]->get_own_expr();
1079  auto cv = std::dynamic_pointer_cast<Analyzer::ColumnVar>(gexpr);
1080  if (cv != nullptr) {
1081  // inherit all ColumnVar info for lineage.
1082  *std::static_pointer_cast<Analyzer::ColumnVar>(v) = *cv;
1083  }
1084  v->set_which_row(Analyzer::Var::kGROUPBY);
1085  v->set_varno(gexpr_no);
1086  tlist[n - 1]->set_expr(v);
1087  }
1088  if (set_new_type) {
1089  auto new_e = gexpr->add_cast(ti);
1090  groupby.push_back(new_e);
1091  if (v != nullptr) {
1092  v->set_type_info(new_e->get_type_info());
1093  }
1094  } else {
1095  groupby.push_back(gexpr);
1096  }
1097  gexpr_no++;
1098  }
1099  }
1100  if (query.get_num_aggs() > 0 || !groupby.empty()) {
1101  for (auto t : query.get_targetlist()) {
1102  auto e = t->get_expr();
1103  e->check_group_by(groupby);
1104  }
1105  }
1106  query.set_group_by(groupby);
1107 }
bool is_string() const
Definition: sqltypes.h:416
const std::vector< std::shared_ptr< TargetEntry > > & get_targetlist() const
Definition: Analyzer.h:1611
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:267
void set_group_by(std::list< std::shared_ptr< Analyzer::Expr >> &g)
Definition: Analyzer.h:1633
std::list< std::unique_ptr< Expr > > groupby_clause
Definition: ParserNode.h:1724
#define TRANSIENT_DICT_ID
Definition: sqltypes.h:198
virtual std::shared_ptr< Analyzer::Expr > add_cast(const SQLTypeInfo &new_type_info)
Definition: Analyzer.cpp:689
int get_varno() const
Definition: Analyzer.h:276
int get_num_aggs() const
Definition: Analyzer.h:1610
+ Here is the call graph for this function:

◆ analyze_having_clause()

void Parser::QuerySpec::analyze_having_clause ( const Catalog_Namespace::Catalog catalog,
Analyzer::Query query 
) const
private

Definition at line 1022 of file ParserNode.cpp.

References Analyzer::Query::get_group_by(), kBOOLEAN, and Analyzer::Query::set_having_predicate().

1023  {
1024  std::shared_ptr<Analyzer::Expr> p;
1025  if (having_clause != nullptr) {
1026  p = having_clause->analyze(catalog, query, Expr::TlistRefType::TLIST_COPY);
1027  if (p->get_type_info().get_type() != kBOOLEAN) {
1028  throw std::runtime_error("Only boolean expressions can be in HAVING clause.");
1029  }
1030  p->check_group_by(query.get_group_by());
1031  }
1032  query.set_having_predicate(p);
1033 }
const std::list< std::shared_ptr< Analyzer::Expr > > & get_group_by() const
Definition: Analyzer.h:1619
void set_having_predicate(std::shared_ptr< Analyzer::Expr > p)
Definition: Analyzer.h:1634
std::unique_ptr< Expr > having_clause
Definition: ParserNode.h:1725
+ Here is the call graph for this function:

◆ analyze_select_clause()

void Parser::QuerySpec::analyze_select_clause ( const Catalog_Namespace::Catalog catalog,
Analyzer::Query query 
) const
private

Definition at line 1122 of file ParserNode.cpp.

References Parser::Expr::analyze(), ColumnDescriptor::columnName, Analyzer::RangeTableEntry::expand_star_in_targetlist(), Analyzer::Query::get_rangetable(), Analyzer::Query::get_rte(), Analyzer::Query::get_rte_idx(), Analyzer::Query::get_targetlist_nonconst(), Catalog_Namespace::Catalog::getMetadataForColumn(), kNULLT, and kUNNEST.

1123  {
1124  std::vector<std::shared_ptr<Analyzer::TargetEntry>>& tlist =
1125  query.get_targetlist_nonconst();
1126  if (select_clause.empty()) {
1127  // this means SELECT *
1128  int rte_idx = 0;
1129  for (auto rte : query.get_rangetable()) {
1130  rte->expand_star_in_targetlist(catalog, tlist, rte_idx++);
1131  }
1132  } else {
1133  for (auto& p : select_clause) {
1134  const Parser::Expr* select_expr = p->get_select_expr();
1135  // look for the case of range_var.*
1136  if (typeid(*select_expr) == typeid(ColumnRef) &&
1137  dynamic_cast<const ColumnRef*>(select_expr)->get_column() == nullptr) {
1138  const std::string* range_var_name =
1139  dynamic_cast<const ColumnRef*>(select_expr)->get_table();
1140  int rte_idx = query.get_rte_idx(*range_var_name);
1141  if (rte_idx < 0) {
1142  throw std::runtime_error("invalid range variable name: " + *range_var_name);
1143  }
1144  Analyzer::RangeTableEntry* rte = query.get_rte(rte_idx);
1145  rte->expand_star_in_targetlist(catalog, tlist, rte_idx);
1146  } else {
1147  auto e = select_expr->analyze(catalog, query);
1148  std::string resname;
1149 
1150  if (p->get_alias() != nullptr) {
1151  resname = *p->get_alias();
1152  } else if (std::dynamic_pointer_cast<Analyzer::ColumnVar>(e) &&
1153  !std::dynamic_pointer_cast<Analyzer::Var>(e)) {
1154  auto colvar = std::static_pointer_cast<Analyzer::ColumnVar>(e);
1155  const ColumnDescriptor* col_desc = catalog.getMetadataForColumn(
1156  colvar->get_table_id(), colvar->get_column_id());
1157  resname = col_desc->columnName;
1158  }
1159  if (e->get_type_info().get_type() == kNULLT) {
1160  throw std::runtime_error(
1161  "Untyped NULL in SELECT clause. Use CAST to specify a type.");
1162  }
1163  auto o = std::static_pointer_cast<Analyzer::UOper>(e);
1164  bool unnest = (o != nullptr && o->get_optype() == kUNNEST);
1165  auto tle = std::make_shared<Analyzer::TargetEntry>(resname, e, unnest);
1166  tlist.push_back(tle);
1167  }
1168  }
1169  }
1170 }
void expand_star_in_targetlist(const Catalog_Namespace::Catalog &catalog, std::vector< std::shared_ptr< TargetEntry >> &tlist, int rte_idx)
const ColumnDescriptor * getMetadataForColumn(int tableId, const std::string &colName) const
RangeTableEntry * get_rte(int rte_idx) const
Definition: Analyzer.h:1641
std::list< std::unique_ptr< SelectEntry > > select_clause
Definition: ParserNode.h:1721
specifies the content in-memory of a row in the column metadata table
std::vector< std::shared_ptr< TargetEntry > > & get_targetlist_nonconst()
Definition: Analyzer.h:1614
int get_rte_idx(const std::string &range_var_name) const
Definition: Analyzer.cpp:1338
const std::vector< RangeTableEntry * > & get_rangetable() const
Definition: Analyzer.h:1617
virtual std::shared_ptr< Analyzer::Expr > analyze(const Catalog_Namespace::Catalog &catalog, Analyzer::Query &query, TlistRefType allow_tlist_ref=TLIST_NONE) const =0
std::string columnName
+ Here is the call graph for this function:

◆ analyze_where_clause()

void Parser::QuerySpec::analyze_where_clause ( const Catalog_Namespace::Catalog catalog,
Analyzer::Query query 
) const
private

Definition at line 1109 of file ParserNode.cpp.

References kBOOLEAN, and Analyzer::Query::set_where_predicate().

1110  {
1111  if (where_clause == nullptr) {
1112  query.set_where_predicate(nullptr);
1113  return;
1114  }
1115  auto p = where_clause->analyze(catalog, query, Expr::TlistRefType::TLIST_COPY);
1116  if (p->get_type_info().get_type() != kBOOLEAN) {
1117  throw std::runtime_error("Only boolean expressions can be in WHERE clause.");
1118  }
1119  query.set_where_predicate(p);
1120 }
std::unique_ptr< Expr > where_clause
Definition: ParserNode.h:1723
void set_where_predicate(std::shared_ptr< Analyzer::Expr > p)
Definition: Analyzer.h:1632
+ Here is the call graph for this function:

◆ get_from_clause()

const std::list<std::unique_ptr<TableRef> >& Parser::QuerySpec::get_from_clause ( ) const
inline

Definition at line 1707 of file ParserNode.h.

1707  {
1708  return from_clause;
1709  }
std::list< std::unique_ptr< TableRef > > from_clause
Definition: ParserNode.h:1722

◆ get_groupby_clause()

const std::list<std::unique_ptr<Expr> >& Parser::QuerySpec::get_groupby_clause ( ) const
inline

Definition at line 1711 of file ParserNode.h.

1711  {
1712  return groupby_clause;
1713  }
std::list< std::unique_ptr< Expr > > groupby_clause
Definition: ParserNode.h:1724

◆ get_having_clause()

const Expr* Parser::QuerySpec::get_having_clause ( ) const
inline

Definition at line 1714 of file ParserNode.h.

References to_string().

1714 { return having_clause.get(); }
std::unique_ptr< Expr > having_clause
Definition: ParserNode.h:1725
+ Here is the call graph for this function:

◆ get_is_distinct()

bool Parser::QuerySpec::get_is_distinct ( ) const
inline

Definition at line 1703 of file ParserNode.h.

References anonymous_namespace{RelAlgOptimizer.cpp}::is_distinct().

1703 { return is_distinct; }
+ Here is the call graph for this function:

◆ get_select_clause()

const std::list<std::unique_ptr<SelectEntry> >& Parser::QuerySpec::get_select_clause ( ) const
inline

Definition at line 1704 of file ParserNode.h.

1704  {
1705  return select_clause;
1706  }
std::list< std::unique_ptr< SelectEntry > > select_clause
Definition: ParserNode.h:1721

◆ get_where_clause()

const Expr* Parser::QuerySpec::get_where_clause ( ) const
inline

Definition at line 1710 of file ParserNode.h.

1710 { return where_clause.get(); }
std::unique_ptr< Expr > where_clause
Definition: ParserNode.h:1723

◆ to_string()

std::string Parser::QuerySpec::to_string ( ) const

Definition at line 1435 of file ParserNode.cpp.

References anonymous_namespace{RelAlgOptimizer.cpp}::is_distinct().

1435  {
1436  std::string query_str = "SELECT ";
1437  if (is_distinct) {
1438  query_str += "DISTINCT ";
1439  }
1440  if (select_clause.empty()) {
1441  query_str += "* ";
1442  } else {
1443  bool notfirst = false;
1444  for (auto& p : select_clause) {
1445  if (notfirst) {
1446  query_str += ", ";
1447  } else {
1448  notfirst = true;
1449  }
1450  query_str += p->to_string();
1451  }
1452  }
1453  query_str += " FROM ";
1454  bool notfirst = false;
1455  for (auto& p : from_clause) {
1456  if (notfirst) {
1457  query_str += ", ";
1458  } else {
1459  notfirst = true;
1460  }
1461  query_str += p->to_string();
1462  }
1463  if (where_clause) {
1464  query_str += " WHERE " + where_clause->to_string();
1465  }
1466  if (!groupby_clause.empty()) {
1467  query_str += " GROUP BY ";
1468  bool notfirst = false;
1469  for (auto& p : groupby_clause) {
1470  if (notfirst) {
1471  query_str += ", ";
1472  } else {
1473  notfirst = true;
1474  }
1475  query_str += p->to_string();
1476  }
1477  }
1478  if (having_clause) {
1479  query_str += " HAVING " + having_clause->to_string();
1480  }
1481  query_str += ";";
1482  return query_str;
1483 }
std::unique_ptr< Expr > where_clause
Definition: ParserNode.h:1723
std::list< std::unique_ptr< TableRef > > from_clause
Definition: ParserNode.h:1722
std::list< std::unique_ptr< SelectEntry > > select_clause
Definition: ParserNode.h:1721
std::list< std::unique_ptr< Expr > > groupby_clause
Definition: ParserNode.h:1724
std::unique_ptr< Expr > having_clause
Definition: ParserNode.h:1725
+ Here is the call graph for this function:

Member Data Documentation

◆ from_clause

std::list<std::unique_ptr<TableRef> > Parser::QuerySpec::from_clause
private

Definition at line 1722 of file ParserNode.h.

◆ groupby_clause

std::list<std::unique_ptr<Expr> > Parser::QuerySpec::groupby_clause
private

Definition at line 1724 of file ParserNode.h.

◆ having_clause

std::unique_ptr<Expr> Parser::QuerySpec::having_clause
private

Definition at line 1725 of file ParserNode.h.

◆ is_distinct

bool Parser::QuerySpec::is_distinct
private

Definition at line 1720 of file ParserNode.h.

◆ select_clause

std::list<std::unique_ptr<SelectEntry> > Parser::QuerySpec::select_clause
private

Definition at line 1721 of file ParserNode.h.

◆ where_clause

std::unique_ptr<Expr> Parser::QuerySpec::where_clause
private

Definition at line 1723 of file ParserNode.h.


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