OmniSciDB  4201147b46
 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 1777 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 1779 of file ParserNode.h.

References CHECK, anonymous_namespace{Utm.h}::f, from_clause_, groupby_clause_, and select_clause_.

1786  if (s) {
1787  for (const auto e : *s) {
1788  select_clause_.emplace_back(e);
1789  }
1790  delete s;
1791  }
1792  CHECK(f);
1793  for (const auto e : *f) {
1794  from_clause_.emplace_back(e);
1795  }
1796  delete f;
1797  if (g) {
1798  for (const auto e : *g) {
1799  groupby_clause_.emplace_back(e);
1800  }
1801  delete g;
1802  }
1803  }
std::unique_ptr< Expr > having_clause_
Definition: ParserNode.h:1826
std::list< std::unique_ptr< Expr > > groupby_clause_
Definition: ParserNode.h:1825
constexpr double f
Definition: Utm.h:31
std::list< std::unique_ptr< TableRef > > from_clause_
Definition: ParserNode.h:1823
std::unique_ptr< Expr > where_clause_
Definition: ParserNode.h:1824
std::list< std::unique_ptr< SelectEntry > > select_clause_
Definition: ParserNode.h:1822
#define CHECK(condition)
Definition: Logger.h:222

Member Function Documentation

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

Implements Parser::QueryExpr.

Definition at line 1933 of file ParserNode.cpp.

References Analyzer::Query::set_is_distinct().

1934  {
1936  analyze_from_clause(catalog, query);
1937  analyze_select_clause(catalog, query);
1938  analyze_where_clause(catalog, query);
1939  analyze_group_by(catalog, query);
1940  analyze_having_clause(catalog, query);
1941 }
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:2456
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 1913 of file ParserNode.cpp.

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

1914  {
1916  for (auto& p : from_clause_) {
1917  const TableDescriptor* table_desc;
1918  table_desc = catalog.getMetadataForTable(*p->get_table_name());
1919  if (table_desc == nullptr) {
1920  throw std::runtime_error("Table " + *p->get_table_name() + " does not exist.");
1921  }
1922  std::string range_var;
1923  if (p->get_range_var() == nullptr) {
1924  range_var = *p->get_table_name();
1925  } else {
1926  range_var = *p->get_range_var();
1927  }
1928  rte = new Analyzer::RangeTableEntry(range_var, table_desc, nullptr);
1929  query.add_rte(rte);
1930  }
1931 }
void add_rte(RangeTableEntry *rte)
Definition: Analyzer.cpp:1416
std::list< std::unique_ptr< TableRef > > from_clause_
Definition: ParserNode.h:1823
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 1776 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(), 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(), and TRANSIENT_DICT_ID.

1777  {
1778  std::list<std::shared_ptr<Analyzer::Expr>> groupby;
1779  if (!groupby_clause_.empty()) {
1780  int gexpr_no = 1;
1781  std::shared_ptr<Analyzer::Expr> gexpr;
1782  const std::vector<std::shared_ptr<Analyzer::TargetEntry>>& tlist =
1783  query.get_targetlist();
1784  for (auto& c : groupby_clause_) {
1785  // special-case ordinal numbers in GROUP BY
1786  if (dynamic_cast<Literal*>(c.get())) {
1787  IntLiteral* i = dynamic_cast<IntLiteral*>(c.get());
1788  if (!i) {
1789  throw std::runtime_error("Invalid literal in GROUP BY clause.");
1790  }
1791  int varno = (int)i->get_intval();
1792  if (varno <= 0 || varno > static_cast<int>(tlist.size())) {
1793  throw std::runtime_error("Invalid ordinal number in GROUP BY clause.");
1794  }
1795  if (tlist[varno - 1]->get_expr()->get_contains_agg()) {
1796  throw std::runtime_error(
1797  "Ordinal number in GROUP BY cannot reference an expression containing "
1798  "aggregate "
1799  "functions.");
1800  }
1801  gexpr = makeExpr<Analyzer::Var>(
1802  tlist[varno - 1]->get_expr()->get_type_info(), Analyzer::Var::kOUTPUT, varno);
1803  } else {
1804  gexpr = c->analyze(catalog, query, Expr::TlistRefType::TLIST_REF);
1805  }
1806  const SQLTypeInfo gti = gexpr->get_type_info();
1807  bool set_new_type = false;
1808  SQLTypeInfo ti(gti);
1809  if (gti.is_string() && gti.get_compression() == kENCODING_NONE) {
1810  set_new_type = true;
1811  ti.set_compression(kENCODING_DICT);
1812  ti.set_comp_param(TRANSIENT_DICT_ID);
1813  ti.set_fixed_size();
1814  }
1815  std::shared_ptr<Analyzer::Var> v;
1816  if (std::dynamic_pointer_cast<Analyzer::Var>(gexpr)) {
1817  v = std::static_pointer_cast<Analyzer::Var>(gexpr);
1818  int n = v->get_varno();
1819  gexpr = tlist[n - 1]->get_own_expr();
1820  auto cv = std::dynamic_pointer_cast<Analyzer::ColumnVar>(gexpr);
1821  if (cv != nullptr) {
1822  // inherit all ColumnVar info for lineage.
1823  *std::static_pointer_cast<Analyzer::ColumnVar>(v) = *cv;
1824  }
1825  v->set_which_row(Analyzer::Var::kGROUPBY);
1826  v->set_varno(gexpr_no);
1827  tlist[n - 1]->set_expr(v);
1828  }
1829  if (set_new_type) {
1830  auto new_e = gexpr->add_cast(ti);
1831  groupby.push_back(new_e);
1832  if (v != nullptr) {
1833  v->set_type_info(new_e->get_type_info());
1834  }
1835  } else {
1836  groupby.push_back(gexpr);
1837  }
1838  gexpr_no++;
1839  }
1840  }
1841  if (query.get_num_aggs() > 0 || !groupby.empty()) {
1842  for (auto t : query.get_targetlist()) {
1843  auto e = t->get_expr();
1844  e->check_group_by(groupby);
1845  }
1846  }
1847  query.set_group_by(groupby);
1848 }
std::list< std::unique_ptr< Expr > > groupby_clause_
Definition: ParserNode.h:1825
int get_num_aggs() const
Definition: Analyzer.h:2429
void set_group_by(std::list< std::shared_ptr< Analyzer::Expr >> &g)
Definition: Analyzer.h:2458
#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:704
bool is_string() const
Definition: sqltypes.h:510
constexpr double n
Definition: Utm.h:38
const std::vector< std::shared_ptr< TargetEntry > > & get_targetlist() const
Definition: Analyzer.h:2430

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

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

1764  {
1765  std::shared_ptr<Analyzer::Expr> p;
1766  if (having_clause_ != nullptr) {
1767  p = having_clause_->analyze(catalog, query, Expr::TlistRefType::TLIST_COPY);
1768  if (p->get_type_info().get_type() != kBOOLEAN) {
1769  throw std::runtime_error("Only boolean expressions can be in HAVING clause.");
1770  }
1771  p->check_group_by(query.get_group_by());
1772  }
1773  query.set_having_predicate(p);
1774 }
std::unique_ptr< Expr > having_clause_
Definition: ParserNode.h:1826
void set_having_predicate(std::shared_ptr< Analyzer::Expr > p)
Definition: Analyzer.h:2459
const std::list< std::shared_ptr< Analyzer::Expr > > & get_group_by() const
Definition: Analyzer.h:2444

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

1864  {
1865  std::vector<std::shared_ptr<Analyzer::TargetEntry>>& tlist =
1866  query.get_targetlist_nonconst();
1867  if (select_clause_.empty()) {
1868  // this means SELECT *
1869  int rte_idx = 0;
1870  for (auto rte : query.get_rangetable()) {
1871  rte->expand_star_in_targetlist(catalog, tlist, rte_idx++);
1872  }
1873  } else {
1874  for (auto& p : select_clause_) {
1875  const Parser::Expr* select_expr = p->get_select_expr();
1876  // look for the case of range_var.*
1877  if (typeid(*select_expr) == typeid(ColumnRef) &&
1878  dynamic_cast<const ColumnRef*>(select_expr)->get_column() == nullptr) {
1879  const std::string* range_var_name =
1880  dynamic_cast<const ColumnRef*>(select_expr)->get_table();
1881  int rte_idx = query.get_rte_idx(*range_var_name);
1882  if (rte_idx < 0) {
1883  throw std::runtime_error("invalid range variable name: " + *range_var_name);
1884  }
1885  Analyzer::RangeTableEntry* rte = query.get_rte(rte_idx);
1886  rte->expand_star_in_targetlist(catalog, tlist, rte_idx);
1887  } else {
1888  auto e = select_expr->analyze(catalog, query);
1889  std::string resname;
1890 
1891  if (p->get_alias() != nullptr) {
1892  resname = *p->get_alias();
1893  } else if (std::dynamic_pointer_cast<Analyzer::ColumnVar>(e) &&
1894  !std::dynamic_pointer_cast<Analyzer::Var>(e)) {
1895  auto colvar = std::static_pointer_cast<Analyzer::ColumnVar>(e);
1896  const ColumnDescriptor* col_desc = catalog.getMetadataForColumn(
1897  colvar->get_table_id(), colvar->get_column_id());
1898  resname = col_desc->columnName;
1899  }
1900  if (e->get_type_info().get_type() == kNULLT) {
1901  throw std::runtime_error(
1902  "Untyped NULL in SELECT clause. Use CAST to specify a type.");
1903  }
1904  auto o = std::static_pointer_cast<Analyzer::UOper>(e);
1905  bool unnest = (o != nullptr && o->get_optype() == kUNNEST);
1906  auto tle = std::make_shared<Analyzer::TargetEntry>(resname, e, unnest);
1907  tlist.push_back(tle);
1908  }
1909  }
1910  }
1911 }
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:1405
RangeTableEntry * get_rte(int rte_idx) const
Definition: Analyzer.h:2466
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:2433
std::list< std::unique_ptr< SelectEntry > > select_clause_
Definition: ParserNode.h:1822
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:2442

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

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

1851  {
1852  if (where_clause_ == nullptr) {
1853  query.set_where_predicate(nullptr);
1854  return;
1855  }
1856  auto p = where_clause_->analyze(catalog, query, Expr::TlistRefType::TLIST_COPY);
1857  if (p->get_type_info().get_type() != kBOOLEAN) {
1858  throw std::runtime_error("Only boolean expressions can be in WHERE clause.");
1859  }
1860  query.set_where_predicate(p);
1861 }
std::unique_ptr< Expr > where_clause_
Definition: ParserNode.h:1824
void set_where_predicate(std::shared_ptr< Analyzer::Expr > p)
Definition: Analyzer.h:2457

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

References from_clause_.

1808  {
1809  return from_clause_;
1810  }
std::list< std::unique_ptr< TableRef > > from_clause_
Definition: ParserNode.h:1823
const std::list<std::unique_ptr<Expr> >& Parser::QuerySpec::get_groupby_clause ( ) const
inline

Definition at line 1812 of file ParserNode.h.

References groupby_clause_.

1812  {
1813  return groupby_clause_;
1814  }
std::list< std::unique_ptr< Expr > > groupby_clause_
Definition: ParserNode.h:1825
const Expr* Parser::QuerySpec::get_having_clause ( ) const
inline

Definition at line 1815 of file ParserNode.h.

References having_clause_.

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

Definition at line 1804 of file ParserNode.h.

References is_distinct_.

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

Definition at line 1805 of file ParserNode.h.

References select_clause_.

1805  {
1806  return select_clause_;
1807  }
std::list< std::unique_ptr< SelectEntry > > select_clause_
Definition: ParserNode.h:1822
const Expr* Parser::QuerySpec::get_where_clause ( ) const
inline

Definition at line 1811 of file ParserNode.h.

References where_clause_.

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

Definition at line 2262 of file ParserNode.cpp.

2262  {
2263  std::string query_str = "SELECT ";
2264  if (is_distinct_) {
2265  query_str += "DISTINCT ";
2266  }
2267  if (select_clause_.empty()) {
2268  query_str += "* ";
2269  } else {
2270  bool notfirst = false;
2271  for (auto& p : select_clause_) {
2272  if (notfirst) {
2273  query_str += ", ";
2274  } else {
2275  notfirst = true;
2276  }
2277  query_str += p->to_string();
2278  }
2279  }
2280  query_str += " FROM ";
2281  bool notfirst = false;
2282  for (auto& p : from_clause_) {
2283  if (notfirst) {
2284  query_str += ", ";
2285  } else {
2286  notfirst = true;
2287  }
2288  query_str += p->to_string();
2289  }
2290  if (where_clause_) {
2291  query_str += " WHERE " + where_clause_->to_string();
2292  }
2293  if (!groupby_clause_.empty()) {
2294  query_str += " GROUP BY ";
2295  bool notfirst = false;
2296  for (auto& p : groupby_clause_) {
2297  if (notfirst) {
2298  query_str += ", ";
2299  } else {
2300  notfirst = true;
2301  }
2302  query_str += p->to_string();
2303  }
2304  }
2305  if (having_clause_) {
2306  query_str += " HAVING " + having_clause_->to_string();
2307  }
2308  query_str += ";";
2309  return query_str;
2310 }
std::unique_ptr< Expr > having_clause_
Definition: ParserNode.h:1826
std::list< std::unique_ptr< Expr > > groupby_clause_
Definition: ParserNode.h:1825
std::list< std::unique_ptr< TableRef > > from_clause_
Definition: ParserNode.h:1823
std::unique_ptr< Expr > where_clause_
Definition: ParserNode.h:1824
std::list< std::unique_ptr< SelectEntry > > select_clause_
Definition: ParserNode.h:1822

Member Data Documentation

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

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

Referenced by get_groupby_clause(), and QuerySpec().

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

Definition at line 1826 of file ParserNode.h.

Referenced by get_having_clause().

bool Parser::QuerySpec::is_distinct_
private

Definition at line 1821 of file ParserNode.h.

Referenced by get_is_distinct().

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

Definition at line 1822 of file ParserNode.h.

Referenced by get_select_clause(), and QuerySpec().

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

Definition at line 1824 of file ParserNode.h.

Referenced by get_where_clause().


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