From ca1d38914ab622f9b46c7d0417958ecc5d846d3a Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Fri, 30 Nov 2018 22:37:31 +0300 Subject: [PATCH] Don't log to stderr within RE2 library [#CLICKHOUSE-2] --- dbms/src/Common/ErrorCodes.cpp | 1 + ...n.inl.h => OptimizedRegularExpression.cpp} | 34 ++++++++++++------- dbms/src/Common/OptimizedRegularExpression.h | 8 ++--- 3 files changed, 25 insertions(+), 18 deletions(-) rename dbms/src/Common/{OptimizedRegularExpression.inl.h => OptimizedRegularExpression.cpp} (93%) diff --git a/dbms/src/Common/ErrorCodes.cpp b/dbms/src/Common/ErrorCodes.cpp index 719e593e3b1..669e7f9c36b 100644 --- a/dbms/src/Common/ErrorCodes.cpp +++ b/dbms/src/Common/ErrorCodes.cpp @@ -401,6 +401,7 @@ namespace ErrorCodes extern const int CANNOT_LINK = 424; extern const int SYSTEM_ERROR = 425; extern const int NULL_POINTER_DEREFERENCE = 426; + extern const int CANNOT_COMPILE_REGEXP = 427; extern const int KEEPER_EXCEPTION = 999; extern const int POCO_EXCEPTION = 1000; diff --git a/dbms/src/Common/OptimizedRegularExpression.inl.h b/dbms/src/Common/OptimizedRegularExpression.cpp similarity index 93% rename from dbms/src/Common/OptimizedRegularExpression.inl.h rename to dbms/src/Common/OptimizedRegularExpression.cpp index 04860622fe7..16251ae1ee0 100644 --- a/dbms/src/Common/OptimizedRegularExpression.inl.h +++ b/dbms/src/Common/OptimizedRegularExpression.cpp @@ -1,7 +1,4 @@ -#include - -#include - +#include #include @@ -9,6 +6,15 @@ #define MAX_SUBPATTERNS 5 +namespace DB +{ + namespace ErrorCodes + { + extern const int CANNOT_COMPILE_REGEXP; + } +} + + template void OptimizedRegularExpressionImpl::analyze( const std::string & regexp, @@ -254,11 +260,11 @@ OptimizedRegularExpressionImpl::OptimizedRegularExpressionImpl(cons /// Just three following options are supported if (options & (~(RE_CASELESS | RE_NO_CAPTURE | RE_DOT_NL))) - throw Poco::Exception("OptimizedRegularExpression: Unsupported option."); + throw DB::Exception("OptimizedRegularExpression: Unsupported option.", DB::ErrorCodes::CANNOT_COMPILE_REGEXP); - is_case_insensitive = options & RE_CASELESS; - bool is_no_capture = options & RE_NO_CAPTURE; - bool is_dot_nl = options & RE_DOT_NL; + is_case_insensitive = options & RE_CASELESS; + bool is_no_capture = options & RE_NO_CAPTURE; + bool is_dot_nl = options & RE_DOT_NL; number_of_subpatterns = 0; if (!is_trivial) @@ -266,6 +272,9 @@ OptimizedRegularExpressionImpl::OptimizedRegularExpressionImpl(cons /// Compile the re2 regular expression. typename RegexType::Options regexp_options; + /// Never write error messages to stderr. It's ignorant to do it from library code. + regexp_options.set_log_errors(false); + if (is_case_insensitive) regexp_options.set_case_sensitive(false); @@ -274,13 +283,13 @@ OptimizedRegularExpressionImpl::OptimizedRegularExpressionImpl(cons re2 = std::make_unique(regexp_, regexp_options); if (!re2->ok()) - throw Poco::Exception("OptimizedRegularExpression: cannot compile re2: " + regexp_ + ", error: " + re2->error()); + throw DB::Exception("OptimizedRegularExpression: cannot compile re2: " + regexp_ + ", error: " + re2->error() + ". Look at https://github.com/google/re2/wiki/Syntax for reference.", DB::ErrorCodes::CANNOT_COMPILE_REGEXP); if (!is_no_capture) { number_of_subpatterns = re2->NumberOfCapturingGroups(); if (number_of_subpatterns > MAX_SUBPATTERNS) - throw Poco::Exception("OptimizedRegularExpression: too many subpatterns in regexp: " + regexp_); + throw DB::Exception("OptimizedRegularExpression: too many subpatterns in regexp: " + regexp_, DB::ErrorCodes::CANNOT_COMPILE_REGEXP); } } } @@ -432,6 +441,5 @@ unsigned OptimizedRegularExpressionImpl::match(const char * subject } } -#undef MIN_LENGTH_FOR_STRSTR -#undef MAX_SUBPATTERNS - +template class OptimizedRegularExpressionImpl; +template class OptimizedRegularExpressionImpl; diff --git a/dbms/src/Common/OptimizedRegularExpression.h b/dbms/src/Common/OptimizedRegularExpression.h index 60566c9f029..729711277b1 100644 --- a/dbms/src/Common/OptimizedRegularExpression.h +++ b/dbms/src/Common/OptimizedRegularExpression.h @@ -45,9 +45,9 @@ class OptimizedRegularExpressionImpl public: enum Options { - RE_CASELESS = 0x00000001, - RE_NO_CAPTURE = 0x00000010, - RE_DOT_NL = 0x00000100 + RE_CASELESS = 0x00000001, + RE_NO_CAPTURE = 0x00000010, + RE_DOT_NL = 0x00000100 }; using Match = OptimizedRegularExpressionDetails::Match; @@ -106,5 +106,3 @@ private: }; using OptimizedRegularExpression = OptimizedRegularExpressionImpl; - -#include "OptimizedRegularExpression.inl.h"