OmniSciDB  b24e664e58
 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 680 of file ParserNode.h.

Constructor & Destructor Documentation

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

Definition at line 682 of file ParserNode.h.

References CHECK(), and when_then_list.

682  : else_expr(e) {
683  CHECK(w);
684  for (const auto e : *w) {
685  when_then_list.emplace_back(e);
686  }
687  delete w;
688  }
std::list< std::unique_ptr< ExprPair > > when_then_list
Definition: ParserNode.h:700
CHECK(cgen_state)
std::unique_ptr< Expr > else_expr
Definition: ParserNode.h:701

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

References kBOOLEAN.

866  {
867  SQLTypeInfo ti;
868  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
869  expr_pair_list;
870  for (auto& p : when_then_list) {
871  auto e1 = p->get_expr1()->analyze(catalog, query, allow_tlist_ref);
872  if (e1->get_type_info().get_type() != kBOOLEAN) {
873  throw std::runtime_error("Only boolean expressions can be used after WHEN.");
874  }
875  auto e2 = p->get_expr2()->analyze(catalog, query, allow_tlist_ref);
876  expr_pair_list.emplace_back(e1, e2);
877  }
878  auto else_e = else_expr ? else_expr->analyze(catalog, query, allow_tlist_ref) : nullptr;
879  return normalize(expr_pair_list, else_e);
880 }
std::list< std::unique_ptr< ExprPair > > when_then_list
Definition: ParserNode.h:700
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:905
std::unique_ptr< Expr > else_expr
Definition: ParserNode.h:701
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 905 of file ParserNode.cpp.

References CHECK(), Analyzer::BinOper::common_numeric_type(), Analyzer::BinOper::common_string_type(), Parser::anonymous_namespace{ParserNode.cpp}::expr_is_null(), SQLTypeInfoCore< TYPE_FACET_PACK >::get_type(), SQLTypeInfoCore< TYPE_FACET_PACK >::is_boolean(), SQLTypeInfoCore< TYPE_FACET_PACK >::is_number(), SQLTypeInfoCore< TYPE_FACET_PACK >::is_string(), kNULLT, and SQLTypeInfoCore< TYPE_FACET_PACK >::set_notnull().

Referenced by RelAlgTranslator::translateCase(), and anonymous_namespace{CalciteAdapter.cpp}::CalciteAdapter::translateCase().

908  {
909  SQLTypeInfo ti;
910  bool has_agg = false;
911  for (auto& p : expr_pair_list) {
912  auto e1 = p.first;
913  CHECK(e1->get_type_info().is_boolean());
914  auto e2 = p.second;
915  if (ti.get_type() == kNULLT) {
916  ti = e2->get_type_info();
917  } else if (e2->get_type_info().get_type() == kNULLT) {
918  ti.set_notnull(false);
919  e2->set_type_info(ti);
920  } else if (ti != e2->get_type_info()) {
921  if (ti.is_string() && e2->get_type_info().is_string()) {
922  ti = Analyzer::BinOper::common_string_type(ti, e2->get_type_info());
923  } else if (ti.is_number() && e2->get_type_info().is_number()) {
924  ti = Analyzer::BinOper::common_numeric_type(ti, e2->get_type_info());
925  } else if (ti.is_boolean() && e2->get_type_info().is_boolean()) {
926  ti = Analyzer::BinOper::common_numeric_type(ti, e2->get_type_info());
927  } else {
928  throw std::runtime_error(
929  "expressions in THEN clause must be of the same or compatible types.");
930  }
931  }
932  if (e2->get_contains_agg()) {
933  has_agg = true;
934  }
935  }
936  auto else_e = else_e_in;
937  if (else_e) {
938  if (else_e->get_contains_agg()) {
939  has_agg = true;
940  }
941  if (expr_is_null(else_e.get())) {
942  ti.set_notnull(false);
943  else_e->set_type_info(ti);
944  } else if (ti != else_e->get_type_info()) {
945  ti.set_notnull(false);
946  if (ti.is_string() && else_e->get_type_info().is_string()) {
947  ti = Analyzer::BinOper::common_string_type(ti, else_e->get_type_info());
948  } else if (ti.is_number() && else_e->get_type_info().is_number()) {
949  ti = Analyzer::BinOper::common_numeric_type(ti, else_e->get_type_info());
950  } else if (ti.is_boolean() && else_e->get_type_info().is_boolean()) {
951  ti = Analyzer::BinOper::common_numeric_type(ti, else_e->get_type_info());
952  } else {
953  throw std::runtime_error(
954  "expressions in ELSE clause must be of the same or compatible types as those "
955  "in the THEN clauses.");
956  }
957  }
958  }
959  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
960  cast_expr_pair_list;
961  for (auto p : expr_pair_list) {
962  ti.set_notnull(false);
963  cast_expr_pair_list.emplace_back(p.first, p.second->add_cast(ti));
964  }
965  if (else_e != nullptr) {
966  else_e = else_e->add_cast(ti);
967  } else {
968  Datum d;
969  // always create an else expr so that executor doesn't need to worry about it
970  ti.set_notnull(false);
971  else_e = makeExpr<Analyzer::Constant>(ti, true, d);
972  }
973  if (ti.get_type() == kNULLT) {
974  throw std::runtime_error(
975  "Can't deduce the type for case expressions, all branches null");
976  }
977  return makeExpr<Analyzer::CaseExpr>(ti, has_agg, cast_expr_pair_list, else_e);
978 }
bool is_boolean() const
Definition: sqltypes.h:484
void set_notnull(bool n)
Definition: sqltypes.h:423
bool is_number() const
Definition: sqltypes.h:482
static SQLTypeInfo common_string_type(const SQLTypeInfo &type1, const SQLTypeInfo &type2)
Definition: Analyzer.cpp:387
CHECK(cgen_state)
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:326
static SQLTypeInfo common_numeric_type(const SQLTypeInfo &type1, const SQLTypeInfo &type2)
Definition: Analyzer.cpp:427
bool expr_is_null(const Analyzer::Expr *expr)
Definition: ParserNode.cpp:884
bool is_string() const
Definition: sqltypes.h:477

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

980  {
981  std::string str("CASE ");
982  for (auto& p : when_then_list) {
983  str += "WHEN " + p->get_expr1()->to_string() + " THEN " +
984  p->get_expr2()->to_string() + " ";
985  }
986  if (else_expr != nullptr) {
987  str += "ELSE " + else_expr->to_string();
988  }
989  str += " END";
990  return str;
991 }
std::list< std::unique_ptr< ExprPair > > when_then_list
Definition: ParserNode.h:700
std::unique_ptr< Expr > else_expr
Definition: ParserNode.h:701

Member Data Documentation

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

Definition at line 701 of file ParserNode.h.

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

Definition at line 700 of file ParserNode.h.

Referenced by CaseExpr().


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