-
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 Add string regex functions 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
bfc1b0d
commit 13ace76
Showing
15 changed files
with
503 additions
and
76 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#include "common/re2_regex.h" | ||
|
||
#include <regex> | ||
|
||
namespace kuzu { | ||
namespace common { | ||
|
||
Regex::Regex(const std::string& pattern, RegexOptions options) { | ||
regex::RE2::Options o; | ||
o.set_case_sensitive(options == RegexOptions::CASE_INSENSITIVE); | ||
regex = std::make_shared<regex::RE2>(regex::StringPiece(pattern), o); | ||
} | ||
|
||
bool RegexSearchInternal(const char* input, Match& match, const Regex& r, regex::RE2::Anchor anchor, | ||
size_t start, size_t end) { | ||
auto& regex = r.getRegex(); | ||
std::vector<regex::StringPiece> targetSubMatches; | ||
auto submatchCount = regex.NumberOfCapturingGroups() + 1; | ||
targetSubMatches.resize(submatchCount); | ||
match.clear(); | ||
if (!regex.Match(regex::StringPiece(input), start, end, anchor, targetSubMatches.data(), | ||
submatchCount)) { | ||
return false; | ||
} | ||
for (auto& subMatch : targetSubMatches) { | ||
SubMatch group_match(subMatch.ToString(), subMatch.data() - input); | ||
match.emplace_back(group_match); | ||
} | ||
return true; | ||
} | ||
|
||
bool RegexFullMatch(const std::string& input, const Regex& regex) { | ||
Match nopMatch; | ||
return RegexSearchInternal( | ||
input.c_str(), nopMatch, regex, regex::RE2::ANCHOR_BOTH, 0, input.size()); | ||
} | ||
|
||
bool RegexPartialMatch(const std::string& input, const Regex& regex) { | ||
Match nopMatch; | ||
return RegexSearchInternal( | ||
input.c_str(), nopMatch, regex, regex::RE2::UNANCHORED, 0, input.size()); | ||
} | ||
|
||
std::string RegexReplace( | ||
const std::string& input, const Regex& regex, const std::string& replacement) { | ||
std::string result = input; | ||
RE2::Replace(&result, regex.getRegex(), replacement); | ||
return result; | ||
} | ||
|
||
bool RegexExtract(const std::string& input, Match& match, const Regex& regex) { | ||
return RegexSearchInternal( | ||
input.c_str(), match, regex, regex::RE2::UNANCHORED, 0, input.size()); | ||
} | ||
|
||
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; | ||
} | ||
|
||
std::vector<Match> RegexExtractAll(const std::string& input, const Regex& regex) { | ||
std::vector<Match> matches; | ||
size_t position = 0; | ||
Match match; | ||
while (RegexSearchInternal( | ||
input.c_str(), match, regex, regex::RE2::UNANCHORED, position, input.size())) { | ||
size_t newPosition = match.getPosition(0) + match.length(0); | ||
if (newPosition == position) { | ||
// Empty match found, increment the position manually | ||
newPosition++; | ||
while (newPosition < input.length() && !IsCharacter(input[newPosition])) { | ||
newPosition++; | ||
} | ||
} | ||
position = newPosition; | ||
matches.emplace_back(std::move(match)); | ||
} | ||
return matches; | ||
} | ||
|
||
std::string ParseCypherRegex(const std::string& input) { | ||
// Cypher parses escape characters with 2 backslash eg. for expressing '.' requires '\\.' | ||
// Since Regular Expression requires only 1 backslash '\.' we need to replace double slash | ||
// with single | ||
return std::regex_replace(input, std::regex(R"(\\\\)"), "\\"); | ||
} | ||
|
||
} // namespace common | ||
} // namespace kuzu |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
#include <memory> | ||
#include <stdexcept> | ||
#include <string> | ||
|
||
#include "re2.h" | ||
|
||
namespace kuzu { | ||
namespace common { | ||
enum class RegexOptions : uint8_t { NONE, CASE_INSENSITIVE }; | ||
|
||
class Regex { | ||
public: | ||
Regex(const std::string& pattern, RegexOptions options = RegexOptions::NONE); | ||
const kuzu::regex::RE2& getRegex() const { return *regex; } | ||
|
||
private: | ||
std::shared_ptr<kuzu::regex::RE2> regex; | ||
}; | ||
|
||
/** | ||
* This represents a single submatch coming from RE2::Match | ||
*/ | ||
class SubMatch { | ||
public: | ||
SubMatch(const std::string& text, const uint32_t& position) : text{text}, position{position} {}; | ||
const std::string& getText() const { return text; } | ||
const uint32_t getPosition() const { return position; } | ||
|
||
private: | ||
std::string text; | ||
uint32_t position; | ||
}; | ||
|
||
/** | ||
* This represents a list of submatches in a single iteration of RE2::Match | ||
* i.e. matching Regex("([a-z ]+)_?") on "hello_world" will have two | ||
* submatches, "hello_" and "hello". | ||
*/ | ||
class Match { | ||
public: | ||
SubMatch& getSubMatch(uint64_t index) { | ||
if (index >= subMatches.size()) { | ||
throw std::runtime_error("RE2: Match index is out of range"); | ||
} | ||
return subMatches[index]; | ||
} | ||
std::string getText(uint64_t index) { return getSubMatch(index).getText(); } | ||
uint64_t getPosition(uint64_t index) { return getSubMatch(index).getPosition(); } | ||
uint64_t length(uint64_t index) { return getSubMatch(index).getText().size(); } | ||
void emplace_back(SubMatch& group) { subMatches.emplace_back(group); } | ||
void clear() { subMatches.clear(); } | ||
|
||
private: | ||
std::vector<SubMatch> subMatches; | ||
}; | ||
|
||
bool RegexFullMatch(const std::string& input, const Regex& regex); | ||
bool RegexPartialMatch(const std::string& input, const Regex& regex); | ||
std::string RegexReplace( | ||
const std::string& input, const Regex& regex, const std::string& replacement); | ||
bool RegexExtract(const std::string& input, Match& match, const Regex& regex); | ||
std::vector<Match> RegexExtractAll(const std::string& input, const Regex& regex); | ||
std::string ParseCypherRegex(const std::string& input); | ||
|
||
} // namespace common | ||
} // namespace kuzu |
26 changes: 0 additions & 26 deletions
26
src/include/function/string/operations/reg_expr_operation.h
This file was deleted.
Oops, something went wrong.
46 changes: 46 additions & 0 deletions
46
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,46 @@ | ||
#pragma once | ||
|
||
#include "common/re2_regex.h" | ||
#include "common/types/ku_list.h" | ||
#include "common/types/ku_string.h" | ||
#include "common/vector/value_vector.h" | ||
#include "common/vector/value_vector_utils.h" | ||
|
||
namespace kuzu::function::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<common::Match> matches = common::RegexExtractAll( | ||
value.getAsString(), common::ParseCypherRegex(pattern.getAsString())); | ||
|
||
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 (auto match : matches) { | ||
common::ku_string_t kuString; | ||
copyToKuzuString(match.getText(group), 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 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 kuzu::function::operation |
Oops, something went wrong.