ClickHouse/src/Functions/FunctionsMultiStringFuzzySearch.h

152 lines
6.0 KiB
C++
Raw Normal View History

2019-03-29 01:02:05 +00:00
#pragma once
#include <Columns/ColumnArray.h>
#include <Columns/ColumnConst.h>
#include <Columns/ColumnString.h>
#include <Columns/ColumnVector.h>
#include <Columns/ColumnsNumber.h>
#include <DataTypes/DataTypeArray.h>
#include <DataTypes/DataTypeString.h>
#include <DataTypes/DataTypesNumber.h>
#include <Functions/FunctionHelpers.h>
2021-05-17 07:30:42 +00:00
#include <Functions/IFunction.h>
#include <Functions/hyperscanRegexpChecker.h>
2019-03-29 01:02:05 +00:00
#include <IO/WriteHelpers.h>
#include <Interpreters/Context.h>
2021-10-02 07:13:14 +00:00
#include <base/StringRef.h>
2019-03-29 01:02:05 +00:00
#include <optional>
namespace DB
{
namespace ErrorCodes
{
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
extern const int ILLEGAL_COLUMN;
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
extern const int FUNCTION_NOT_ALLOWED;
}
template <typename Impl, size_t LimitArgs>
class FunctionsMultiStringFuzzySearch : public IFunction
2019-03-29 01:02:05 +00:00
{
static_assert(LimitArgs > 0);
public:
static constexpr auto name = Impl::name;
2021-06-01 12:20:52 +00:00
static FunctionPtr create(ContextPtr context)
2019-03-29 01:02:05 +00:00
{
if (Impl::is_using_hyperscan && !context->getSettingsRef().allow_hyperscan)
2019-03-29 01:02:05 +00:00
throw Exception(
"Hyperscan functions are disabled, because setting 'allow_hyperscan' is set to 0", ErrorCodes::FUNCTION_NOT_ALLOWED);
return std::make_shared<FunctionsMultiStringFuzzySearch>(
context->getSettingsRef().max_hyperscan_regexp_length, context->getSettingsRef().max_hyperscan_regexp_total_length);
}
FunctionsMultiStringFuzzySearch(size_t max_hyperscan_regexp_length_, size_t max_hyperscan_regexp_total_length_)
: max_hyperscan_regexp_length(max_hyperscan_regexp_length_), max_hyperscan_regexp_total_length(max_hyperscan_regexp_total_length_)
{
2019-03-29 01:02:05 +00:00
}
String getName() const override { return name; }
size_t getNumberOfArguments() const override { return 3; }
bool useDefaultImplementationForConstants() const override { return true; }
2021-06-22 16:21:23 +00:00
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
2019-03-29 01:02:05 +00:00
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {1, 2}; }
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
{
if (!isString(arguments[0]))
throw Exception(
"Illegal type " + arguments[0]->getName() + " of argument of function " + getName(), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
if (!isUnsignedInteger(arguments[1]))
throw Exception(
"Illegal type " + arguments[1]->getName() + " of argument of function " + getName(), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
const DataTypeArray * array_type = checkAndGetDataType<DataTypeArray>(arguments[2].get());
if (!array_type || !checkAndGetDataType<DataTypeString>(array_type->getNestedType().get()))
throw Exception(
"Illegal type " + arguments[2]->getName() + " of argument of function " + getName(), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
return Impl::getReturnType();
2019-03-29 01:02:05 +00:00
}
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /*input_rows_count*/) const override
2019-03-29 01:02:05 +00:00
{
using ResultType = typename Impl::ResultType;
2020-10-18 19:00:13 +00:00
const ColumnPtr & column_haystack = arguments[0].column;
2019-03-29 01:02:05 +00:00
const ColumnString * col_haystack_vector = checkAndGetColumn<ColumnString>(&*column_haystack);
2020-10-18 19:00:13 +00:00
const ColumnPtr & num_ptr = arguments[1].column;
2019-03-29 01:02:05 +00:00
const ColumnConst * col_const_num = nullptr;
UInt32 edit_distance = 0;
if ((col_const_num = checkAndGetColumnConst<ColumnUInt8>(num_ptr.get())))
edit_distance = col_const_num->getValue<UInt8>();
else if ((col_const_num = checkAndGetColumnConst<ColumnUInt16>(num_ptr.get())))
edit_distance = col_const_num->getValue<UInt16>();
else if ((col_const_num = checkAndGetColumnConst<ColumnUInt32>(num_ptr.get())))
edit_distance = col_const_num->getValue<UInt32>();
else
throw Exception(
2020-10-18 19:00:13 +00:00
"Illegal column " + arguments[1].column->getName()
2019-03-29 01:02:05 +00:00
+ ". The number is not const or does not fit in UInt32",
ErrorCodes::ILLEGAL_COLUMN);
2020-10-18 19:00:13 +00:00
const ColumnPtr & arr_ptr = arguments[2].column;
2019-03-29 01:02:05 +00:00
const ColumnConst * col_const_arr = checkAndGetColumnConst<ColumnArray>(arr_ptr.get());
if (!col_const_arr)
throw Exception(
2020-10-18 19:00:13 +00:00
"Illegal column " + arguments[2].column->getName() + ". The array is not const",
2019-03-29 01:02:05 +00:00
ErrorCodes::ILLEGAL_COLUMN);
Array src_arr = col_const_arr->getValue<Array>();
if (src_arr.size() > LimitArgs)
throw Exception(
"Number of arguments for function " + getName() + " doesn't match: passed " + std::to_string(src_arr.size())
+ ", should be at most " + std::to_string(LimitArgs),
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
std::vector<StringRef> refs;
refs.reserve(src_arr.size());
for (const auto & el : src_arr)
refs.emplace_back(el.get<String>());
if (Impl::is_using_hyperscan)
checkRegexp(refs, max_hyperscan_regexp_length, max_hyperscan_regexp_total_length);
2019-03-29 01:02:05 +00:00
auto col_res = ColumnVector<ResultType>::create();
auto col_offsets = ColumnArray::ColumnOffsets::create();
2019-03-29 01:02:05 +00:00
auto & vec_res = col_res->getData();
auto & offsets_res = col_offsets->getData();
2019-03-29 01:02:05 +00:00
/// The blame for resizing output is for the callee.
2019-03-29 01:02:05 +00:00
if (col_haystack_vector)
Impl::vectorConstant(
col_haystack_vector->getChars(), col_haystack_vector->getOffsets(), refs, vec_res, offsets_res, edit_distance);
2019-03-29 01:02:05 +00:00
else
2020-10-18 19:00:13 +00:00
throw Exception("Illegal column " + arguments[0].column->getName(), ErrorCodes::ILLEGAL_COLUMN);
2019-03-29 01:02:05 +00:00
if constexpr (Impl::is_column_array)
2020-10-18 19:00:13 +00:00
return ColumnArray::create(std::move(col_res), std::move(col_offsets));
else
2020-10-18 19:00:13 +00:00
return col_res;
2019-03-29 01:02:05 +00:00
}
private:
size_t max_hyperscan_regexp_length;
size_t max_hyperscan_regexp_total_length;
2019-03-29 01:02:05 +00:00
};
}