OmniSciDB  06b3bd477c
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 >)
 

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 664 of file ParserNode.h.

Constructor & Destructor Documentation

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

Definition at line 666 of file ParserNode.h.

References CHECK(), and when_then_list.

666  : else_expr(e) {
667  CHECK(w);
668  for (const auto e : *w) {
669  when_then_list.emplace_back(e);
670  }
671  delete w;
672  }
std::list< std::unique_ptr< ExprPair > > when_then_list
Definition: ParserNode.h:684
CHECK(cgen_state)
std::unique_ptr< Expr > else_expr
Definition: ParserNode.h:685

+ Here is the call graph for this function:

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

References kBOOLEAN.

883  {
884  SQLTypeInfo ti;
885  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
886  expr_pair_list;
887  for (auto& p : when_then_list) {
888  auto e1 = p->get_expr1()->analyze(catalog, query, allow_tlist_ref);
889  if (e1->get_type_info().get_type() != kBOOLEAN) {
890  throw std::runtime_error("Only boolean expressions can be used after WHEN.");
891  }
892  auto e2 = p->get_expr2()->analyze(catalog, query, allow_tlist_ref);
893  expr_pair_list.emplace_back(e1, e2);
894  }
895  auto else_e = else_expr ? else_expr->analyze(catalog, query, allow_tlist_ref) : nullptr;
896  return normalize(expr_pair_list, else_e);
897 }
std::list< std::unique_ptr< ExprPair > > when_then_list
Definition: ParserNode.h:684
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 >)
Definition: ParserNode.cpp:922
std::unique_ptr< Expr > else_expr
Definition: ParserNode.h:685
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 
)
static

Definition at line 922 of file ParserNode.cpp.

References CHECK(), Analyzer::BinOper::common_numeric_type(), Analyzer::BinOper::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().

925  {
926  SQLTypeInfo ti;
927  bool has_agg = false;
928  for (auto& p : expr_pair_list) {
929  auto e1 = p.first;
930  CHECK(e1->get_type_info().is_boolean());
931  auto e2 = p.second;
932  if (ti.get_type() == kNULLT) {
933  ti = e2->get_type_info();
934  } else if (e2->get_type_info().get_type() == kNULLT) {
935  ti.set_notnull(false);
936  e2->set_type_info(ti);
937  } else if (ti != e2->get_type_info()) {
938  if (ti.is_string() && e2->get_type_info().is_string()) {
939  ti = Analyzer::BinOper::common_string_type(ti, e2->get_type_info());
940  } else if (ti.is_number() && e2->get_type_info().is_number()) {
941  ti = Analyzer::BinOper::common_numeric_type(ti, e2->get_type_info());
942  } else if (ti.is_boolean() && e2->get_type_info().is_boolean()) {
943  ti = Analyzer::BinOper::common_numeric_type(ti, e2->get_type_info());
944  } else {
945  throw std::runtime_error(
946  "expressions in THEN clause must be of the same or compatible types.");
947  }
948  }
949  if (e2->get_contains_agg()) {
950  has_agg = true;
951  }
952  }
953  auto else_e = else_e_in;
954  if (else_e) {
955  if (else_e->get_contains_agg()) {
956  has_agg = true;
957  }
958  if (expr_is_null(else_e.get())) {
959  ti.set_notnull(false);
960  else_e->set_type_info(ti);
961  } else if (ti != else_e->get_type_info()) {
962  ti.set_notnull(false);
963  if (ti.is_string() && else_e->get_type_info().is_string()) {
964  ti = Analyzer::BinOper::common_string_type(ti, else_e->get_type_info());
965  } else if (ti.is_number() && else_e->get_type_info().is_number()) {
966  ti = Analyzer::BinOper::common_numeric_type(ti, else_e->get_type_info());
967  } else if (ti.is_boolean() && else_e->get_type_info().is_boolean()) {
968  ti = Analyzer::BinOper::common_numeric_type(ti, else_e->get_type_info());
969  } else if (get_logical_type_info(ti) !=
970  get_logical_type_info(else_e->get_type_info())) {
971  throw std::runtime_error(
972  // types differing by encoding will be resolved at decode
973 
974  "expressions in ELSE clause must be of the same or compatible types as those "
975  "in the THEN clauses.");
976  }
977  }
978  }
979  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
980  cast_expr_pair_list;
981  for (auto p : expr_pair_list) {
982  ti.set_notnull(false);
983  cast_expr_pair_list.emplace_back(p.first, p.second->add_cast(ti));
984  }
985  if (else_e != nullptr) {
986  else_e = else_e->add_cast(ti);
987  } else {
988  Datum d;
989  // always create an else expr so that executor doesn't need to worry about it
990  ti.set_notnull(false);
991  else_e = makeExpr<Analyzer::Constant>(ti, true, d);
992  }
993  if (ti.get_type() == kNULLT) {
994  throw std::runtime_error(
995  "Can't deduce the type for case expressions, all branches null");
996  }
997  return makeExpr<Analyzer::CaseExpr>(ti, has_agg, cast_expr_pair_list, else_e);
998 }
SQLTypeInfo get_logical_type_info(const SQLTypeInfo &type_info)
Definition: sqltypes.h:818
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:258
bool is_number() const
Definition: sqltypes.h:420
static SQLTypeInfo common_string_type(const SQLTypeInfo &type1, const SQLTypeInfo &type2)
Definition: Analyzer.cpp:398
CHECK(cgen_state)
static SQLTypeInfo common_numeric_type(const SQLTypeInfo &type1, const SQLTypeInfo &type2)
Definition: Analyzer.cpp:438
bool is_boolean() const
Definition: sqltypes.h:422
bool expr_is_null(const Analyzer::Expr *expr)
Definition: ParserNode.cpp:901
void set_notnull(bool n)
Definition: sqltypes.h:355
bool is_string() const
Definition: sqltypes.h:415

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

1000  {
1001  std::string str("CASE ");
1002  for (auto& p : when_then_list) {
1003  str += "WHEN " + p->get_expr1()->to_string() + " THEN " +
1004  p->get_expr2()->to_string() + " ";
1005  }
1006  if (else_expr != nullptr) {
1007  str += "ELSE " + else_expr->to_string();
1008  }
1009  str += " END";
1010  return str;
1011 }
std::list< std::unique_ptr< ExprPair > > when_then_list
Definition: ParserNode.h:684
std::unique_ptr< Expr > else_expr
Definition: ParserNode.h:685

Member Data Documentation

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

Definition at line 685 of file ParserNode.h.

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

Definition at line 684 of file ParserNode.h.

Referenced by CaseExpr().


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