ClickHouse/src/Functions/appendTrailingCharIfAbsent.cpp

124 lines
4.0 KiB
C++
Raw Normal View History

2018-09-09 23:36:06 +00:00
#include <Columns/ColumnString.h>
#include <Common/assert_cast.h>
2018-09-09 23:36:06 +00:00
#include <DataTypes/DataTypeString.h>
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionHelpers.h>
2021-05-17 07:30:42 +00:00
#include <Functions/IFunction.h>
2021-10-02 07:13:14 +00:00
#include <base/range.h>
2018-09-09 23:36:06 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int ILLEGAL_COLUMN;
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
extern const int BAD_ARGUMENTS;
}
2020-09-07 18:00:37 +00:00
namespace
{
2018-09-09 23:36:06 +00:00
class FunctionAppendTrailingCharIfAbsent : public IFunction
{
public:
static constexpr auto name = "appendTrailingCharIfAbsent";
2021-06-01 12:20:52 +00:00
static FunctionPtr create(ContextPtr)
2018-09-09 23:36:06 +00:00
{
return std::make_shared<FunctionAppendTrailingCharIfAbsent>();
}
String getName() const override
{
return name;
}
2021-06-22 16:21:23 +00:00
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
2018-09-09 23:36:06 +00:00
private:
size_t getNumberOfArguments() const override
{
return 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 (!isString(arguments[1]))
throw Exception{"Illegal type " + arguments[1]->getName() + " of argument of function " + getName(), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
return std::make_shared<DataTypeString>();
}
bool useDefaultImplementationForConstants() const override { return true; }
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {1}; }
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /*input_rows_count*/) const override
2018-09-09 23:36:06 +00:00
{
2020-10-17 15:22:42 +00:00
const auto & column = arguments[0].column;
const auto & column_char = arguments[1].column;
2018-09-09 23:36:06 +00:00
if (!checkColumnConst<ColumnString>(column_char.get()))
throw Exception{"Second argument of function " + getName() + " must be a constant string", ErrorCodes::ILLEGAL_COLUMN};
String trailing_char_str = assert_cast<const ColumnConst &>(*column_char).getValue<String>();
2018-09-09 23:36:06 +00:00
if (trailing_char_str.size() != 1)
throw Exception{"Second argument of function " + getName() + " must be a one-character string", ErrorCodes::BAD_ARGUMENTS};
2020-04-22 08:31:10 +00:00
if (const auto * col = checkAndGetColumn<ColumnString>(column.get()))
2018-09-09 23:36:06 +00:00
{
auto col_res = ColumnString::create();
const auto & src_data = col->getChars();
const auto & src_offsets = col->getOffsets();
auto & dst_data = col_res->getChars();
auto & dst_offsets = col_res->getOffsets();
const auto size = src_offsets.size();
dst_data.resize(src_data.size() + size);
dst_offsets.resize(size);
ColumnString::Offset src_offset{};
ColumnString::Offset dst_offset{};
2021-06-15 19:55:21 +00:00
for (const auto i : collections::range(0, size))
2018-09-09 23:36:06 +00:00
{
const auto src_length = src_offsets[i] - src_offset;
memcpySmallAllowReadWriteOverflow15(&dst_data[dst_offset], &src_data[src_offset], src_length);
src_offset = src_offsets[i];
dst_offset += src_length;
2020-09-18 23:04:36 +00:00
if (src_length > 1 && dst_data[dst_offset - 2] != UInt8(trailing_char_str.front()))
2018-09-09 23:36:06 +00:00
{
dst_data[dst_offset - 1] = trailing_char_str.front();
dst_data[dst_offset] = 0;
++dst_offset;
}
dst_offsets[i] = dst_offset;
}
dst_data.resize_assume_reserved(dst_offset);
2020-10-17 15:22:42 +00:00
return col_res;
2018-09-09 23:36:06 +00:00
}
else
2020-10-17 15:22:42 +00:00
throw Exception{"Illegal column " + arguments[0].column->getName() + " of argument of function " + getName(),
2018-09-09 23:36:06 +00:00
ErrorCodes::ILLEGAL_COLUMN};
}
};
2020-09-07 18:00:37 +00:00
}
2018-09-09 23:36:06 +00:00
void registerFunctionAppendTrailingCharIfAbsent(FunctionFactory & factory)
{
factory.registerFunction<FunctionAppendTrailingCharIfAbsent>();
}
}