OmniSciDB  085a039ca4
 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:223
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 1046 of file ParserNode.cpp.

References kBOOLEAN.

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

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

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

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

1735  {
1736  std::string str("CASE ");
1737  for (auto& p : when_then_list_) {
1738  str += "WHEN " + p->get_expr1()->to_string() + " THEN " +
1739  p->get_expr2()->to_string() + " ";
1740  }
1741  if (else_expr_ != nullptr) {
1742  str += "ELSE " + else_expr_->to_string();
1743  }
1744  str += " END";
1745  return str;
1746 }
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: