OmniSciDB  c1a53651b2
 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:291
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 1052 of file ParserNode.cpp.

References kBOOLEAN.

1055  {
1056  SQLTypeInfo ti;
1057  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
1058  expr_pair_list;
1059  for (auto& p : when_then_list_) {
1060  auto e1 = p->get_expr1()->analyze(catalog, query, allow_tlist_ref);
1061  if (e1->get_type_info().get_type() != kBOOLEAN) {
1062  throw std::runtime_error("Only boolean expressions can be used after WHEN.");
1063  }
1064  auto e2 = p->get_expr2()->analyze(catalog, query, allow_tlist_ref);
1065  expr_pair_list.emplace_back(e1, e2);
1066  }
1067  auto else_e =
1068  else_expr_ ? else_expr_->analyze(catalog, query, allow_tlist_ref) : nullptr;
1069  return normalize(expr_pair_list, else_e);
1070 }
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 1637 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().

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

1761  {
1762  std::string str("CASE ");
1763  for (auto& p : when_then_list_) {
1764  str += "WHEN " + p->get_expr1()->to_string() + " THEN " +
1765  p->get_expr2()->to_string() + " ";
1766  }
1767  if (else_expr_ != nullptr) {
1768  str += "ELSE " + else_expr_->to_string();
1769  }
1770  str += " END";
1771  return str;
1772 }
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: