gix comment and useDefault*(), add tests for nullables

This commit is contained in:
Artem Zuikov 2020-08-28 13:16:42 +03:00
parent 2a0b98b19c
commit 881cd3331a
6 changed files with 19 additions and 17 deletions

View File

@ -5,7 +5,6 @@
#include <DataTypes/DataTypesDecimal.h>
#include <Columns/ColumnsNumber.h>
#include <Columns/ColumnDecimal.h>
#include <Columns/ColumnConst.h>
namespace DB
@ -17,8 +16,10 @@ namespace ErrorCodes
extern const int ILLEGAL_COLUMN;
}
/// Returns 1 if and Decimal value has more digits then it's Precision allow, 0 otherwise.
/// Precision could be set as second argument or omitted. If ommited function uses Decimal presicion of the first argument.
/// 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().
class FunctionCountDigits : public IFunction
{
public:
@ -30,7 +31,7 @@ public:
}
String getName() const override { return name; }
bool useDefaultImplementationForNulls() const override { return false; }
bool useDefaultImplementationForConstants() const override { return true; }
size_t getNumberOfArguments() const override { return 1; }
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
@ -58,18 +59,7 @@ public:
using Type = typename Types::RightType;
using ColVecType = std::conditional_t<IsDecimalNumber<Type>, ColumnDecimal<Type>, ColumnVector<Type>>;
if (const ColumnConst * const_column = checkAndGetColumnConst<ColVecType>(src_column.column.get()))
{
Type const_value = checkAndGetColumn<ColVecType>(const_column->getDataColumnPtr().get())->getData()[0];
UInt32 num_digits = 0;
if constexpr (IsDecimalNumber<Type>)
num_digits = digits(const_value.value);
else
num_digits = digits(const_value);
result_column->getData().resize_fill(input_rows_count, num_digits);
return true;
}
else if (const ColVecType * col_vec = checkAndGetColumn<ColVecType>(src_column.column.get()))
if (const ColVecType * col_vec = checkAndGetColumn<ColVecType>(src_column.column.get()))
{
execute<Type>(*col_vec, *result_column, input_rows_count);
return true;

View File

@ -32,7 +32,6 @@ public:
}
String getName() const override { return name; }
bool useDefaultImplementationForNulls() const override { return false; }
bool isVariadic() const override { return true; }
size_t getNumberOfArguments() const override { return 0; }

View File

@ -4,3 +4,4 @@
2 2 2 2 2 2 2 2 2 2 2 2
0 0 0 0 0 0 0 0 0 0 0 0
3 3 3 5 5 5 10 10 10 19 19 20
2 3 4 5 6 7

View File

@ -24,3 +24,7 @@ SELECT countDigits(toInt8(127)), countDigits(toInt8(-128)), countDigits(toUInt8
countDigits(toInt16(32767)), countDigits(toInt16(-32768)), countDigits(toUInt16(65535)),
countDigits(toInt32(2147483647)), countDigits(toInt32(-2147483648)), countDigits(toUInt32(4294967295)),
countDigits(toInt64(9223372036854775807)), countDigits(toInt64(-9223372036854775808)), countDigits(toUInt64(18446744073709551615));
SELECT countDigits(toNullable(toDecimal32(4.2, 1))), countDigits(materialize(toNullable(toDecimal32(4.2, 2)))),
countDigits(toNullable(toDecimal64(4.2, 3))), countDigits(materialize(toNullable(toDecimal64(4.2, 4)))),
countDigits(toNullable(toDecimal128(4.2, 5))), countDigits(materialize(toNullable(toDecimal128(4.2, 6))));

View File

@ -17,3 +17,4 @@
1 1 1 1
0 0 0 0
1 1 1 1 1 1
1 0 1 0 1 0

View File

@ -91,3 +91,10 @@ SELECT isDecimalOverflow(materialize(toDecimal128('99999999999999999999999999999
isDecimalOverflow(materialize(toDecimal128('-99999999999999999999999999999999999999', 0)), 37),
isDecimalOverflow(materialize(toDecimal128('-10', 0)), 1),
isDecimalOverflow(materialize(toDecimal128('-1', 0)), 0);
SELECT isDecimalOverflow(toNullable(toDecimal32(42, 0)), 1),
isDecimalOverflow(materialize(toNullable(toDecimal32(42, 0))), 2),
isDecimalOverflow(toNullable(toDecimal64(42, 0)), 1),
isDecimalOverflow(materialize(toNullable(toDecimal64(42, 0))), 2),
isDecimalOverflow(toNullable(toDecimal128(42, 0)), 1),
isDecimalOverflow(materialize(toNullable(toDecimal128(42, 0))), 2);