未验证 提交 695e2807 编写于 作者: A alexey-milovidov 提交者: GitHub

Merge pull request #19960 from ClickHouse/json-extract-inaccurate-conversion-from-double-to-float

Allow inaccurate conversion from double to float in function JSONExtract beacuse the users want that
......@@ -25,6 +25,7 @@
#include <DataTypes/DataTypeTuple.h>
#include <Interpreters/Context.h>
#include <ext/range.h>
#include <type_traits>
#include <boost/tti/has_member_function.hpp>
#if !defined(ARCADIA_BUILD)
......@@ -507,11 +508,20 @@ public:
}
else if (element.isDouble())
{
if (!accurate::convertNumeric(element.getDouble(), value))
if constexpr (std::is_floating_point_v<NumberType>)
{
/// We permit inaccurate conversion of double to float.
/// Example: double 0.1 from JSON is not representable in float.
/// But it will be more convenient for user to perform conversion.
value = element.getDouble();
}
else if (!accurate::convertNumeric(element.getDouble(), value))
return false;
}
else if (element.isBool() && is_integer_v<NumberType> && convert_bool_to_integer)
{
value = static_cast<NumberType>(element.getBool());
}
else
return false;
......
1.1 1.1 1.1 1.1
0.01 0.01 0.01 0.01
0
\N
-1e300
-inf
0
0
0
0
WITH '{ "v":1.1}' AS raw
SELECT
JSONExtract(raw, 'v', 'float') AS float32_1,
JSONExtract(raw, 'v', 'Float32') AS float32_2,
JSONExtractFloat(raw, 'v') AS float64_1,
JSONExtract(raw, 'v', 'double') AS float64_2;
WITH '{ "v":1E-2}' AS raw
SELECT
JSONExtract(raw, 'v', 'float') AS float32_1,
JSONExtract(raw, 'v', 'Float32') AS float32_2,
JSONExtractFloat(raw, 'v') AS float64_1,
JSONExtract(raw, 'v', 'double') AS float64_2;
SELECT JSONExtract('{"v":1.1}', 'v', 'UInt64');
SELECT JSONExtract('{"v":1.1}', 'v', 'Nullable(UInt64)');
SELECT JSONExtract('{"v":-1e300}', 'v', 'Float64');
SELECT JSONExtract('{"v":-1e300}', 'v', 'Float32');
SELECT JSONExtract('{"v":-1e300}', 'v', 'UInt64');
SELECT JSONExtract('{"v":-1e300}', 'v', 'Int64');
SELECT JSONExtract('{"v":-1e300}', 'v', 'UInt8');
SELECT JSONExtract('{"v":-1e300}', 'v', 'Int8');
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册