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

Constructor & Destructor Documentation

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

Definition at line 539 of file ParserNode.h.

540  : is_not(n), arg(a), pattern_string(p), escape_string(e) {}
std::unique_ptr< Expr > arg
Definition: ParserNode.h:557
std::unique_ptr< Expr > pattern_string
Definition: ParserNode.h:558
std::unique_ptr< Expr > escape_string
Definition: ParserNode.h:559

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

616  {
617  auto arg_expr = arg->analyze(catalog, query, allow_tlist_ref);
618  auto pattern_expr = pattern_string->analyze(catalog, query, allow_tlist_ref);
619  auto escape_expr = escape_string == nullptr
620  ? nullptr
621  : escape_string->analyze(catalog, query, allow_tlist_ref);
622  return RegexpExpr::get(arg_expr, pattern_expr, escape_expr, is_not);
623 }
std::unique_ptr< Expr > arg
Definition: ParserNode.h:557
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:625
std::unique_ptr< Expr > pattern_string
Definition: ParserNode.h:558
std::unique_ptr< Expr > escape_string
Definition: ParserNode.h:559
void Parser::RegexpExpr::check_pattern_expr ( const std::string &  pattern_str,
char  escape_char 
)
staticprivate

Definition at line 576 of file ParserNode.cpp.

576  {
577  if (pattern_str.back() == escape_char) {
578  throw std::runtime_error("REGEXP pattern must not end with escape character.");
579  }
580 }
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 625 of file ParserNode.cpp.

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

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

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

References arg.

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

Definition at line 544 of file ParserNode.h.

References escape_string.

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

Definition at line 541 of file ParserNode.h.

References is_not.

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

Definition at line 543 of file ParserNode.h.

References pattern_string.

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

Implements Parser::Expr.

Definition at line 1456 of file ParserNode.cpp.

1456  {
1457  std::string str = arg->to_string();
1458  if (is_not) {
1459  str += " NOT REGEXP ";
1460  } else {
1461  str += " REGEXP ";
1462  }
1463  str += pattern_string->to_string();
1464  if (escape_string != nullptr) {
1465  str += " ESCAPE " + escape_string->to_string();
1466  }
1467  return str;
1468 }
std::unique_ptr< Expr > arg
Definition: ParserNode.h:557
std::unique_ptr< Expr > pattern_string
Definition: ParserNode.h:558
std::unique_ptr< Expr > escape_string
Definition: ParserNode.h:559
bool Parser::RegexpExpr::translate_to_like_pattern ( std::string &  pattern_str,
char  escape_char 
)
staticprivate

Definition at line 582 of file ParserNode.cpp.

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

Member Data Documentation

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

Definition at line 557 of file ParserNode.h.

Referenced by get_arg().

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

Definition at line 559 of file ParserNode.h.

Referenced by get_escape_string().

bool Parser::RegexpExpr::is_not
private

Definition at line 556 of file ParserNode.h.

Referenced by get_is_not().

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

Definition at line 558 of file ParserNode.h.

Referenced by get_pattern_string().


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