2020-03-29 17:04:16 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2022-06-26 16:25:43 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
}
|
|
|
|
|
2021-09-21 16:43:46 +00:00
|
|
|
template <typename Name, typename Impl>
|
2020-03-29 17:04:16 +00:00
|
|
|
struct MultiSearchFirstPositionImpl
|
|
|
|
{
|
|
|
|
using ResultType = UInt64;
|
|
|
|
/// Variable for understanding, if we used offsets for the output, most
|
|
|
|
/// likely to determine whether the function returns ColumnVector of ColumnArray.
|
|
|
|
static constexpr bool is_column_array = false;
|
2021-09-21 16:43:46 +00:00
|
|
|
static constexpr auto name = Name::name;
|
|
|
|
|
2020-03-29 17:04:16 +00:00
|
|
|
static auto getReturnType() { return std::make_shared<DataTypeNumber<ResultType>>(); }
|
|
|
|
|
|
|
|
static void vectorConstant(
|
|
|
|
const ColumnString::Chars & haystack_data,
|
|
|
|
const ColumnString::Offsets & haystack_offsets,
|
2022-06-24 13:16:57 +00:00
|
|
|
const std::vector<std::string_view> & needles,
|
2020-03-29 17:04:16 +00:00
|
|
|
PaddedPODArray<UInt64> & res,
|
2022-06-24 14:12:38 +00:00
|
|
|
[[maybe_unused]] PaddedPODArray<UInt64> & offsets,
|
2022-06-25 15:53:11 +00:00
|
|
|
bool /*allow_hyperscan*/,
|
2022-06-24 14:12:38 +00:00
|
|
|
size_t /*max_hyperscan_regexp_length*/,
|
|
|
|
size_t /*max_hyperscan_regexp_total_length*/)
|
2020-03-29 17:04:16 +00:00
|
|
|
{
|
2022-06-26 16:25:43 +00:00
|
|
|
// For performance of Volnitsky search, it is crucial to save only one byte for pattern number.
|
|
|
|
if (needles.size() > std::numeric_limits<UInt8>::max())
|
|
|
|
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH,
|
|
|
|
"Number of arguments for function {} doesn't match: passed {}, should be at most {}",
|
|
|
|
name, std::to_string(needles.size()), std::to_string(std::numeric_limits<UInt8>::max()));
|
|
|
|
|
2020-03-29 17:04:16 +00:00
|
|
|
auto res_callback = [](const UInt8 * start, const UInt8 * end) -> UInt64
|
|
|
|
{
|
|
|
|
return 1 + Impl::countChars(reinterpret_cast<const char *>(start), reinterpret_cast<const char *>(end));
|
|
|
|
};
|
|
|
|
auto searcher = Impl::createMultiSearcherInBigHaystack(needles);
|
|
|
|
const size_t haystack_string_size = haystack_offsets.size();
|
2021-08-05 08:36:35 +00:00
|
|
|
res.resize(haystack_string_size);
|
2020-03-29 17:04:16 +00:00
|
|
|
size_t iteration = 0;
|
|
|
|
while (searcher.hasMoreToSearch())
|
|
|
|
{
|
|
|
|
size_t prev_offset = 0;
|
|
|
|
for (size_t j = 0; j < haystack_string_size; ++j)
|
|
|
|
{
|
|
|
|
const auto * haystack = &haystack_data[prev_offset];
|
|
|
|
const auto * haystack_end = haystack + haystack_offsets[j] - prev_offset - 1;
|
|
|
|
if (iteration == 0 || res[j] == 0)
|
|
|
|
res[j] = searcher.searchOneFirstPosition(haystack, haystack_end, res_callback);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
UInt64 result = searcher.searchOneFirstPosition(haystack, haystack_end, res_callback);
|
|
|
|
if (result != 0)
|
|
|
|
res[j] = std::min(result, res[j]);
|
|
|
|
}
|
|
|
|
prev_offset = haystack_offsets[j];
|
|
|
|
}
|
|
|
|
++iteration;
|
|
|
|
}
|
2021-08-05 08:36:35 +00:00
|
|
|
if (iteration == 0)
|
|
|
|
std::fill(res.begin(), res.end(), 0);
|
2020-03-29 17:04:16 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|