refactor: Use re2::StringPiece for RE2::FullMatch()#15259
refactor: Use re2::StringPiece for RE2::FullMatch()#15259kou wants to merge 5 commits intofacebookincubator:mainfrom
re2::StringPiece for RE2::FullMatch()#15259Conversation
✅ Deploy Preview for meta-velox canceled.
|
3958f22 to
fbfc654
Compare
|
@abhinavmuk04 Could you take a look at this? This is a follow-up PR for your PR GH-15124. |
| @@ -1937,7 +1937,10 @@ struct ParseDurationFunction { | |||
| re2::StringPiece valueStr; | |||
| re2::StringPiece unit; | |||
| if (!RE2::FullMatch( | |||
| std::string_view{amountUnit}, *durationRegex_, &valueStr, &unit)) { | |||
| absl::string_view(amountUnit.data(), amountUnit.size()), | |||
There was a problem hiding this comment.
Oh, old RE2 that is used by our CentOS Stream 9 based image uses StringPiece not absl::string_view/std::string_view...
There was a problem hiding this comment.
Let's use re2::StringPiece instead for old RE2 and new RE2.
2584f29 to
cd6ca16
Compare
absl::string_view for RE2::FullMatch()re2::StringPiece for RE2::FullMatch()
cd6ca16 to
e827d9e
Compare
| @@ -1937,7 +1937,12 @@ struct ParseDurationFunction { | |||
| re2::StringPiece valueStr; | |||
| re2::StringPiece unit; | |||
| if (!RE2::FullMatch( | |||
| std::string_view{amountUnit}, *durationRegex_, &valueStr, &unit)) { | |||
| // We can use absl::string_view() once we require RE2 | |||
There was a problem hiding this comment.
I'm surprised these libraries don't take a std::string_view. Is this because we are using old versions?
In general, for now we need this explicit cast (velox::StringView->std::string_view), but we are working on making this conversion implicit.
There was a problem hiding this comment.
We can use std::string_view when:
- We use old RE2 (because
re2::StringPieceprovides an implicit cast fromstd::string_view) or - We use new RE2 and Abseil that uses
std::stringas an alias ofabsl::string_view(becauseabsl::string_viewdoes not provide an implicit cast fromstd::string_view)
If we use new RE2 and Abseil that does not use std::string as an alias of absl::string_view (e.g. Debian's Abseil package), we can not use std::string_view. We need to use absl::string_view. FYI: re2::StringPiece is an alias of absl::string_view in new RE2.
FYI: This works with new RE2:
diff --git a/velox/functions/prestosql/DateTimeFunctions.h b/velox/functions/prestosql/DateTimeFunctions.h
index 09200e41f..aa86d196a 100644
--- a/velox/functions/prestosql/DateTimeFunctions.h
+++ b/velox/functions/prestosql/DateTimeFunctions.h
@@ -1936,8 +1936,7 @@ struct ParseDurationFunction {
const arg_type<Varchar>& amountUnit) {
re2::StringPiece valueStr;
re2::StringPiece unit;
- if (!RE2::FullMatch(
- std::string_view{amountUnit}, *durationRegex_, &valueStr, &unit)) {
+ if (!RE2::FullMatch(amountUnit, *durationRegex_, &valueStr, &unit)) {
VELOX_USER_FAIL(
"Input duration is not a valid data duration string: {}", amountUnit);
}
diff --git a/velox/type/StringView.h b/velox/type/StringView.h
index 5c1bc00a0..6613882fd 100644
--- a/velox/type/StringView.h
+++ b/velox/type/StringView.h
@@ -25,6 +25,8 @@
#include <folly/Range.h>
#include <folly/dynamic.h>
+#include <absl/strings/string_view.h>
+
#include <fmt/format.h>
#include "velox/common/base/BitUtil.h"
@@ -213,10 +215,17 @@ struct StringView {
}
operator std::string_view() && = delete;
- explicit operator std::string_view() const& {
+ operator std::string_view() const& {
return std::string_view(data(), size());
}
+#ifndef ABSL_USES_STD_STRING_VIEW
+ operator absl::string_view() && = delete;
+ operator absl::string_view() const& {
+ return absl::string_view(data(), size());
+ }
+#endif
+
const char* begin() && = delete;
const char* begin() const& {
return data();
Can we update the image instead? CC: @assignUser @kgpai |
|
Yeah, using a more recent re2 seems like the better solution imo. |
|
I'm surprised there is no implicit conversion between std::string_view and absl's or RE2's. Is it just because we are using old versions of these libraries? |
No. New |
e827d9e to
2fa054a
Compare
| @@ -1937,7 +1937,10 @@ struct ParseDurationFunction { | |||
| re2::StringPiece valueStr; | |||
| re2::StringPiece unit; | |||
| if (!RE2::FullMatch( | |||
| std::string_view{amountUnit}, *durationRegex_, &valueStr, &unit)) { | |||
| absl::string_view(amountUnit.data(), amountUnit.size()), | |||
There was a problem hiding this comment.
PR title says "Use re2::StringPiece", but the code uses "absl::string_view". Other code (2 lines above) uses re2::StringPiece. It would be nice to limit the number of different string views used in this code.
There was a problem hiding this comment.
Sorry. I forgot to update PR title/description.
I've replaced all re2::StringPiece with absl::string_view.
There was a problem hiding this comment.
I see more usages of re2::StringPiece in the repo. Should we replace all of them with absl::string_view? Might be simpler to just use re2::StringPiece everywhere, no?
There was a problem hiding this comment.
Oh... Let's use re2::StringPiece for now because:
- RE2 will use
std::string_viewnotabsl::string_vieweventually because RE2 migrated tostd::optionalfromabsl::optional: google/re2@2a029e2 re2::StringPieceworks with old RE2 and new RE2
We can revisit this when RE2 migrates to std::string_view from absl::string_view.
scripts/setup-centos9.sh
Outdated
| @@ -70,7 +70,7 @@ function install_build_prerequisites { | |||
| # Install dependencies from the package managers. | |||
| function install_velox_deps_from_dnf { | |||
| dnf_install libevent-devel \ | |||
| openssl-devel re2-devel libzstd-devel lz4-devel double-conversion-devel \ | |||
| openssl-devel libzstd-devel lz4-devel double-conversion-devel \ | |||
There was a problem hiding this comment.
Please, document this change in the PR description.
There was a problem hiding this comment.
Sorry. I forgot to do it. Done.
|
CI is red. |
re2::StringPiece for RE2::FullMatch()absl::string_view for RE2::FullMatch()
c7012c2 to
f41e6b1
Compare
|
The "Linux release with adapters" failure was fixed by #15272 . I've rebased. The "Fuzzer Jobs" failure is caused because |
f41e6b1 to
a3b9271
Compare
absl::string_view for RE2::FullMatch()re2::StringPiece for RE2::FullMatch()
Do you expect CentOS Stream 9 to be supported going forward? If so, we may need to add CI for it. CC: @assignUser @kgpai |
|
@mbasmanova has imported this pull request. If you are a Meta employee, you can view this in D85539525. |
`RE2::FullMatch()` uses `absl::string_view` not `std::string_view`: https://github.com/google/re2/blob/61c4644171ee6b480540bf9e569cba06d9090b4b/re2/re2.h#L411 `absl::string_view` may not be an alias of `std::string_view`. In the case, the following error is reported: ```text In file included from velox/functions/prestosql/registration/DateTimeFunctionsRegistration.cpp:18: velox/functions/prestosql/DateTimeFunctions.h:1939:10: error: no matching function for call to 'FullMatch' 1939 | if (!RE2::FullMatch( | ^~~~~~~~~~~~~~ /include/re2/re2.h:411:15: note: candidate function template not viable: no known conversion from 'std::string_view' (aka 'basic_string_view<char>') to 'absl::string_view' for 1st argument 411 | static bool FullMatch(absl::string_view text, const RE2& re, A&&... a) { | ^ ~~~~~~~~~~~~~~~~~~~~~~ ``` Related: facebookincubatorGH-15124
a3b9271 to
fc49726
Compare
|
@mbasmanova merged this pull request in 9f94be0. |
|
this PR #15134 already exists more than two weeks |
…bator#15259) Summary: `RE2::FullMatch()` uses `absl::string_view` not `std::string_view`: https://github.com/google/re2/blob/61c4644171ee6b480540bf9e569cba06d9090b4b/re2/re2.h#L411 `absl::string_view` may not be an alias of `std::string_view`. In the case, the following error is reported: ```text In file included from velox/functions/prestosql/registration/DateTimeFunctionsRegistration.cpp:18: velox/functions/prestosql/DateTimeFunctions.h:1939:10: error: no matching function for call to 'FullMatch' 1939 | if (!RE2::FullMatch( | ^~~~~~~~~~~~~~ /include/re2/re2.h:411:15: note: candidate function template not viable: no known conversion from 'std::string_view' (aka 'basic_string_view<char>') to 'absl::string_view' for 1st argument 411 | static bool FullMatch(absl::string_view text, const RE2& re, A&&... a) { | ^ ~~~~~~~~~~~~~~~~~~~~~~ ``` Old RE2 that is provided by CentOS Stream 9 doesn't accept `absl::string_view`. Old RE2 uses `re2::StringPiece` for `RE2::FullMatch()` and new RE2 provides `re2::StringPiece` as an alias of `absl::string_view`. So we can use `re2::StringPiece` for both of old and new RE2. We can drop support for old RE2 to always use `absl::string_view` but we use `re2::StringPiece` for now. It seems that RE2 will use `std::string_view` instead of `absl::string_view` eventually. For example, google/re2@2a029e2 is a commit to migrate to `std::optional` from `absl::optional`. We can revisit this after RE2 migrates to `std::string_view`. Related: facebookincubatorGH-15124 Pull Request resolved: facebookincubator#15259 Reviewed By: kevinwilfong Differential Revision: D85539525 Pulled By: mbasmanova fbshipit-source-id: 1dde1c47d7a337d220488aa64b5efa3408876d1e
|
Oh, sorry. I missed the PR. |
RE2::FullMatch()usesabsl::string_viewnotstd::string_view:https://github.com/google/re2/blob/61c4644171ee6b480540bf9e569cba06d9090b4b/re2/re2.h#L411
absl::string_viewmay not be an alias ofstd::string_view. In the case, the following error is reported:Old RE2 that is provided by CentOS Stream 9 doesn't accept
absl::string_view.Old RE2 uses
re2::StringPieceforRE2::FullMatch()and new RE2 providesre2::StringPieceas an alias ofabsl::string_view. So we can usere2::StringPiecefor both of old and new RE2.We can drop support for old RE2 to always use
absl::string_viewbut we usere2::StringPiecefor now. It seems that RE2 will usestd::string_viewinstead ofabsl::string_vieweventually. For example, google/re2@2a029e2 is a commit to migrate tostd::optionalfromabsl::optional.We can revisit this after RE2 migrates to
std::string_view.Related: GH-15124