#include "function/string/vector_string_functions.h" #include "common/exception/runtime.h" #include "function/string/functions/array_extract_function.h" #include "function/string/functions/contains_function.h" #include "function/string/functions/ends_with_function.h" #include "function/string/functions/left_operation.h" #include "function/string/functions/lpad_function.h" #include "function/string/functions/regexp_extract_all_function.h" #include "function/string/functions/regexp_extract_function.h" #include "function/string/functions/regexp_matches_function.h" #include "function/string/functions/regexp_split_to_array_function.h" #include "function/string/functions/repeat_function.h" #include "function/string/functions/right_function.h" #include "function/string/functions/rpad_function.h" #include "function/string/functions/starts_with_function.h" #include "function/string/functions/substr_function.h" #include using namespace lbug::common; namespace lbug { namespace function { void BaseLowerUpperFunction::operation(string_t& input, string_t& result, ValueVector& resultValueVector, bool isUpper) { uint32_t resultLen = getResultLen((char*)input.getData(), input.len, isUpper); result.len = resultLen; if (resultLen <= string_t::SHORT_STR_LENGTH) { convertCase((char*)result.prefix, input.len, (char*)input.getData(), isUpper); } else { StringVector::reserveString(&resultValueVector, result, resultLen); auto buffer = reinterpret_cast(result.overflowPtr); convertCase(buffer, input.len, (char*)input.getData(), isUpper); memcpy(result.prefix, buffer, string_t::PREFIX_LENGTH); } } void BaseStrOperation::operation(string_t& input, string_t& result, ValueVector& resultValueVector, uint32_t (*strOperation)(char* data, uint32_t len)) { if (input.len <= string_t::SHORT_STR_LENGTH) { memcpy(result.prefix, input.prefix, input.len); result.len = strOperation((char*)result.prefix, input.len); } else { StringVector::reserveString(&resultValueVector, result, input.len); auto buffer = reinterpret_cast(result.overflowPtr); memcpy(buffer, input.getData(), input.len); result.len = strOperation(buffer, input.len); memcpy(result.prefix, buffer, result.len < string_t::PREFIX_LENGTH ? result.len : string_t::PREFIX_LENGTH); } } [[noreturn]] static void throwRepeatTooLong() { throw RuntimeException( std::format("repeat(): the result would exceed the maximum string size of {} bytes.", string_t::MAX_LENGTH)); } void Repeat::operation(string_t& left, int64_t& right, string_t& result, ValueVector& resultValueVector) { if (right <= 0 || left.len == 0) { result.len = 0; return; } const auto count = static_cast(right); // The count is checked first, so that the product cannot overflow. if (count > string_t::MAX_LENGTH || left.len * count > string_t::MAX_LENGTH) { throwRepeatTooLong(); } result.len = static_cast(left.len * count); if (result.len <= string_t::SHORT_STR_LENGTH) { repeatStr((char*)result.prefix, left.getAsString(), count); } else { StringVector::reserveString(&resultValueVector, result, result.len); auto buffer = reinterpret_cast(result.overflowPtr); repeatStr(buffer, left.getAsString(), count); memcpy(result.prefix, buffer, string_t::PREFIX_LENGTH); } } void Reverse::operation(string_t& input, string_t& result, ValueVector& resultValueVector) { bool isAscii = true; std::string inputStr = input.getAsString(); for (uint32_t i = 0; i < input.len; i++) { if (inputStr[i] & 0x80) { isAscii = false; break; } } if (isAscii) { BaseStrOperation::operation(input, result, resultValueVector, reverseStr); } else { result.len = input.len; if (result.len > string_t::SHORT_STR_LENGTH) { StringVector::reserveString(&resultValueVector, result, input.len); } auto resultBuffer = result.len <= string_t::SHORT_STR_LENGTH ? reinterpret_cast(result.prefix) : reinterpret_cast(result.overflowPtr); utf8proc::utf8proc_grapheme_callback(inputStr.c_str(), input.len, [&](size_t start, size_t end) { memcpy(resultBuffer + input.len - end, input.getData() + start, end - start); return true; }); if (result.len > string_t::SHORT_STR_LENGTH) { memcpy(result.prefix, resultBuffer, string_t::PREFIX_LENGTH); } } } function_set ArrayExtractFunction::getFunctionSet() { function_set functionSet; functionSet.emplace_back(make_unique(name, std::vector{LogicalTypeID::STRING, LogicalTypeID::INT64}, LogicalTypeID::STRING, ScalarFunction::BinaryStringExecFunction)); return functionSet; } void ConcatFunction::execFunc(const std::vector>& parameters, const std::vector& parameterSelVectors, common::ValueVector& result, common::SelectionVector* resultSelVector, void* /*dataPtr*/) { result.resetAuxiliaryBuffer(); for (auto selectedPos = 0u; selectedPos < resultSelVector->getSelSize(); ++selectedPos) { auto pos = (*resultSelVector)[selectedPos]; auto strLen = 0u; for (auto i = 0u; i < parameters.size(); i++) { const auto& parameter = *parameters[i]; const auto& parameterSelVector = parameterSelVectors[i]; auto paramPos = (*parameterSelVector)[parameter.state->isFlat() ? 0 : selectedPos]; if (!parameter.isNull(paramPos)) { strLen += parameter.getValue(paramPos).len; } } auto& resultStr = result.getValue(pos); StringVector::reserveString(&result, resultStr, strLen); auto dstData = strLen <= string_t::SHORT_STR_LENGTH ? resultStr.prefix : reinterpret_cast(resultStr.overflowPtr); for (auto i = 0u; i < parameters.size(); i++) { const auto& parameter = *parameters[i]; const auto& parameterSelVector = parameterSelVectors[i]; auto paramPos = (*parameterSelVector)[parameter.state->isFlat() ? 0 : selectedPos]; if (!parameter.isNull(paramPos)) { auto srcStr = parameter.getValue(paramPos); memcpy(dstData, srcStr.getData(), srcStr.len); dstData += srcStr.len; } } if (strLen > string_t::SHORT_STR_LENGTH) { memcpy(resultStr.prefix, resultStr.getData(), string_t::PREFIX_LENGTH); } } } function_set ConcatFunction::getFunctionSet() { function_set functionSet; auto function = std::make_unique(name, std::vector{LogicalTypeID::STRING}, LogicalTypeID::STRING, execFunc); function->isVarLength = true; functionSet.emplace_back(std::move(function)); return functionSet; } function_set ContainsFunction::getFunctionSet() { function_set functionSet; functionSet.emplace_back(make_unique(name, std::vector{LogicalTypeID::STRING, LogicalTypeID::STRING}, LogicalTypeID::BOOL, ScalarFunction::BinaryExecFunction, ScalarFunction::BinarySelectFunction)); return functionSet; } function_set EndsWithFunction::getFunctionSet() { function_set functionSet; functionSet.emplace_back(make_unique(name, std::vector{LogicalTypeID::STRING, LogicalTypeID::STRING}, LogicalTypeID::BOOL, ScalarFunction::BinaryExecFunction, ScalarFunction::BinarySelectFunction)); return functionSet; } function_set LeftFunction::getFunctionSet() { function_set functionSet; functionSet.emplace_back(make_unique(name, std::vector{LogicalTypeID::STRING, LogicalTypeID::INT64}, LogicalTypeID::STRING, ScalarFunction::BinaryStringExecFunction)); return functionSet; } function_set LpadFunction::getFunctionSet() { function_set functionSet; functionSet.emplace_back(make_unique(name, std::vector{LogicalTypeID::STRING, LogicalTypeID::INT64, LogicalTypeID::STRING}, LogicalTypeID::STRING, ScalarFunction::TernaryStringExecFunction)); return functionSet; } function_set RepeatFunction::getFunctionSet() { function_set functionSet; functionSet.emplace_back(make_unique(name, std::vector{LogicalTypeID::STRING, LogicalTypeID::INT64}, LogicalTypeID::STRING, ScalarFunction::BinaryStringExecFunction)); return functionSet; } struct Replace { static void operation(string_t& input, string_t& source, string_t& target, string_t& result, ValueVector& resultValueVector) { if (source.len == 0) { StringVector::addString(&resultValueVector, result, input); return; } auto inputStr = input.getAsString(); auto sourceStr = source.getAsString(); auto targetStr = target.getAsString(); std::string resultStr; resultStr.reserve(inputStr.length()); size_t start = 0; while (true) { auto pos = inputStr.find(sourceStr, start); if (pos == std::string::npos) { resultStr.append(inputStr, start, std::string::npos); break; } resultStr.append(inputStr, start, pos - start); resultStr.append(targetStr); start = pos + sourceStr.length(); } StringVector::addString(&resultValueVector, result, resultStr.data(), resultStr.length()); } }; function_set ReplaceFunction::getFunctionSet() { function_set functionSet; functionSet.emplace_back(make_unique(name, std::vector{LogicalTypeID::STRING, LogicalTypeID::STRING, LogicalTypeID::STRING}, LogicalTypeID::STRING, ScalarFunction::TernaryStringExecFunction)); return functionSet; } function_set RightFunction::getFunctionSet() { function_set functionSet; functionSet.emplace_back(make_unique(name, std::vector{LogicalTypeID::STRING, LogicalTypeID::INT64}, LogicalTypeID::STRING, ScalarFunction::BinaryStringExecFunction)); return functionSet; } function_set RpadFunction::getFunctionSet() { function_set functionSet; functionSet.emplace_back(make_unique(name, std::vector{LogicalTypeID::STRING, LogicalTypeID::INT64, LogicalTypeID::STRING}, LogicalTypeID::STRING, ScalarFunction::TernaryStringExecFunction)); return functionSet; } function_set StartsWithFunction::getFunctionSet() { function_set functionSet; functionSet.emplace_back(make_unique(name, std::vector{LogicalTypeID::STRING, LogicalTypeID::STRING}, LogicalTypeID::BOOL, ScalarFunction::BinaryExecFunction, ScalarFunction::BinarySelectFunction)); return functionSet; } function_set SubStrFunction::getFunctionSet() { function_set functionSet; functionSet.emplace_back(make_unique(name, std::vector{LogicalTypeID::STRING, LogicalTypeID::INT64, LogicalTypeID::INT64}, LogicalTypeID::STRING, ScalarFunction::TernaryStringExecFunction)); return functionSet; } template static std::unique_ptr regexpBindFunc(const ScalarBindFuncInput& /*input*/) { return std::make_unique(logical_type_vec_t{}, LogicalType(RESULT_TYPE_ID)); } static std::unique_ptr regexpListBindFunc(const ScalarBindFuncInput& /*input*/) { return std::make_unique(logical_type_vec_t{}, LogicalType::LIST(LogicalType::STRING())); } function_set RegexpMatchesFunction::getFunctionSet() { function_set functionSet; auto func = make_unique(name, std::vector{LogicalTypeID::STRING, LogicalTypeID::STRING}, LogicalTypeID::BOOL, ScalarFunction::BinaryExecWithBindData, ScalarFunction::BinarySelectWithBindData); func->bindFunc = regexpBindFunc; functionSet.emplace_back(std::move(func)); return functionSet; } function_set RegexpExtractFunction::getFunctionSet() { function_set functionSet; auto func = make_unique(name, std::vector{LogicalTypeID::STRING, LogicalTypeID::STRING}, LogicalTypeID::STRING, ScalarFunction::BinaryExecWithBindData); func->bindFunc = regexpBindFunc; functionSet.emplace_back(std::move(func)); func = make_unique(name, std::vector{LogicalTypeID::STRING, LogicalTypeID::STRING, LogicalTypeID::INT64}, LogicalTypeID::STRING, ScalarFunction::TernaryRegexExecFunction); func->bindFunc = regexpBindFunc; functionSet.emplace_back(std::move(func)); return functionSet; } function_set RegexpExtractAllFunction::getFunctionSet() { function_set functionSet; std::unique_ptr func; func = std::make_unique(name, std::vector{LogicalTypeID::STRING, LogicalTypeID::STRING}, LogicalTypeID::LIST, ScalarFunction::BinaryExecWithBindData); func->bindFunc = regexpListBindFunc; functionSet.emplace_back(std::move(func)); func = std::make_unique(name, std::vector{LogicalTypeID::STRING, LogicalTypeID::STRING, LogicalTypeID::INT64}, LogicalTypeID::LIST, ScalarFunction::TernaryRegexExecFunction); func->bindFunc = regexpListBindFunc; functionSet.emplace_back(std::move(func)); return functionSet; } function_set RegexpSplitToArrayFunction::getFunctionSet() { function_set functionSet; auto func = std::make_unique(name, std::vector{LogicalTypeID::STRING, LogicalTypeID::STRING}, LogicalTypeID::LIST, ScalarFunction::BinaryExecWithBindData); func->bindFunc = regexpListBindFunc; functionSet.emplace_back(std::move(func)); return functionSet; } } // namespace function } // namespace lbug