OmniSciDB  ba1bac9284
 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 668 of file ParserNode.h.

Constructor & Destructor Documentation

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

Definition at line 670 of file ParserNode.h.

References CHECK, and when_then_list.

670  : else_expr(e) {
671  CHECK(w);
672  for (const auto e : *w) {
673  when_then_list.emplace_back(e);
674  }
675  delete w;
676  }
std::list< std::unique_ptr< ExprPair > > when_then_list
Definition: ParserNode.h:688
#define CHECK(condition)
Definition: Logger.h:206
std::unique_ptr< Expr > else_expr
Definition: ParserNode.h:689

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

References kBOOLEAN.

895  {
896  SQLTypeInfo ti;
897  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
898  expr_pair_list;
899  for (auto& p : when_then_list) {
900  auto e1 = p->get_expr1()->analyze(catalog, query, allow_tlist_ref);
901  if (e1->get_type_info().get_type() != kBOOLEAN) {
902  throw std::runtime_error("Only boolean expressions can be used after WHEN.");
903  }
904  auto e2 = p->get_expr2()->analyze(catalog, query, allow_tlist_ref);
905  expr_pair_list.emplace_back(e1, e2);
906  }
907  auto else_e = else_expr ? else_expr->analyze(catalog, query, allow_tlist_ref) : nullptr;
908  return normalize(expr_pair_list, else_e);
909 }
std::list< std::unique_ptr< ExprPair > > when_then_list
Definition: ParserNode.h:688
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:934
std::unique_ptr< Expr > else_expr
Definition: ParserNode.h:689
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 934 of file ParserNode.cpp.

References CHECK, Analyzer::BinOper::common_numeric_type(), Analyzer::BinOper::common_string_type(), test_fsi::d, 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, and SQLTypeInfo::set_notnull().

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

937  {
938  SQLTypeInfo ti;
939  bool has_agg = false;
940  std::set<int> dictionary_ids;
941 
942  for (auto& p : expr_pair_list) {
943  auto e1 = p.first;
944  CHECK(e1->get_type_info().is_boolean());
945  auto e2 = p.second;
946  if (e2->get_type_info().is_dict_encoded_string()) {
947  dictionary_ids.insert(e2->get_type_info().get_comp_param());
948  }
949  if (ti.get_type() == kNULLT) {
950  ti = e2->get_type_info();
951  } else if (e2->get_type_info().get_type() == kNULLT) {
952  ti.set_notnull(false);
953  e2->set_type_info(ti);
954  } else if (ti != e2->get_type_info()) {
955  if (ti.is_string() && e2->get_type_info().is_string()) {
956  ti = Analyzer::BinOper::common_string_type(ti, e2->get_type_info());
957  } else if (ti.is_number() && e2->get_type_info().is_number()) {
958  ti = Analyzer::BinOper::common_numeric_type(ti, e2->get_type_info());
959  } else if (ti.is_boolean() && e2->get_type_info().is_boolean()) {
960  ti = Analyzer::BinOper::common_numeric_type(ti, e2->get_type_info());
961  } else {
962  throw std::runtime_error(
963  "expressions in THEN clause must be of the same or compatible types.");
964  }
965  }
966  if (e2->get_contains_agg()) {
967  has_agg = true;
968  }
969  }
970  auto else_e = else_e_in;
971  if (else_e) {
972  if (else_e->get_contains_agg()) {
973  has_agg = true;
974  }
975  if (expr_is_null(else_e.get())) {
976  ti.set_notnull(false);
977  else_e->set_type_info(ti);
978  } else if (ti != else_e->get_type_info()) {
979  if (else_e->get_type_info().is_dict_encoded_string()) {
980  dictionary_ids.insert(else_e->get_type_info().get_comp_param());
981  }
982  ti.set_notnull(false);
983  if (ti.is_string() && else_e->get_type_info().is_string()) {
984  ti = Analyzer::BinOper::common_string_type(ti, else_e->get_type_info());
985  } else if (ti.is_number() && else_e->get_type_info().is_number()) {
986  ti = Analyzer::BinOper::common_numeric_type(ti, else_e->get_type_info());
987  } else if (ti.is_boolean() && else_e->get_type_info().is_boolean()) {
988  ti = Analyzer::BinOper::common_numeric_type(ti, else_e->get_type_info());
989  } else if (get_logical_type_info(ti) !=
990  get_logical_type_info(else_e->get_type_info())) {
991  throw std::runtime_error(
992  // types differing by encoding will be resolved at decode
993 
994  "expressions in ELSE clause must be of the same or compatible types as those "
995  "in the THEN clauses.");
996  }
997  }
998  }
999  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
1000  cast_expr_pair_list;
1001  for (auto p : expr_pair_list) {
1002  ti.set_notnull(false);
1003  cast_expr_pair_list.emplace_back(p.first, p.second->add_cast(ti));
1004  }
1005  if (else_e != nullptr) {
1006  else_e = else_e->add_cast(ti);
1007  } else {
1008  Datum d;
1009  // always create an else expr so that executor doesn't need to worry about it
1010  ti.set_notnull(false);
1011  else_e = makeExpr<Analyzer::Constant>(ti, true, d);
1012  }
1013  if (ti.get_type() == kNULLT) {
1014  throw std::runtime_error(
1015  "Can't deduce the type for case expressions, all branches null");
1016  }
1017 
1018  auto case_expr = makeExpr<Analyzer::CaseExpr>(ti, has_agg, cast_expr_pair_list, else_e);
1019  if (ti.get_compression() != kENCODING_DICT && dictionary_ids.size() == 1 &&
1020  *(dictionary_ids.begin()) > 0) {
1021  // the above logic makes two assumptions when strings are present. 1) that all types
1022  // in the case statement are either null or strings, and 2) that none-encoded strings
1023  // will always win out over dict encoding. If we only have one dictionary, and that
1024  // dictionary is not a transient dictionary, we can cast the entire case to be dict
1025  // encoded and use transient dictionaries for any literals
1026  ti.set_compression(kENCODING_DICT);
1027  ti.set_comp_param(*dictionary_ids.begin());
1028  case_expr->add_cast(ti);
1029  }
1030  return case_expr;
1031 }
tuple d
Definition: test_fsi.py:9
SQLTypeInfo get_logical_type_info(const SQLTypeInfo &type_info)
Definition: sqltypes.h:912
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:314
bool is_number() const
Definition: sqltypes.h:494
static SQLTypeInfo common_string_type(const SQLTypeInfo &type1, const SQLTypeInfo &type2)
Definition: Analyzer.cpp:399
static SQLTypeInfo common_numeric_type(const SQLTypeInfo &type1, const SQLTypeInfo &type2)
Definition: Analyzer.cpp:439
bool is_boolean() const
Definition: sqltypes.h:496
bool expr_is_null(const Analyzer::Expr *expr)
Definition: ParserNode.cpp:913
void set_notnull(bool n)
Definition: sqltypes.h:411
#define CHECK(condition)
Definition: Logger.h:206
bool is_string() const
Definition: sqltypes.h:489

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

1033  {
1034  std::string str("CASE ");
1035  for (auto& p : when_then_list) {
1036  str += "WHEN " + p->get_expr1()->to_string() + " THEN " +
1037  p->get_expr2()->to_string() + " ";
1038  }
1039  if (else_expr != nullptr) {
1040  str += "ELSE " + else_expr->to_string();
1041  }
1042  str += " END";
1043  return str;
1044 }
std::list< std::unique_ptr< ExprPair > > when_then_list
Definition: ParserNode.h:688
std::unique_ptr< Expr > else_expr
Definition: ParserNode.h:689

Member Data Documentation

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

Definition at line 689 of file ParserNode.h.

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

Definition at line 688 of file ParserNode.h.

Referenced by CaseExpr().


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