OmniSciDB  4201147b46
 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:222
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 1618 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().

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

1741  {
1742  std::string str("CASE ");
1743  for (auto& p : when_then_list_) {
1744  str += "WHEN " + p->get_expr1()->to_string() + " THEN " +
1745  p->get_expr2()->to_string() + " ";
1746  }
1747  if (else_expr_ != nullptr) {
1748  str += "ELSE " + else_expr_->to_string();
1749  }
1750  str += " END";
1751  return str;
1752 }
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: