mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-10-19 06:50:50 +00:00
54 lines
1.3 KiB
C++
54 lines
1.3 KiB
C++
|
#include <DataTypes/DataTypeString.h>
|
||
|
#include <Functions/FunctionFactory.h>
|
||
|
#include <Functions/FunctionStringOrArrayToT.h>
|
||
|
|
||
|
|
||
|
namespace DB
|
||
|
{
|
||
|
|
||
|
|
||
|
/** Calculates the length of a string in bytes.
|
||
|
*/
|
||
|
struct LengthImpl
|
||
|
{
|
||
|
static constexpr auto is_fixed_to_constant = true;
|
||
|
|
||
|
static void vector(const ColumnString::Chars_t & /*data*/, const ColumnString::Offsets & offsets, PaddedPODArray<UInt64> & res)
|
||
|
{
|
||
|
size_t size = offsets.size();
|
||
|
for (size_t i = 0; i < size; ++i)
|
||
|
res[i] = i == 0 ? (offsets[i] - 1) : (offsets[i] - 1 - offsets[i - 1]);
|
||
|
}
|
||
|
|
||
|
static void vector_fixed_to_constant(const ColumnString::Chars_t & /*data*/, size_t n, UInt64 & res)
|
||
|
{
|
||
|
res = n;
|
||
|
}
|
||
|
|
||
|
static void vector_fixed_to_vector(const ColumnString::Chars_t & /*data*/, size_t /*n*/, PaddedPODArray<UInt64> & /*res*/)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
static void array(const ColumnString::Offsets & offsets, PaddedPODArray<UInt64> & res)
|
||
|
{
|
||
|
size_t size = offsets.size();
|
||
|
for (size_t i = 0; i < size; ++i)
|
||
|
res[i] = i == 0 ? (offsets[i]) : (offsets[i] - offsets[i - 1]);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
|
||
|
struct NameLength
|
||
|
{
|
||
|
static constexpr auto name = "length";
|
||
|
};
|
||
|
|
||
|
using FunctionLength = FunctionStringOrArrayToT<LengthImpl, NameLength, UInt64>;
|
||
|
|
||
|
void registerFunctionLength(FunctionFactory & factory)
|
||
|
{
|
||
|
factory.registerFunction<FunctionLength>();
|
||
|
}
|
||
|
|
||
|
}
|