-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename RE_MATCH function to REGEXP_FULL_MATCH The latter is more descriptive and complaint with duckdb's naming convention. Introduce regexp utils based on re2 Refactor regex_full_match implementation Functions added: 1.regexp_matches(string, regex) Returns true if a part of string matches the regex. 2. regexp_replace(string, regex, replacement) Replaces the first occurrence of regex with the replacement, 3. regexp_extract(string, regex[, group = 0]) Split the string along the regex and extract first occurrence of group. 4. regexp_extract_all(string, regex[, group = 0]) Split the string along the regex and extract all occurrences of group.
- Loading branch information
1 parent
3108a21
commit 395fb82
Showing
13 changed files
with
399 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 0 additions & 26 deletions
26
src/include/function/string/operations/reg_expr_operation.h
This file was deleted.
Oops, something went wrong.
89 changes: 89 additions & 0 deletions
89
src/include/function/string/operations/regexp_extract_all_operation.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#pragma once | ||
|
||
#include "common/types/ku_list.h" | ||
#include "common/vector/value_vector_utils.h" | ||
#include "re2.h" | ||
|
||
namespace kuzu { | ||
namespace function { | ||
namespace operation { | ||
|
||
struct RegexpExtractAll { | ||
static inline void operation(common::ku_string_t& value, common::ku_string_t& pattern, | ||
std::int64_t& group, common::list_entry_t& result, common::ValueVector& resultVector) { | ||
std::vector<std::string> matches = | ||
regexExtractAll(value.getAsString(), pattern.getAsString(), group); | ||
|
||
result = common::ListVector::addList(&resultVector, matches.size()); | ||
auto resultValues = common::ListVector::getListValues(&resultVector, result); | ||
auto resultDataVector = common::ListVector::getDataVector(&resultVector); | ||
auto numBytesPerValue = resultDataVector->getNumBytesPerValue(); | ||
|
||
for (const auto& match : matches) { | ||
common::ku_string_t kuString; | ||
copyToKuzuString(match, kuString, *resultDataVector); | ||
common::ValueVectorUtils::copyValue(resultValues, *resultDataVector, | ||
reinterpret_cast<uint8_t*>(&kuString), *resultDataVector); | ||
resultValues += numBytesPerValue; | ||
} | ||
} | ||
|
||
static inline void operation(common::ku_string_t& value, common::ku_string_t& pattern, | ||
common::list_entry_t& result, common::ValueVector& resultVector) { | ||
int64_t defaultGroup = 0; | ||
operation(value, pattern, defaultGroup, result, resultVector); | ||
} | ||
|
||
static std::vector<std::string> regexExtractAll( | ||
const std::string& value, const std::string& pattern, std::int64_t& group) { | ||
RE2 regex(common::parseCypherPatten(pattern)); | ||
auto submatchCount = regex.NumberOfCapturingGroups() + 1; | ||
if (group > submatchCount) { | ||
throw std::runtime_error("RE2: Match index is out of range"); | ||
} | ||
|
||
regex::StringPiece input(value); | ||
std::vector<regex::StringPiece> targetSubMatches; | ||
targetSubMatches.resize(submatchCount); | ||
uint64_t startPos = 0; | ||
|
||
std::vector<std::string> matches; | ||
while (regex.Match(input, startPos, input.length(), RE2::UNANCHORED, | ||
targetSubMatches.data(), submatchCount)) { | ||
uint64_t consumed = | ||
static_cast<size_t>(targetSubMatches[0].end() - (input.begin() + startPos)); | ||
if (!consumed) { | ||
// Empty match found, increment the position manually | ||
consumed++; | ||
while (startPos + consumed < input.length() && | ||
!IsCharacter(input[startPos + consumed])) { | ||
consumed++; | ||
} | ||
} | ||
startPos += consumed; | ||
matches.emplace_back(targetSubMatches[group]); | ||
} | ||
|
||
return matches; | ||
} | ||
|
||
static inline bool IsCharacter(char c) { | ||
// Check if this character is not the middle of utf-8 character i.e. it shouldn't begin with | ||
// 10 XX XX XX | ||
return (c & 0xc0) != 0x80; | ||
} | ||
|
||
static void copyToKuzuString( | ||
const std::string& value, common::ku_string_t& kuString, common::ValueVector& valueVector) { | ||
if (!common::ku_string_t::isShortString(value.length())) { | ||
kuString.overflowPtr = reinterpret_cast<uint64_t>( | ||
common::StringVector::getInMemOverflowBuffer(&valueVector) | ||
->allocateSpace(value.length())); | ||
} | ||
kuString.set(value); | ||
} | ||
}; | ||
|
||
} // namespace operation | ||
} // namespace function | ||
} // namespace kuzu |
56 changes: 56 additions & 0 deletions
56
src/include/function/string/operations/regexp_extract_operation.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#pragma once | ||
|
||
#include "common/types/ku_string.h" | ||
#include "common/vector/value_vector.h" | ||
#include "re2.h" | ||
|
||
namespace kuzu { | ||
namespace function { | ||
namespace operation { | ||
|
||
struct RegexpExtract { | ||
static inline void operation(common::ku_string_t& value, common::ku_string_t& pattern, | ||
std::int64_t& group, common::ku_string_t& result, common::ValueVector& resultValueVector) { | ||
regexExtract(value.getAsString(), pattern.getAsString(), group, result, resultValueVector); | ||
} | ||
|
||
static inline void operation(common::ku_string_t& value, common::ku_string_t& pattern, | ||
common::ku_string_t& result, common::ValueVector& resultValueVector) { | ||
int64_t defaultGroup = 0; | ||
regexExtract( | ||
value.getAsString(), pattern.getAsString(), defaultGroup, result, resultValueVector); | ||
} | ||
|
||
static void regexExtract(const std::string& input, const std::string& pattern, | ||
std::int64_t& group, common::ku_string_t& result, common::ValueVector& resultValueVector) { | ||
RE2 regex(common::parseCypherPatten(pattern)); | ||
auto submatchCount = regex.NumberOfCapturingGroups() + 1; | ||
if (group > submatchCount) { | ||
throw std::runtime_error("RE2: Match index is out of range"); | ||
} | ||
|
||
std::vector<regex::StringPiece> targetSubMatches; | ||
targetSubMatches.resize(submatchCount); | ||
|
||
if (!regex.Match(regex::StringPiece(input), 0, input.length(), RE2::UNANCHORED, | ||
targetSubMatches.data(), submatchCount)) { | ||
return; | ||
} | ||
|
||
copyToKuzuString(targetSubMatches[group].ToString(), result, resultValueVector); | ||
} | ||
|
||
static void copyToKuzuString( | ||
const std::string& value, common::ku_string_t& kuString, common::ValueVector& valueVector) { | ||
if (!common::ku_string_t::isShortString(value.length())) { | ||
kuString.overflowPtr = reinterpret_cast<uint64_t>( | ||
common::StringVector::getInMemOverflowBuffer(&valueVector) | ||
->allocateSpace(value.length())); | ||
} | ||
kuString.set(value); | ||
} | ||
}; | ||
|
||
} // namespace operation | ||
} // namespace function | ||
} // namespace kuzu |
19 changes: 19 additions & 0 deletions
19
src/include/function/string/operations/regexp_full_match_operation.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#pragma once | ||
|
||
#include "common/types/ku_string.h" | ||
#include "re2.h" | ||
|
||
namespace kuzu { | ||
namespace function { | ||
namespace operation { | ||
|
||
struct RegexpFullMatch { | ||
static inline void operation( | ||
common::ku_string_t& left, common::ku_string_t& right, uint8_t& result) { | ||
result = RE2::FullMatch(left.getAsString(), common::parseCypherPatten(right.getAsString())); | ||
} | ||
}; | ||
|
||
} // namespace operation | ||
} // namespace function | ||
} // namespace kuzu |
20 changes: 20 additions & 0 deletions
20
src/include/function/string/operations/regexp_matches_operation.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#pragma once | ||
|
||
#include "common/types/ku_string.h" | ||
#include "re2.h" | ||
|
||
namespace kuzu { | ||
namespace function { | ||
namespace operation { | ||
|
||
struct RegexpMatches { | ||
static inline void operation( | ||
common::ku_string_t& left, common::ku_string_t& right, uint8_t& result) { | ||
result = | ||
RE2::PartialMatch(left.getAsString(), common::parseCypherPatten(right.getAsString())); | ||
} | ||
}; | ||
|
||
} // namespace operation | ||
} // namespace function | ||
} // namespace kuzu |
Oops, something went wrong.