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

Constructor & Destructor Documentation

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

Definition at line 698 of file ParserNode.h.

References CHECK, and when_then_list_.

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

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

References kBOOLEAN.

1060  {
1061  SQLTypeInfo ti;
1062  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
1063  expr_pair_list;
1064  for (auto& p : when_then_list_) {
1065  auto e1 = p->get_expr1()->analyze(catalog, query, allow_tlist_ref);
1066  if (e1->get_type_info().get_type() != kBOOLEAN) {
1067  throw std::runtime_error("Only boolean expressions can be used after WHEN.");
1068  }
1069  auto e2 = p->get_expr2()->analyze(catalog, query, allow_tlist_ref);
1070  expr_pair_list.emplace_back(e1, e2);
1071  }
1072  auto else_e =
1073  else_expr_ ? else_expr_->analyze(catalog, query, allow_tlist_ref) : nullptr;
1074  return normalize(expr_pair_list, else_e);
1075 }
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:718
std::list< std::unique_ptr< ExprPair > > when_then_list_
Definition: ParserNode.h:717
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 1654 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().

1658  {
1659  SQLTypeInfo ti;
1660  bool has_agg = false;
1661  // We need to keep track of whether there was at
1662  // least one none-encoded string literal expression
1663  // type among any of the case sub-expressions separately
1664  // from rest of type determination logic, as it will
1665  // be casted to the output dictionary type if all output
1666  // types are either dictionary encoded or none-encoded
1667  // literals, or a transient encoded dictionary if all
1668  // types are none-encoded (column or literal)
1669  SQLTypeInfo none_encoded_literal_ti;
1670 
1671  for (auto& p : expr_pair_list) {
1672  auto e1 = p.first;
1673  CHECK(e1->get_type_info().is_boolean());
1674  auto e2 = p.second;
1675  if (e2->get_contains_agg()) {
1676  has_agg = true;
1677  }
1678  const auto& e2_ti = e2->get_type_info();
1679  const auto col_var = std::dynamic_pointer_cast<const Analyzer::ColumnVar>(e2);
1680  if (e2_ti.is_string() && !e2_ti.is_dict_encoded_string() && !col_var) {
1681  CHECK(e2_ti.is_none_encoded_string());
1682  none_encoded_literal_ti =
1683  none_encoded_literal_ti.get_type() == kNULLT
1684  ? e2_ti
1685  : common_string_type(none_encoded_literal_ti, e2_ti, executor);
1686  continue;
1687  }
1688  if (ti.get_type() == kNULLT) {
1689  if (!expr_is_null(e2.get())) {
1690  ti = e2_ti;
1691  }
1692  } else if (expr_is_null(e2.get())) {
1693  ti.set_notnull(false);
1694  e2->set_type_info(ti);
1695  } else if (ti != e2_ti) {
1696  if (ti.is_string() && e2_ti.is_string()) {
1697  // Executor is needed to determine which dictionary is the largest
1698  // in case of two dictionary types with different encodings
1699  ti = common_string_type(ti, e2_ti, executor);
1700  } else if (ti.is_number() && e2_ti.is_number()) {
1702  } else if (ti.is_boolean() && e2_ti.is_boolean()) {
1704  } else {
1705  throw std::runtime_error(
1706  "Expressions in THEN clause must be of the same or compatible types.");
1707  }
1708  }
1709  }
1710  auto else_e = else_e_in;
1711  const auto& else_ti = else_e->get_type_info();
1712  if (else_e) {
1713  const auto col_var = std::dynamic_pointer_cast<const Analyzer::ColumnVar>(else_e);
1714  if (else_e->get_contains_agg()) {
1715  has_agg = true;
1716  }
1717  if (else_ti.is_string() && !else_ti.is_dict_encoded_string() && !col_var) {
1718  CHECK(else_ti.is_none_encoded_string());
1719  none_encoded_literal_ti =
1720  none_encoded_literal_ti.get_type() == kNULLT
1721  ? else_ti
1722  : common_string_type(none_encoded_literal_ti, else_ti, executor);
1723  } else {
1724  if (ti.get_type() == kNULLT) {
1725  ti = else_ti;
1726  } else if (expr_is_null(else_e.get())) {
1727  ti.set_notnull(false);
1728  else_e->set_type_info(ti);
1729  } else if (ti != else_ti) {
1730  ti.set_notnull(false);
1731  if (ti.is_string() && else_ti.is_string()) {
1732  // Executor is needed to determine which dictionary is the largest
1733  // in case of two dictionary types with different encodings
1734  ti = common_string_type(ti, else_ti, executor);
1735  } else if (ti.is_number() && else_ti.is_number()) {
1736  ti = Analyzer::BinOper::common_numeric_type(ti, else_ti);
1737  } else if (ti.is_boolean() && else_ti.is_boolean()) {
1738  ti = Analyzer::BinOper::common_numeric_type(ti, else_ti);
1739  } else if (get_logical_type_info(ti) != get_logical_type_info(else_ti)) {
1740  throw std::runtime_error(
1741  // types differing by encoding will be resolved at decode
1742  "Expressions in ELSE clause must be of the same or compatible types as "
1743  "those in the THEN clauses.");
1744  }
1745  }
1746  }
1747  }
1748 
1749  if (ti.get_type() == kNULLT && none_encoded_literal_ti.get_type() != kNULLT) {
1750  // If we haven't set a type so far it's because
1751  // every case sub-expression has a none-encoded
1752  // literal output. Output a transient-encoded dictionary
1753  // so we can use the output downstream
1755  }
1756 
1757  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
1758  cast_expr_pair_list;
1759  for (auto p : expr_pair_list) {
1760  ti.set_notnull(false);
1761  cast_expr_pair_list.emplace_back(p.first, p.second->add_cast(ti));
1762  }
1763  if (else_e != nullptr) {
1764  else_e = else_e->add_cast(ti);
1765  } else {
1766  Datum d;
1767  // always create an else expr so that executor doesn't need to worry about it
1768  ti.set_notnull(false);
1769  else_e = makeExpr<Analyzer::Constant>(ti, true, d);
1770  }
1771  if (ti.get_type() == kNULLT) {
1772  throw std::runtime_error(
1773  "Cannot deduce the type for case expressions, all branches null");
1774  }
1775 
1776  auto case_expr = makeExpr<Analyzer::CaseExpr>(ti, has_agg, cast_expr_pair_list, else_e);
1777  return case_expr;
1778 }
SQLTypeInfo get_logical_type_info(const SQLTypeInfo &type_info)
Definition: sqltypes.h:1470
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:391
SQLTypeInfo common_string_type(const SQLTypeInfo &lhs_type_info, const SQLTypeInfo &rhs_type_info, const Executor *executor)
Definition: ParserNode.cpp:354
#define TRANSIENT_DICT_ID
Definition: DbObjectKeys.h:24
bool is_number() const
Definition: sqltypes.h:574
static SQLTypeInfo common_numeric_type(const SQLTypeInfo &type1, const SQLTypeInfo &type2)
Definition: Analyzer.cpp:500
bool is_boolean() const
Definition: sqltypes.h:580
bool expr_is_null(const Analyzer::Expr *expr)
Definition: sqltypes.h:79
void set_notnull(bool n)
Definition: sqltypes.h:475
#define CHECK(condition)
Definition: Logger.h:291
bool is_string() const
Definition: sqltypes.h:559
Definition: Datum.h:69

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

1780  {
1781  std::string str("CASE ");
1782  for (auto& p : when_then_list_) {
1783  str += "WHEN " + p->get_expr1()->to_string() + " THEN " +
1784  p->get_expr2()->to_string() + " ";
1785  }
1786  if (else_expr_ != nullptr) {
1787  str += "ELSE " + else_expr_->to_string();
1788  }
1789  str += " END";
1790  return str;
1791 }
std::unique_ptr< Expr > else_expr_
Definition: ParserNode.h:718
std::list< std::unique_ptr< ExprPair > > when_then_list_
Definition: ParserNode.h:717

Member Data Documentation

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

Definition at line 718 of file ParserNode.h.

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

Definition at line 717 of file ParserNode.h.

Referenced by CaseExpr().


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