Created: function hex for Decimal32, Decimal64, Decimal128

This commit is contained in:
millb 2019-10-16 18:28:10 +03:00
parent 12768ddf45
commit 19a54a59bb
3 changed files with 68 additions and 2 deletions

View File

@ -19,6 +19,7 @@
#include <Columns/ColumnArray.h>
#include <Columns/ColumnConst.h>
#include <Columns/ColumnTuple.h>
#include <Columns/ColumnDecimal.h>
#include <Functions/IFunction.h>
#include <Functions/FunctionHelpers.h>
@ -949,7 +950,8 @@ public:
if (!which.isStringOrFixedString() &&
!which.isDateOrDateTime() &&
!which.isUInt() &&
!which.isFloat())
!which.isFloat() &&
!which.isDecimal())
throw Exception("Illegal type " + arguments[0]->getName() + " of argument of function " + getName(),
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
@ -1061,6 +1063,48 @@ public:
}
}
template <typename T>
bool tryExecuteDecimal(const IColumn * col, ColumnPtr & col_res)
{
const ColumnDecimal<T> * col_dec = checkAndGetColumn<ColumnDecimal<T>>(col);
static constexpr size_t DECIMAL_HEX_LENGTH = sizeof(T) * 2 + 1; /// Including trailing zero byte.
if (col_dec)
{
auto col_str = ColumnString::create();
ColumnString::Chars & out_vec = col_str->getChars();
ColumnString::Offsets & out_offsets = col_str->getOffsets();
const typename ColumnDecimal<T>::Container & in_vec = col_dec->getData();
size_t size = in_vec.size();
out_offsets.resize(size);
out_vec.resize(size * DECIMAL_HEX_LENGTH);
size_t pos = 0;
char * out = reinterpret_cast<char *>(&out_vec[0]);
for (size_t i = 0; i < size; ++i)
{
const UInt8 * in_pos = reinterpret_cast<const UInt8 *>(&in_vec[i]);
executeOneString(in_pos, in_pos + sizeof(T), out);
pos += DECIMAL_HEX_LENGTH;
out_offsets[i] = pos;
}
col_res = std::move(col_str);
return true;
}
else
{
return false;
}
}
void executeOneString(const UInt8 * pos, const UInt8 * end, char *& out)
{
while (pos < end)
@ -1177,7 +1221,10 @@ public:
tryExecuteString(column, res_column) ||
tryExecuteFixedString(column, res_column) ||
tryExecuteFloat<Float32>(column, res_column) ||
tryExecuteFloat<Float64>(column, res_column))
tryExecuteFloat<Float64>(column, res_column) ||
tryExecuteDecimal<Decimal32>(column, res_column) ||
tryExecuteDecimal<Decimal64>(column, res_column) ||
tryExecuteDecimal<Decimal128>(column, res_column))
return;
throw Exception("Illegal column " + block.getByPosition(arguments[0]).column->getName()

View File

@ -0,0 +1,11 @@
64000000
64000000
42020000
B61BFEFFFFFFFFFF
EF260000000000000000000000000000
400D0300
28110300
403A340100000000
E0C0350100000000
00B08EF01B0000000000000000000000
007A292C1C0000000000000000000000

View File

@ -0,0 +1,8 @@
SELECT hex(toDecimal32(1.0, 2));
SELECT hex(toDecimal32(1., 2));
SELECT hex(toDecimal32(0.000578, 6));
SELECT hex(toDecimal64(-123.978, 3));
SELECT hex(toDecimal128(99.67, 2));
SELECT hex(toDecimal32(number, 3)) FROM numbers(200, 2);
SELECT hex(toDecimal64(number, 5)) FROM numbers(202, 2);
SELECT hex(toDecimal128(number, 9)) FROM numbers(120, 2);