OmniSciDB  c1a53651b2
 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 1733 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 1735 of file ParserNode.h.

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

1742  if (s) {
1743  for (const auto e : *s) {
1744  select_clause_.emplace_back(e);
1745  }
1746  delete s;
1747  }
1748  CHECK(f);
1749  for (const auto e : *f) {
1750  from_clause_.emplace_back(e);
1751  }
1752  delete f;
1753  if (g) {
1754  for (const auto e : *g) {
1755  groupby_clause_.emplace_back(e);
1756  }
1757  delete g;
1758  }
1759  }
std::unique_ptr< Expr > having_clause_
Definition: ParserNode.h:1782
std::list< std::unique_ptr< Expr > > groupby_clause_
Definition: ParserNode.h:1781
constexpr double f
Definition: Utm.h:31
std::list< std::unique_ptr< TableRef > > from_clause_
Definition: ParserNode.h:1779
std::unique_ptr< Expr > where_clause_
Definition: ParserNode.h:1780
std::list< std::unique_ptr< SelectEntry > > select_clause_
Definition: ParserNode.h:1778
#define CHECK(condition)
Definition: Logger.h:291

Member Function Documentation

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

Implements Parser::QueryExpr.

Definition at line 1954 of file ParserNode.cpp.

References Analyzer::Query::set_is_distinct().

1955  {
1957  analyze_from_clause(catalog, query);
1958  analyze_select_clause(catalog, query);
1959  analyze_where_clause(catalog, query);
1960  analyze_group_by(catalog, query);
1961  analyze_having_clause(catalog, query);
1962 }
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:2821
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 1934 of file ParserNode.cpp.

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

1935  {
1937  for (auto& p : from_clause_) {
1938  const TableDescriptor* table_desc;
1939  table_desc = catalog.getMetadataForTable(*p->get_table_name());
1940  if (table_desc == nullptr) {
1941  throw std::runtime_error("Table " + *p->get_table_name() + " does not exist.");
1942  }
1943  std::string range_var;
1944  if (p->get_range_var() == nullptr) {
1945  range_var = *p->get_table_name();
1946  } else {
1947  range_var = *p->get_range_var();
1948  }
1949  rte = new Analyzer::RangeTableEntry(range_var, table_desc, nullptr);
1950  query.add_rte(rte);
1951  }
1952 }
void add_rte(RangeTableEntry *rte)
Definition: Analyzer.cpp:1479
std::list< std::unique_ptr< TableRef > > from_clause_
Definition: ParserNode.h:1779
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 1796 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.

1797  {
1798  std::list<std::shared_ptr<Analyzer::Expr>> groupby;
1799  if (!groupby_clause_.empty()) {
1800  int gexpr_no = 1;
1801  std::shared_ptr<Analyzer::Expr> gexpr;
1802  const std::vector<std::shared_ptr<Analyzer::TargetEntry>>& tlist =
1803  query.get_targetlist();
1804  for (auto& c : groupby_clause_) {
1805  // special-case ordinal numbers in GROUP BY
1806  if (dynamic_cast<Literal*>(c.get())) {
1807  IntLiteral* i = dynamic_cast<IntLiteral*>(c.get());
1808  if (!i) {
1809  throw std::runtime_error("Invalid literal in GROUP BY clause.");
1810  }
1811  int varno = (int)i->get_intval();
1812  if (varno <= 0 || varno > static_cast<int>(tlist.size())) {
1813  throw std::runtime_error("Invalid ordinal number in GROUP BY clause.");
1814  }
1815  if (tlist[varno - 1]->get_expr()->get_contains_agg()) {
1816  throw std::runtime_error(
1817  "Ordinal number in GROUP BY cannot reference an expression containing "
1818  "aggregate "
1819  "functions.");
1820  }
1821  gexpr = makeExpr<Analyzer::Var>(
1822  tlist[varno - 1]->get_expr()->get_type_info(), Analyzer::Var::kOUTPUT, varno);
1823  } else {
1824  gexpr = c->analyze(catalog, query, Expr::TlistRefType::TLIST_REF);
1825  }
1826  const SQLTypeInfo gti = gexpr->get_type_info();
1827  bool set_new_type = false;
1828  SQLTypeInfo ti(gti);
1829  if (gti.is_string() && gti.get_compression() == kENCODING_NONE) {
1830  set_new_type = true;
1831  ti.set_compression(kENCODING_DICT);
1832  ti.set_comp_param(TRANSIENT_DICT_ID);
1833  ti.set_fixed_size();
1834  }
1835  std::shared_ptr<Analyzer::Var> v;
1836  if (std::dynamic_pointer_cast<Analyzer::Var>(gexpr)) {
1837  v = std::static_pointer_cast<Analyzer::Var>(gexpr);
1838  int n = v->get_varno();
1839  gexpr = tlist[n - 1]->get_own_expr();
1840  auto cv = std::dynamic_pointer_cast<Analyzer::ColumnVar>(gexpr);
1841  if (cv != nullptr) {
1842  // inherit all ColumnVar info for lineage.
1843  *std::static_pointer_cast<Analyzer::ColumnVar>(v) = *cv;
1844  }
1845  v->set_which_row(Analyzer::Var::kGROUPBY);
1846  v->set_varno(gexpr_no);
1847  tlist[n - 1]->set_expr(v);
1848  }
1849  if (set_new_type) {
1850  auto new_e = gexpr->add_cast(ti);
1851  groupby.push_back(new_e);
1852  if (v != nullptr) {
1853  v->set_type_info(new_e->get_type_info());
1854  }
1855  } else {
1856  groupby.push_back(gexpr);
1857  }
1858  gexpr_no++;
1859  }
1860  }
1861  if (query.get_num_aggs() > 0 || !groupby.empty()) {
1862  for (auto t : query.get_targetlist()) {
1863  auto e = t->get_expr();
1864  e->check_group_by(groupby);
1865  }
1866  }
1867  query.set_group_by(groupby);
1868 }
std::list< std::unique_ptr< Expr > > groupby_clause_
Definition: ParserNode.h:1781
int get_num_aggs() const
Definition: Analyzer.h:2794
#define TRANSIENT_DICT_ID
Definition: DbObjectKeys.h:24
void set_group_by(std::list< std::shared_ptr< Analyzer::Expr >> &g)
Definition: Analyzer.h:2823
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:389
virtual std::shared_ptr< Analyzer::Expr > add_cast(const SQLTypeInfo &new_type_info)
Definition: Analyzer.cpp:746
bool is_string() const
Definition: sqltypes.h:580
constexpr double n
Definition: Utm.h:38
const std::vector< std::shared_ptr< TargetEntry > > & get_targetlist() const
Definition: Analyzer.h:2795

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

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

1784  {
1785  std::shared_ptr<Analyzer::Expr> p;
1786  if (having_clause_ != nullptr) {
1787  p = having_clause_->analyze(catalog, query, Expr::TlistRefType::TLIST_COPY);
1788  if (p->get_type_info().get_type() != kBOOLEAN) {
1789  throw std::runtime_error("Only boolean expressions can be in HAVING clause.");
1790  }
1791  p->check_group_by(query.get_group_by());
1792  }
1793  query.set_having_predicate(p);
1794 }
std::unique_ptr< Expr > having_clause_
Definition: ParserNode.h:1782
void set_having_predicate(std::shared_ptr< Analyzer::Expr > p)
Definition: Analyzer.h:2824
const std::list< std::shared_ptr< Analyzer::Expr > > & get_group_by() const
Definition: Analyzer.h:2809

+ 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 1883 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(), Analyzer::ColumnVar::getColumnKey(), Catalog_Namespace::Catalog::getMetadataForColumn(), kNULLT, and kUNNEST.

1884  {
1885  std::vector<std::shared_ptr<Analyzer::TargetEntry>>& tlist =
1886  query.get_targetlist_nonconst();
1887  if (select_clause_.empty()) {
1888  // this means SELECT *
1889  int rte_idx = 0;
1890  for (auto rte : query.get_rangetable()) {
1891  rte->expand_star_in_targetlist(catalog, tlist, rte_idx++);
1892  }
1893  } else {
1894  for (auto& p : select_clause_) {
1895  const Parser::Expr* select_expr = p->get_select_expr();
1896  // look for the case of range_var.*
1897  if (typeid(*select_expr) == typeid(ColumnRef) &&
1898  dynamic_cast<const ColumnRef*>(select_expr)->get_column() == nullptr) {
1899  const std::string* range_var_name =
1900  dynamic_cast<const ColumnRef*>(select_expr)->get_table();
1901  int rte_idx = query.get_rte_idx(*range_var_name);
1902  if (rte_idx < 0) {
1903  throw std::runtime_error("invalid range variable name: " + *range_var_name);
1904  }
1905  Analyzer::RangeTableEntry* rte = query.get_rte(rte_idx);
1906  rte->expand_star_in_targetlist(catalog, tlist, rte_idx);
1907  } else {
1908  auto e = select_expr->analyze(catalog, query);
1909  std::string resname;
1910 
1911  if (p->get_alias() != nullptr) {
1912  resname = *p->get_alias();
1913  } else if (std::dynamic_pointer_cast<Analyzer::ColumnVar>(e) &&
1914  !std::dynamic_pointer_cast<Analyzer::Var>(e)) {
1915  auto colvar = std::static_pointer_cast<Analyzer::ColumnVar>(e);
1916  const auto& column_key = colvar->getColumnKey();
1917  const ColumnDescriptor* col_desc =
1918  catalog.getMetadataForColumn(column_key.table_id, column_key.column_id);
1919  resname = col_desc->columnName;
1920  }
1921  if (e->get_type_info().get_type() == kNULLT) {
1922  throw std::runtime_error(
1923  "Untyped NULL in SELECT clause. Use CAST to specify a type.");
1924  }
1925  auto o = std::static_pointer_cast<Analyzer::UOper>(e);
1926  bool unnest = (o != nullptr && o->get_optype() == kUNNEST);
1927  auto tle = std::make_shared<Analyzer::TargetEntry>(resname, e, unnest);
1928  tlist.push_back(tle);
1929  }
1930  }
1931  }
1932 }
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:1468
RangeTableEntry * get_rte(int rte_idx) const
Definition: Analyzer.h:2831
const ColumnDescriptor * getMetadataForColumn(int tableId, const std::string &colName) const
specifies the content in-memory of a row in the column metadata table
const shared::ColumnKey & getColumnKey() const
Definition: Analyzer.h:198
std::vector< std::shared_ptr< TargetEntry > > & get_targetlist_nonconst()
Definition: Analyzer.h:2798
std::list< std::unique_ptr< SelectEntry > > select_clause_
Definition: ParserNode.h:1778
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:2807

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

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

1871  {
1872  if (where_clause_ == nullptr) {
1873  query.set_where_predicate(nullptr);
1874  return;
1875  }
1876  auto p = where_clause_->analyze(catalog, query, Expr::TlistRefType::TLIST_COPY);
1877  if (p->get_type_info().get_type() != kBOOLEAN) {
1878  throw std::runtime_error("Only boolean expressions can be in WHERE clause.");
1879  }
1880  query.set_where_predicate(p);
1881 }
std::unique_ptr< Expr > where_clause_
Definition: ParserNode.h:1780
void set_where_predicate(std::shared_ptr< Analyzer::Expr > p)
Definition: Analyzer.h:2822

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

References from_clause_.

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

Definition at line 1768 of file ParserNode.h.

References groupby_clause_.

1768  {
1769  return groupby_clause_;
1770  }
std::list< std::unique_ptr< Expr > > groupby_clause_
Definition: ParserNode.h:1781
const Expr* Parser::QuerySpec::get_having_clause ( ) const
inline

Definition at line 1771 of file ParserNode.h.

References having_clause_.

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

Definition at line 1760 of file ParserNode.h.

References is_distinct_.

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

Definition at line 1761 of file ParserNode.h.

References select_clause_.

1761  {
1762  return select_clause_;
1763  }
std::list< std::unique_ptr< SelectEntry > > select_clause_
Definition: ParserNode.h:1778
const Expr* Parser::QuerySpec::get_where_clause ( ) const
inline

Definition at line 1767 of file ParserNode.h.

References where_clause_.

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

Definition at line 2284 of file ParserNode.cpp.

2284  {
2285  std::string query_str = "SELECT ";
2286  if (is_distinct_) {
2287  query_str += "DISTINCT ";
2288  }
2289  if (select_clause_.empty()) {
2290  query_str += "* ";
2291  } else {
2292  bool notfirst = false;
2293  for (auto& p : select_clause_) {
2294  if (notfirst) {
2295  query_str += ", ";
2296  } else {
2297  notfirst = true;
2298  }
2299  query_str += p->to_string();
2300  }
2301  }
2302  query_str += " FROM ";
2303  bool notfirst = false;
2304  for (auto& p : from_clause_) {
2305  if (notfirst) {
2306  query_str += ", ";
2307  } else {
2308  notfirst = true;
2309  }
2310  query_str += p->to_string();
2311  }
2312  if (where_clause_) {
2313  query_str += " WHERE " + where_clause_->to_string();
2314  }
2315  if (!groupby_clause_.empty()) {
2316  query_str += " GROUP BY ";
2317  bool notfirst = false;
2318  for (auto& p : groupby_clause_) {
2319  if (notfirst) {
2320  query_str += ", ";
2321  } else {
2322  notfirst = true;
2323  }
2324  query_str += p->to_string();
2325  }
2326  }
2327  if (having_clause_) {
2328  query_str += " HAVING " + having_clause_->to_string();
2329  }
2330  query_str += ";";
2331  return query_str;
2332 }
std::unique_ptr< Expr > having_clause_
Definition: ParserNode.h:1782
std::list< std::unique_ptr< Expr > > groupby_clause_
Definition: ParserNode.h:1781
std::list< std::unique_ptr< TableRef > > from_clause_
Definition: ParserNode.h:1779
std::unique_ptr< Expr > where_clause_
Definition: ParserNode.h:1780
std::list< std::unique_ptr< SelectEntry > > select_clause_
Definition: ParserNode.h:1778

Member Data Documentation

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

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

Referenced by get_groupby_clause(), and QuerySpec().

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

Definition at line 1782 of file ParserNode.h.

Referenced by get_having_clause().

bool Parser::QuerySpec::is_distinct_
private

Definition at line 1777 of file ParserNode.h.

Referenced by get_is_distinct().

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

Definition at line 1778 of file ParserNode.h.

Referenced by get_select_clause(), and QuerySpec().

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

Definition at line 1780 of file ParserNode.h.

Referenced by get_where_clause().


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