2021-07-16 13:58:06 +00:00
|
|
|
#include <Columns/ColumnFixedString.h>
|
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <DataTypes/DataTypeFixedString.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
}
|
|
|
|
|
|
|
|
class FunctionToStringCutToZero : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "toStringCutToZero";
|
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionToStringCutToZero>(); }
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
|
|
|
|
2021-08-10 11:31:15 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
|
|
|
|
|
2021-07-16 13:58:06 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
if (!isStringOrFixedString(arguments[0]))
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of argument of function {}",
|
|
|
|
arguments[0]->getName(), getName());
|
2021-07-16 13:58:06 +00:00
|
|
|
|
|
|
|
return std::make_shared<DataTypeString>();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
2024-07-25 09:58:32 +00:00
|
|
|
static bool tryExecuteString(const IColumn * col, ColumnPtr & col_res, size_t input_rows_count)
|
2021-07-16 13:58:06 +00:00
|
|
|
{
|
|
|
|
const ColumnString * col_str_in = checkAndGetColumn<ColumnString>(col);
|
|
|
|
|
|
|
|
if (col_str_in)
|
|
|
|
{
|
|
|
|
auto col_str = ColumnString::create();
|
|
|
|
ColumnString::Chars & out_vec = col_str->getChars();
|
|
|
|
ColumnString::Offsets & out_offsets = col_str->getOffsets();
|
|
|
|
|
|
|
|
const ColumnString::Chars & in_vec = col_str_in->getChars();
|
|
|
|
const ColumnString::Offsets & in_offsets = col_str_in->getOffsets();
|
|
|
|
|
2024-07-25 09:58:32 +00:00
|
|
|
out_offsets.resize(input_rows_count);
|
2021-07-16 13:58:06 +00:00
|
|
|
out_vec.resize(in_vec.size());
|
|
|
|
|
|
|
|
char * begin = reinterpret_cast<char *>(out_vec.data());
|
|
|
|
char * pos = begin;
|
|
|
|
|
|
|
|
ColumnString::Offset current_in_offset = 0;
|
|
|
|
|
2024-07-25 09:58:32 +00:00
|
|
|
for (size_t i = 0; i < input_rows_count; ++i)
|
2021-07-16 13:58:06 +00:00
|
|
|
{
|
|
|
|
const char * pos_in = reinterpret_cast<const char *>(&in_vec[current_in_offset]);
|
|
|
|
size_t current_size = strlen(pos_in);
|
|
|
|
memcpySmallAllowReadWriteOverflow15(pos, pos_in, current_size);
|
|
|
|
pos += current_size;
|
|
|
|
*pos = '\0';
|
|
|
|
++pos;
|
|
|
|
out_offsets[i] = pos - begin;
|
|
|
|
current_in_offset = in_offsets[i];
|
|
|
|
}
|
|
|
|
out_vec.resize(pos - begin);
|
|
|
|
|
|
|
|
if (!out_offsets.empty() && out_offsets.back() != out_vec.size())
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Column size mismatch (internal logical error)");
|
2021-07-16 13:58:06 +00:00
|
|
|
|
|
|
|
col_res = std::move(col_str);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-25 09:58:32 +00:00
|
|
|
static bool tryExecuteFixedString(const IColumn * col, ColumnPtr & col_res, size_t input_rows_count)
|
2021-07-16 13:58:06 +00:00
|
|
|
{
|
|
|
|
const ColumnFixedString * col_fstr_in = checkAndGetColumn<ColumnFixedString>(col);
|
|
|
|
|
|
|
|
if (col_fstr_in)
|
|
|
|
{
|
|
|
|
auto col_str = ColumnString::create();
|
|
|
|
ColumnString::Chars & out_vec = col_str->getChars();
|
|
|
|
ColumnString::Offsets & out_offsets = col_str->getOffsets();
|
|
|
|
|
|
|
|
const ColumnString::Chars & in_vec = col_fstr_in->getChars();
|
|
|
|
|
2024-07-25 09:58:32 +00:00
|
|
|
out_offsets.resize(input_rows_count);
|
|
|
|
out_vec.resize(in_vec.size() + input_rows_count);
|
2021-07-16 13:58:06 +00:00
|
|
|
|
|
|
|
char * begin = reinterpret_cast<char *>(out_vec.data());
|
|
|
|
char * pos = begin;
|
|
|
|
const char * pos_in = reinterpret_cast<const char *>(in_vec.data());
|
|
|
|
|
|
|
|
size_t n = col_fstr_in->getN();
|
|
|
|
|
2024-07-25 09:58:32 +00:00
|
|
|
for (size_t i = 0; i < input_rows_count; ++i)
|
2021-07-16 13:58:06 +00:00
|
|
|
{
|
|
|
|
size_t current_size = strnlen(pos_in, n);
|
|
|
|
memcpySmallAllowReadWriteOverflow15(pos, pos_in, current_size);
|
|
|
|
pos += current_size;
|
|
|
|
*pos = '\0';
|
|
|
|
out_offsets[i] = ++pos - begin;
|
|
|
|
pos_in += n;
|
|
|
|
}
|
|
|
|
out_vec.resize(pos - begin);
|
|
|
|
|
|
|
|
if (!out_offsets.empty() && out_offsets.back() != out_vec.size())
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Column size mismatch (internal logical error)");
|
2021-07-16 13:58:06 +00:00
|
|
|
|
|
|
|
col_res = std::move(col_str);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-25 09:58:32 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
|
2021-07-16 13:58:06 +00:00
|
|
|
{
|
|
|
|
const IColumn * column = arguments[0].column.get();
|
|
|
|
ColumnPtr res_column;
|
|
|
|
|
2024-07-25 09:58:32 +00:00
|
|
|
if (tryExecuteFixedString(column, res_column, input_rows_count) || tryExecuteString(column, res_column, input_rows_count))
|
2021-07-16 13:58:06 +00:00
|
|
|
return res_column;
|
|
|
|
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Illegal column {} of argument of function {}",
|
|
|
|
arguments[0].column->getName(), getName());
|
2021-07-16 13:58:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(ToStringCutToZero)
|
2021-07-16 13:58:06 +00:00
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionToStringCutToZero>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|