#include "binder/expression/expression_util.h" #include "common/exception/binder.h" #include "expression_evaluator/expression_evaluator_utils.h" #include "function/string/functions/base_regexp_function.h" #include "function/string/vector_string_functions.h" #include "re2.h" namespace lbug { namespace function { using namespace common; using re2_replace_func_t = std::function; // A pattern that is not a STRING literal is compiled as given, without the Cypher escape handling // a STRING literal pattern gets. struct RegexReplaceBindData : public RegexpBindData { re2_replace_func_t replaceFunc; RegexReplaceBindData(common::logical_type_vec_t paramTypes, re2_replace_func_t replaceFunc) : RegexpBindData{std::move(paramTypes), common::LogicalType::STRING(), false /* parseCypherEscapes */}, replaceFunc{std::move(replaceFunc)} {} std::unique_ptr copy() const override { return std::make_unique(copyVector(paramTypes), replaceFunc); } }; struct RegexpReplace { static void operation(string_t& value, string_t& pattern, string_t& replacement, string_t& result, common::ValueVector& resultValueVector, void* dataPtr) { auto bindData = reinterpret_cast(dataPtr); std::string resultStr = value.getAsString(); bindData->replaceFunc(&resultStr, RegexpBindData::getRegex(dataPtr, pattern), replacement.getAsString()); BaseRegexpOperation::copyToLbugString(resultStr, result, resultValueVector); } }; struct RegexReplaceBindDataStaticPattern : public RegexReplaceBindData { regex::RE2 pattern; RegexReplaceBindDataStaticPattern(common::logical_type_vec_t paramTypes, re2_replace_func_t replaceFunc, std::string patternInStr) : RegexReplaceBindData{std::move(paramTypes), std::move(replaceFunc)}, pattern{patternInStr} {} std::unique_ptr copy() const override { return std::make_unique(copyVector(paramTypes), replaceFunc, pattern.pattern()); } }; struct RegexpReplaceStaticPattern { static void operation(string_t& value, string_t& /*pattern*/, string_t& replacement, string_t& result, common::ValueVector& resultValueVector, void* dataPtr) { auto bindData = reinterpret_cast(dataPtr); auto resultStr = value.getAsString(); bindData->replaceFunc(&resultStr, bindData->pattern, replacement.getAsString()); BaseRegexpOperation::copyToLbugString(resultStr, result, resultValueVector); } }; static re2_replace_func_t bindReplaceFunc(const binder::expression_vector& expr) { re2_replace_func_t result; switch (expr.size()) { case 3: { result = RE2::Replace; } break; case 4: { result = RE2::GlobalReplace; } break; default: UNREACHABLE_CODE; } return result; } template scalar_func_exec_t getExecFunc(const binder::expression_vector& expr) { scalar_func_exec_t execFunc; switch (expr.size()) { case 3: { execFunc = ScalarFunction::TernaryRegexExecFunction; } break; case 4: { auto option = expr[3]; binder::ExpressionUtil::validateExpressionType(*option, ExpressionType::LITERAL); binder::ExpressionUtil::validateDataType(*option, LogicalType::STRING()); auto optionVal = binder::ExpressionUtil::getLiteralValue(*option); if (optionVal != RegexpReplaceFunction::GLOBAL_REPLACE_OPTION) { throw common::BinderException{ "regex_replace can only support global replace option: g."}; } execFunc = ScalarFunction::TernaryRegexExecFunction; } break; default: UNREACHABLE_CODE; } return execFunc; } std::unique_ptr bindFunc(ScalarBindFuncInput input) { auto definition = input.definition->ptrCast(); re2_replace_func_t replaceFunc = bindReplaceFunc(input.arguments); if (BaseRegexpOperation::isStringLiteral(*input.arguments[1])) { definition->execFunc = getExecFunc(input.arguments); auto value = evaluator::ExpressionEvaluatorUtils::evaluateConstantExpression( input.arguments[1], input.context); return std::make_unique(logical_type_vec_t{}, std::move(replaceFunc), BaseRegexpOperation::parseCypherPattern(value.getValue())); } else { definition->execFunc = getExecFunc(input.arguments); return std::make_unique(logical_type_vec_t{}, std::move(replaceFunc)); } } function_set RegexpReplaceFunction::getFunctionSet() { function_set functionSet; std::unique_ptr func; func = std::make_unique(name, std::vector{LogicalTypeID::STRING, LogicalTypeID::STRING, LogicalTypeID::STRING, LogicalTypeID::STRING}, LogicalTypeID::STRING); func->bindFunc = bindFunc; functionSet.emplace_back(std::move(func)); func = std::make_unique(name, std::vector{LogicalTypeID::STRING, LogicalTypeID::STRING, LogicalTypeID::STRING}, LogicalTypeID::STRING); func->bindFunc = bindFunc; functionSet.emplace_back(std::move(func)); return functionSet; } } // namespace function } // namespace lbug