OmniSciDB  a987f07e93
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Parser::CaseExpr Class Reference

#include <ParserNode.h>

+ Inheritance diagram for Parser::CaseExpr:
+ Collaboration diagram for Parser::CaseExpr:

Public Member Functions

 CaseExpr (std::list< ExprPair * > *w, Expr *e)
 
std::shared_ptr< Analyzer::Expranalyze (const Catalog_Namespace::Catalog &catalog, Analyzer::Query &query, TlistRefType allow_tlist_ref=TLIST_NONE) const override
 
std::string to_string () const override
 
- Public Member Functions inherited from Parser::Node
virtual ~Node ()
 

Static Public Member Functions

static std::shared_ptr
< Analyzer::Expr
normalize (const std::list< std::pair< std::shared_ptr< Analyzer::Expr >, std::shared_ptr< Analyzer::Expr >>> &, const std::shared_ptr< Analyzer::Expr >, const Executor *executor=nullptr)
 

Private Attributes

std::list< std::unique_ptr
< ExprPair > > 
when_then_list_
 
std::unique_ptr< Exprelse_expr_
 

Additional Inherited Members

- Public Types inherited from Parser::Expr
enum  TlistRefType { TLIST_NONE, TLIST_REF, TLIST_COPY }
 

Detailed Description

Definition at line 695 of file ParserNode.h.

Constructor & Destructor Documentation

Parser::CaseExpr::CaseExpr ( std::list< ExprPair * > *  w,
Expr e 
)
inline

Definition at line 697 of file ParserNode.h.

References CHECK, and when_then_list_.

697  : else_expr_(e) {
698  CHECK(w);
699  for (const auto e : *w) {
700  when_then_list_.emplace_back(e);
701  }
702  delete w;
703  }
std::unique_ptr< Expr > else_expr_
Definition: ParserNode.h:717
#define CHECK(condition)
Definition: Logger.h:289
std::list< std::unique_ptr< ExprPair > > when_then_list_
Definition: ParserNode.h:716

Member Function Documentation

std::shared_ptr< Analyzer::Expr > Parser::CaseExpr::analyze ( const Catalog_Namespace::Catalog catalog,
Analyzer::Query query,
TlistRefType  allow_tlist_ref = TLIST_NONE 
) const
overridevirtual

Implements Parser::Expr.

Definition at line 1045 of file ParserNode.cpp.

References kBOOLEAN.

1048  {
1049  SQLTypeInfo ti;
1050  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
1051  expr_pair_list;
1052  for (auto& p : when_then_list_) {
1053  auto e1 = p->get_expr1()->analyze(catalog, query, allow_tlist_ref);
1054  if (e1->get_type_info().get_type() != kBOOLEAN) {
1055  throw std::runtime_error("Only boolean expressions can be used after WHEN.");
1056  }
1057  auto e2 = p->get_expr2()->analyze(catalog, query, allow_tlist_ref);
1058  expr_pair_list.emplace_back(e1, e2);
1059  }
1060  auto else_e =
1061  else_expr_ ? else_expr_->analyze(catalog, query, allow_tlist_ref) : nullptr;
1062  return normalize(expr_pair_list, else_e);
1063 }
static std::shared_ptr< Analyzer::Expr > normalize(const std::list< std::pair< std::shared_ptr< Analyzer::Expr >, std::shared_ptr< Analyzer::Expr >>> &, const std::shared_ptr< Analyzer::Expr >, const Executor *executor=nullptr)
std::unique_ptr< Expr > else_expr_
Definition: ParserNode.h:717
std::list< std::unique_ptr< ExprPair > > when_then_list_
Definition: ParserNode.h:716
std::shared_ptr< Analyzer::Expr > Parser::CaseExpr::normalize ( const std::list< std::pair< std::shared_ptr< Analyzer::Expr >, std::shared_ptr< Analyzer::Expr >>> &  expr_pair_list,
const std::shared_ptr< Analyzer::Expr else_e_in,
const Executor executor = nullptr 
)
static

Definition at line 1630 of file ParserNode.cpp.

References CHECK, Analyzer::BinOper::common_numeric_type(), Parser::common_string_type(), Parser::anonymous_namespace{ParserNode.cpp}::expr_is_null(), get_logical_type_info(), SQLTypeInfo::get_type(), SQLTypeInfo::is_boolean(), SQLTypeInfo::is_number(), SQLTypeInfo::is_string(), kENCODING_DICT, kNULLT, kTEXT, SQLTypeInfo::set_notnull(), and TRANSIENT_DICT_ID.

Referenced by QueryRewriter::rewriteColumnarDelete(), QueryRewriter::rewriteColumnarUpdate(), and RelAlgTranslator::translateCase().

1634  {
1635  SQLTypeInfo ti;
1636  bool has_agg = false;
1637  // We need to keep track of whether there was at
1638  // least one none-encoded string literal expression
1639  // type among any of the case sub-expressions separately
1640  // from rest of type determination logic, as it will
1641  // be casted to the output dictionary type if all output
1642  // types are either dictionary encoded or none-encoded
1643  // literals, or a transient encoded dictionary if all
1644  // types are none-encoded (column or literal)
1645  SQLTypeInfo none_encoded_literal_ti;
1646 
1647  for (auto& p : expr_pair_list) {
1648  auto e1 = p.first;
1649  CHECK(e1->get_type_info().is_boolean());
1650  auto e2 = p.second;
1651  if (e2->get_contains_agg()) {
1652  has_agg = true;
1653  }
1654  const auto& e2_ti = e2->get_type_info();
1655  if (e2_ti.is_string() && !e2_ti.is_dict_encoded_string() &&
1656  !std::dynamic_pointer_cast<const Analyzer::ColumnVar>(e2)) {
1657  CHECK(e2_ti.is_none_encoded_string());
1658  none_encoded_literal_ti =
1659  none_encoded_literal_ti.get_type() == kNULLT
1660  ? e2_ti
1661  : common_string_type(none_encoded_literal_ti, e2_ti, executor);
1662  continue;
1663  }
1664  if (ti.get_type() == kNULLT) {
1665  ti = e2_ti;
1666  } else if (e2_ti.get_type() == kNULLT) {
1667  ti.set_notnull(false);
1668  e2->set_type_info(ti);
1669  } else if (ti != e2_ti) {
1670  if (ti.is_string() && e2_ti.is_string()) {
1671  // Executor is needed to determine which dictionary is the largest
1672  // in case of two dictionary types with different encodings
1673  ti = common_string_type(ti, e2_ti, executor);
1674  } else if (ti.is_number() && e2_ti.is_number()) {
1676  } else if (ti.is_boolean() && e2_ti.is_boolean()) {
1678  } else {
1679  throw std::runtime_error(
1680  "Expressions in THEN clause must be of the same or compatible types.");
1681  }
1682  }
1683  }
1684  auto else_e = else_e_in;
1685  const auto& else_ti = else_e->get_type_info();
1686  if (else_e) {
1687  if (else_e->get_contains_agg()) {
1688  has_agg = true;
1689  }
1690  if (else_ti.is_string() && !else_ti.is_dict_encoded_string() &&
1691  !std::dynamic_pointer_cast<const Analyzer::ColumnVar>(else_e)) {
1692  CHECK(else_ti.is_none_encoded_string());
1693  none_encoded_literal_ti =
1694  none_encoded_literal_ti.get_type() == kNULLT
1695  ? else_ti
1696  : common_string_type(none_encoded_literal_ti, else_ti, executor);
1697  } else {
1698  if (ti.get_type() == kNULLT) {
1699  ti = else_ti;
1700  } else if (expr_is_null(else_e.get())) {
1701  ti.set_notnull(false);
1702  else_e->set_type_info(ti);
1703  } else if (ti != else_ti) {
1704  ti.set_notnull(false);
1705  if (ti.is_string() && else_ti.is_string()) {
1706  // Executor is needed to determine which dictionary is the largest
1707  // in case of two dictionary types with different encodings
1708  ti = common_string_type(ti, else_ti, executor);
1709  } else if (ti.is_number() && else_ti.is_number()) {
1710  ti = Analyzer::BinOper::common_numeric_type(ti, else_ti);
1711  } else if (ti.is_boolean() && else_ti.is_boolean()) {
1712  ti = Analyzer::BinOper::common_numeric_type(ti, else_ti);
1713  } else if (get_logical_type_info(ti) != get_logical_type_info(else_ti)) {
1714  throw std::runtime_error(
1715  // types differing by encoding will be resolved at decode
1716  "Expressions in ELSE clause must be of the same or compatible types as "
1717  "those in the THEN clauses.");
1718  }
1719  }
1720  }
1721  }
1722 
1723  if (ti.get_type() == kNULLT && none_encoded_literal_ti.get_type() != kNULLT) {
1724  // If we haven't set a type so far it's because
1725  // every case sub-expression has a none-encoded
1726  // literal output. Output a transient-encoded dictionary
1727  // so we can use the output downstream
1729  }
1730 
1731  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
1732  cast_expr_pair_list;
1733  for (auto p : expr_pair_list) {
1734  ti.set_notnull(false);
1735  cast_expr_pair_list.emplace_back(p.first, p.second->add_cast(ti));
1736  }
1737  if (else_e != nullptr) {
1738  else_e = else_e->add_cast(ti);
1739  } else {
1740  Datum d;
1741  // always create an else expr so that executor doesn't need to worry about it
1742  ti.set_notnull(false);
1743  else_e = makeExpr<Analyzer::Constant>(ti, true, d);
1744  }
1745  if (ti.get_type() == kNULLT) {
1746  throw std::runtime_error(
1747  "Cannot deduce the type for case expressions, all branches null");
1748  }
1749 
1750  auto case_expr = makeExpr<Analyzer::CaseExpr>(ti, has_agg, cast_expr_pair_list, else_e);
1751  return case_expr;
1752 }
SQLTypeInfo get_logical_type_info(const SQLTypeInfo &type_info)
Definition: sqltypes.h:1206
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:380
SQLTypeInfo common_string_type(const SQLTypeInfo &lhs_type_info, const SQLTypeInfo &rhs_type_info, const Executor *executor)
Definition: ParserNode.cpp:346
bool is_number() const
Definition: sqltypes.h:581
static SQLTypeInfo common_numeric_type(const SQLTypeInfo &type1, const SQLTypeInfo &type2)
Definition: Analyzer.cpp:465
bool is_boolean() const
Definition: sqltypes.h:583
bool expr_is_null(const Analyzer::Expr *expr)
Definition: sqltypes.h:67
#define TRANSIENT_DICT_ID
Definition: sqltypes.h:310
void set_notnull(bool n)
Definition: sqltypes.h:497
#define CHECK(condition)
Definition: Logger.h:289
bool is_string() const
Definition: sqltypes.h:576
Definition: Datum.h:67

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::string Parser::CaseExpr::to_string ( ) const
overridevirtual

Implements Parser::Expr.

Definition at line 1754 of file ParserNode.cpp.

1754  {
1755  std::string str("CASE ");
1756  for (auto& p : when_then_list_) {
1757  str += "WHEN " + p->get_expr1()->to_string() + " THEN " +
1758  p->get_expr2()->to_string() + " ";
1759  }
1760  if (else_expr_ != nullptr) {
1761  str += "ELSE " + else_expr_->to_string();
1762  }
1763  str += " END";
1764  return str;
1765 }
std::unique_ptr< Expr > else_expr_
Definition: ParserNode.h:717
std::list< std::unique_ptr< ExprPair > > when_then_list_
Definition: ParserNode.h:716

Member Data Documentation

std::unique_ptr<Expr> Parser::CaseExpr::else_expr_
private

Definition at line 717 of file ParserNode.h.

std::list<std::unique_ptr<ExprPair> > Parser::CaseExpr::when_then_list_
private

Definition at line 716 of file ParserNode.h.

Referenced by CaseExpr().


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