mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-22 07:31:57 +00:00
Merge pull request #7024 from millb/master
Fixed Function Hex for Float32 and Float64
This commit is contained in:
commit
e24d882326
@ -946,9 +946,10 @@ public:
|
||||
{
|
||||
WhichDataType which(arguments[0]);
|
||||
|
||||
if (!which.isStringOrFixedString()
|
||||
&& !which.isDateOrDateTime()
|
||||
&& !which.isUInt())
|
||||
if (!which.isStringOrFixedString() &&
|
||||
!which.isDateOrDateTime() &&
|
||||
!which.isUInt() &&
|
||||
!which.isFloat())
|
||||
throw Exception("Illegal type " + arguments[0]->getName() + " of argument of function " + getName(),
|
||||
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
||||
|
||||
@ -1021,6 +1022,45 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool tryExecuteFloat(const IColumn * col, ColumnPtr & col_res)
|
||||
{
|
||||
const ColumnVector<T> * col_vec = checkAndGetColumn<ColumnVector<T>>(col);
|
||||
|
||||
static constexpr size_t FLOAT_HEX_LENGTH = sizeof(T) * 2 + 1; /// Including trailing zero byte.
|
||||
|
||||
if (col_vec)
|
||||
{
|
||||
auto col_str = ColumnString::create();
|
||||
ColumnString::Chars & out_vec = col_str->getChars();
|
||||
ColumnString::Offsets & out_offsets = col_str->getOffsets();
|
||||
|
||||
const typename ColumnVector<T>::Container & in_vec = col_vec->getData();
|
||||
|
||||
size_t size = in_vec.size();
|
||||
out_offsets.resize(size);
|
||||
out_vec.resize(size * FLOAT_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 += FLOAT_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)
|
||||
@ -1135,7 +1175,9 @@ public:
|
||||
tryExecuteUInt<UInt32>(column, res_column) ||
|
||||
tryExecuteUInt<UInt64>(column, res_column) ||
|
||||
tryExecuteString(column, res_column) ||
|
||||
tryExecuteFixedString(column, res_column))
|
||||
tryExecuteFixedString(column, res_column) ||
|
||||
tryExecuteFloat<Float32>(column, res_column) ||
|
||||
tryExecuteFloat<Float64>(column, res_column))
|
||||
return;
|
||||
|
||||
throw Exception("Illegal column " + block.getByPosition(arguments[0]).column->getName()
|
||||
|
12
dbms/tests/queries/0_stateless/01013_hex_float.reference
Normal file
12
dbms/tests/queries/0_stateless/01013_hex_float.reference
Normal file
@ -0,0 +1,12 @@
|
||||
000000000000F03F
|
||||
0000000000405940
|
||||
00C84E676DC1AB43
|
||||
2342920CA19CC73B
|
||||
7DC39425AD49B254
|
||||
2C616D8C9DF0423F
|
||||
3BDF4F8D97FE5EC0
|
||||
0A57C742
|
||||
00004843
|
||||
00004943
|
||||
0000000000406940
|
||||
0000000000606940
|
10
dbms/tests/queries/0_stateless/01013_hex_float.sql
Normal file
10
dbms/tests/queries/0_stateless/01013_hex_float.sql
Normal file
@ -0,0 +1,10 @@
|
||||
SELECT hex(1.0);
|
||||
SELECT hex(101.);
|
||||
SELECT hex(1e+18);
|
||||
SELECT hex(1e-20);
|
||||
SELECT hex(1e+100);
|
||||
SELECT hex(0.000578);
|
||||
SELECT hex(-123.978);
|
||||
SELECT hex(toFloat32(99.67));
|
||||
SELECT hex(toFloat32(number)) FROM numbers(200, 2);
|
||||
SELECT hex(toFloat64(number)) FROM numbers(202, 2);
|
Loading…
Reference in New Issue
Block a user