OmniSciDB  c1a53651b2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 526 of file ParserNode.h.

Constructor & Destructor Documentation

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

Definition at line 528 of file ParserNode.h.

529  : is_not_(n), arg_(a), pattern_string_(p), escape_string_(e) {}
constexpr double a
Definition: Utm.h:32
std::unique_ptr< Expr > pattern_string_
Definition: ParserNode.h:547
std::unique_ptr< Expr > arg_
Definition: ParserNode.h:546
std::unique_ptr< Expr > escape_string_
Definition: ParserNode.h:548
constexpr double n
Definition: Utm.h:38

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

781  {
782  auto arg_expr = arg_->analyze(catalog, query, allow_tlist_ref);
783  auto pattern_expr = pattern_string_->analyze(catalog, query, allow_tlist_ref);
784  auto escape_expr = escape_string_ == nullptr
785  ? nullptr
786  : escape_string_->analyze(catalog, query, allow_tlist_ref);
787  return RegexpExpr::get(arg_expr, pattern_expr, escape_expr, is_not_);
788 }
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:790
std::unique_ptr< Expr > pattern_string_
Definition: ParserNode.h:547
std::unique_ptr< Expr > arg_
Definition: ParserNode.h:546
std::unique_ptr< Expr > escape_string_
Definition: ParserNode.h:548
void Parser::RegexpExpr::check_pattern_expr ( const std::string &  pattern_str,
char  escape_char 
)
staticprivate

Definition at line 741 of file ParserNode.cpp.

741  {
742  if (pattern_str.back() == escape_char) {
743  throw std::runtime_error("REGEXP pattern must not end with escape character.");
744  }
745 }
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 790 of file ParserNode.cpp.

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

Referenced by RelAlgTranslator::translateRegexp().

794  {
795  if (!arg_expr->get_type_info().is_string()) {
796  throw std::runtime_error("expression before REGEXP must be of a string type.");
797  }
798  if (!pattern_expr->get_type_info().is_string()) {
799  throw std::runtime_error("expression after REGEXP must be of a string type.");
800  }
801  char escape_char = '\\';
802  if (escape_expr != nullptr) {
803  if (!escape_expr->get_type_info().is_string()) {
804  throw std::runtime_error("expression after ESCAPE must be of a string type.");
805  }
806  if (!escape_expr->get_type_info().is_string()) {
807  throw std::runtime_error("expression after ESCAPE must be of a string type.");
808  }
809  auto c = std::dynamic_pointer_cast<Analyzer::Constant>(escape_expr);
810  if (c != nullptr && c->get_constval().stringval->length() > 1) {
811  throw std::runtime_error("String after ESCAPE must have a single character.");
812  }
813  escape_char = (*c->get_constval().stringval)[0];
814  if (escape_char != '\\') {
815  throw std::runtime_error("Only supporting '\\' escape character.");
816  }
817  }
818  auto c = std::dynamic_pointer_cast<Analyzer::Constant>(pattern_expr);
819  if (c != nullptr) {
820  std::string& pattern = *c->get_constval().stringval;
821  if (translate_to_like_pattern(pattern, escape_char)) {
822  return LikeExpr::get(arg_expr, pattern_expr, escape_expr, false, is_not);
823  }
824  }
825  std::shared_ptr<Analyzer::Expr> result =
826  makeExpr<Analyzer::RegexpExpr>(arg_expr->decompress(), pattern_expr, escape_expr);
827  if (is_not) {
828  result = makeExpr<Analyzer::UOper>(kBOOLEAN, kNOT, result);
829  }
830  return result;
831 }
static bool translate_to_like_pattern(std::string &pattern_str, char escape_char)
Definition: ParserNode.cpp:747
std::string * stringval
Definition: Datum.h:77
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:695
Datum get_constval() const
Definition: Analyzer.h:348
Definition: sqldefs.h:38

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

References arg_.

531 { return arg_.get(); }
std::unique_ptr< Expr > arg_
Definition: ParserNode.h:546
const Expr* Parser::RegexpExpr::get_escape_string ( ) const
inline

Definition at line 533 of file ParserNode.h.

References escape_string_.

533 { return escape_string_.get(); }
std::unique_ptr< Expr > escape_string_
Definition: ParserNode.h:548
bool Parser::RegexpExpr::get_is_not ( ) const
inline

Definition at line 530 of file ParserNode.h.

References is_not_.

530 { return is_not_; }
const Expr* Parser::RegexpExpr::get_pattern_string ( ) const
inline

Definition at line 532 of file ParserNode.h.

References pattern_string_.

532 { return pattern_string_.get(); }
std::unique_ptr< Expr > pattern_string_
Definition: ParserNode.h:547
std::string Parser::RegexpExpr::to_string ( ) const
overridevirtual

Implements Parser::Expr.

Definition at line 2236 of file ParserNode.cpp.

2236  {
2237  std::string str = arg_->to_string();
2238  if (is_not_) {
2239  str += " NOT REGEXP ";
2240  } else {
2241  str += " REGEXP ";
2242  }
2243  str += pattern_string_->to_string();
2244  if (escape_string_ != nullptr) {
2245  str += " ESCAPE " + escape_string_->to_string();
2246  }
2247  return str;
2248 }
std::unique_ptr< Expr > pattern_string_
Definition: ParserNode.h:547
std::unique_ptr< Expr > arg_
Definition: ParserNode.h:546
std::unique_ptr< Expr > escape_string_
Definition: ParserNode.h:548
bool Parser::RegexpExpr::translate_to_like_pattern ( std::string &  pattern_str,
char  escape_char 
)
staticprivate

Definition at line 747 of file ParserNode.cpp.

747  {
748  char prev_char = '\0';
749  char prev_prev_char = '\0';
750  std::string like_str;
751  for (char& cur_char : pattern_str) {
752  if (prev_char == escape_char || isalnum(cur_char) || cur_char == ' ' ||
753  cur_char == '.') {
754  like_str.push_back((cur_char == '.') ? '_' : cur_char);
755  prev_prev_char = prev_char;
756  prev_char = cur_char;
757  continue;
758  }
759  if (prev_char == '.' && prev_prev_char != escape_char) {
760  if (cur_char == '*' || cur_char == '+') {
761  if (cur_char == '*') {
762  like_str.pop_back();
763  }
764  // .* --> %
765  // .+ --> _%
766  like_str.push_back('%');
767  prev_prev_char = prev_char;
768  prev_char = cur_char;
769  continue;
770  }
771  }
772  return false;
773  }
774  pattern_str = like_str;
775  return true;
776 }

Member Data Documentation

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

Definition at line 546 of file ParserNode.h.

Referenced by get_arg().

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

Definition at line 548 of file ParserNode.h.

Referenced by get_escape_string().

bool Parser::RegexpExpr::is_not_
private

Definition at line 545 of file ParserNode.h.

Referenced by get_is_not().

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

Definition at line 547 of file ParserNode.h.

Referenced by get_pattern_string().


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