mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-24 08:32:02 +00:00
Created: function hex for Decimal32, Decimal64, Decimal128
This commit is contained in:
parent
12768ddf45
commit
19a54a59bb
@ -19,6 +19,7 @@
|
|||||||
#include <Columns/ColumnArray.h>
|
#include <Columns/ColumnArray.h>
|
||||||
#include <Columns/ColumnConst.h>
|
#include <Columns/ColumnConst.h>
|
||||||
#include <Columns/ColumnTuple.h>
|
#include <Columns/ColumnTuple.h>
|
||||||
|
#include <Columns/ColumnDecimal.h>
|
||||||
#include <Functions/IFunction.h>
|
#include <Functions/IFunction.h>
|
||||||
#include <Functions/FunctionHelpers.h>
|
#include <Functions/FunctionHelpers.h>
|
||||||
|
|
||||||
@ -949,7 +950,8 @@ public:
|
|||||||
if (!which.isStringOrFixedString() &&
|
if (!which.isStringOrFixedString() &&
|
||||||
!which.isDateOrDateTime() &&
|
!which.isDateOrDateTime() &&
|
||||||
!which.isUInt() &&
|
!which.isUInt() &&
|
||||||
!which.isFloat())
|
!which.isFloat() &&
|
||||||
|
!which.isDecimal())
|
||||||
throw Exception("Illegal type " + arguments[0]->getName() + " of argument of function " + getName(),
|
throw Exception("Illegal type " + arguments[0]->getName() + " of argument of function " + getName(),
|
||||||
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
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)
|
void executeOneString(const UInt8 * pos, const UInt8 * end, char *& out)
|
||||||
{
|
{
|
||||||
while (pos < end)
|
while (pos < end)
|
||||||
@ -1177,7 +1221,10 @@ public:
|
|||||||
tryExecuteString(column, res_column) ||
|
tryExecuteString(column, res_column) ||
|
||||||
tryExecuteFixedString(column, res_column) ||
|
tryExecuteFixedString(column, res_column) ||
|
||||||
tryExecuteFloat<Float32>(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;
|
return;
|
||||||
|
|
||||||
throw Exception("Illegal column " + block.getByPosition(arguments[0]).column->getName()
|
throw Exception("Illegal column " + block.getByPosition(arguments[0]).column->getName()
|
||||||
|
11
dbms/tests/queries/0_stateless/01013_hex_decimal.reference
Normal file
11
dbms/tests/queries/0_stateless/01013_hex_decimal.reference
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
64000000
|
||||||
|
64000000
|
||||||
|
42020000
|
||||||
|
B61BFEFFFFFFFFFF
|
||||||
|
EF260000000000000000000000000000
|
||||||
|
400D0300
|
||||||
|
28110300
|
||||||
|
403A340100000000
|
||||||
|
E0C0350100000000
|
||||||
|
00B08EF01B0000000000000000000000
|
||||||
|
007A292C1C0000000000000000000000
|
8
dbms/tests/queries/0_stateless/01013_hex_decimal.sql
Normal file
8
dbms/tests/queries/0_stateless/01013_hex_decimal.sql
Normal 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);
|
Loading…
Reference in New Issue
Block a user