-
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.
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
c7b29cf
commit d4f9833
Showing
9 changed files
with
285 additions
and
0 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
57 changes: 57 additions & 0 deletions
57
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,57 @@ | ||
#pragma once | ||
|
||
#include <regex> | ||
|
||
#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) { | ||
// 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 | ||
std::string sanitizedPattern = std::regex_replace(pattern.getAsString(), std::regex(R"(\\\\)"), "\\"); | ||
std::vector<common::Match> matches = common::RegexExtractAll(value.getAsString(), sanitizedPattern); | ||
|
||
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.str(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 |
45 changes: 45 additions & 0 deletions
45
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,45 @@ | ||
#pragma once | ||
|
||
#include <regex> | ||
|
||
#include "common/re2_regex.h" | ||
#include "common/types/ku_string.h" | ||
#include "common/vector/value_vector.h" | ||
|
||
namespace kuzu::function::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) { | ||
// 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 | ||
std::string sanitized_pattern = std::regex_replace(pattern.getAsString(), std::regex(R"(\\\\)"), "\\"); | ||
common::Match match; | ||
if (common::RegexExtract(value.getAsString(), match, sanitized_pattern)) { | ||
copyToKuzuString(match.str(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; | ||
operation(value, pattern, defaultGroup, 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 kuzu::function::operation |
21 changes: 21 additions & 0 deletions
21
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,21 @@ | ||
#pragma once | ||
|
||
#include <regex> | ||
|
||
#include "common/types/ku_string.h" | ||
#include "common/re2_regex.h" | ||
#include "function/string/operations/find_operation.h" | ||
|
||
namespace kuzu::function::operation { | ||
|
||
struct RegexpMatches { | ||
static inline void operation(common::ku_string_t& left, common::ku_string_t& right, uint8_t& result) { | ||
// 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 | ||
std::string pattern = std::regex_replace(right.getAsString(), std::regex(R"(\\\\)"), "\\"); | ||
result = common::RegexPartialMatch(left.getAsString(), pattern); | ||
} | ||
}; | ||
|
||
} // namespace kuzu |
35 changes: 35 additions & 0 deletions
35
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,35 @@ | ||
#pragma once | ||
|
||
#include <regex> | ||
|
||
#include "common/types/ku_string.h" | ||
#include "common/re2_regex.h" | ||
#include "function/string/operations/find_operation.h" | ||
|
||
namespace kuzu::function::operation { | ||
struct RegexpReplace { | ||
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) { | ||
// 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 | ||
std::string sanitized_pattern = std::regex_replace(pattern.getAsString(), std::regex(R"(\\\\)"), "\\"); | ||
std::string result_str = common::RegexReplace(value.getAsString(), sanitized_pattern, replacement.getAsString()); | ||
copyToKuzuString(result_str, 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 kuzu::function::operation |
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