OmniSciDB  dfae7c3b14
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::Exprget (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 521 of file ParserNode.h.

Constructor & Destructor Documentation

◆ RegexpExpr()

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

Definition at line 523 of file ParserNode.h.

524  : is_not(n), arg(a), pattern_string(p), escape_string(e) {}
std::unique_ptr< Expr > arg
Definition: ParserNode.h:541
std::unique_ptr< Expr > pattern_string
Definition: ParserNode.h:542
std::unique_ptr< Expr > escape_string
Definition: ParserNode.h:543

Member Function Documentation

◆ analyze()

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

631  {
632  auto arg_expr = arg->analyze(catalog, query, allow_tlist_ref);
633  auto pattern_expr = pattern_string->analyze(catalog, query, allow_tlist_ref);
634  auto escape_expr = escape_string == nullptr
635  ? nullptr
636  : escape_string->analyze(catalog, query, allow_tlist_ref);
637  return RegexpExpr::get(arg_expr, pattern_expr, escape_expr, is_not);
638 }
std::unique_ptr< Expr > arg
Definition: ParserNode.h:541
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:640
std::unique_ptr< Expr > pattern_string
Definition: ParserNode.h:542
std::unique_ptr< Expr > escape_string
Definition: ParserNode.h:543

◆ check_pattern_expr()

void Parser::RegexpExpr::check_pattern_expr ( const std::string &  pattern_str,
char  escape_char 
)
staticprivate

Definition at line 591 of file ParserNode.cpp.

591  {
592  if (pattern_str.back() == escape_char) {
593  throw std::runtime_error("REGEXP pattern must not end with escape character.");
594  }
595 }

◆ get()

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

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

Referenced by RelAlgTranslator::translateRegexp().

644  {
645  if (!arg_expr->get_type_info().is_string()) {
646  throw std::runtime_error("expression before REGEXP must be of a string type.");
647  }
648  if (!pattern_expr->get_type_info().is_string()) {
649  throw std::runtime_error("expression after REGEXP must be of a string type.");
650  }
651  char escape_char = '\\';
652  if (escape_expr != nullptr) {
653  if (!escape_expr->get_type_info().is_string()) {
654  throw std::runtime_error("expression after ESCAPE must be of a string type.");
655  }
656  if (!escape_expr->get_type_info().is_string()) {
657  throw std::runtime_error("expression after ESCAPE must be of a string type.");
658  }
659  auto c = std::dynamic_pointer_cast<Analyzer::Constant>(escape_expr);
660  if (c != nullptr && c->get_constval().stringval->length() > 1) {
661  throw std::runtime_error("String after ESCAPE must have a single character.");
662  }
663  escape_char = (*c->get_constval().stringval)[0];
664  if (escape_char != '\\') {
665  throw std::runtime_error("Only supporting '\\' escape character.");
666  }
667  }
668  auto c = std::dynamic_pointer_cast<Analyzer::Constant>(pattern_expr);
669  if (c != nullptr) {
670  std::string& pattern = *c->get_constval().stringval;
671  if (translate_to_like_pattern(pattern, escape_char)) {
672  return LikeExpr::get(arg_expr, pattern_expr, escape_expr, false, is_not);
673  }
674  }
675  std::shared_ptr<Analyzer::Expr> result =
676  makeExpr<Analyzer::RegexpExpr>(arg_expr->decompress(), pattern_expr, escape_expr);
677  if (is_not) {
678  result = makeExpr<Analyzer::UOper>(kBOOLEAN, kNOT, result);
679  }
680  return result;
681 }
static bool translate_to_like_pattern(std::string &pattern_str, char escape_char)
Definition: ParserNode.cpp:597
Datum get_constval() const
Definition: Analyzer.h:335
std::string * stringval
Definition: sqltypes.h:143
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
Definition: sqldefs.h:39
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get_arg()

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

Definition at line 526 of file ParserNode.h.

526 { return arg.get(); }
std::unique_ptr< Expr > arg
Definition: ParserNode.h:541

◆ get_escape_string()

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

Definition at line 528 of file ParserNode.h.

References to_string().

528 { return escape_string.get(); }
std::unique_ptr< Expr > escape_string
Definition: ParserNode.h:543
+ Here is the call graph for this function:

◆ get_is_not()

bool Parser::RegexpExpr::get_is_not ( ) const
inline

Definition at line 525 of file ParserNode.h.

525 { return is_not; }

◆ get_pattern_string()

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

Definition at line 527 of file ParserNode.h.

527 { return pattern_string.get(); }
std::unique_ptr< Expr > pattern_string
Definition: ParserNode.h:542

◆ to_string()

std::string Parser::RegexpExpr::to_string ( ) const
overridevirtual

Implements Parser::Expr.

Definition at line 1398 of file ParserNode.cpp.

1398  {
1399  std::string str = arg->to_string();
1400  if (is_not) {
1401  str += " NOT REGEXP ";
1402  } else {
1403  str += " REGEXP ";
1404  }
1405  str += pattern_string->to_string();
1406  if (escape_string != nullptr) {
1407  str += " ESCAPE " + escape_string->to_string();
1408  }
1409  return str;
1410 }
std::unique_ptr< Expr > arg
Definition: ParserNode.h:541
std::unique_ptr< Expr > pattern_string
Definition: ParserNode.h:542
std::unique_ptr< Expr > escape_string
Definition: ParserNode.h:543

◆ translate_to_like_pattern()

bool Parser::RegexpExpr::translate_to_like_pattern ( std::string &  pattern_str,
char  escape_char 
)
staticprivate

Definition at line 597 of file ParserNode.cpp.

597  {
598  char prev_char = '\0';
599  char prev_prev_char = '\0';
600  std::string like_str;
601  for (char& cur_char : pattern_str) {
602  if (prev_char == escape_char || isalnum(cur_char) || cur_char == ' ' ||
603  cur_char == '.') {
604  like_str.push_back((cur_char == '.') ? '_' : cur_char);
605  prev_prev_char = prev_char;
606  prev_char = cur_char;
607  continue;
608  }
609  if (prev_char == '.' && prev_prev_char != escape_char) {
610  if (cur_char == '*' || cur_char == '+') {
611  if (cur_char == '*') {
612  like_str.pop_back();
613  }
614  // .* --> %
615  // .+ --> _%
616  like_str.push_back('%');
617  prev_prev_char = prev_char;
618  prev_char = cur_char;
619  continue;
620  }
621  }
622  return false;
623  }
624  pattern_str = like_str;
625  return true;
626 }

Member Data Documentation

◆ arg

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

Definition at line 541 of file ParserNode.h.

◆ escape_string

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

Definition at line 543 of file ParserNode.h.

◆ is_not

bool Parser::RegexpExpr::is_not
private

Definition at line 540 of file ParserNode.h.

◆ pattern_string

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

Definition at line 542 of file ParserNode.h.


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