OmniSciDB  1dac507f6e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 1616 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 1618 of file ParserNode.h.

References CHECK(), from_clause, groupby_clause, and select_clause.

1624  : is_distinct(d), where_clause(w), having_clause(h) {
1625  if (s) {
1626  for (const auto e : *s) {
1627  select_clause.emplace_back(e);
1628  }
1629  delete s;
1630  }
1631  CHECK(f);
1632  for (const auto e : *f) {
1633  from_clause.emplace_back(e);
1634  }
1635  delete f;
1636  if (g) {
1637  for (const auto e : *g) {
1638  groupby_clause.emplace_back(e);
1639  }
1640  delete g;
1641  }
1642  }
std::unique_ptr< Expr > where_clause
Definition: ParserNode.h:1663
std::list< std::unique_ptr< TableRef > > from_clause
Definition: ParserNode.h:1662
std::list< std::unique_ptr< SelectEntry > > select_clause
Definition: ParserNode.h:1661
CHECK(cgen_state)
std::list< std::unique_ptr< Expr > > groupby_clause
Definition: ParserNode.h:1664
std::unique_ptr< Expr > having_clause
Definition: ParserNode.h:1665

+ Here is the call graph for this function:

Member Function Documentation

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

Implements Parser::QueryExpr.

Definition at line 1172 of file ParserNode.cpp.

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

1173  {
1175  analyze_from_clause(catalog, query);
1176  analyze_select_clause(catalog, query);
1177  analyze_where_clause(catalog, query);
1178  analyze_group_by(catalog, query);
1179  analyze_having_clause(catalog, query);
1180 }
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:1546
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 1152 of file ParserNode.cpp.

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

1153  {
1155  for (auto& p : from_clause) {
1156  const TableDescriptor* table_desc;
1157  table_desc = catalog.getMetadataForTable(*p->get_table_name());
1158  if (table_desc == nullptr) {
1159  throw std::runtime_error("Table " + *p->get_table_name() + " does not exist.");
1160  }
1161  std::string range_var;
1162  if (p->get_range_var() == nullptr) {
1163  range_var = *p->get_table_name();
1164  } else {
1165  range_var = *p->get_range_var();
1166  }
1167  rte = new Analyzer::RangeTableEntry(range_var, table_desc, nullptr);
1168  query.add_rte(rte);
1169  }
1170 }
std::list< std::unique_ptr< TableRef > > from_clause
Definition: ParserNode.h:1662
void add_rte(RangeTableEntry *rte)
Definition: Analyzer.cpp:1274
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:

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

Definition at line 1015 of file ParserNode.cpp.

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

1016  {
1017  std::list<std::shared_ptr<Analyzer::Expr>> groupby;
1018  if (!groupby_clause.empty()) {
1019  int gexpr_no = 1;
1020  std::shared_ptr<Analyzer::Expr> gexpr;
1021  const std::vector<std::shared_ptr<Analyzer::TargetEntry>>& tlist =
1022  query.get_targetlist();
1023  for (auto& c : groupby_clause) {
1024  // special-case ordinal numbers in GROUP BY
1025  if (dynamic_cast<Literal*>(c.get())) {
1026  IntLiteral* i = dynamic_cast<IntLiteral*>(c.get());
1027  if (!i) {
1028  throw std::runtime_error("Invalid literal in GROUP BY clause.");
1029  }
1030  int varno = (int)i->get_intval();
1031  if (varno <= 0 || varno > static_cast<int>(tlist.size())) {
1032  throw std::runtime_error("Invalid ordinal number in GROUP BY clause.");
1033  }
1034  if (tlist[varno - 1]->get_expr()->get_contains_agg()) {
1035  throw std::runtime_error(
1036  "Ordinal number in GROUP BY cannot reference an expression containing "
1037  "aggregate "
1038  "functions.");
1039  }
1040  gexpr = makeExpr<Analyzer::Var>(
1041  tlist[varno - 1]->get_expr()->get_type_info(), Analyzer::Var::kOUTPUT, varno);
1042  } else {
1043  gexpr = c->analyze(catalog, query, Expr::TlistRefType::TLIST_REF);
1044  }
1045  const SQLTypeInfo gti = gexpr->get_type_info();
1046  bool set_new_type = false;
1047  SQLTypeInfo ti(gti);
1048  if (gti.is_string() && gti.get_compression() == kENCODING_NONE) {
1049  set_new_type = true;
1050  ti.set_compression(kENCODING_DICT);
1051  ti.set_comp_param(TRANSIENT_DICT_ID);
1052  ti.set_fixed_size();
1053  }
1054  std::shared_ptr<Analyzer::Var> v;
1055  if (std::dynamic_pointer_cast<Analyzer::Var>(gexpr)) {
1056  v = std::static_pointer_cast<Analyzer::Var>(gexpr);
1057  int n = v->get_varno();
1058  gexpr = tlist[n - 1]->get_own_expr();
1059  auto cv = std::dynamic_pointer_cast<Analyzer::ColumnVar>(gexpr);
1060  if (cv != nullptr) {
1061  // inherit all ColumnVar info for lineage.
1062  *std::static_pointer_cast<Analyzer::ColumnVar>(v) = *cv;
1063  }
1064  v->set_which_row(Analyzer::Var::kGROUPBY);
1065  v->set_varno(gexpr_no);
1066  tlist[n - 1]->set_expr(v);
1067  }
1068  if (set_new_type) {
1069  auto new_e = gexpr->add_cast(ti);
1070  groupby.push_back(new_e);
1071  if (v != nullptr) {
1072  v->set_type_info(new_e->get_type_info());
1073  }
1074  } else {
1075  groupby.push_back(gexpr);
1076  }
1077  gexpr_no++;
1078  }
1079  }
1080  if (query.get_num_aggs() > 0 || !groupby.empty()) {
1081  for (auto t : query.get_targetlist()) {
1082  auto e = t->get_expr();
1083  e->check_group_by(groupby);
1084  }
1085  }
1086  query.set_group_by(groupby);
1087 }
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:334
int get_num_aggs() const
Definition: Analyzer.h:1525
int get_varno() const
Definition: Analyzer.h:275
void set_group_by(std::list< std::shared_ptr< Analyzer::Expr >> &g)
Definition: Analyzer.h:1548
std::list< std::unique_ptr< Expr > > groupby_clause
Definition: ParserNode.h:1664
#define TRANSIENT_DICT_ID
Definition: sqltypes.h:189
virtual std::shared_ptr< Analyzer::Expr > add_cast(const SQLTypeInfo &new_type_info)
Definition: Analyzer.cpp:671
bool is_string() const
Definition: sqltypes.h:477
const std::vector< std::shared_ptr< TargetEntry > > & get_targetlist() const
Definition: Analyzer.h:1526

+ 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 1002 of file ParserNode.cpp.

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

1003  {
1004  std::shared_ptr<Analyzer::Expr> p;
1005  if (having_clause != nullptr) {
1006  p = having_clause->analyze(catalog, query, Expr::TlistRefType::TLIST_COPY);
1007  if (p->get_type_info().get_type() != kBOOLEAN) {
1008  throw std::runtime_error("Only boolean expressions can be in HAVING clause.");
1009  }
1010  p->check_group_by(query.get_group_by());
1011  }
1012  query.set_having_predicate(p);
1013 }
void set_having_predicate(std::shared_ptr< Analyzer::Expr > p)
Definition: Analyzer.h:1549
const std::list< std::shared_ptr< Analyzer::Expr > > & get_group_by() const
Definition: Analyzer.h:1534
std::unique_ptr< Expr > having_clause
Definition: ParserNode.h:1665

+ 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 1102 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.

1103  {
1104  std::vector<std::shared_ptr<Analyzer::TargetEntry>>& tlist =
1105  query.get_targetlist_nonconst();
1106  if (select_clause.empty()) {
1107  // this means SELECT *
1108  int rte_idx = 0;
1109  for (auto rte : query.get_rangetable()) {
1110  rte->expand_star_in_targetlist(catalog, tlist, rte_idx++);
1111  }
1112  } else {
1113  for (auto& p : select_clause) {
1114  const Parser::Expr* select_expr = p->get_select_expr();
1115  // look for the case of range_var.*
1116  if (typeid(*select_expr) == typeid(ColumnRef) &&
1117  dynamic_cast<const ColumnRef*>(select_expr)->get_column() == nullptr) {
1118  const std::string* range_var_name =
1119  dynamic_cast<const ColumnRef*>(select_expr)->get_table();
1120  int rte_idx = query.get_rte_idx(*range_var_name);
1121  if (rte_idx < 0) {
1122  throw std::runtime_error("invalid range variable name: " + *range_var_name);
1123  }
1124  Analyzer::RangeTableEntry* rte = query.get_rte(rte_idx);
1125  rte->expand_star_in_targetlist(catalog, tlist, rte_idx);
1126  } else {
1127  auto e = select_expr->analyze(catalog, query);
1128  std::string resname;
1129 
1130  if (p->get_alias() != nullptr) {
1131  resname = *p->get_alias();
1132  } else if (std::dynamic_pointer_cast<Analyzer::ColumnVar>(e) &&
1133  !std::dynamic_pointer_cast<Analyzer::Var>(e)) {
1134  auto colvar = std::static_pointer_cast<Analyzer::ColumnVar>(e);
1135  const ColumnDescriptor* col_desc = catalog.getMetadataForColumn(
1136  colvar->get_table_id(), colvar->get_column_id());
1137  resname = col_desc->columnName;
1138  }
1139  if (e->get_type_info().get_type() == kNULLT) {
1140  throw std::runtime_error(
1141  "Untyped NULL in SELECT clause. Use CAST to specify a type.");
1142  }
1143  auto o = std::static_pointer_cast<Analyzer::UOper>(e);
1144  bool unnest = (o != nullptr && o->get_optype() == kUNNEST);
1145  auto tle = std::make_shared<Analyzer::TargetEntry>(resname, e, unnest);
1146  tlist.push_back(tle);
1147  }
1148  }
1149  }
1150 }
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:1263
std::list< std::unique_ptr< SelectEntry > > select_clause
Definition: ParserNode.h:1661
RangeTableEntry * get_rte(int rte_idx) const
Definition: Analyzer.h:1556
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:1529
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:1532

+ 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 1089 of file ParserNode.cpp.

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

1090  {
1091  if (where_clause == nullptr) {
1092  query.set_where_predicate(nullptr);
1093  return;
1094  }
1095  auto p = where_clause->analyze(catalog, query, Expr::TlistRefType::TLIST_COPY);
1096  if (p->get_type_info().get_type() != kBOOLEAN) {
1097  throw std::runtime_error("Only boolean expressions can be in WHERE clause.");
1098  }
1099  query.set_where_predicate(p);
1100 }
std::unique_ptr< Expr > where_clause
Definition: ParserNode.h:1663
void set_where_predicate(std::shared_ptr< Analyzer::Expr > p)
Definition: Analyzer.h:1547

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

References from_clause.

1647  {
1648  return from_clause;
1649  }
std::list< std::unique_ptr< TableRef > > from_clause
Definition: ParserNode.h:1662
const std::list<std::unique_ptr<Expr> >& Parser::QuerySpec::get_groupby_clause ( ) const
inline

Definition at line 1651 of file ParserNode.h.

References groupby_clause.

1651  {
1652  return groupby_clause;
1653  }
std::list< std::unique_ptr< Expr > > groupby_clause
Definition: ParserNode.h:1664
const Expr* Parser::QuerySpec::get_having_clause ( ) const
inline

Definition at line 1654 of file ParserNode.h.

References having_clause.

1654 { return having_clause.get(); }
std::unique_ptr< Expr > having_clause
Definition: ParserNode.h:1665
bool Parser::QuerySpec::get_is_distinct ( ) const
inline

Definition at line 1643 of file ParserNode.h.

References is_distinct.

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

Definition at line 1644 of file ParserNode.h.

References select_clause.

1644  {
1645  return select_clause;
1646  }
std::list< std::unique_ptr< SelectEntry > > select_clause
Definition: ParserNode.h:1661
const Expr* Parser::QuerySpec::get_where_clause ( ) const
inline

Definition at line 1650 of file ParserNode.h.

References where_clause.

1650 { return where_clause.get(); }
std::unique_ptr< Expr > where_clause
Definition: ParserNode.h:1663
std::string Parser::QuerySpec::to_string ( ) const

Definition at line 1491 of file ParserNode.cpp.

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

1491  {
1492  std::string query_str = "SELECT ";
1493  if (is_distinct) {
1494  query_str += "DISTINCT ";
1495  }
1496  if (select_clause.empty()) {
1497  query_str += "* ";
1498  } else {
1499  bool notfirst = false;
1500  for (auto& p : select_clause) {
1501  if (notfirst) {
1502  query_str += ", ";
1503  } else {
1504  notfirst = true;
1505  }
1506  query_str += p->to_string();
1507  }
1508  }
1509  query_str += " FROM ";
1510  bool notfirst = false;
1511  for (auto& p : from_clause) {
1512  if (notfirst) {
1513  query_str += ", ";
1514  } else {
1515  notfirst = true;
1516  }
1517  query_str += p->to_string();
1518  }
1519  if (where_clause) {
1520  query_str += " WHERE " + where_clause->to_string();
1521  }
1522  if (!groupby_clause.empty()) {
1523  query_str += " GROUP BY ";
1524  bool notfirst = false;
1525  for (auto& p : groupby_clause) {
1526  if (notfirst) {
1527  query_str += ", ";
1528  } else {
1529  notfirst = true;
1530  }
1531  query_str += p->to_string();
1532  }
1533  }
1534  if (having_clause) {
1535  query_str += " HAVING " + having_clause->to_string();
1536  }
1537  query_str += ";";
1538  return query_str;
1539 }
std::unique_ptr< Expr > where_clause
Definition: ParserNode.h:1663
std::list< std::unique_ptr< TableRef > > from_clause
Definition: ParserNode.h:1662
std::list< std::unique_ptr< SelectEntry > > select_clause
Definition: ParserNode.h:1661
std::list< std::unique_ptr< Expr > > groupby_clause
Definition: ParserNode.h:1664
std::unique_ptr< Expr > having_clause
Definition: ParserNode.h:1665

+ Here is the call graph for this function:

Member Data Documentation

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

Definition at line 1662 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 1664 of file ParserNode.h.

Referenced by get_groupby_clause(), and QuerySpec().

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

Definition at line 1665 of file ParserNode.h.

Referenced by get_having_clause().

bool Parser::QuerySpec::is_distinct
private

Definition at line 1660 of file ParserNode.h.

Referenced by get_is_distinct().

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

Definition at line 1661 of file ParserNode.h.

Referenced by get_select_clause(), and QuerySpec().

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

Definition at line 1663 of file ParserNode.h.

Referenced by get_where_clause().


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