OmniSciDB  ba1bac9284
 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 1730 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 1732 of file ParserNode.h.

References CHECK, f, from_clause, groupby_clause, and select_clause.

1739  if (s) {
1740  for (const auto e : *s) {
1741  select_clause.emplace_back(e);
1742  }
1743  delete s;
1744  }
1745  CHECK(f);
1746  for (const auto e : *f) {
1747  from_clause.emplace_back(e);
1748  }
1749  delete f;
1750  if (g) {
1751  for (const auto e : *g) {
1752  groupby_clause.emplace_back(e);
1753  }
1754  delete g;
1755  }
1756  }
std::unique_ptr< Expr > where_clause
Definition: ParserNode.h:1777
std::list< std::unique_ptr< TableRef > > from_clause
Definition: ParserNode.h:1776
tuple d
Definition: test_fsi.py:9
std::list< std::unique_ptr< SelectEntry > > select_clause
Definition: ParserNode.h:1775
std::list< std::unique_ptr< Expr > > groupby_clause
Definition: ParserNode.h:1778
#define CHECK(condition)
Definition: Logger.h:206
char * f
std::unique_ptr< Expr > having_clause
Definition: ParserNode.h:1779

Member Function Documentation

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

Implements Parser::QueryExpr.

Definition at line 1225 of file ParserNode.cpp.

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

1226  {
1228  analyze_from_clause(catalog, query);
1229  analyze_select_clause(catalog, query);
1230  analyze_where_clause(catalog, query);
1231  analyze_group_by(catalog, query);
1232  analyze_having_clause(catalog, query);
1233 }
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:1636
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 1205 of file ParserNode.cpp.

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

1206  {
1208  for (auto& p : from_clause) {
1209  const TableDescriptor* table_desc;
1210  table_desc = catalog.getMetadataForTable(*p->get_table_name());
1211  if (table_desc == nullptr) {
1212  throw std::runtime_error("Table " + *p->get_table_name() + " does not exist.");
1213  }
1214  std::string range_var;
1215  if (p->get_range_var() == nullptr) {
1216  range_var = *p->get_table_name();
1217  } else {
1218  range_var = *p->get_range_var();
1219  }
1220  rte = new Analyzer::RangeTableEntry(range_var, table_desc, nullptr);
1221  query.add_rte(rte);
1222  }
1223 }
std::list< std::unique_ptr< TableRef > > from_clause
Definition: ParserNode.h:1776
void add_rte(RangeTableEntry *rte)
Definition: Analyzer.cpp:1402
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 1068 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(), i, 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(), t, and TRANSIENT_DICT_ID.

1069  {
1070  std::list<std::shared_ptr<Analyzer::Expr>> groupby;
1071  if (!groupby_clause.empty()) {
1072  int gexpr_no = 1;
1073  std::shared_ptr<Analyzer::Expr> gexpr;
1074  const std::vector<std::shared_ptr<Analyzer::TargetEntry>>& tlist =
1075  query.get_targetlist();
1076  for (auto& c : groupby_clause) {
1077  // special-case ordinal numbers in GROUP BY
1078  if (dynamic_cast<Literal*>(c.get())) {
1079  IntLiteral* i = dynamic_cast<IntLiteral*>(c.get());
1080  if (!i) {
1081  throw std::runtime_error("Invalid literal in GROUP BY clause.");
1082  }
1083  int varno = (int)i->get_intval();
1084  if (varno <= 0 || varno > static_cast<int>(tlist.size())) {
1085  throw std::runtime_error("Invalid ordinal number in GROUP BY clause.");
1086  }
1087  if (tlist[varno - 1]->get_expr()->get_contains_agg()) {
1088  throw std::runtime_error(
1089  "Ordinal number in GROUP BY cannot reference an expression containing "
1090  "aggregate "
1091  "functions.");
1092  }
1093  gexpr = makeExpr<Analyzer::Var>(
1094  tlist[varno - 1]->get_expr()->get_type_info(), Analyzer::Var::kOUTPUT, varno);
1095  } else {
1096  gexpr = c->analyze(catalog, query, Expr::TlistRefType::TLIST_REF);
1097  }
1098  const SQLTypeInfo gti = gexpr->get_type_info();
1099  bool set_new_type = false;
1100  SQLTypeInfo ti(gti);
1101  if (gti.is_string() && gti.get_compression() == kENCODING_NONE) {
1102  set_new_type = true;
1103  ti.set_compression(kENCODING_DICT);
1104  ti.set_comp_param(TRANSIENT_DICT_ID);
1105  ti.set_fixed_size();
1106  }
1107  std::shared_ptr<Analyzer::Var> v;
1108  if (std::dynamic_pointer_cast<Analyzer::Var>(gexpr)) {
1109  v = std::static_pointer_cast<Analyzer::Var>(gexpr);
1110  int n = v->get_varno();
1111  gexpr = tlist[n - 1]->get_own_expr();
1112  auto cv = std::dynamic_pointer_cast<Analyzer::ColumnVar>(gexpr);
1113  if (cv != nullptr) {
1114  // inherit all ColumnVar info for lineage.
1115  *std::static_pointer_cast<Analyzer::ColumnVar>(v) = *cv;
1116  }
1117  v->set_which_row(Analyzer::Var::kGROUPBY);
1118  v->set_varno(gexpr_no);
1119  tlist[n - 1]->set_expr(v);
1120  }
1121  if (set_new_type) {
1122  auto new_e = gexpr->add_cast(ti);
1123  groupby.push_back(new_e);
1124  if (v != nullptr) {
1125  v->set_type_info(new_e->get_type_info());
1126  }
1127  } else {
1128  groupby.push_back(gexpr);
1129  }
1130  gexpr_no++;
1131  }
1132  }
1133  if (query.get_num_aggs() > 0 || !groupby.empty()) {
1134  for (auto t : query.get_targetlist()) {
1135  auto e = t->get_expr();
1136  e->check_group_by(groupby);
1137  }
1138  }
1139  query.set_group_by(groupby);
1140 }
int get_num_aggs() const
Definition: Analyzer.h:1615
int get_varno() const
Definition: Analyzer.h:275
void set_group_by(std::list< std::shared_ptr< Analyzer::Expr >> &g)
Definition: Analyzer.h:1638
std::list< std::unique_ptr< Expr > > groupby_clause
Definition: ParserNode.h:1778
#define TRANSIENT_DICT_ID
Definition: sqltypes.h:253
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:322
virtual std::shared_ptr< Analyzer::Expr > add_cast(const SQLTypeInfo &new_type_info)
Definition: Analyzer.cpp:690
char * t
bool is_string() const
Definition: sqltypes.h:489
const std::vector< std::shared_ptr< TargetEntry > > & get_targetlist() const
Definition: Analyzer.h:1616

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

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

1056  {
1057  std::shared_ptr<Analyzer::Expr> p;
1058  if (having_clause != nullptr) {
1059  p = having_clause->analyze(catalog, query, Expr::TlistRefType::TLIST_COPY);
1060  if (p->get_type_info().get_type() != kBOOLEAN) {
1061  throw std::runtime_error("Only boolean expressions can be in HAVING clause.");
1062  }
1063  p->check_group_by(query.get_group_by());
1064  }
1065  query.set_having_predicate(p);
1066 }
void set_having_predicate(std::shared_ptr< Analyzer::Expr > p)
Definition: Analyzer.h:1639
const std::list< std::shared_ptr< Analyzer::Expr > > & get_group_by() const
Definition: Analyzer.h:1624
std::unique_ptr< Expr > having_clause
Definition: ParserNode.h:1779

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

1156  {
1157  std::vector<std::shared_ptr<Analyzer::TargetEntry>>& tlist =
1158  query.get_targetlist_nonconst();
1159  if (select_clause.empty()) {
1160  // this means SELECT *
1161  int rte_idx = 0;
1162  for (auto rte : query.get_rangetable()) {
1163  rte->expand_star_in_targetlist(catalog, tlist, rte_idx++);
1164  }
1165  } else {
1166  for (auto& p : select_clause) {
1167  const Parser::Expr* select_expr = p->get_select_expr();
1168  // look for the case of range_var.*
1169  if (typeid(*select_expr) == typeid(ColumnRef) &&
1170  dynamic_cast<const ColumnRef*>(select_expr)->get_column() == nullptr) {
1171  const std::string* range_var_name =
1172  dynamic_cast<const ColumnRef*>(select_expr)->get_table();
1173  int rte_idx = query.get_rte_idx(*range_var_name);
1174  if (rte_idx < 0) {
1175  throw std::runtime_error("invalid range variable name: " + *range_var_name);
1176  }
1177  Analyzer::RangeTableEntry* rte = query.get_rte(rte_idx);
1178  rte->expand_star_in_targetlist(catalog, tlist, rte_idx);
1179  } else {
1180  auto e = select_expr->analyze(catalog, query);
1181  std::string resname;
1182 
1183  if (p->get_alias() != nullptr) {
1184  resname = *p->get_alias();
1185  } else if (std::dynamic_pointer_cast<Analyzer::ColumnVar>(e) &&
1186  !std::dynamic_pointer_cast<Analyzer::Var>(e)) {
1187  auto colvar = std::static_pointer_cast<Analyzer::ColumnVar>(e);
1188  const ColumnDescriptor* col_desc = catalog.getMetadataForColumn(
1189  colvar->get_table_id(), colvar->get_column_id());
1190  resname = col_desc->columnName;
1191  }
1192  if (e->get_type_info().get_type() == kNULLT) {
1193  throw std::runtime_error(
1194  "Untyped NULL in SELECT clause. Use CAST to specify a type.");
1195  }
1196  auto o = std::static_pointer_cast<Analyzer::UOper>(e);
1197  bool unnest = (o != nullptr && o->get_optype() == kUNNEST);
1198  auto tle = std::make_shared<Analyzer::TargetEntry>(resname, e, unnest);
1199  tlist.push_back(tle);
1200  }
1201  }
1202  }
1203 }
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:1391
std::list< std::unique_ptr< SelectEntry > > select_clause
Definition: ParserNode.h:1775
RangeTableEntry * get_rte(int rte_idx) const
Definition: Analyzer.h:1646
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:1619
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:1622

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

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

1143  {
1144  if (where_clause == nullptr) {
1145  query.set_where_predicate(nullptr);
1146  return;
1147  }
1148  auto p = where_clause->analyze(catalog, query, Expr::TlistRefType::TLIST_COPY);
1149  if (p->get_type_info().get_type() != kBOOLEAN) {
1150  throw std::runtime_error("Only boolean expressions can be in WHERE clause.");
1151  }
1152  query.set_where_predicate(p);
1153 }
std::unique_ptr< Expr > where_clause
Definition: ParserNode.h:1777
void set_where_predicate(std::shared_ptr< Analyzer::Expr > p)
Definition: Analyzer.h:1637

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

References from_clause.

1761  {
1762  return from_clause;
1763  }
std::list< std::unique_ptr< TableRef > > from_clause
Definition: ParserNode.h:1776
const std::list<std::unique_ptr<Expr> >& Parser::QuerySpec::get_groupby_clause ( ) const
inline

Definition at line 1765 of file ParserNode.h.

References groupby_clause.

1765  {
1766  return groupby_clause;
1767  }
std::list< std::unique_ptr< Expr > > groupby_clause
Definition: ParserNode.h:1778
const Expr* Parser::QuerySpec::get_having_clause ( ) const
inline

Definition at line 1768 of file ParserNode.h.

References having_clause.

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

Definition at line 1757 of file ParserNode.h.

References is_distinct.

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

Definition at line 1758 of file ParserNode.h.

References select_clause.

1758  {
1759  return select_clause;
1760  }
std::list< std::unique_ptr< SelectEntry > > select_clause
Definition: ParserNode.h:1775
const Expr* Parser::QuerySpec::get_where_clause ( ) const
inline

Definition at line 1764 of file ParserNode.h.

References where_clause.

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

Definition at line 1468 of file ParserNode.cpp.

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

1468  {
1469  std::string query_str = "SELECT ";
1470  if (is_distinct) {
1471  query_str += "DISTINCT ";
1472  }
1473  if (select_clause.empty()) {
1474  query_str += "* ";
1475  } else {
1476  bool notfirst = false;
1477  for (auto& p : select_clause) {
1478  if (notfirst) {
1479  query_str += ", ";
1480  } else {
1481  notfirst = true;
1482  }
1483  query_str += p->to_string();
1484  }
1485  }
1486  query_str += " FROM ";
1487  bool notfirst = false;
1488  for (auto& p : from_clause) {
1489  if (notfirst) {
1490  query_str += ", ";
1491  } else {
1492  notfirst = true;
1493  }
1494  query_str += p->to_string();
1495  }
1496  if (where_clause) {
1497  query_str += " WHERE " + where_clause->to_string();
1498  }
1499  if (!groupby_clause.empty()) {
1500  query_str += " GROUP BY ";
1501  bool notfirst = false;
1502  for (auto& p : groupby_clause) {
1503  if (notfirst) {
1504  query_str += ", ";
1505  } else {
1506  notfirst = true;
1507  }
1508  query_str += p->to_string();
1509  }
1510  }
1511  if (having_clause) {
1512  query_str += " HAVING " + having_clause->to_string();
1513  }
1514  query_str += ";";
1515  return query_str;
1516 }
std::unique_ptr< Expr > where_clause
Definition: ParserNode.h:1777
std::list< std::unique_ptr< TableRef > > from_clause
Definition: ParserNode.h:1776
std::list< std::unique_ptr< SelectEntry > > select_clause
Definition: ParserNode.h:1775
std::list< std::unique_ptr< Expr > > groupby_clause
Definition: ParserNode.h:1778
std::unique_ptr< Expr > having_clause
Definition: ParserNode.h:1779

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

Referenced by get_groupby_clause(), and QuerySpec().

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

Definition at line 1779 of file ParserNode.h.

Referenced by get_having_clause().

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

Definition at line 1775 of file ParserNode.h.

Referenced by get_select_clause(), and QuerySpec().

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

Definition at line 1777 of file ParserNode.h.

Referenced by get_where_clause().


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