ClickHouse
156 строк · 4.8 Кб
1#include <Functions/IFunction.h>
2#include <Functions/FunctionFactory.h>
3#include <Functions/FunctionHelpers.h>
4#include <DataTypes/DataTypesNumber.h>
5#include <Columns/ColumnsNumber.h>
6#include <Columns/ColumnDecimal.h>
7#include <base/extended_types.h>
8#include <base/itoa.h>
9
10
11namespace DB
12{
13
14namespace ErrorCodes
15{
16extern const int ILLEGAL_TYPE_OF_ARGUMENT;
17extern const int ILLEGAL_COLUMN;
18}
19
20namespace
21{
22
23template <typename T>
24int digits10(T x)
25{
26if (x < 10ULL)
27return 1;
28if (x < 100ULL)
29return 2;
30if (x < 1000ULL)
31return 3;
32
33if (x < 1000000000000ULL)
34{
35if (x < 100000000ULL)
36{
37if (x < 1000000ULL)
38{
39if (x < 10000ULL)
40return 4;
41else
42return 5 + (x >= 100000ULL);
43}
44
45return 7 + (x >= 10000000ULL);
46}
47
48if (x < 10000000000ULL)
49return 9 + (x >= 1000000000ULL);
50
51return 11 + (x >= 100000000000ULL);
52}
53
54return 12 + digits10(x / 1000000000000ULL);
55}
56
57/// Returns number of decimal digits you need to represent the value.
58/// For Decimal values takes in account their scales: calculates result over underlying int type which is (value * scale).
59/// countDigits(42) = 2, countDigits(42.000) = 5, countDigits(0.04200) = 4.
60/// I.e. you may check decimal overflow for Decimal64 with 'countDecimal(x) > 18'. It's a slow variant of isDecimalOverflow().
61class FunctionCountDigits : public IFunction
62{
63public:
64static constexpr auto name = "countDigits";
65
66static FunctionPtr create(ContextPtr)
67{
68return std::make_shared<FunctionCountDigits>();
69}
70
71String getName() const override { return name; }
72bool useDefaultImplementationForConstants() const override { return true; }
73size_t getNumberOfArguments() const override { return 1; }
74bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
75
76DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
77{
78WhichDataType which_first(arguments[0]->getTypeId());
79
80if (!which_first.isInt() && !which_first.isUInt() && !which_first.isDecimal())
81throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of argument of function {}",
82arguments[0]->getName(), getName());
83
84return std::make_shared<DataTypeUInt8>(); /// Up to 255 decimal digits.
85}
86
87ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
88{
89const auto & src_column = arguments[0];
90if (!src_column.column)
91throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal column while execute function {}", getName());
92
93auto result_column = ColumnUInt8::create();
94
95auto call = [&](const auto & types) -> bool
96{
97using Types = std::decay_t<decltype(types)>;
98using Type = typename Types::RightType;
99using ColVecType = ColumnVectorOrDecimal<Type>;
100
101if (const ColVecType * col_vec = checkAndGetColumn<ColVecType>(src_column.column.get()))
102{
103execute<Type>(*col_vec, *result_column, input_rows_count);
104return true;
105}
106
107throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal column while execute function {}", getName());
108};
109
110TypeIndex dec_type_idx = src_column.type->getTypeId();
111if (!callOnBasicType<void, true, false, true, false>(dec_type_idx, call))
112throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Wrong call for {} with {}", getName(), src_column.type->getName());
113
114return result_column;
115}
116
117private:
118template <typename T, typename ColVecType>
119static void execute(const ColVecType & col, ColumnUInt8 & result_column, size_t rows_count)
120{
121using NativeT = make_unsigned_t<NativeType<T>>;
122
123const auto & src_data = col.getData();
124auto & dst_data = result_column.getData();
125dst_data.resize(rows_count);
126
127for (size_t i = 0; i < rows_count; ++i)
128{
129if constexpr (is_decimal<T>)
130{
131auto value = src_data[i].value;
132if (unlikely(value < 0))
133dst_data[i] = digits10<NativeT>(-static_cast<NativeT>(value));
134else
135dst_data[i] = digits10<NativeT>(value);
136}
137else
138{
139auto value = src_data[i];
140if (unlikely(value < 0))
141dst_data[i] = digits10<NativeT>(-static_cast<NativeT>(value));
142else
143dst_data[i] = digits10<NativeT>(value);
144}
145}
146}
147};
148
149}
150
151REGISTER_FUNCTION(CountDigits)
152{
153factory.registerFunction<FunctionCountDigits>();
154}
155
156}
157