-
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.
Merge pull request #1516 from gaurav8297/string_functions
Regex String Functions
- Loading branch information
Showing
13 changed files
with
405 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
32 changes: 32 additions & 0 deletions
32
src/include/function/string/operations/base_regexp_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,32 @@ | ||
#pragma once | ||
|
||
#include <regex> | ||
|
||
#include "common/vector/value_vector.h" | ||
|
||
namespace kuzu { | ||
namespace function { | ||
namespace operation { | ||
|
||
struct BaseRegexpOperation { | ||
static inline std::string parseCypherPatten(const std::string& pattern) { | ||
// 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(pattern, std::regex(R"(\\\\)"), "\\"); | ||
} | ||
|
||
static inline 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 |
26 changes: 0 additions & 26 deletions
26
src/include/function/string/operations/reg_expr_operation.h
This file was deleted.
Oops, something went wrong.
77 changes: 77 additions & 0 deletions
77
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,77 @@ | ||
#pragma once | ||
|
||
#include "base_regexp_operation.h" | ||
#include "common/vector/value_vector_utils.h" | ||
#include "re2.h" | ||
|
||
namespace kuzu { | ||
namespace function { | ||
namespace operation { | ||
|
||
struct RegexpExtractAll : BaseRegexpOperation { | ||
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(parseCypherPatten(pattern)); | ||
auto submatchCount = regex.NumberOfCapturingGroups() + 1; | ||
if (group > submatchCount) { | ||
throw common::RuntimeException("Regex match group 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; | ||
} | ||
}; | ||
|
||
} // namespace operation | ||
} // namespace function | ||
} // namespace kuzu |
46 changes: 46 additions & 0 deletions
46
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,46 @@ | ||
#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 : BaseRegexpOperation { | ||
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(parseCypherPatten(pattern)); | ||
auto submatchCount = regex.NumberOfCapturingGroups() + 1; | ||
if (group > submatchCount) { | ||
throw common::RuntimeException("Regex match group 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); | ||
} | ||
}; | ||
|
||
} // 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 : BaseRegexpOperation { | ||
static inline void operation( | ||
common::ku_string_t& left, common::ku_string_t& right, uint8_t& result) { | ||
result = RE2::FullMatch(left.getAsString(), parseCypherPatten(right.getAsString())); | ||
} | ||
}; | ||
|
||
} // namespace operation | ||
} // namespace function | ||
} // namespace kuzu |
19 changes: 19 additions & 0 deletions
19
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,19 @@ | ||
#pragma once | ||
|
||
#include "common/types/ku_string.h" | ||
#include "re2.h" | ||
|
||
namespace kuzu { | ||
namespace function { | ||
namespace operation { | ||
|
||
struct RegexpMatches : BaseRegexpOperation { | ||
static inline void operation( | ||
common::ku_string_t& left, common::ku_string_t& right, uint8_t& result) { | ||
result = RE2::PartialMatch(left.getAsString(), parseCypherPatten(right.getAsString())); | ||
} | ||
}; | ||
|
||
} // namespace operation | ||
} // namespace function | ||
} // namespace kuzu |
23 changes: 23 additions & 0 deletions
23
src/include/function/string/operations/regexp_replace_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,23 @@ | ||
#pragma once | ||
|
||
#include "common/types/ku_string.h" | ||
#include "re2.h" | ||
|
||
namespace kuzu { | ||
namespace function { | ||
namespace operation { | ||
|
||
struct RegexpReplace : BaseRegexpOperation { | ||
static inline void operation(common::ku_string_t& value, common::ku_string_t& pattern, | ||
common::ku_string_t& replacement, common::ku_string_t& result, | ||
common::ValueVector& resultValueVector) { | ||
std::string resultStr = value.getAsString(); | ||
RE2::Replace( | ||
&resultStr, parseCypherPatten(pattern.getAsString()), replacement.getAsString()); | ||
copyToKuzuString(resultStr, result, resultValueVector); | ||
} | ||
}; | ||
|
||
} // namespace operation | ||
} // namespace function | ||
} // namespace kuzu |
Oops, something went wrong.