2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2020-08-27 13:17:13 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionHelpers.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <Columns/ColumnsNumber.h>
|
|
|
|
#include <Columns/ColumnDecimal.h>
|
2023-02-09 03:39:12 +00:00
|
|
|
#include <base/extended_types.h>
|
|
|
|
#include <base/itoa.h>
|
2020-08-27 13:17:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
}
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2020-08-28 10:16:42 +00:00
|
|
|
/// Returns number of decimal digits you need to represent the value.
|
|
|
|
/// For Decimal values takes in account their scales: calculates result over underlying int type which is (value * scale).
|
|
|
|
/// countDigits(42) = 2, countDigits(42.000) = 5, countDigits(0.04200) = 4.
|
|
|
|
/// I.e. you may check decimal overflow for Decimal64 with 'countDecimal(x) > 18'. It's a slow variant of isDecimalOverflow().
|
2020-08-27 13:17:13 +00:00
|
|
|
class FunctionCountDigits : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "countDigits";
|
|
|
|
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr)
|
2020-08-27 13:17:13 +00:00
|
|
|
{
|
|
|
|
return std::make_shared<FunctionCountDigits>();
|
|
|
|
}
|
|
|
|
|
|
|
|
String getName() const override { return name; }
|
2020-08-28 10:16:42 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
2020-08-27 13:17:13 +00:00
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
2020-08-27 13:17:13 +00:00
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
WhichDataType which_first(arguments[0]->getTypeId());
|
|
|
|
|
|
|
|
if (!which_first.isInt() && !which_first.isUInt() && !which_first.isDecimal())
|
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());
|
2020-08-27 13:17:13 +00:00
|
|
|
|
|
|
|
return std::make_shared<DataTypeUInt8>(); /// Up to 255 decimal digits.
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
|
2020-08-27 13:17:13 +00:00
|
|
|
{
|
2020-10-17 16:48:53 +00:00
|
|
|
const auto & src_column = arguments[0];
|
2020-08-27 13:17:13 +00:00
|
|
|
if (!src_column.column)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal column while execute function {}", getName());
|
2020-08-27 13:17:13 +00:00
|
|
|
|
|
|
|
auto result_column = ColumnUInt8::create();
|
|
|
|
|
|
|
|
auto call = [&](const auto & types) -> bool
|
|
|
|
{
|
|
|
|
using Types = std::decay_t<decltype(types)>;
|
|
|
|
using Type = typename Types::RightType;
|
2021-09-10 11:49:22 +00:00
|
|
|
using ColVecType = ColumnVectorOrDecimal<Type>;
|
2020-08-27 13:17:13 +00:00
|
|
|
|
2020-08-28 10:16:42 +00:00
|
|
|
if (const ColVecType * col_vec = checkAndGetColumn<ColVecType>(src_column.column.get()))
|
2020-08-27 13:17:13 +00:00
|
|
|
{
|
|
|
|
execute<Type>(*col_vec, *result_column, input_rows_count);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal column while execute function {}", getName());
|
2020-08-27 13:17:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TypeIndex dec_type_idx = src_column.type->getTypeId();
|
|
|
|
if (!callOnBasicType<void, true, false, true, false>(dec_type_idx, call))
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Wrong call for {} with {}", getName(), src_column.type->getName());
|
2020-08-27 13:17:13 +00:00
|
|
|
|
2020-10-17 16:48:53 +00:00
|
|
|
return result_column;
|
2020-08-27 13:17:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
template <typename T, typename ColVecType>
|
|
|
|
static void execute(const ColVecType & col, ColumnUInt8 & result_column, size_t rows_count)
|
|
|
|
{
|
2023-02-09 03:39:12 +00:00
|
|
|
using NativeT = make_unsigned_t<NativeType<T>>;
|
2020-08-27 13:17:13 +00:00
|
|
|
|
|
|
|
const auto & src_data = col.getData();
|
|
|
|
auto & dst_data = result_column.getData();
|
|
|
|
dst_data.resize(rows_count);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < rows_count; ++i)
|
|
|
|
{
|
2021-09-10 11:49:22 +00:00
|
|
|
if constexpr (is_decimal<T>)
|
2023-02-09 03:39:12 +00:00
|
|
|
{
|
|
|
|
auto value = src_data[i].value;
|
|
|
|
if (unlikely(value < 0))
|
|
|
|
dst_data[i] = digits10<NativeT>(-static_cast<NativeT>(value));
|
|
|
|
else
|
|
|
|
dst_data[i] = digits10<NativeT>(value);
|
|
|
|
}
|
2020-08-27 13:17:13 +00:00
|
|
|
else
|
|
|
|
{
|
2023-02-09 03:39:12 +00:00
|
|
|
auto value = src_data[i];
|
|
|
|
if (unlikely(value < 0))
|
|
|
|
dst_data[i] = digits10<NativeT>(-static_cast<NativeT>(value));
|
|
|
|
else
|
|
|
|
dst_data[i] = digits10<NativeT>(value);
|
2020-08-27 13:17:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
2020-08-27 13:17:13 +00:00
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(CountDigits)
|
2020-08-27 13:17:13 +00:00
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionCountDigits>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|