OmniSciDB  06b3bd477c
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Parser::SelectStmt Class Reference

#include <ParserNode.h>

+ Inheritance diagram for Parser::SelectStmt:
+ Collaboration diagram for Parser::SelectStmt:

Public Member Functions

 SelectStmt (QueryExpr *q, std::list< OrderSpec * > *o, int64_t l, int64_t f)
 
const QueryExprget_query_expr () const
 
const std::list
< std::unique_ptr< OrderSpec > > & 
get_orderby_clause () const
 
void analyze (const Catalog_Namespace::Catalog &catalog, Analyzer::Query &query) const override
 
- Public Member Functions inherited from Parser::Node
virtual ~Node ()
 

Private Attributes

std::unique_ptr< QueryExprquery_expr
 
std::list< std::unique_ptr
< OrderSpec > > 
orderby_clause
 
int64_t limit
 
int64_t offset
 

Detailed Description

Definition at line 1762 of file ParserNode.h.

Constructor & Destructor Documentation

Parser::SelectStmt::SelectStmt ( QueryExpr q,
std::list< OrderSpec * > *  o,
int64_t  l,
int64_t  f 
)
inline

Definition at line 1764 of file ParserNode.h.

References orderby_clause.

1765  : query_expr(q), limit(l), offset(f) {
1766  if (o) {
1767  for (const auto e : *o) {
1768  orderby_clause.emplace_back(e);
1769  }
1770  delete o;
1771  }
1772  }
std::unique_ptr< QueryExpr > query_expr
Definition: ParserNode.h:1781
std::list< std::unique_ptr< OrderSpec > > orderby_clause
Definition: ParserNode.h:1782

Member Function Documentation

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

Implements Parser::DMLStmt.

Definition at line 1202 of file ParserNode.cpp.

References Analyzer::Query::get_is_distinct(), Analyzer::Query::get_targetlist(), kSELECT, Analyzer::Query::set_limit(), Analyzer::Query::set_offset(), Analyzer::Query::set_order_by(), and Analyzer::Query::set_stmt_type().

1203  {
1204  query.set_stmt_type(kSELECT);
1205  query.set_limit(limit);
1206  if (offset < 0) {
1207  throw std::runtime_error("OFFSET cannot be negative.");
1208  }
1209  query.set_offset(offset);
1210  query_expr->analyze(catalog, query);
1211  if (orderby_clause.empty() && !query.get_is_distinct()) {
1212  query.set_order_by(nullptr);
1213  return;
1214  }
1215  const std::vector<std::shared_ptr<Analyzer::TargetEntry>>& tlist =
1216  query.get_targetlist();
1217  std::list<Analyzer::OrderEntry>* order_by = new std::list<Analyzer::OrderEntry>();
1218  if (!orderby_clause.empty()) {
1219  for (auto& p : orderby_clause) {
1220  int tle_no = p->get_colno();
1221  if (tle_no == 0) {
1222  // use column name
1223  // search through targetlist for matching name
1224  const std::string* name = p->get_column()->get_column();
1225  tle_no = 1;
1226  bool found = false;
1227  for (auto tle : tlist) {
1228  if (tle->get_resname() == *name) {
1229  found = true;
1230  break;
1231  }
1232  tle_no++;
1233  }
1234  if (!found) {
1235  throw std::runtime_error("invalid name in order by: " + *name);
1236  }
1237  }
1238  order_by->push_back(
1239  Analyzer::OrderEntry(tle_no, p->get_is_desc(), p->get_nulls_first()));
1240  }
1241  }
1242  if (query.get_is_distinct()) {
1243  // extend order_by to include all targetlist entries.
1244  for (int i = 1; i <= static_cast<int>(tlist.size()); i++) {
1245  bool in_orderby = false;
1246  std::for_each(order_by->begin(),
1247  order_by->end(),
1248  [&in_orderby, i](const Analyzer::OrderEntry& oe) {
1249  in_orderby = in_orderby || (i == oe.tle_no);
1250  });
1251  if (!in_orderby) {
1252  order_by->push_back(Analyzer::OrderEntry(i, false, false));
1253  }
1254  }
1255  }
1256  query.set_order_by(order_by);
1257 }
void set_order_by(std::list< OrderEntry > *o)
Definition: Analyzer.h:1635
void set_offset(int64_t o)
Definition: Analyzer.h:1647
bool get_is_distinct() const
Definition: Analyzer.h:1609
std::unique_ptr< QueryExpr > query_expr
Definition: ParserNode.h:1781
void set_stmt_type(SQLStmtType t)
Definition: Analyzer.h:1638
Definition: Analyzer.h:1414
void set_limit(int64_t l)
Definition: Analyzer.h:1645
const std::vector< std::shared_ptr< TargetEntry > > & get_targetlist() const
Definition: Analyzer.h:1611
std::list< std::unique_ptr< OrderSpec > > orderby_clause
Definition: ParserNode.h:1782

+ Here is the call graph for this function:

const std::list<std::unique_ptr<OrderSpec> >& Parser::SelectStmt::get_orderby_clause ( ) const
inline

Definition at line 1774 of file ParserNode.h.

References orderby_clause.

1774  {
1775  return orderby_clause;
1776  }
std::list< std::unique_ptr< OrderSpec > > orderby_clause
Definition: ParserNode.h:1782
const QueryExpr* Parser::SelectStmt::get_query_expr ( ) const
inline

Definition at line 1773 of file ParserNode.h.

References query_expr.

1773 { return query_expr.get(); }
std::unique_ptr< QueryExpr > query_expr
Definition: ParserNode.h:1781

Member Data Documentation

int64_t Parser::SelectStmt::limit
private

Definition at line 1783 of file ParserNode.h.

int64_t Parser::SelectStmt::offset
private

Definition at line 1784 of file ParserNode.h.

std::list<std::unique_ptr<OrderSpec> > Parser::SelectStmt::orderby_clause
private

Definition at line 1782 of file ParserNode.h.

Referenced by get_orderby_clause(), and SelectStmt().

std::unique_ptr<QueryExpr> Parser::SelectStmt::query_expr
private

Definition at line 1781 of file ParserNode.h.

Referenced by get_query_expr().


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