2020-03-29 17:04:16 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Columns/ColumnArray.h>
|
|
|
|
#include <Columns/ColumnConst.h>
|
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <Columns/ColumnFixedString.h>
|
|
|
|
#include <Columns/ColumnVector.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>
|
2020-03-29 17:04:16 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2022-07-12 06:18:19 +00:00
|
|
|
/**
|
|
|
|
* multiSearchAllPositions(haystack, [pattern_1, pattern_2, ..., pattern_n]) -- find first occurrences (positions) of all the const patterns inside haystack
|
2020-03-29 17:04:16 +00:00
|
|
|
* multiSearchAllPositionsUTF8(haystack, [pattern_1, pattern_2, ..., pattern_n])
|
|
|
|
* multiSearchAllPositionsCaseInsensitive(haystack, [pattern_1, pattern_2, ..., pattern_n])
|
|
|
|
* multiSearchAllPositionsCaseInsensitiveUTF8(haystack, [pattern_1, pattern_2, ..., pattern_n])
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2022-07-14 08:02:49 +00:00
|
|
|
extern const int ILLEGAL_COLUMN;
|
2020-03-29 17:04:16 +00:00
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-07-12 07:24:12 +00:00
|
|
|
template <typename Impl>
|
2020-03-29 17:04:16 +00:00
|
|
|
class FunctionsMultiStringPosition : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
2022-07-12 07:24:12 +00:00
|
|
|
static constexpr auto name = Impl::name;
|
2020-03-29 17:04:16 +00:00
|
|
|
|
2022-07-12 06:18:19 +00:00
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionsMultiStringPosition>(); }
|
2020-03-29 17:04:16 +00:00
|
|
|
String getName() const override { return name; }
|
|
|
|
size_t getNumberOfArguments() const override { return 2; }
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
|
2020-03-29 17:04:16 +00:00
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
if (!isString(arguments[0]))
|
|
|
|
throw Exception(
|
2022-07-12 06:27:08 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"Illegal type {} of argument of function {}", arguments[0]->getName(), getName());
|
2020-03-29 17:04:16 +00:00
|
|
|
|
|
|
|
const DataTypeArray * array_type = checkAndGetDataType<DataTypeArray>(arguments[1].get());
|
|
|
|
if (!array_type || !checkAndGetDataType<DataTypeString>(array_type->getNestedType().get()))
|
|
|
|
throw Exception(
|
2022-07-12 06:27:08 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"Illegal type {} of argument of function {}", arguments[1]->getName(), getName());
|
2020-03-29 17:04:16 +00:00
|
|
|
|
|
|
|
return std::make_shared<DataTypeArray>(std::make_shared<DataTypeUInt64>());
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /*input_rows_count*/) const override
|
2020-03-29 17:04:16 +00:00
|
|
|
{
|
2022-07-12 06:23:03 +00:00
|
|
|
const ColumnPtr & haystack_ptr = arguments[0].column;
|
|
|
|
const ColumnPtr & needles_ptr = arguments[1].column;
|
2020-03-29 17:04:16 +00:00
|
|
|
|
2022-07-12 06:23:03 +00:00
|
|
|
const ColumnString * col_haystack_vector = checkAndGetColumn<ColumnString>(&*haystack_ptr);
|
2022-07-14 08:02:49 +00:00
|
|
|
const ColumnConst * col_haystack_const = checkAndGetColumnConst<ColumnString>(&*haystack_ptr);
|
|
|
|
assert(static_cast<bool>(col_haystack_vector) ^ static_cast<bool>(col_haystack_const));
|
2020-03-29 17:04:16 +00:00
|
|
|
|
2022-07-12 07:24:12 +00:00
|
|
|
const ColumnArray * col_needles_vector = checkAndGetColumn<ColumnArray>(needles_ptr.get());
|
2022-07-12 06:23:03 +00:00
|
|
|
const ColumnConst * col_needles_const = checkAndGetColumnConst<ColumnArray>(needles_ptr.get());
|
2022-07-12 07:24:12 +00:00
|
|
|
assert(static_cast<bool>(col_needles_vector) ^ static_cast<bool>(col_needles_const));
|
2020-03-29 17:04:16 +00:00
|
|
|
|
2022-07-14 08:02:49 +00:00
|
|
|
if (col_haystack_const && col_needles_vector)
|
|
|
|
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Function '{}' doesn't support search with non-constant needles in constant haystack", name);
|
|
|
|
|
2022-07-12 06:18:19 +00:00
|
|
|
using ResultType = typename Impl::ResultType;
|
2020-03-29 17:04:16 +00:00
|
|
|
auto col_res = ColumnVector<ResultType>::create();
|
2022-07-12 07:24:12 +00:00
|
|
|
auto col_offsets = ColumnArray::ColumnOffsets::create();
|
2020-03-29 17:04:16 +00:00
|
|
|
|
|
|
|
auto & vec_res = col_res->getData();
|
|
|
|
auto & offsets_res = col_offsets->getData();
|
2022-07-12 07:24:12 +00:00
|
|
|
/// the implementations are responsible for resizing the output column
|
2020-03-29 17:04:16 +00:00
|
|
|
|
2022-07-12 07:24:12 +00:00
|
|
|
if (col_needles_const)
|
|
|
|
Impl::vectorConstant(
|
|
|
|
col_haystack_vector->getChars(), col_haystack_vector->getOffsets(),
|
|
|
|
col_needles_const->getValue<Array>(),
|
|
|
|
vec_res, offsets_res);
|
2020-03-29 17:04:16 +00:00
|
|
|
else
|
2022-07-12 07:24:12 +00:00
|
|
|
Impl::vectorVector(
|
|
|
|
col_haystack_vector->getChars(), col_haystack_vector->getOffsets(),
|
|
|
|
col_needles_vector->getData(), col_needles_vector->getOffsets(),
|
|
|
|
vec_res, offsets_res);
|
2020-03-29 17:04:16 +00:00
|
|
|
|
2022-07-14 08:02:49 +00:00
|
|
|
// the combination of const haystack + const needle is not implemented because
|
|
|
|
// useDefaultImplementationForConstants() == true makes upper layers convert both to
|
|
|
|
// non-const columns
|
2020-03-29 17:04:16 +00:00
|
|
|
|
2020-10-18 19:00:13 +00:00
|
|
|
return ColumnArray::create(std::move(col_res), std::move(col_offsets));
|
2020-03-29 17:04:16 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|