2020-05-06 23:21:13 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <Columns/ColumnFixedString.h>
|
|
|
|
#include <Columns/ColumnConst.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <Functions/FunctionFactory.h>
|
2023-02-20 09:10:25 +00:00
|
|
|
#include <Functions/FunctionHelpers.h>
|
2020-05-06 23:21:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Impl, typename Name>
|
|
|
|
class FunctionStringReplace : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = Name::name;
|
2023-02-20 09:10:25 +00:00
|
|
|
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionStringReplace>(); }
|
2020-05-06 23:21:13 +00:00
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 3; }
|
|
|
|
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
|
2021-04-29 14:48:26 +00:00
|
|
|
|
2020-05-06 23:21:13 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
2023-02-20 09:10:25 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
|
2020-05-06 23:21:13 +00:00
|
|
|
{
|
2023-02-20 09:10:25 +00:00
|
|
|
FunctionArgumentDescriptors args{
|
2024-03-07 16:16:13 +00:00
|
|
|
{"haystack", static_cast<FunctionArgumentDescriptor::TypeValidator>(&isStringOrFixedString), nullptr, "String or FixedString"},
|
|
|
|
{"pattern", static_cast<FunctionArgumentDescriptor::TypeValidator>(&isString), nullptr, "String"},
|
|
|
|
{"replacement", static_cast<FunctionArgumentDescriptor::TypeValidator>(&isString), nullptr, "String"}
|
2023-02-20 09:10:25 +00:00
|
|
|
};
|
2020-05-06 23:21:13 +00:00
|
|
|
|
2023-02-20 09:10:25 +00:00
|
|
|
validateFunctionArgumentTypes(*this, arguments, args);
|
2020-05-06 23:21:13 +00:00
|
|
|
|
|
|
|
return std::make_shared<DataTypeString>();
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /*input_rows_count*/) const override
|
2020-05-06 23:21:13 +00:00
|
|
|
{
|
2023-04-26 18:17:32 +00:00
|
|
|
ColumnPtr column_haystack = arguments[0].column;
|
|
|
|
column_haystack = column_haystack->convertToFullColumnIfConst();
|
|
|
|
|
2020-10-18 19:00:13 +00:00
|
|
|
const ColumnPtr column_needle = arguments[1].column;
|
|
|
|
const ColumnPtr column_replacement = arguments[2].column;
|
2020-05-06 23:21:13 +00:00
|
|
|
|
2023-02-20 09:10:25 +00:00
|
|
|
const ColumnString * col_haystack = checkAndGetColumn<ColumnString>(column_haystack.get());
|
|
|
|
const ColumnFixedString * col_haystack_fixed = checkAndGetColumn<ColumnFixedString>(column_haystack.get());
|
2020-05-06 23:21:13 +00:00
|
|
|
|
2023-02-20 09:10:25 +00:00
|
|
|
const ColumnString * col_needle_vector = checkAndGetColumn<ColumnString>(column_needle.get());
|
|
|
|
const ColumnConst * col_needle_const = checkAndGetColumn<ColumnConst>(column_needle.get());
|
2020-05-06 23:21:13 +00:00
|
|
|
|
2023-02-20 09:10:25 +00:00
|
|
|
const ColumnString * col_replacement_vector = checkAndGetColumn<ColumnString>(column_replacement.get());
|
|
|
|
const ColumnConst * col_replacement_const = checkAndGetColumn<ColumnConst>(column_replacement.get());
|
|
|
|
|
|
|
|
auto col_res = ColumnString::create();
|
2020-05-06 23:21:13 +00:00
|
|
|
|
2023-02-20 09:10:25 +00:00
|
|
|
if (col_haystack && col_needle_const && col_replacement_const)
|
|
|
|
{
|
|
|
|
Impl::vectorConstantConstant(
|
|
|
|
col_haystack->getChars(), col_haystack->getOffsets(),
|
|
|
|
col_needle_const->getValue<String>(),
|
|
|
|
col_replacement_const->getValue<String>(),
|
|
|
|
col_res->getChars(), col_res->getOffsets());
|
|
|
|
return col_res;
|
|
|
|
}
|
|
|
|
else if (col_haystack && col_needle_vector && col_replacement_const)
|
|
|
|
{
|
|
|
|
Impl::vectorVectorConstant(
|
|
|
|
col_haystack->getChars(), col_haystack->getOffsets(),
|
|
|
|
col_needle_vector->getChars(), col_needle_vector->getOffsets(),
|
|
|
|
col_replacement_const->getValue<String>(),
|
|
|
|
col_res->getChars(), col_res->getOffsets());
|
|
|
|
return col_res;
|
|
|
|
}
|
|
|
|
else if (col_haystack && col_needle_const && col_replacement_vector)
|
|
|
|
{
|
|
|
|
Impl::vectorConstantVector(
|
|
|
|
col_haystack->getChars(), col_haystack->getOffsets(),
|
|
|
|
col_needle_const->getValue<String>(),
|
|
|
|
col_replacement_vector->getChars(), col_replacement_vector->getOffsets(),
|
|
|
|
col_res->getChars(), col_res->getOffsets());
|
|
|
|
return col_res;
|
|
|
|
}
|
|
|
|
else if (col_haystack && col_needle_vector && col_replacement_vector)
|
2020-05-06 23:21:13 +00:00
|
|
|
{
|
2023-02-20 09:10:25 +00:00
|
|
|
Impl::vectorVectorVector(
|
|
|
|
col_haystack->getChars(), col_haystack->getOffsets(),
|
|
|
|
col_needle_vector->getChars(), col_needle_vector->getOffsets(),
|
|
|
|
col_replacement_vector->getChars(), col_replacement_vector->getOffsets(),
|
|
|
|
col_res->getChars(), col_res->getOffsets());
|
2020-10-18 19:00:13 +00:00
|
|
|
return col_res;
|
2020-05-06 23:21:13 +00:00
|
|
|
}
|
2023-02-20 09:10:25 +00:00
|
|
|
else if (col_haystack_fixed && col_needle_const && col_replacement_const)
|
2020-05-06 23:21:13 +00:00
|
|
|
{
|
2023-02-20 09:10:25 +00:00
|
|
|
Impl::vectorFixedConstantConstant(
|
|
|
|
col_haystack_fixed->getChars(), col_haystack_fixed->getN(),
|
|
|
|
col_needle_const->getValue<String>(),
|
|
|
|
col_replacement_const->getValue<String>(),
|
|
|
|
col_res->getChars(), col_res->getOffsets());
|
2020-10-18 19:00:13 +00:00
|
|
|
return col_res;
|
2020-05-06 23:21:13 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
throw Exception(
|
2022-10-24 08:41:29 +00:00
|
|
|
ErrorCodes::ILLEGAL_COLUMN,
|
|
|
|
"Illegal column {} of first argument of function {}",
|
|
|
|
arguments[0].column->getName(), getName());
|
2020-05-06 23:21:13 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|