OmniSciDB  72c90bc290
 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 1720 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 1722 of file ParserNode.h.

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

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

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

References Analyzer::Query::set_is_distinct().

1975  {
1977  analyze_from_clause(catalog, query);
1978  analyze_select_clause(catalog, query);
1979  analyze_where_clause(catalog, query);
1980  analyze_group_by(catalog, query);
1981  analyze_having_clause(catalog, query);
1982 }
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:3046
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 1954 of file ParserNode.cpp.

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

1955  {
1957  for (auto& p : from_clause_) {
1958  const TableDescriptor* table_desc;
1959  table_desc = catalog.getMetadataForTable(*p->get_table_name());
1960  if (table_desc == nullptr) {
1961  throw std::runtime_error("Table " + *p->get_table_name() + " does not exist.");
1962  }
1963  std::string range_var;
1964  if (p->get_range_var() == nullptr) {
1965  range_var = *p->get_table_name();
1966  } else {
1967  range_var = *p->get_range_var();
1968  }
1969  rte = new Analyzer::RangeTableEntry(range_var, table_desc, nullptr);
1970  query.add_rte(rte);
1971  }
1972 }
void add_rte(RangeTableEntry *rte)
Definition: Analyzer.cpp:1506
std::list< std::unique_ptr< TableRef > > from_clause_
Definition: ParserNode.h:1766
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 1815 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, shared::StringDictKey::kTransientDictKey, anonymous_namespace{Utm.h}::n, SQLTypeInfo::set_comp_param(), SQLTypeInfo::set_compression(), SQLTypeInfo::set_fixed_size(), Analyzer::Query::set_group_by(), SQLTypeInfo::setStringDictKey(), and TRANSIENT_DICT_ID.

1816  {
1817  std::list<std::shared_ptr<Analyzer::Expr>> groupby;
1818  if (!groupby_clause_.empty()) {
1819  int gexpr_no = 1;
1820  std::shared_ptr<Analyzer::Expr> gexpr;
1821  const std::vector<std::shared_ptr<Analyzer::TargetEntry>>& tlist =
1822  query.get_targetlist();
1823  for (auto& c : groupby_clause_) {
1824  // special-case ordinal numbers in GROUP BY
1825  if (dynamic_cast<Literal*>(c.get())) {
1826  IntLiteral* i = dynamic_cast<IntLiteral*>(c.get());
1827  if (!i) {
1828  throw std::runtime_error("Invalid literal in GROUP BY clause.");
1829  }
1830  int varno = (int)i->get_intval();
1831  if (varno <= 0 || varno > static_cast<int>(tlist.size())) {
1832  throw std::runtime_error("Invalid ordinal number in GROUP BY clause.");
1833  }
1834  if (tlist[varno - 1]->get_expr()->get_contains_agg()) {
1835  throw std::runtime_error(
1836  "Ordinal number in GROUP BY cannot reference an expression containing "
1837  "aggregate "
1838  "functions.");
1839  }
1840  gexpr = makeExpr<Analyzer::Var>(
1841  tlist[varno - 1]->get_expr()->get_type_info(), Analyzer::Var::kOUTPUT, varno);
1842  } else {
1843  gexpr = c->analyze(catalog, query, Expr::TlistRefType::TLIST_REF);
1844  }
1845  const SQLTypeInfo gti = gexpr->get_type_info();
1846  bool set_new_type = false;
1847  SQLTypeInfo ti(gti);
1848  if (gti.is_string() && gti.get_compression() == kENCODING_NONE) {
1849  set_new_type = true;
1850  ti.set_compression(kENCODING_DICT);
1851  ti.set_comp_param(TRANSIENT_DICT_ID);
1852  ti.setStringDictKey(shared::StringDictKey::kTransientDictKey);
1853  ti.set_fixed_size();
1854  }
1855  std::shared_ptr<Analyzer::Var> v;
1856  if (std::dynamic_pointer_cast<Analyzer::Var>(gexpr)) {
1857  v = std::static_pointer_cast<Analyzer::Var>(gexpr);
1858  int n = v->get_varno();
1859  gexpr = tlist[n - 1]->get_own_expr();
1860  auto cv = std::dynamic_pointer_cast<Analyzer::ColumnVar>(gexpr);
1861  if (cv != nullptr) {
1862  // inherit all ColumnVar info for lineage.
1863  *std::static_pointer_cast<Analyzer::ColumnVar>(v) = *cv;
1864  }
1865  v->set_which_row(Analyzer::Var::kGROUPBY);
1866  v->set_varno(gexpr_no);
1867  tlist[n - 1]->set_expr(v);
1868  }
1869  if (set_new_type) {
1870  auto new_e = gexpr->add_cast(ti);
1871  groupby.push_back(new_e);
1872  if (v != nullptr) {
1873  v->set_type_info(new_e->get_type_info());
1874  }
1875  } else {
1876  groupby.push_back(gexpr);
1877  }
1878  gexpr_no++;
1879  }
1880  }
1881  if (query.get_num_aggs() > 0 || !groupby.empty()) {
1882  for (auto t : query.get_targetlist()) {
1883  auto e = t->get_expr();
1884  e->check_group_by(groupby);
1885  }
1886  }
1887  query.set_group_by(groupby);
1888 }
std::list< std::unique_ptr< Expr > > groupby_clause_
Definition: ParserNode.h:1768
int get_num_aggs() const
Definition: Analyzer.h:3019
#define TRANSIENT_DICT_ID
Definition: DbObjectKeys.h:24
void set_group_by(std::list< std::shared_ptr< Analyzer::Expr >> &g)
Definition: Analyzer.h:3048
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:399
virtual std::shared_ptr< Analyzer::Expr > add_cast(const SQLTypeInfo &new_type_info)
Definition: Analyzer.cpp:774
static const StringDictKey kTransientDictKey
Definition: DbObjectKeys.h:45
bool is_string() const
Definition: sqltypes.h:559
constexpr double n
Definition: Utm.h:38
const std::vector< std::shared_ptr< TargetEntry > > & get_targetlist() const
Definition: Analyzer.h:3020

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

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

1803  {
1804  std::shared_ptr<Analyzer::Expr> p;
1805  if (having_clause_ != nullptr) {
1806  p = having_clause_->analyze(catalog, query, Expr::TlistRefType::TLIST_COPY);
1807  if (p->get_type_info().get_type() != kBOOLEAN) {
1808  throw std::runtime_error("Only boolean expressions can be in HAVING clause.");
1809  }
1810  p->check_group_by(query.get_group_by());
1811  }
1812  query.set_having_predicate(p);
1813 }
std::unique_ptr< Expr > having_clause_
Definition: ParserNode.h:1769
void set_having_predicate(std::shared_ptr< Analyzer::Expr > p)
Definition: Analyzer.h:3049
const std::list< std::shared_ptr< Analyzer::Expr > > & get_group_by() const
Definition: Analyzer.h:3034

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

1904  {
1905  std::vector<std::shared_ptr<Analyzer::TargetEntry>>& tlist =
1906  query.get_targetlist_nonconst();
1907  if (select_clause_.empty()) {
1908  // this means SELECT *
1909  int rte_idx = 0;
1910  for (auto rte : query.get_rangetable()) {
1911  rte->expand_star_in_targetlist(catalog, tlist, rte_idx++);
1912  }
1913  } else {
1914  for (auto& p : select_clause_) {
1915  const Parser::Expr* select_expr = p->get_select_expr();
1916  // look for the case of range_var.*
1917  if (typeid(*select_expr) == typeid(ColumnRef) &&
1918  dynamic_cast<const ColumnRef*>(select_expr)->get_column() == nullptr) {
1919  const std::string* range_var_name =
1920  dynamic_cast<const ColumnRef*>(select_expr)->get_table();
1921  int rte_idx = query.get_rte_idx(*range_var_name);
1922  if (rte_idx < 0) {
1923  throw std::runtime_error("invalid range variable name: " + *range_var_name);
1924  }
1925  Analyzer::RangeTableEntry* rte = query.get_rte(rte_idx);
1926  rte->expand_star_in_targetlist(catalog, tlist, rte_idx);
1927  } else {
1928  auto e = select_expr->analyze(catalog, query);
1929  std::string resname;
1930 
1931  if (p->get_alias() != nullptr) {
1932  resname = *p->get_alias();
1933  } else if (std::dynamic_pointer_cast<Analyzer::ColumnVar>(e) &&
1934  !std::dynamic_pointer_cast<Analyzer::Var>(e)) {
1935  auto colvar = std::static_pointer_cast<Analyzer::ColumnVar>(e);
1936  const auto& column_key = colvar->getColumnKey();
1937  const ColumnDescriptor* col_desc =
1938  catalog.getMetadataForColumn(column_key.table_id, column_key.column_id);
1939  resname = col_desc->columnName;
1940  }
1941  if (e->get_type_info().get_type() == kNULLT) {
1942  throw std::runtime_error(
1943  "Untyped NULL in SELECT clause. Use CAST to specify a type.");
1944  }
1945  auto o = std::static_pointer_cast<Analyzer::UOper>(e);
1946  bool unnest = (o != nullptr && o->get_optype() == kUNNEST);
1947  auto tle = std::make_shared<Analyzer::TargetEntry>(resname, e, unnest);
1948  tlist.push_back(tle);
1949  }
1950  }
1951  }
1952 }
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:1495
RangeTableEntry * get_rte(int rte_idx) const
Definition: Analyzer.h:3056
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:3023
std::list< std::unique_ptr< SelectEntry > > select_clause_
Definition: ParserNode.h:1765
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:3032

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

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

1891  {
1892  if (where_clause_ == nullptr) {
1893  query.set_where_predicate(nullptr);
1894  return;
1895  }
1896  auto p = where_clause_->analyze(catalog, query, Expr::TlistRefType::TLIST_COPY);
1897  if (p->get_type_info().get_type() != kBOOLEAN) {
1898  throw std::runtime_error("Only boolean expressions can be in WHERE clause.");
1899  }
1900  query.set_where_predicate(p);
1901 }
std::unique_ptr< Expr > where_clause_
Definition: ParserNode.h:1767
void set_where_predicate(std::shared_ptr< Analyzer::Expr > p)
Definition: Analyzer.h:3047

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

References from_clause_.

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

Definition at line 1755 of file ParserNode.h.

References groupby_clause_.

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

Definition at line 1758 of file ParserNode.h.

References having_clause_.

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

Definition at line 1747 of file ParserNode.h.

References is_distinct_.

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

Definition at line 1748 of file ParserNode.h.

References select_clause_.

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

Definition at line 1754 of file ParserNode.h.

References where_clause_.

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

Definition at line 2306 of file ParserNode.cpp.

2306  {
2307  std::string query_str = "SELECT ";
2308  if (is_distinct_) {
2309  query_str += "DISTINCT ";
2310  }
2311  if (select_clause_.empty()) {
2312  query_str += "* ";
2313  } else {
2314  bool notfirst = false;
2315  for (auto& p : select_clause_) {
2316  if (notfirst) {
2317  query_str += ", ";
2318  } else {
2319  notfirst = true;
2320  }
2321  query_str += p->to_string();
2322  }
2323  }
2324  query_str += " FROM ";
2325  bool notfirst = false;
2326  for (auto& p : from_clause_) {
2327  if (notfirst) {
2328  query_str += ", ";
2329  } else {
2330  notfirst = true;
2331  }
2332  query_str += p->to_string();
2333  }
2334  if (where_clause_) {
2335  query_str += " WHERE " + where_clause_->to_string();
2336  }
2337  if (!groupby_clause_.empty()) {
2338  query_str += " GROUP BY ";
2339  bool notfirst = false;
2340  for (auto& p : groupby_clause_) {
2341  if (notfirst) {
2342  query_str += ", ";
2343  } else {
2344  notfirst = true;
2345  }
2346  query_str += p->to_string();
2347  }
2348  }
2349  if (having_clause_) {
2350  query_str += " HAVING " + having_clause_->to_string();
2351  }
2352  query_str += ";";
2353  return query_str;
2354 }
std::unique_ptr< Expr > having_clause_
Definition: ParserNode.h:1769
std::list< std::unique_ptr< Expr > > groupby_clause_
Definition: ParserNode.h:1768
std::list< std::unique_ptr< TableRef > > from_clause_
Definition: ParserNode.h:1766
std::unique_ptr< Expr > where_clause_
Definition: ParserNode.h:1767
std::list< std::unique_ptr< SelectEntry > > select_clause_
Definition: ParserNode.h:1765

Member Data Documentation

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

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

Referenced by get_groupby_clause(), and QuerySpec().

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

Definition at line 1769 of file ParserNode.h.

Referenced by get_having_clause().

bool Parser::QuerySpec::is_distinct_
private

Definition at line 1764 of file ParserNode.h.

Referenced by get_is_distinct().

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

Definition at line 1765 of file ParserNode.h.

Referenced by get_select_clause(), and QuerySpec().

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

Definition at line 1767 of file ParserNode.h.

Referenced by get_where_clause().


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