ClickHouse/src/Functions/FunctionStartsEndsWith.h

167 lines
6.6 KiB
C++
Raw Normal View History

#include <Functions/IFunctionImpl.h>
2018-09-09 23:36:06 +00:00
#include <Functions/FunctionHelpers.h>
#include <Functions/GatherUtils/GatherUtils.h>
2018-09-09 23:47:56 +00:00
#include <Functions/GatherUtils/Sources.h>
2018-09-09 23:36:06 +00:00
#include <DataTypes/DataTypeString.h>
2018-09-09 23:47:56 +00:00
#include <DataTypes/DataTypesNumber.h>
2018-09-09 23:36:06 +00:00
#include <Columns/ColumnString.h>
2020-04-02 13:48:14 +00:00
#include <Functions/DynamicTarget/Target.h>
#include <Functions/DynamicTarget/Selector.h>
2018-09-09 23:36:06 +00:00
namespace DB
{
using namespace GatherUtils;
namespace ErrorCodes
{
extern const int ILLEGAL_COLUMN;
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
}
struct NameStartsWith
{
static constexpr auto name = "startsWith";
};
struct NameEndsWith
{
static constexpr auto name = "endsWith";
};
2020-04-02 13:48:14 +00:00
using DynamicTarget::TargetArch;
2018-09-09 23:36:06 +00:00
2020-04-02 13:48:14 +00:00
DECLARE_MULTITARGET_CODE(
2018-09-09 23:36:06 +00:00
2020-04-02 13:48:14 +00:00
template <typename Name>
class FunctionStartsEndsWithImpl {
public:
static void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t input_rows_count)
2018-09-09 23:36:06 +00:00
{
const IColumn * haystack_column = block.getByPosition(arguments[0]).column.get();
const IColumn * needle_column = block.getByPosition(arguments[1]).column.get();
auto col_res = ColumnVector<UInt8>::create();
typename ColumnVector<UInt8>::Container & vec_res = col_res->getData();
vec_res.resize(input_rows_count);
if (const ColumnString * haystack = checkAndGetColumn<ColumnString>(haystack_column))
dispatch<StringSource>(StringSource(*haystack), needle_column, vec_res);
else if (const ColumnFixedString * haystack_fixed = checkAndGetColumn<ColumnFixedString>(haystack_column))
dispatch<FixedStringSource>(FixedStringSource(*haystack_fixed), needle_column, vec_res);
else if (const ColumnConst * haystack_const = checkAndGetColumnConst<ColumnString>(haystack_column))
dispatch<ConstSource<StringSource>>(ConstSource<StringSource>(*haystack_const), needle_column, vec_res);
else if (const ColumnConst * haystack_const_fixed = checkAndGetColumnConst<ColumnFixedString>(haystack_column))
dispatch<ConstSource<FixedStringSource>>(ConstSource<FixedStringSource>(*haystack_const_fixed), needle_column, vec_res);
2018-09-09 23:36:06 +00:00
else
2020-04-02 13:48:14 +00:00
throw Exception("Illegal combination of columns as arguments of function " "getName()", ErrorCodes::ILLEGAL_COLUMN);
2018-09-09 23:36:06 +00:00
block.getByPosition(result).column = std::move(col_res);
}
private:
template <typename HaystackSource>
2020-04-02 13:48:14 +00:00
static void dispatch(HaystackSource haystack_source, const IColumn * needle_column, PaddedPODArray<UInt8> & res_data)
2018-09-09 23:36:06 +00:00
{
if (const ColumnString * needle = checkAndGetColumn<ColumnString>(needle_column))
execute<HaystackSource, StringSource>(haystack_source, StringSource(*needle), res_data);
else if (const ColumnFixedString * needle_fixed = checkAndGetColumn<ColumnFixedString>(needle_column))
execute<HaystackSource, FixedStringSource>(haystack_source, FixedStringSource(*needle_fixed), res_data);
else if (const ColumnConst * needle_const = checkAndGetColumnConst<ColumnString>(needle_column))
execute<HaystackSource, ConstSource<StringSource>>(haystack_source, ConstSource<StringSource>(*needle_const), res_data);
else if (const ColumnConst * needle_const_fixed = checkAndGetColumnConst<ColumnFixedString>(needle_column))
execute<HaystackSource, ConstSource<FixedStringSource>>(haystack_source, ConstSource<FixedStringSource>(*needle_const_fixed), res_data);
2018-09-09 23:36:06 +00:00
else
2020-04-02 13:48:14 +00:00
throw Exception("Illegal combination of columns as arguments of function " "getName()", ErrorCodes::ILLEGAL_COLUMN);
2018-09-09 23:36:06 +00:00
}
template <typename HaystackSource, typename NeedleSource>
static void execute(HaystackSource haystack_source, NeedleSource needle_source, PaddedPODArray<UInt8> & res_data)
{
size_t row_num = 0;
while (!haystack_source.isEnd())
{
auto haystack = haystack_source.getWhole();
auto needle = needle_source.getWhole();
if (needle.size > haystack.size)
{
res_data[row_num] = false;
}
else
{
if constexpr (std::is_same_v<Name, NameStartsWith>)
{
res_data[row_num] = StringRef(haystack.data, needle.size) == StringRef(needle.data, needle.size);
}
else /// endsWith
{
res_data[row_num] = StringRef(haystack.data + haystack.size - needle.size, needle.size) == StringRef(needle.data, needle.size);
}
}
haystack_source.next();
needle_source.next();
++row_num;
}
}
};
2020-04-02 13:48:14 +00:00
) // DECLARE_MULTITARGET_CODE
template <typename Name>
class FunctionStartsEndsWith : public IFunction
{
public:
static constexpr auto name = Name::name;
static FunctionPtr create(const Context &)
{
return std::make_shared<FunctionStartsEndsWith>();
}
FunctionStartsEndsWith() {
executor_.registerExecutor(std::nullopt, TargetSpecific::Default::FunctionStartsEndsWithImpl<Name>::executeImpl);
executor_.registerExecutor(TargetArch::SSE4, TargetSpecific::SSE4::FunctionStartsEndsWithImpl<Name>::executeImpl);
executor_.registerExecutor(TargetArch::AVX, TargetSpecific::AVX::FunctionStartsEndsWithImpl<Name>::executeImpl);
executor_.registerExecutor(TargetArch::AVX2, TargetSpecific::AVX2::FunctionStartsEndsWithImpl<Name>::executeImpl);
executor_.registerExecutor(TargetArch::AVX512, TargetSpecific::AVX512::FunctionStartsEndsWithImpl<Name>::executeImpl);
}
String getName() const override
{
return name;
}
size_t getNumberOfArguments() const override
{
return 2;
}
bool useDefaultImplementationForConstants() const override
{
return true;
}
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
{
if (!isStringOrFixedString(arguments[0]))
throw Exception("Illegal type " + arguments[0]->getName() + " of argument of function " + getName(), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
if (!isStringOrFixedString(arguments[1]))
throw Exception("Illegal type " + arguments[1]->getName() + " of argument of function " + getName(), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
return std::make_shared<DataTypeUInt8>();
}
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t input_rows_count) override
{
executor_.execute(block, arguments, result, input_rows_count);
}
private:
DynamicTarget::SelectorExecutor<Block &, const ColumnNumbers &, size_t, size_t> executor_;
};
2018-09-09 23:36:06 +00:00
}