OmniSciDB  a5dc49c757
 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 1984 of file ParserNode.cpp.

References Analyzer::Query::set_is_distinct().

1985  {
1987  analyze_from_clause(catalog, query);
1988  analyze_select_clause(catalog, query);
1989  analyze_where_clause(catalog, query);
1990  analyze_group_by(catalog, query);
1991  analyze_having_clause(catalog, query);
1992 }
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:3177
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 1964 of file ParserNode.cpp.

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

1965  {
1967  for (auto& p : from_clause_) {
1968  const TableDescriptor* table_desc;
1969  table_desc = catalog.getMetadataForTable(*p->get_table_name());
1970  if (table_desc == nullptr) {
1971  throw std::runtime_error("Table " + *p->get_table_name() + " does not exist.");
1972  }
1973  std::string range_var;
1974  if (p->get_range_var() == nullptr) {
1975  range_var = *p->get_table_name();
1976  } else {
1977  range_var = *p->get_range_var();
1978  }
1979  rte = new Analyzer::RangeTableEntry(range_var, table_desc, nullptr);
1980  query.add_rte(rte);
1981  }
1982 }
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 1825 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.

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

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

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

1813  {
1814  std::shared_ptr<Analyzer::Expr> p;
1815  if (having_clause_ != nullptr) {
1816  p = having_clause_->analyze(catalog, query, Expr::TlistRefType::TLIST_COPY);
1817  if (p->get_type_info().get_type() != kBOOLEAN) {
1818  throw std::runtime_error("Only boolean expressions can be in HAVING clause.");
1819  }
1820  p->check_group_by(query.get_group_by());
1821  }
1822  query.set_having_predicate(p);
1823 }
std::unique_ptr< Expr > having_clause_
Definition: ParserNode.h:1769
void set_having_predicate(std::shared_ptr< Analyzer::Expr > p)
Definition: Analyzer.h:3180
const std::list< std::shared_ptr< Analyzer::Expr > > & get_group_by() const
Definition: Analyzer.h:3165

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

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

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

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

1901  {
1902  if (where_clause_ == nullptr) {
1903  query.set_where_predicate(nullptr);
1904  return;
1905  }
1906  auto p = where_clause_->analyze(catalog, query, Expr::TlistRefType::TLIST_COPY);
1907  if (p->get_type_info().get_type() != kBOOLEAN) {
1908  throw std::runtime_error("Only boolean expressions can be in WHERE clause.");
1909  }
1910  query.set_where_predicate(p);
1911 }
std::unique_ptr< Expr > where_clause_
Definition: ParserNode.h:1767
void set_where_predicate(std::shared_ptr< Analyzer::Expr > p)
Definition: Analyzer.h:3178

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

2316  {
2317  std::string query_str = "SELECT ";
2318  if (is_distinct_) {
2319  query_str += "DISTINCT ";
2320  }
2321  if (select_clause_.empty()) {
2322  query_str += "* ";
2323  } else {
2324  bool notfirst = false;
2325  for (auto& p : select_clause_) {
2326  if (notfirst) {
2327  query_str += ", ";
2328  } else {
2329  notfirst = true;
2330  }
2331  query_str += p->to_string();
2332  }
2333  }
2334  query_str += " FROM ";
2335  bool notfirst = false;
2336  for (auto& p : from_clause_) {
2337  if (notfirst) {
2338  query_str += ", ";
2339  } else {
2340  notfirst = true;
2341  }
2342  query_str += p->to_string();
2343  }
2344  if (where_clause_) {
2345  query_str += " WHERE " + where_clause_->to_string();
2346  }
2347  if (!groupby_clause_.empty()) {
2348  query_str += " GROUP BY ";
2349  bool notfirst = false;
2350  for (auto& p : groupby_clause_) {
2351  if (notfirst) {
2352  query_str += ", ";
2353  } else {
2354  notfirst = true;
2355  }
2356  query_str += p->to_string();
2357  }
2358  }
2359  if (having_clause_) {
2360  query_str += " HAVING " + having_clause_->to_string();
2361  }
2362  query_str += ";";
2363  return query_str;
2364 }
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: