2020-10-10 18:37:02 +00:00
|
|
|
#pragma once
|
2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2020-06-21 18:57:20 +00:00
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <DataTypes/DataTypeFixedString.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <Columns/ColumnFixedString.h>
|
2020-11-05 19:09:17 +00:00
|
|
|
#include <Columns/ColumnsNumber.h>
|
|
|
|
#include <Columns/ColumnNullable.h>
|
2020-06-21 18:57:20 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
2020-06-21 10:54:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2020-06-21 18:57:20 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
extern const int TOO_LARGE_STRING_SIZE;
|
|
|
|
extern const int NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2020-11-05 19:09:17 +00:00
|
|
|
enum class ConvertToFixedStringExceptionMode
|
|
|
|
{
|
|
|
|
Throw,
|
|
|
|
Null
|
|
|
|
};
|
2020-06-21 18:57:20 +00:00
|
|
|
|
2020-06-21 10:54:28 +00:00
|
|
|
/** Conversion to fixed string is implemented only for strings.
|
|
|
|
*/
|
|
|
|
class FunctionToFixedString : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "toFixedString";
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionToFixedString>(); }
|
2020-06-21 10:54:28 +00:00
|
|
|
static FunctionPtr create() { return std::make_shared<FunctionToFixedString>(); }
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 2; }
|
2020-10-09 07:41:28 +00:00
|
|
|
bool isInjective(const ColumnsWithTypeAndName &) const override { return true; }
|
2020-06-21 10:54:28 +00:00
|
|
|
|
2020-06-21 18:57:20 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
|
|
|
|
{
|
|
|
|
if (!isUnsignedInteger(arguments[1].type))
|
|
|
|
throw Exception("Second argument for function " + getName() + " must be unsigned integer", ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
if (!arguments[1].column)
|
|
|
|
throw Exception("Second argument for function " + getName() + " must be constant", ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
if (!isStringOrFixedString(arguments[0].type))
|
|
|
|
throw Exception(getName() + " is only implemented for types String and FixedString", ErrorCodes::NOT_IMPLEMENTED);
|
|
|
|
|
|
|
|
const size_t n = arguments[1].column->getUInt(0);
|
|
|
|
return std::make_shared<DataTypeFixedString>(n);
|
|
|
|
}
|
2020-06-21 10:54:28 +00:00
|
|
|
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {1}; }
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /*input_rows_count*/) const override
|
2020-06-21 18:57:20 +00:00
|
|
|
{
|
2020-10-17 14:23:37 +00:00
|
|
|
const auto n = arguments[1].column->getUInt(0);
|
2020-11-05 19:09:17 +00:00
|
|
|
return executeForN<ConvertToFixedStringExceptionMode::Throw>(arguments, n);
|
2020-06-21 18:57:20 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 19:09:17 +00:00
|
|
|
template<ConvertToFixedStringExceptionMode exception_mode>
|
2020-11-17 13:24:45 +00:00
|
|
|
static ColumnPtr executeForN(const ColumnsWithTypeAndName & arguments, const size_t n)
|
2020-06-21 18:57:20 +00:00
|
|
|
{
|
2020-10-17 14:23:37 +00:00
|
|
|
const auto & column = arguments[0].column;
|
2020-06-21 18:57:20 +00:00
|
|
|
|
2020-11-05 19:09:17 +00:00
|
|
|
ColumnUInt8::MutablePtr col_null_map_to;
|
|
|
|
ColumnUInt8::Container * vec_null_map_to [[maybe_unused]] = nullptr;
|
|
|
|
if constexpr (exception_mode == ConvertToFixedStringExceptionMode::Null)
|
|
|
|
{
|
|
|
|
col_null_map_to = ColumnUInt8::create(column->size(), false);
|
|
|
|
vec_null_map_to = &col_null_map_to->getData();
|
|
|
|
}
|
|
|
|
|
2020-10-17 14:23:37 +00:00
|
|
|
if (const auto * column_string = checkAndGetColumn<ColumnString>(column.get()))
|
2020-06-21 18:57:20 +00:00
|
|
|
{
|
|
|
|
auto column_fixed = ColumnFixedString::create(n);
|
|
|
|
|
|
|
|
auto & out_chars = column_fixed->getChars();
|
|
|
|
const auto & in_chars = column_string->getChars();
|
|
|
|
const auto & in_offsets = column_string->getOffsets();
|
|
|
|
|
|
|
|
out_chars.resize_fill(in_offsets.size() * n);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < in_offsets.size(); ++i)
|
|
|
|
{
|
|
|
|
const size_t off = i ? in_offsets[i - 1] : 0;
|
|
|
|
const size_t len = in_offsets[i] - off - 1;
|
|
|
|
if (len > n)
|
2020-11-05 19:09:17 +00:00
|
|
|
{
|
|
|
|
if constexpr (exception_mode == ConvertToFixedStringExceptionMode::Throw)
|
|
|
|
{
|
|
|
|
throw Exception("String too long for type FixedString(" + toString(n) + ")",
|
|
|
|
ErrorCodes::TOO_LARGE_STRING_SIZE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
(*vec_null_map_to)[i] = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2020-06-21 18:57:20 +00:00
|
|
|
memcpy(&out_chars[i * n], &in_chars[off], len);
|
|
|
|
}
|
2020-06-21 10:54:28 +00:00
|
|
|
|
2020-11-05 19:09:17 +00:00
|
|
|
if constexpr (exception_mode == ConvertToFixedStringExceptionMode::Null)
|
|
|
|
return ColumnNullable::create(std::move(column_fixed), std::move(col_null_map_to));
|
|
|
|
else
|
2020-11-30 08:26:00 +00:00
|
|
|
return column_fixed;
|
2020-06-21 18:57:20 +00:00
|
|
|
}
|
2020-10-17 14:23:37 +00:00
|
|
|
else if (const auto * column_fixed_string = checkAndGetColumn<ColumnFixedString>(column.get()))
|
2020-06-21 18:57:20 +00:00
|
|
|
{
|
|
|
|
const auto src_n = column_fixed_string->getN();
|
|
|
|
if (src_n > n)
|
2020-11-05 19:09:17 +00:00
|
|
|
{
|
|
|
|
if constexpr (exception_mode == ConvertToFixedStringExceptionMode::Throw)
|
|
|
|
{
|
|
|
|
throw Exception{"String too long for type FixedString(" + toString(n) + ")", ErrorCodes::TOO_LARGE_STRING_SIZE};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto column_fixed = ColumnFixedString::create(n);
|
|
|
|
std::fill(vec_null_map_to->begin(), vec_null_map_to->end(), true);
|
|
|
|
return ColumnNullable::create(column_fixed->cloneResized(column->size()), std::move(col_null_map_to));
|
|
|
|
}
|
|
|
|
}
|
2020-06-21 18:57:20 +00:00
|
|
|
|
|
|
|
auto column_fixed = ColumnFixedString::create(n);
|
|
|
|
|
|
|
|
auto & out_chars = column_fixed->getChars();
|
|
|
|
const auto & in_chars = column_fixed_string->getChars();
|
|
|
|
const auto size = column_fixed_string->size();
|
|
|
|
out_chars.resize_fill(size * n);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
memcpy(&out_chars[i * n], &in_chars[i * src_n], src_n);
|
|
|
|
|
2020-10-17 14:23:37 +00:00
|
|
|
return column_fixed;
|
2020-06-21 18:57:20 +00:00
|
|
|
}
|
|
|
|
else
|
2020-11-05 19:09:17 +00:00
|
|
|
{
|
|
|
|
if constexpr (exception_mode == ConvertToFixedStringExceptionMode::Throw)
|
|
|
|
throw Exception("Unexpected column: " + column->getName(), ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto column_fixed = ColumnFixedString::create(n);
|
|
|
|
std::fill(vec_null_map_to->begin(), vec_null_map_to->end(), true);
|
|
|
|
return ColumnNullable::create(column_fixed->cloneResized(column->size()), std::move(col_null_map_to));
|
|
|
|
}
|
|
|
|
}
|
2020-06-21 18:57:20 +00:00
|
|
|
}
|
2020-06-21 10:54:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|