2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2020-08-27 01:14:10 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionHelpers.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <DataTypes/DataTypesDecimal.h>
|
|
|
|
#include <Columns/ColumnsNumber.h>
|
|
|
|
#include <Columns/ColumnDecimal.h>
|
|
|
|
#include <Columns/ColumnConst.h>
|
2020-08-27 13:17:13 +00:00
|
|
|
#include <Common/intExp.h>
|
2020-08-27 01:14:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
}
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2020-08-27 01:14:10 +00:00
|
|
|
/// Returns 1 if and Decimal value has more digits then it's Precision allow, 0 otherwise.
|
2020-10-27 11:04:03 +00:00
|
|
|
/// Precision could be set as second argument or omitted. If omitted function uses Decimal precision of the first argument.
|
2020-08-27 01:14:10 +00:00
|
|
|
class FunctionIsDecimalOverflow : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "isDecimalOverflow";
|
|
|
|
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr)
|
2020-08-27 01:14:10 +00:00
|
|
|
{
|
|
|
|
return std::make_shared<FunctionIsDecimalOverflow>();
|
|
|
|
}
|
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
bool isVariadic() const override { return true; }
|
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
2020-08-27 01:14:10 +00:00
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
2020-08-27 13:17:13 +00:00
|
|
|
if (arguments.empty() || arguments.size() > 2)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH,
|
|
|
|
"Number of arguments for function {} doesn't match: passed {}, should be 1 or 2.",
|
|
|
|
getName(), arguments.size());
|
2020-08-27 01:14:10 +00:00
|
|
|
|
|
|
|
WhichDataType which_first(arguments[0]->getTypeId());
|
|
|
|
|
|
|
|
if (!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 01:14:10 +00:00
|
|
|
|
|
|
|
if (arguments.size() == 2)
|
|
|
|
{
|
|
|
|
WhichDataType which_second(arguments[1]->getTypeId());
|
|
|
|
if (!which_second.isUInt8())
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of argument of function {}",
|
|
|
|
arguments[1]->getName(), getName());
|
2020-08-27 01:14:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeUInt8>();
|
|
|
|
}
|
|
|
|
|
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 01:14:10 +00:00
|
|
|
{
|
2020-10-19 13:42:14 +00:00
|
|
|
const auto & src_column = arguments[0];
|
2020-08-27 01:14:10 +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 01:14:10 +00:00
|
|
|
|
|
|
|
UInt32 precision = 0;
|
|
|
|
if (arguments.size() == 2)
|
|
|
|
{
|
2020-10-19 13:42:14 +00:00
|
|
|
const auto & precision_column = arguments[1];
|
2020-08-27 01:14:10 +00:00
|
|
|
if (!precision_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 01:14:10 +00:00
|
|
|
|
|
|
|
const ColumnConst * const_column = checkAndGetColumnConst<ColumnUInt8>(precision_column.column.get());
|
|
|
|
if (!const_column)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Second argument for function {} must be constant UInt8: "
|
|
|
|
"precision.", getName());
|
2020-08-27 01:14:10 +00:00
|
|
|
|
|
|
|
precision = const_column->getValue<UInt8>();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
precision = getDecimalPrecision(*src_column.type);
|
|
|
|
|
|
|
|
auto result_column = ColumnUInt8::create();
|
|
|
|
|
2023-02-19 22:15:09 +00:00
|
|
|
auto call = [&](const auto & types) -> bool
|
2020-08-27 01:14:10 +00:00
|
|
|
{
|
|
|
|
using Types = std::decay_t<decltype(types)>;
|
|
|
|
using Type = typename Types::RightType;
|
|
|
|
using ColVecType = ColumnDecimal<Type>;
|
|
|
|
|
|
|
|
if (const ColumnConst * const_column = checkAndGetColumnConst<ColVecType>(src_column.column.get()))
|
|
|
|
{
|
|
|
|
Type const_decimal = checkAndGetColumn<ColVecType>(const_column->getDataColumnPtr().get())->getData()[0];
|
2020-08-27 13:17:13 +00:00
|
|
|
UInt8 res_value = outOfDigits<Type>(const_decimal, precision);
|
2020-08-27 01:14:10 +00:00
|
|
|
result_column->getData().resize_fill(input_rows_count, res_value);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (const ColVecType * col_vec = checkAndGetColumn<ColVecType>(src_column.column.get()))
|
|
|
|
{
|
|
|
|
execute<Type>(*col_vec, *result_column, input_rows_count, precision);
|
|
|
|
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 01:14:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TypeIndex dec_type_idx = src_column.type->getTypeId();
|
|
|
|
if (!callOnBasicType<void, false, 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 01:14:10 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
return result_column;
|
2020-08-27 01:14:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
template <typename T>
|
|
|
|
static void execute(const ColumnDecimal<T> & col, ColumnUInt8 & result_column, size_t rows_count, UInt32 precision)
|
|
|
|
{
|
|
|
|
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)
|
2020-08-27 13:17:13 +00:00
|
|
|
dst_data[i] = outOfDigits<T>(src_data[i], precision);
|
2020-08-27 01:14:10 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 11:49:22 +00:00
|
|
|
template <is_decimal T>
|
2020-08-27 13:17:13 +00:00
|
|
|
static bool outOfDigits(T dec, UInt32 precision)
|
2020-08-27 01:14:10 +00:00
|
|
|
{
|
2020-08-27 13:17:13 +00:00
|
|
|
using NativeT = typename T::NativeType;
|
2020-08-27 01:14:10 +00:00
|
|
|
|
2021-02-20 18:13:36 +00:00
|
|
|
if (precision > DecimalUtils::max_precision<T>)
|
2020-08-27 13:17:13 +00:00
|
|
|
return false;
|
2020-08-27 01:14:10 +00:00
|
|
|
|
2020-08-27 13:17:13 +00:00
|
|
|
NativeT pow10 = intExp10OfSize<NativeT>(precision);
|
2020-08-27 01:14:10 +00:00
|
|
|
|
2020-08-27 13:17:13 +00:00
|
|
|
if (dec.value < 0)
|
|
|
|
return dec.value <= -pow10;
|
|
|
|
return dec.value >= pow10;
|
2020-08-27 01:14:10 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
2020-08-27 01:14:10 +00:00
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(IsDecimalOverflow)
|
2020-08-27 01:14:10 +00:00
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionIsDecimalOverflow>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|