diff --git a/dbms/src/Functions/FunctionsCoding.h b/dbms/src/Functions/FunctionsCoding.h index adaaa875a71..1ab00d725f6 100644 --- a/dbms/src/Functions/FunctionsCoding.h +++ b/dbms/src/Functions/FunctionsCoding.h @@ -946,9 +946,10 @@ public: { WhichDataType which(arguments[0]); - if (!which.isStringOrFixedString() - && !which.isDateOrDateTime() - && !which.isUInt() && !which.isFloat()) + 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); @@ -1026,7 +1027,7 @@ public: { const ColumnVector * col_vec = checkAndGetColumn>(col); - static constexpr size_t MAX_FLOAT_HEX_LENGTH = sizeof(T) * 2 + 1; /// Including trailing zero byte. + static constexpr size_t FLOAT_HEX_LENGTH = sizeof(T) * 2 + 1; /// Including trailing zero byte. if (col_vec) { @@ -1038,27 +1039,19 @@ public: size_t size = in_vec.size(); out_offsets.resize(size); - out_vec.resize(size * 3 + MAX_FLOAT_HEX_LENGTH); /// 3 is length of one byte in hex plus zero byte. + out_vec.resize(size * FLOAT_HEX_LENGTH); size_t pos = 0; + char * out = reinterpret_cast(&out_vec[0]); for (size_t i = 0; i < size; ++i) { - /// Manual exponential growth, so as not to rely on the linear amortized work time of `resize` (no one guarantees it). - if (pos + MAX_FLOAT_HEX_LENGTH > out_vec.size()) - out_vec.resize(out_vec.size() * 2 + MAX_FLOAT_HEX_LENGTH); - - char * begin = reinterpret_cast(&out_vec[pos]); - char * end = begin; - const UInt8 * in_pos = reinterpret_cast(&in_vec[i]); - executeOneString(in_pos, in_pos + sizeof(in_vec[i]), end); + executeOneString(in_pos, in_pos + sizeof(T), out); - pos += end - begin; + pos += FLOAT_HEX_LENGTH; out_offsets[i] = pos; } - out_vec.resize(pos); - col_res = std::move(col_str); return true; } diff --git a/dbms/tests/queries/0_stateless/01013_hex_float.reference b/dbms/tests/queries/0_stateless/01013_hex_float.reference index ac428aa6bea..c3e4ec26847 100644 --- a/dbms/tests/queries/0_stateless/01013_hex_float.reference +++ b/dbms/tests/queries/0_stateless/01013_hex_float.reference @@ -4,4 +4,9 @@ 2342920CA19CC73B 7DC39425AD49B254 2C616D8C9DF0423F -BA490C022BFF5EC0 +3BDF4F8D97FE5EC0 +0A57C742 +00004843 +00004943 +0000000000406940 +0000000000606940 diff --git a/dbms/tests/queries/0_stateless/01013_hex_float.sql b/dbms/tests/queries/0_stateless/01013_hex_float.sql index e6da504657f..30869529d87 100644 --- a/dbms/tests/queries/0_stateless/01013_hex_float.sql +++ b/dbms/tests/queries/0_stateless/01013_hex_float.sql @@ -4,4 +4,7 @@ SELECT hex(1e+18); SELECT hex(1e-20); SELECT hex(1e+100); SELECT hex(0.000578); -SELECt hex(-123.987); +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);