OmniSciDB  d2f719934e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 1719 of file ParserNode.h.

Constructor & Destructor Documentation

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 1721 of file ParserNode.h.

References CHECK, f, from_clause_, groupby_clause_, and select_clause_.

1728  if (s) {
1729  for (const auto e : *s) {
1730  select_clause_.emplace_back(e);
1731  }
1732  delete s;
1733  }
1734  CHECK(f);
1735  for (const auto e : *f) {
1736  from_clause_.emplace_back(e);
1737  }
1738  delete f;
1739  if (g) {
1740  for (const auto e : *g) {
1741  groupby_clause_.emplace_back(e);
1742  }
1743  delete g;
1744  }
1745  }
std::unique_ptr< Expr > having_clause_
Definition: ParserNode.h:1768
std::list< std::unique_ptr< Expr > > groupby_clause_
Definition: ParserNode.h:1767
std::list< std::unique_ptr< TableRef > > from_clause_
Definition: ParserNode.h:1765
std::unique_ptr< Expr > where_clause_
Definition: ParserNode.h:1766
std::list< std::unique_ptr< SelectEntry > > select_clause_
Definition: ParserNode.h:1764
#define CHECK(condition)
Definition: Logger.h:211
char * f

Member Function Documentation

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

Implements Parser::QueryExpr.

Definition at line 1266 of file ParserNode.cpp.

References Analyzer::Query::set_is_distinct().

1267  {
1269  analyze_from_clause(catalog, query);
1270  analyze_select_clause(catalog, query);
1271  analyze_where_clause(catalog, query);
1272  analyze_group_by(catalog, query);
1273  analyze_having_clause(catalog, query);
1274 }
void analyze_having_clause(const Catalog_Namespace::Catalog &catalog, Analyzer::Query &query) const
void analyze_where_clause(const Catalog_Namespace::Catalog &catalog, Analyzer::Query &query) const
void set_is_distinct(bool d)
Definition: Analyzer.h:1796
void analyze_select_clause(const Catalog_Namespace::Catalog &catalog, Analyzer::Query &query) const
void analyze_from_clause(const Catalog_Namespace::Catalog &catalog, Analyzer::Query &query) const
void analyze_group_by(const Catalog_Namespace::Catalog &catalog, Analyzer::Query &query) const

+ Here is the call graph for this function:

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

Definition at line 1246 of file ParserNode.cpp.

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

1247  {
1249  for (auto& p : from_clause_) {
1250  const TableDescriptor* table_desc;
1251  table_desc = catalog.getMetadataForTable(*p->get_table_name());
1252  if (table_desc == nullptr) {
1253  throw std::runtime_error("Table " + *p->get_table_name() + " does not exist.");
1254  }
1255  std::string range_var;
1256  if (p->get_range_var() == nullptr) {
1257  range_var = *p->get_table_name();
1258  } else {
1259  range_var = *p->get_range_var();
1260  }
1261  rte = new Analyzer::RangeTableEntry(range_var, table_desc, nullptr);
1262  query.add_rte(rte);
1263  }
1264 }
void add_rte(RangeTableEntry *rte)
Definition: Analyzer.cpp:1412
std::list< std::unique_ptr< TableRef > > from_clause_
Definition: ParserNode.h:1765
const TableDescriptor * getMetadataForTable(const std::string &tableName, const bool populateFragmenter=true) const
Returns a pointer to a const TableDescriptor struct matching the provided tableName.

+ Here is the call graph for this function:

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

Definition at line 1109 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(), i, SQLTypeInfo::is_string(), kENCODING_DICT, kENCODING_NONE, Analyzer::Var::kGROUPBY, Analyzer::Var::kOUTPUT, anonymous_namespace{Utm.h}::n, SQLTypeInfo::set_comp_param(), SQLTypeInfo::set_compression(), SQLTypeInfo::set_fixed_size(), Analyzer::Query::set_group_by(), t, and TRANSIENT_DICT_ID.

1110  {
1111  std::list<std::shared_ptr<Analyzer::Expr>> groupby;
1112  if (!groupby_clause_.empty()) {
1113  int gexpr_no = 1;
1114  std::shared_ptr<Analyzer::Expr> gexpr;
1115  const std::vector<std::shared_ptr<Analyzer::TargetEntry>>& tlist =
1116  query.get_targetlist();
1117  for (auto& c : groupby_clause_) {
1118  // special-case ordinal numbers in GROUP BY
1119  if (dynamic_cast<Literal*>(c.get())) {
1120  IntLiteral* i = dynamic_cast<IntLiteral*>(c.get());
1121  if (!i) {
1122  throw std::runtime_error("Invalid literal in GROUP BY clause.");
1123  }
1124  int varno = (int)i->get_intval();
1125  if (varno <= 0 || varno > static_cast<int>(tlist.size())) {
1126  throw std::runtime_error("Invalid ordinal number in GROUP BY clause.");
1127  }
1128  if (tlist[varno - 1]->get_expr()->get_contains_agg()) {
1129  throw std::runtime_error(
1130  "Ordinal number in GROUP BY cannot reference an expression containing "
1131  "aggregate "
1132  "functions.");
1133  }
1134  gexpr = makeExpr<Analyzer::Var>(
1135  tlist[varno - 1]->get_expr()->get_type_info(), Analyzer::Var::kOUTPUT, varno);
1136  } else {
1137  gexpr = c->analyze(catalog, query, Expr::TlistRefType::TLIST_REF);
1138  }
1139  const SQLTypeInfo gti = gexpr->get_type_info();
1140  bool set_new_type = false;
1141  SQLTypeInfo ti(gti);
1142  if (gti.is_string() && gti.get_compression() == kENCODING_NONE) {
1143  set_new_type = true;
1144  ti.set_compression(kENCODING_DICT);
1145  ti.set_comp_param(TRANSIENT_DICT_ID);
1146  ti.set_fixed_size();
1147  }
1148  std::shared_ptr<Analyzer::Var> v;
1149  if (std::dynamic_pointer_cast<Analyzer::Var>(gexpr)) {
1150  v = std::static_pointer_cast<Analyzer::Var>(gexpr);
1151  int n = v->get_varno();
1152  gexpr = tlist[n - 1]->get_own_expr();
1153  auto cv = std::dynamic_pointer_cast<Analyzer::ColumnVar>(gexpr);
1154  if (cv != nullptr) {
1155  // inherit all ColumnVar info for lineage.
1156  *std::static_pointer_cast<Analyzer::ColumnVar>(v) = *cv;
1157  }
1158  v->set_which_row(Analyzer::Var::kGROUPBY);
1159  v->set_varno(gexpr_no);
1160  tlist[n - 1]->set_expr(v);
1161  }
1162  if (set_new_type) {
1163  auto new_e = gexpr->add_cast(ti);
1164  groupby.push_back(new_e);
1165  if (v != nullptr) {
1166  v->set_type_info(new_e->get_type_info());
1167  }
1168  } else {
1169  groupby.push_back(gexpr);
1170  }
1171  gexpr_no++;
1172  }
1173  }
1174  if (query.get_num_aggs() > 0 || !groupby.empty()) {
1175  for (auto t : query.get_targetlist()) {
1176  auto e = t->get_expr();
1177  e->check_group_by(groupby);
1178  }
1179  }
1180  query.set_group_by(groupby);
1181 }
std::list< std::unique_ptr< Expr > > groupby_clause_
Definition: ParserNode.h:1767
int get_num_aggs() const
Definition: Analyzer.h:1775
void set_group_by(std::list< std::shared_ptr< Analyzer::Expr >> &g)
Definition: Analyzer.h:1798
#define TRANSIENT_DICT_ID
Definition: sqltypes.h:259
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:337
virtual std::shared_ptr< Analyzer::Expr > add_cast(const SQLTypeInfo &new_type_info)
Definition: Analyzer.cpp:702
char * t
bool is_string() const
Definition: sqltypes.h:519
constexpr double n
Definition: Utm.h:39
const std::vector< std::shared_ptr< TargetEntry > > & get_targetlist() const
Definition: Analyzer.h:1776

+ Here is the call graph for this function:

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

Definition at line 1096 of file ParserNode.cpp.

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

1097  {
1098  std::shared_ptr<Analyzer::Expr> p;
1099  if (having_clause_ != nullptr) {
1100  p = having_clause_->analyze(catalog, query, Expr::TlistRefType::TLIST_COPY);
1101  if (p->get_type_info().get_type() != kBOOLEAN) {
1102  throw std::runtime_error("Only boolean expressions can be in HAVING clause.");
1103  }
1104  p->check_group_by(query.get_group_by());
1105  }
1106  query.set_having_predicate(p);
1107 }
std::unique_ptr< Expr > having_clause_
Definition: ParserNode.h:1768
void set_having_predicate(std::shared_ptr< Analyzer::Expr > p)
Definition: Analyzer.h:1799
const std::list< std::shared_ptr< Analyzer::Expr > > & get_group_by() const
Definition: Analyzer.h:1784

+ Here is the call graph for this function:

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

Definition at line 1196 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.

1197  {
1198  std::vector<std::shared_ptr<Analyzer::TargetEntry>>& tlist =
1199  query.get_targetlist_nonconst();
1200  if (select_clause_.empty()) {
1201  // this means SELECT *
1202  int rte_idx = 0;
1203  for (auto rte : query.get_rangetable()) {
1204  rte->expand_star_in_targetlist(catalog, tlist, rte_idx++);
1205  }
1206  } else {
1207  for (auto& p : select_clause_) {
1208  const Parser::Expr* select_expr = p->get_select_expr();
1209  // look for the case of range_var.*
1210  if (typeid(*select_expr) == typeid(ColumnRef) &&
1211  dynamic_cast<const ColumnRef*>(select_expr)->get_column() == nullptr) {
1212  const std::string* range_var_name =
1213  dynamic_cast<const ColumnRef*>(select_expr)->get_table();
1214  int rte_idx = query.get_rte_idx(*range_var_name);
1215  if (rte_idx < 0) {
1216  throw std::runtime_error("invalid range variable name: " + *range_var_name);
1217  }
1218  Analyzer::RangeTableEntry* rte = query.get_rte(rte_idx);
1219  rte->expand_star_in_targetlist(catalog, tlist, rte_idx);
1220  } else {
1221  auto e = select_expr->analyze(catalog, query);
1222  std::string resname;
1223 
1224  if (p->get_alias() != nullptr) {
1225  resname = *p->get_alias();
1226  } else if (std::dynamic_pointer_cast<Analyzer::ColumnVar>(e) &&
1227  !std::dynamic_pointer_cast<Analyzer::Var>(e)) {
1228  auto colvar = std::static_pointer_cast<Analyzer::ColumnVar>(e);
1229  const ColumnDescriptor* col_desc = catalog.getMetadataForColumn(
1230  colvar->get_table_id(), colvar->get_column_id());
1231  resname = col_desc->columnName;
1232  }
1233  if (e->get_type_info().get_type() == kNULLT) {
1234  throw std::runtime_error(
1235  "Untyped NULL in SELECT clause. Use CAST to specify a type.");
1236  }
1237  auto o = std::static_pointer_cast<Analyzer::UOper>(e);
1238  bool unnest = (o != nullptr && o->get_optype() == kUNNEST);
1239  auto tle = std::make_shared<Analyzer::TargetEntry>(resname, e, unnest);
1240  tlist.push_back(tle);
1241  }
1242  }
1243  }
1244 }
void expand_star_in_targetlist(const Catalog_Namespace::Catalog &catalog, std::vector< std::shared_ptr< TargetEntry >> &tlist, int rte_idx)
int get_rte_idx(const std::string &range_var_name) const
Definition: Analyzer.cpp:1401
RangeTableEntry * get_rte(int rte_idx) const
Definition: Analyzer.h:1806
const ColumnDescriptor * getMetadataForColumn(int tableId, const std::string &colName) const
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:1779
std::list< std::unique_ptr< SelectEntry > > select_clause_
Definition: ParserNode.h:1764
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
const std::vector< RangeTableEntry * > & get_rangetable() const
Definition: Analyzer.h:1782

+ Here is the call graph for this function:

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

Definition at line 1183 of file ParserNode.cpp.

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

1184  {
1185  if (where_clause_ == nullptr) {
1186  query.set_where_predicate(nullptr);
1187  return;
1188  }
1189  auto p = where_clause_->analyze(catalog, query, Expr::TlistRefType::TLIST_COPY);
1190  if (p->get_type_info().get_type() != kBOOLEAN) {
1191  throw std::runtime_error("Only boolean expressions can be in WHERE clause.");
1192  }
1193  query.set_where_predicate(p);
1194 }
std::unique_ptr< Expr > where_clause_
Definition: ParserNode.h:1766
void set_where_predicate(std::shared_ptr< Analyzer::Expr > p)
Definition: Analyzer.h:1797

+ Here is the call graph for this function:

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

Definition at line 1750 of file ParserNode.h.

References from_clause_.

1750  {
1751  return from_clause_;
1752  }
std::list< std::unique_ptr< TableRef > > from_clause_
Definition: ParserNode.h:1765
const std::list<std::unique_ptr<Expr> >& Parser::QuerySpec::get_groupby_clause ( ) const
inline

Definition at line 1754 of file ParserNode.h.

References groupby_clause_.

1754  {
1755  return groupby_clause_;
1756  }
std::list< std::unique_ptr< Expr > > groupby_clause_
Definition: ParserNode.h:1767
const Expr* Parser::QuerySpec::get_having_clause ( ) const
inline

Definition at line 1757 of file ParserNode.h.

References having_clause_.

1757 { return having_clause_.get(); }
std::unique_ptr< Expr > having_clause_
Definition: ParserNode.h:1768
bool Parser::QuerySpec::get_is_distinct ( ) const
inline

Definition at line 1746 of file ParserNode.h.

References is_distinct_.

1746 { return is_distinct_; }
const std::list<std::unique_ptr<SelectEntry> >& Parser::QuerySpec::get_select_clause ( ) const
inline

Definition at line 1747 of file ParserNode.h.

References select_clause_.

1747  {
1748  return select_clause_;
1749  }
std::list< std::unique_ptr< SelectEntry > > select_clause_
Definition: ParserNode.h:1764
const Expr* Parser::QuerySpec::get_where_clause ( ) const
inline

Definition at line 1753 of file ParserNode.h.

References where_clause_.

1753 { return where_clause_.get(); }
std::unique_ptr< Expr > where_clause_
Definition: ParserNode.h:1766
std::string Parser::QuerySpec::to_string ( ) const

Definition at line 1595 of file ParserNode.cpp.

1595  {
1596  std::string query_str = "SELECT ";
1597  if (is_distinct_) {
1598  query_str += "DISTINCT ";
1599  }
1600  if (select_clause_.empty()) {
1601  query_str += "* ";
1602  } else {
1603  bool notfirst = false;
1604  for (auto& p : select_clause_) {
1605  if (notfirst) {
1606  query_str += ", ";
1607  } else {
1608  notfirst = true;
1609  }
1610  query_str += p->to_string();
1611  }
1612  }
1613  query_str += " FROM ";
1614  bool notfirst = false;
1615  for (auto& p : from_clause_) {
1616  if (notfirst) {
1617  query_str += ", ";
1618  } else {
1619  notfirst = true;
1620  }
1621  query_str += p->to_string();
1622  }
1623  if (where_clause_) {
1624  query_str += " WHERE " + where_clause_->to_string();
1625  }
1626  if (!groupby_clause_.empty()) {
1627  query_str += " GROUP BY ";
1628  bool notfirst = false;
1629  for (auto& p : groupby_clause_) {
1630  if (notfirst) {
1631  query_str += ", ";
1632  } else {
1633  notfirst = true;
1634  }
1635  query_str += p->to_string();
1636  }
1637  }
1638  if (having_clause_) {
1639  query_str += " HAVING " + having_clause_->to_string();
1640  }
1641  query_str += ";";
1642  return query_str;
1643 }
std::unique_ptr< Expr > having_clause_
Definition: ParserNode.h:1768
std::list< std::unique_ptr< Expr > > groupby_clause_
Definition: ParserNode.h:1767
std::list< std::unique_ptr< TableRef > > from_clause_
Definition: ParserNode.h:1765
std::unique_ptr< Expr > where_clause_
Definition: ParserNode.h:1766
std::list< std::unique_ptr< SelectEntry > > select_clause_
Definition: ParserNode.h:1764

Member Data Documentation

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

Definition at line 1765 of file ParserNode.h.

Referenced by get_from_clause(), and QuerySpec().

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

Definition at line 1767 of file ParserNode.h.

Referenced by get_groupby_clause(), and QuerySpec().

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

Definition at line 1768 of file ParserNode.h.

Referenced by get_having_clause().

bool Parser::QuerySpec::is_distinct_
private

Definition at line 1763 of file ParserNode.h.

Referenced by get_is_distinct().

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

Definition at line 1764 of file ParserNode.h.

Referenced by get_select_clause(), and QuerySpec().

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

Definition at line 1766 of file ParserNode.h.

Referenced by get_where_clause().


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