1#include <DataTypes/DataTypeString.h>
2#include <Functions/FunctionFactory.h>
3#include <Functions/FunctionStringOrArrayToT.h>
4
5
6namespace DB
7{
8
9
10/** Calculates the length of a string in bytes.
11 */
12struct LengthImpl
13{
14 static constexpr auto is_fixed_to_constant = true;
15
16 static void vector(const ColumnString::Chars & /*data*/, const ColumnString::Offsets & offsets, PaddedPODArray<UInt64> & res)
17 {
18 size_t size = offsets.size();
19 for (size_t i = 0; i < size; ++i)
20 res[i] = offsets[i] - 1 - offsets[i - 1];
21 }
22
23 static void vector_fixed_to_constant(const ColumnString::Chars & /*data*/, size_t n, UInt64 & res)
24 {
25 res = n;
26 }
27
28 static void vector_fixed_to_vector(const ColumnString::Chars & /*data*/, size_t /*n*/, PaddedPODArray<UInt64> & /*res*/)
29 {
30 }
31
32 static void array(const ColumnString::Offsets & offsets, PaddedPODArray<UInt64> & res)
33 {
34 size_t size = offsets.size();
35 for (size_t i = 0; i < size; ++i)
36 res[i] = offsets[i] - offsets[i - 1];
37 }
38};
39
40
41struct NameLength
42{
43 static constexpr auto name = "length";
44};
45
46using FunctionLength = FunctionStringOrArrayToT<LengthImpl, NameLength, UInt64>;
47
48void registerFunctionLength(FunctionFactory & factory)
49{
50 factory.registerFunction<FunctionLength>(FunctionFactory::CaseInsensitive);
51}
52
53}
54