OmniSciDB  ba1bac9284
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Parser::RegexpExpr Class Reference

#include <ParserNode.h>

+ Inheritance diagram for Parser::RegexpExpr:
+ Collaboration diagram for Parser::RegexpExpr:

Public Member Functions

 RegexpExpr (bool n, Expr *a, Expr *p, Expr *e)
 
bool get_is_not () const
 
const Exprget_arg () const
 
const Exprget_pattern_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 > pattern_expr, std::shared_ptr< Analyzer::Expr > escape_expr, const bool is_not)
 

Static Private Member Functions

static void check_pattern_expr (const std::string &pattern_str, char escape_char)
 
static bool translate_to_like_pattern (std::string &pattern_str, char escape_char)
 

Private Attributes

bool is_not
 
std::unique_ptr< Exprarg
 
std::unique_ptr< Exprpattern_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 525 of file ParserNode.h.

Constructor & Destructor Documentation

Parser::RegexpExpr::RegexpExpr ( bool  n,
Expr a,
Expr p,
Expr e 
)
inline

Definition at line 527 of file ParserNode.h.

528  : is_not(n), arg(a), pattern_string(p), escape_string(e) {}
std::unique_ptr< Expr > arg
Definition: ParserNode.h:545
std::unique_ptr< Expr > pattern_string
Definition: ParserNode.h:546
std::unique_ptr< Expr > escape_string
Definition: ParserNode.h:547

Member Function Documentation

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

Implements Parser::Expr.

Definition at line 642 of file ParserNode.cpp.

645  {
646  auto arg_expr = arg->analyze(catalog, query, allow_tlist_ref);
647  auto pattern_expr = pattern_string->analyze(catalog, query, allow_tlist_ref);
648  auto escape_expr = escape_string == nullptr
649  ? nullptr
650  : escape_string->analyze(catalog, query, allow_tlist_ref);
651  return RegexpExpr::get(arg_expr, pattern_expr, escape_expr, is_not);
652 }
std::unique_ptr< Expr > arg
Definition: ParserNode.h:545
static std::shared_ptr< Analyzer::Expr > get(std::shared_ptr< Analyzer::Expr > arg_expr, std::shared_ptr< Analyzer::Expr > pattern_expr, std::shared_ptr< Analyzer::Expr > escape_expr, const bool is_not)
Definition: ParserNode.cpp:654
std::unique_ptr< Expr > pattern_string
Definition: ParserNode.h:546
std::unique_ptr< Expr > escape_string
Definition: ParserNode.h:547
void Parser::RegexpExpr::check_pattern_expr ( const std::string &  pattern_str,
char  escape_char 
)
staticprivate

Definition at line 605 of file ParserNode.cpp.

605  {
606  if (pattern_str.back() == escape_char) {
607  throw std::runtime_error("REGEXP pattern must not end with escape character.");
608  }
609 }
std::shared_ptr< Analyzer::Expr > Parser::RegexpExpr::get ( std::shared_ptr< Analyzer::Expr arg_expr,
std::shared_ptr< Analyzer::Expr pattern_expr,
std::shared_ptr< Analyzer::Expr escape_expr,
const bool  is_not 
)
static

Definition at line 654 of file ParserNode.cpp.

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

Referenced by RelAlgTranslator::translateRegexp().

658  {
659  if (!arg_expr->get_type_info().is_string()) {
660  throw std::runtime_error("expression before REGEXP must be of a string type.");
661  }
662  if (!pattern_expr->get_type_info().is_string()) {
663  throw std::runtime_error("expression after REGEXP must be of a string type.");
664  }
665  char escape_char = '\\';
666  if (escape_expr != nullptr) {
667  if (!escape_expr->get_type_info().is_string()) {
668  throw std::runtime_error("expression after ESCAPE must be of a string type.");
669  }
670  if (!escape_expr->get_type_info().is_string()) {
671  throw std::runtime_error("expression after ESCAPE must be of a string type.");
672  }
673  auto c = std::dynamic_pointer_cast<Analyzer::Constant>(escape_expr);
674  if (c != nullptr && c->get_constval().stringval->length() > 1) {
675  throw std::runtime_error("String after ESCAPE must have a single character.");
676  }
677  escape_char = (*c->get_constval().stringval)[0];
678  if (escape_char != '\\') {
679  throw std::runtime_error("Only supporting '\\' escape character.");
680  }
681  }
682  auto c = std::dynamic_pointer_cast<Analyzer::Constant>(pattern_expr);
683  if (c != nullptr) {
684  std::string& pattern = *c->get_constval().stringval;
685  if (translate_to_like_pattern(pattern, escape_char)) {
686  return LikeExpr::get(arg_expr, pattern_expr, escape_expr, false, is_not);
687  }
688  }
689  std::shared_ptr<Analyzer::Expr> result =
690  makeExpr<Analyzer::RegexpExpr>(arg_expr->decompress(), pattern_expr, escape_expr);
691  if (is_not) {
692  result = makeExpr<Analyzer::UOper>(kBOOLEAN, kNOT, result);
693  }
694  return result;
695 }
static bool translate_to_like_pattern(std::string &pattern_str, char escape_char)
Definition: ParserNode.cpp:611
std::string * stringval
Definition: sqltypes.h:214
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:559
Datum get_constval() const
Definition: Analyzer.h:335
Definition: sqldefs.h:39

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 530 of file ParserNode.h.

References arg.

530 { return arg.get(); }
std::unique_ptr< Expr > arg
Definition: ParserNode.h:545
const Expr* Parser::RegexpExpr::get_escape_string ( ) const
inline

Definition at line 532 of file ParserNode.h.

References escape_string.

532 { return escape_string.get(); }
std::unique_ptr< Expr > escape_string
Definition: ParserNode.h:547
bool Parser::RegexpExpr::get_is_not ( ) const
inline

Definition at line 529 of file ParserNode.h.

References is_not.

529 { return is_not; }
const Expr* Parser::RegexpExpr::get_pattern_string ( ) const
inline

Definition at line 531 of file ParserNode.h.

References pattern_string.

531 { return pattern_string.get(); }
std::unique_ptr< Expr > pattern_string
Definition: ParserNode.h:546
std::string Parser::RegexpExpr::to_string ( ) const
overridevirtual

Implements Parser::Expr.

Definition at line 1433 of file ParserNode.cpp.

1433  {
1434  std::string str = arg->to_string();
1435  if (is_not) {
1436  str += " NOT REGEXP ";
1437  } else {
1438  str += " REGEXP ";
1439  }
1440  str += pattern_string->to_string();
1441  if (escape_string != nullptr) {
1442  str += " ESCAPE " + escape_string->to_string();
1443  }
1444  return str;
1445 }
std::unique_ptr< Expr > arg
Definition: ParserNode.h:545
std::unique_ptr< Expr > pattern_string
Definition: ParserNode.h:546
std::unique_ptr< Expr > escape_string
Definition: ParserNode.h:547
bool Parser::RegexpExpr::translate_to_like_pattern ( std::string &  pattern_str,
char  escape_char 
)
staticprivate

Definition at line 611 of file ParserNode.cpp.

611  {
612  char prev_char = '\0';
613  char prev_prev_char = '\0';
614  std::string like_str;
615  for (char& cur_char : pattern_str) {
616  if (prev_char == escape_char || isalnum(cur_char) || cur_char == ' ' ||
617  cur_char == '.') {
618  like_str.push_back((cur_char == '.') ? '_' : cur_char);
619  prev_prev_char = prev_char;
620  prev_char = cur_char;
621  continue;
622  }
623  if (prev_char == '.' && prev_prev_char != escape_char) {
624  if (cur_char == '*' || cur_char == '+') {
625  if (cur_char == '*') {
626  like_str.pop_back();
627  }
628  // .* --> %
629  // .+ --> _%
630  like_str.push_back('%');
631  prev_prev_char = prev_char;
632  prev_char = cur_char;
633  continue;
634  }
635  }
636  return false;
637  }
638  pattern_str = like_str;
639  return true;
640 }

Member Data Documentation

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

Definition at line 545 of file ParserNode.h.

Referenced by get_arg().

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

Definition at line 547 of file ParserNode.h.

Referenced by get_escape_string().

bool Parser::RegexpExpr::is_not
private

Definition at line 544 of file ParserNode.h.

Referenced by get_is_not().

std::unique_ptr<Expr> Parser::RegexpExpr::pattern_string
private

Definition at line 546 of file ParserNode.h.

Referenced by get_pattern_string().


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