OmniSciDB  1dac507f6e
 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 502 of file ParserNode.h.

Constructor & Destructor Documentation

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

Definition at line 504 of file ParserNode.h.

505  : is_not(n), is_ilike(i), arg(a), like_string(l), escape_string(e) {}
std::unique_ptr< Expr > escape_string
Definition: ParserNode.h:526
std::unique_ptr< Expr > like_string
Definition: ParserNode.h:525
std::unique_ptr< Expr > arg
Definition: ParserNode.h:524

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

521  {
522  auto arg_expr = arg->analyze(catalog, query, allow_tlist_ref);
523  auto like_expr = like_string->analyze(catalog, query, allow_tlist_ref);
524  auto escape_expr = escape_string == nullptr
525  ? nullptr
526  : escape_string->analyze(catalog, query, allow_tlist_ref);
527  return LikeExpr::get(arg_expr, like_expr, escape_expr, is_ilike, is_not);
528 }
std::unique_ptr< Expr > escape_string
Definition: ParserNode.h:526
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:530
std::unique_ptr< Expr > like_string
Definition: ParserNode.h:525
std::unique_ptr< Expr > arg
Definition: ParserNode.h:524
void Parser::LikeExpr::check_like_expr ( const std::string &  like_str,
char  escape_char 
)
staticprivate

Definition at line 472 of file ParserNode.cpp.

472  {
473  if (like_str.back() == escape_char) {
474  throw std::runtime_error("LIKE pattern must not end with escape character.");
475  }
476 }
void Parser::LikeExpr::erase_cntl_chars ( std::string &  like_str,
char  escape_char 
)
staticprivate

Definition at line 499 of file ParserNode.cpp.

499  {
500  char prev_char = '\0';
501  // easier to create new string of allowable chars
502  // rather than erase chars from
503  // existing string
504  std::string new_str;
505  for (char& cur_char : like_str) {
506  if (cur_char == '%' || cur_char == escape_char) {
507  if (prev_char != escape_char) {
508  prev_char = cur_char;
509  continue;
510  }
511  }
512  new_str.push_back(cur_char);
513  prev_char = cur_char;
514  }
515  like_str = new_str;
516 }
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 530 of file ParserNode.cpp.

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

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

534  {
535  if (!arg_expr->get_type_info().is_string()) {
536  throw std::runtime_error("expression before LIKE must be of a string type.");
537  }
538  if (!like_expr->get_type_info().is_string()) {
539  throw std::runtime_error("expression after LIKE must be of a string type.");
540  }
541  char escape_char = '\\';
542  if (escape_expr != nullptr) {
543  if (!escape_expr->get_type_info().is_string()) {
544  throw std::runtime_error("expression after ESCAPE must be of a string type.");
545  }
546  if (!escape_expr->get_type_info().is_string()) {
547  throw std::runtime_error("expression after ESCAPE must be of a string type.");
548  }
549  auto c = std::dynamic_pointer_cast<Analyzer::Constant>(escape_expr);
550  if (c != nullptr && c->get_constval().stringval->length() > 1) {
551  throw std::runtime_error("String after ESCAPE must have a single character.");
552  }
553  escape_char = (*c->get_constval().stringval)[0];
554  }
555  auto c = std::dynamic_pointer_cast<Analyzer::Constant>(like_expr);
556  bool is_simple = false;
557  if (c != nullptr) {
558  std::string& pattern = *c->get_constval().stringval;
559  if (is_ilike) {
560  std::transform(pattern.begin(), pattern.end(), pattern.begin(), ::tolower);
561  }
562  check_like_expr(pattern, escape_char);
563  is_simple = test_is_simple_expr(pattern, escape_char);
564  if (is_simple) {
565  erase_cntl_chars(pattern, escape_char);
566  }
567  }
568  std::shared_ptr<Analyzer::Expr> result = makeExpr<Analyzer::LikeExpr>(
569  arg_expr->decompress(), like_expr, escape_expr, is_ilike, is_simple);
570  if (is_not) {
571  result = makeExpr<Analyzer::UOper>(kBOOLEAN, kNOT, result);
572  }
573  return result;
574 }
static void erase_cntl_chars(std::string &like_str, char escape_char)
Definition: ParserNode.cpp:499
std::string * stringval
Definition: sqltypes.h:134
Datum get_constval() const
Definition: Analyzer.h:329
static bool test_is_simple_expr(const std::string &like_str, char escape_char)
Definition: ParserNode.cpp:478
Definition: sqldefs.h:39
static void check_like_expr(const std::string &like_str, char escape_char)
Definition: ParserNode.cpp:472

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

References arg.

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

Definition at line 509 of file ParserNode.h.

References escape_string.

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

Definition at line 506 of file ParserNode.h.

References is_not.

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

Definition at line 508 of file ParserNode.h.

References like_string.

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

Definition at line 478 of file ParserNode.cpp.

478  {
479  // if not bounded by '%' then not a simple string
480  if (like_str.size() < 2 || like_str[0] != '%' || like_str[like_str.size() - 1] != '%') {
481  return false;
482  }
483  // if the last '%' is escaped then not a simple string
484  if (like_str[like_str.size() - 2] == escape_char &&
485  like_str[like_str.size() - 3] != escape_char) {
486  return false;
487  }
488  for (size_t i = 1; i < like_str.size() - 1; i++) {
489  if (like_str[i] == '%' || like_str[i] == '_' || like_str[i] == '[' ||
490  like_str[i] == ']') {
491  if (like_str[i - 1] != escape_char) {
492  return false;
493  }
494  }
495  }
496  return true;
497 }
std::string Parser::LikeExpr::to_string ( ) const
overridevirtual

Implements Parser::Expr.

Definition at line 1442 of file ParserNode.cpp.

1442  {
1443  std::string str = arg->to_string();
1444  if (is_not) {
1445  str += " NOT LIKE ";
1446  } else {
1447  str += " LIKE ";
1448  }
1449  str += like_string->to_string();
1450  if (escape_string != nullptr) {
1451  str += " ESCAPE " + escape_string->to_string();
1452  }
1453  return str;
1454 }
std::unique_ptr< Expr > escape_string
Definition: ParserNode.h:526
std::unique_ptr< Expr > like_string
Definition: ParserNode.h:525
std::unique_ptr< Expr > arg
Definition: ParserNode.h:524

Member Data Documentation

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

Definition at line 524 of file ParserNode.h.

Referenced by get_arg().

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

Definition at line 526 of file ParserNode.h.

Referenced by get_escape_string().

bool Parser::LikeExpr::is_ilike
private

Definition at line 523 of file ParserNode.h.

bool Parser::LikeExpr::is_not
private

Definition at line 522 of file ParserNode.h.

Referenced by get_is_not().

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

Definition at line 525 of file ParserNode.h.

Referenced by get_like_string().


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