OmniSciDB  467d548b97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Parser::LikeExpr Class Reference

#include <ParserNode.h>

+ Inheritance diagram for Parser::LikeExpr:
+ Collaboration diagram for Parser::LikeExpr:

Public Member Functions

 LikeExpr (bool n, bool i, Expr *a, Expr *l, Expr *e)
 
bool get_is_not () const
 
const Exprget_arg () const
 
const Exprget_like_string () const
 
const Exprget_escape_string () const
 
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
get (std::shared_ptr< Analyzer::Expr > arg_expr, std::shared_ptr< Analyzer::Expr > like_expr, std::shared_ptr< Analyzer::Expr > escape_expr, const bool is_ilike, const bool is_not)
 

Static Private Member Functions

static void check_like_expr (const std::string &like_str, char escape_char)
 
static bool test_is_simple_expr (const std::string &like_str, char escape_char)
 
static void erase_cntl_chars (std::string &like_str, char escape_char)
 

Private Attributes

bool is_not
 
bool is_ilike
 
std::unique_ptr< Exprarg
 
std::unique_ptr< Exprlike_string
 
std::unique_ptr< Exprescape_string
 

Additional Inherited Members

- Public Types inherited from Parser::Expr
enum  TlistRefType { TLIST_NONE, TLIST_REF, TLIST_COPY }
 

Detailed Description

Definition at line 486 of file ParserNode.h.

Constructor & Destructor Documentation

Parser::LikeExpr::LikeExpr ( bool  n,
bool  i,
Expr a,
Expr l,
Expr e 
)
inline

Definition at line 488 of file ParserNode.h.

489  : is_not(n), is_ilike(i), arg(a), like_string(l), escape_string(e) {}
std::unique_ptr< Expr > escape_string
Definition: ParserNode.h:510
std::unique_ptr< Expr > like_string
Definition: ParserNode.h:509
std::unique_ptr< Expr > arg
Definition: ParserNode.h:508

Member Function Documentation

std::shared_ptr< Analyzer::Expr > Parser::LikeExpr::analyze ( const Catalog_Namespace::Catalog catalog,
Analyzer::Query query,
TlistRefType  allow_tlist_ref = TLIST_NONE 
) const
overridevirtual

Implements Parser::Expr.

Definition at line 533 of file ParserNode.cpp.

536  {
537  auto arg_expr = arg->analyze(catalog, query, allow_tlist_ref);
538  auto like_expr = like_string->analyze(catalog, query, allow_tlist_ref);
539  auto escape_expr = escape_string == nullptr
540  ? nullptr
541  : escape_string->analyze(catalog, query, allow_tlist_ref);
542  return LikeExpr::get(arg_expr, like_expr, escape_expr, is_ilike, is_not);
543 }
std::unique_ptr< Expr > escape_string
Definition: ParserNode.h:510
static std::shared_ptr< Analyzer::Expr > get(std::shared_ptr< Analyzer::Expr > arg_expr, std::shared_ptr< Analyzer::Expr > like_expr, std::shared_ptr< Analyzer::Expr > escape_expr, const bool is_ilike, const bool is_not)
Definition: ParserNode.cpp:545
std::unique_ptr< Expr > like_string
Definition: ParserNode.h:509
std::unique_ptr< Expr > arg
Definition: ParserNode.h:508
void Parser::LikeExpr::check_like_expr ( const std::string &  like_str,
char  escape_char 
)
staticprivate

Definition at line 487 of file ParserNode.cpp.

487  {
488  if (like_str.back() == escape_char) {
489  throw std::runtime_error("LIKE pattern must not end with escape character.");
490  }
491 }
void Parser::LikeExpr::erase_cntl_chars ( std::string &  like_str,
char  escape_char 
)
staticprivate

Definition at line 514 of file ParserNode.cpp.

514  {
515  char prev_char = '\0';
516  // easier to create new string of allowable chars
517  // rather than erase chars from
518  // existing string
519  std::string new_str;
520  for (char& cur_char : like_str) {
521  if (cur_char == '%' || cur_char == escape_char) {
522  if (prev_char != escape_char) {
523  prev_char = cur_char;
524  continue;
525  }
526  }
527  new_str.push_back(cur_char);
528  prev_char = cur_char;
529  }
530  like_str = new_str;
531 }
std::shared_ptr< Analyzer::Expr > Parser::LikeExpr::get ( std::shared_ptr< Analyzer::Expr arg_expr,
std::shared_ptr< Analyzer::Expr like_expr,
std::shared_ptr< Analyzer::Expr escape_expr,
const bool  is_ilike,
const bool  is_not 
)
static

Definition at line 545 of file ParserNode.cpp.

References Analyzer::Constant::get_constval(), kBOOLEAN, kNOT, run_benchmark_import::result, and Datum::stringval.

Referenced by RelAlgTranslator::translateLike().

549  {
550  if (!arg_expr->get_type_info().is_string()) {
551  throw std::runtime_error("expression before LIKE must be of a string type.");
552  }
553  if (!like_expr->get_type_info().is_string()) {
554  throw std::runtime_error("expression after LIKE must be of a string type.");
555  }
556  char escape_char = '\\';
557  if (escape_expr != nullptr) {
558  if (!escape_expr->get_type_info().is_string()) {
559  throw std::runtime_error("expression after ESCAPE must be of a string type.");
560  }
561  if (!escape_expr->get_type_info().is_string()) {
562  throw std::runtime_error("expression after ESCAPE must be of a string type.");
563  }
564  auto c = std::dynamic_pointer_cast<Analyzer::Constant>(escape_expr);
565  if (c != nullptr && c->get_constval().stringval->length() > 1) {
566  throw std::runtime_error("String after ESCAPE must have a single character.");
567  }
568  escape_char = (*c->get_constval().stringval)[0];
569  }
570  auto c = std::dynamic_pointer_cast<Analyzer::Constant>(like_expr);
571  bool is_simple = false;
572  if (c != nullptr) {
573  std::string& pattern = *c->get_constval().stringval;
574  if (is_ilike) {
575  std::transform(pattern.begin(), pattern.end(), pattern.begin(), ::tolower);
576  }
577  check_like_expr(pattern, escape_char);
578  is_simple = test_is_simple_expr(pattern, escape_char);
579  if (is_simple) {
580  erase_cntl_chars(pattern, escape_char);
581  }
582  }
583  std::shared_ptr<Analyzer::Expr> result = makeExpr<Analyzer::LikeExpr>(
584  arg_expr->decompress(), like_expr, escape_expr, is_ilike, is_simple);
585  if (is_not) {
586  result = makeExpr<Analyzer::UOper>(kBOOLEAN, kNOT, result);
587  }
588  return result;
589 }
static void erase_cntl_chars(std::string &like_str, char escape_char)
Definition: ParserNode.cpp:514
std::string * stringval
Definition: sqltypes.h:143
Datum get_constval() const
Definition: Analyzer.h:335
static bool test_is_simple_expr(const std::string &like_str, char escape_char)
Definition: ParserNode.cpp:493
Definition: sqldefs.h:39
static void check_like_expr(const std::string &like_str, char escape_char)
Definition: ParserNode.cpp:487

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

const Expr* Parser::LikeExpr::get_arg ( ) const
inline

Definition at line 491 of file ParserNode.h.

References arg.

491 { return arg.get(); }
std::unique_ptr< Expr > arg
Definition: ParserNode.h:508
const Expr* Parser::LikeExpr::get_escape_string ( ) const
inline

Definition at line 493 of file ParserNode.h.

References escape_string.

493 { return escape_string.get(); }
std::unique_ptr< Expr > escape_string
Definition: ParserNode.h:510
bool Parser::LikeExpr::get_is_not ( ) const
inline

Definition at line 490 of file ParserNode.h.

References is_not.

490 { return is_not; }
const Expr* Parser::LikeExpr::get_like_string ( ) const
inline

Definition at line 492 of file ParserNode.h.

References like_string.

492 { return like_string.get(); }
std::unique_ptr< Expr > like_string
Definition: ParserNode.h:509
bool Parser::LikeExpr::test_is_simple_expr ( const std::string &  like_str,
char  escape_char 
)
staticprivate

Definition at line 493 of file ParserNode.cpp.

493  {
494  // if not bounded by '%' then not a simple string
495  if (like_str.size() < 2 || like_str[0] != '%' || like_str[like_str.size() - 1] != '%') {
496  return false;
497  }
498  // if the last '%' is escaped then not a simple string
499  if (like_str[like_str.size() - 2] == escape_char &&
500  like_str[like_str.size() - 3] != escape_char) {
501  return false;
502  }
503  for (size_t i = 1; i < like_str.size() - 1; i++) {
504  if (like_str[i] == '%' || like_str[i] == '_' || like_str[i] == '[' ||
505  like_str[i] == ']') {
506  if (like_str[i - 1] != escape_char) {
507  return false;
508  }
509  }
510  }
511  return true;
512 }
std::string Parser::LikeExpr::to_string ( ) const
overridevirtual

Implements Parser::Expr.

Definition at line 1384 of file ParserNode.cpp.

1384  {
1385  std::string str = arg->to_string();
1386  if (is_not) {
1387  str += " NOT LIKE ";
1388  } else {
1389  str += " LIKE ";
1390  }
1391  str += like_string->to_string();
1392  if (escape_string != nullptr) {
1393  str += " ESCAPE " + escape_string->to_string();
1394  }
1395  return str;
1396 }
std::unique_ptr< Expr > escape_string
Definition: ParserNode.h:510
std::unique_ptr< Expr > like_string
Definition: ParserNode.h:509
std::unique_ptr< Expr > arg
Definition: ParserNode.h:508

Member Data Documentation

std::unique_ptr<Expr> Parser::LikeExpr::arg
private

Definition at line 508 of file ParserNode.h.

Referenced by get_arg().

std::unique_ptr<Expr> Parser::LikeExpr::escape_string
private

Definition at line 510 of file ParserNode.h.

Referenced by get_escape_string().

bool Parser::LikeExpr::is_ilike
private

Definition at line 507 of file ParserNode.h.

bool Parser::LikeExpr::is_not
private

Definition at line 506 of file ParserNode.h.

Referenced by get_is_not().

std::unique_ptr<Expr> Parser::LikeExpr::like_string
private

Definition at line 509 of file ParserNode.h.

Referenced by get_like_string().


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