OmniSciDB  04ee39c94c
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 526 of file ParserNode.h.

Constructor & Destructor Documentation

◆ RegexpExpr()

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) {}
std::unique_ptr< Expr > arg
Definition: ParserNode.h:546
std::unique_ptr< Expr > pattern_string
Definition: ParserNode.h:547
std::unique_ptr< Expr > escape_string
Definition: ParserNode.h:548

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

612  {
613  auto arg_expr = arg->analyze(catalog, query, allow_tlist_ref);
614  auto pattern_expr = pattern_string->analyze(catalog, query, allow_tlist_ref);
615  auto escape_expr = escape_string == nullptr
616  ? nullptr
617  : escape_string->analyze(catalog, query, allow_tlist_ref);
618  return RegexpExpr::get(arg_expr, pattern_expr, escape_expr, is_not);
619 }
std::unique_ptr< Expr > arg
Definition: ParserNode.h:546
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:621
std::unique_ptr< Expr > pattern_string
Definition: ParserNode.h:547
std::unique_ptr< Expr > escape_string
Definition: ParserNode.h:548

◆ check_pattern_expr()

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

Definition at line 572 of file ParserNode.cpp.

572  {
573  if (pattern_str.back() == escape_char) {
574  throw std::runtime_error("REGEXP pattern must not end with escape character.");
575  }
576 }

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

References anonymous_namespace{ExecuteTest.cpp}::c(), kBOOLEAN, kNOT, and run-benchmark-import::result.

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

625  {
626  if (!arg_expr->get_type_info().is_string()) {
627  throw std::runtime_error("expression before REGEXP must be of a string type.");
628  }
629  if (!pattern_expr->get_type_info().is_string()) {
630  throw std::runtime_error("expression after REGEXP must be of a string type.");
631  }
632  char escape_char = '\\';
633  if (escape_expr != nullptr) {
634  if (!escape_expr->get_type_info().is_string()) {
635  throw std::runtime_error("expression after ESCAPE must be of a string type.");
636  }
637  if (!escape_expr->get_type_info().is_string()) {
638  throw std::runtime_error("expression after ESCAPE must be of a string type.");
639  }
640  auto c = std::dynamic_pointer_cast<Analyzer::Constant>(escape_expr);
641  if (c != nullptr && c->get_constval().stringval->length() > 1) {
642  throw std::runtime_error("String after ESCAPE must have a single character.");
643  }
644  escape_char = (*c->get_constval().stringval)[0];
645  if (escape_char != '\\') {
646  throw std::runtime_error("Only supporting '\\' escape character.");
647  }
648  }
649  auto c = std::dynamic_pointer_cast<Analyzer::Constant>(pattern_expr);
650  if (c != nullptr) {
651  std::string& pattern = *c->get_constval().stringval;
652  if (translate_to_like_pattern(pattern, escape_char)) {
653  return LikeExpr::get(arg_expr, pattern_expr, escape_expr, false, is_not);
654  }
655  }
656  std::shared_ptr<Analyzer::Expr> result =
657  makeExpr<Analyzer::RegexpExpr>(arg_expr->decompress(), pattern_expr, escape_expr);
658  if (is_not) {
659  result = makeExpr<Analyzer::UOper>(kBOOLEAN, kNOT, result);
660  }
661  return result;
662 }
void c(const std::string &query_string, const ExecutorDeviceType device_type)
static bool translate_to_like_pattern(std::string &pattern_str, char escape_char)
Definition: ParserNode.cpp:578
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:526
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 531 of file ParserNode.h.

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

◆ get_escape_string()

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

Definition at line 533 of file ParserNode.h.

References anonymous_namespace{UpdateMetadataTest.cpp}::query, and to_string().

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

◆ get_is_not()

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

Definition at line 530 of file ParserNode.h.

530 { return is_not; }

◆ get_pattern_string()

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

Definition at line 532 of file ParserNode.h.

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

◆ to_string()

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

Implements Parser::Expr.

Definition at line 1441 of file ParserNode.cpp.

1441  {
1442  std::string str = arg->to_string();
1443  if (is_not) {
1444  str += " NOT REGEXP ";
1445  } else {
1446  str += " REGEXP ";
1447  }
1448  str += pattern_string->to_string();
1449  if (escape_string != nullptr) {
1450  str += " ESCAPE " + escape_string->to_string();
1451  }
1452  return str;
1453 }
std::unique_ptr< Expr > arg
Definition: ParserNode.h:546
std::unique_ptr< Expr > pattern_string
Definition: ParserNode.h:547
std::unique_ptr< Expr > escape_string
Definition: ParserNode.h:548

◆ translate_to_like_pattern()

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

Definition at line 578 of file ParserNode.cpp.

578  {
579  char prev_char = '\0';
580  char prev_prev_char = '\0';
581  std::string like_str;
582  for (char& cur_char : pattern_str) {
583  if (prev_char == escape_char || isalnum(cur_char) || cur_char == ' ' ||
584  cur_char == '.') {
585  like_str.push_back((cur_char == '.') ? '_' : cur_char);
586  prev_prev_char = prev_char;
587  prev_char = cur_char;
588  continue;
589  }
590  if (prev_char == '.' && prev_prev_char != escape_char) {
591  if (cur_char == '*' || cur_char == '+') {
592  if (cur_char == '*') {
593  like_str.pop_back();
594  }
595  // .* --> %
596  // .+ --> _%
597  like_str.push_back('%');
598  prev_prev_char = prev_char;
599  prev_char = cur_char;
600  continue;
601  }
602  }
603  return false;
604  }
605  pattern_str = like_str;
606  return true;
607 }

Member Data Documentation

◆ arg

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

Definition at line 546 of file ParserNode.h.

◆ escape_string

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

Definition at line 548 of file ParserNode.h.

◆ is_not

bool Parser::RegexpExpr::is_not
private

Definition at line 545 of file ParserNode.h.

◆ pattern_string

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

Definition at line 547 of file ParserNode.h.


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