Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/iceberg/expression/json_serde.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* under the License.
*/

#include <cmath>
#include <cstdint>
#include <limits>
#include <string>
Expand Down Expand Up @@ -368,17 +369,29 @@ Result<Literal> LiteralFromJson(const nlohmann::json& json, const Type* type) {
return Literal::Long(val);
}

case TypeId::kFloat:
if (!json.is_number_float()) [[unlikely]] {
case TypeId::kFloat: {
if (!json.is_number()) [[unlikely]] {
return JsonParseError("Cannot parse {} as a float value", SafeDumpJson(json));
}
return Literal::Float(json.get<float>());
auto val = json.get<float>();
if (!std::isfinite(val)) [[unlikely]] {
return JsonParseError("Cannot parse {} as a float value: out of range",
SafeDumpJson(json));
}
return Literal::Float(val);
}

case TypeId::kDouble:
if (!json.is_number_float()) [[unlikely]] {
case TypeId::kDouble: {
if (!json.is_number()) [[unlikely]] {
return JsonParseError("Cannot parse {} as a double value", SafeDumpJson(json));
}
return Literal::Double(json.get<double>());
auto val = json.get<double>();
if (!std::isfinite(val)) [[unlikely]] {
return JsonParseError("Cannot parse {} as a double value: out of range",
SafeDumpJson(json));
}
return Literal::Double(val);
}

case TypeId::kString:
if (!json.is_string()) [[unlikely]] {
Expand Down
68 changes: 68 additions & 0 deletions src/iceberg/test/expression_json_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,74 @@ TEST(LiteralFromJsonTest, AcceptsNegativeIntegerUntyped) {
EXPECT_EQ(lit.ToString(), "-123");
}

TEST(LiteralFromJsonTest, FloatAndDoubleValues) {
struct Case {
std::string json;
Literal expected;
};
const std::vector<Case> float_cases = {
{.json = "0", .expected = Literal::Float(0.0f)},
{.json = "-1", .expected = Literal::Float(-1.0f)},
{.json = "2147483648", .expected = Literal::Float(2147483648.0f)},
{.json = "9223372036854775808", .expected = Literal::Float(9223372036854775808.0f)},
{.json = "16777217", .expected = Literal::Float(16777216.0f)},
{.json = "3.4028235e38",
.expected = Literal::Float(std::numeric_limits<float>::max())},
};
for (const auto& c : float_cases) {
SCOPED_TRACE(c.json);
ICEBERG_UNWRAP_OR_FAIL(
auto lit, LiteralFromJson(nlohmann::json::parse(c.json), float32().get()));
EXPECT_EQ(lit.type()->type_id(), TypeId::kFloat);
EXPECT_EQ(lit, c.expected);
}

const std::vector<Case> double_cases = {
{.json = "0", .expected = Literal::Double(0.0)},
{.json = "-1", .expected = Literal::Double(-1.0)},
{.json = "2147483648", .expected = Literal::Double(2147483648.0)},
{.json = "9223372036854775808", .expected = Literal::Double(9223372036854775808.0)},
{.json = "9007199254740993", .expected = Literal::Double(9007199254740992.0)},
{.json = "1" + std::string(39, '0'), .expected = Literal::Double(1e39)},
};
for (const auto& c : double_cases) {
SCOPED_TRACE(c.json);
ICEBERG_UNWRAP_OR_FAIL(
auto lit, LiteralFromJson(nlohmann::json::parse(c.json), float64().get()));
EXPECT_EQ(lit.type()->type_id(), TypeId::kDouble);
EXPECT_EQ(lit, c.expected);
}

ICEBERG_UNWRAP_OR_FAIL(auto one, LiteralFromJson(nlohmann::json(1), float64().get()));
ICEBERG_UNWRAP_OR_FAIL(auto one_json, ToJson(one));
EXPECT_EQ(one_json.dump(), "1.0");
}

TEST(LiteralFromJsonTest, RejectsInvalidFloatAndDouble) {
for (const auto& json : {nlohmann::json::parse("1e39"), nlohmann::json::parse("-1e39"),
nlohmann::json::parse("1" + std::string(39, '0')),
nlohmann::json(std::numeric_limits<double>::infinity())}) {
SCOPED_TRACE(json.dump());
EXPECT_THAT(LiteralFromJson(json, float32().get()), HasErrorMessage("out of range"));
}
for (const auto& json : {nlohmann::json(std::numeric_limits<double>::infinity()),
nlohmann::json(-std::numeric_limits<double>::infinity())}) {
SCOPED_TRACE(json.dump());
EXPECT_THAT(LiteralFromJson(json, float64().get()), HasErrorMessage("out of range"));
}
for (const std::shared_ptr<PrimitiveType>& type :
{std::shared_ptr<PrimitiveType>(float32()),
std::shared_ptr<PrimitiveType>(float64())}) {
for (const auto& json :
{nlohmann::json("1.5"), nlohmann::json("1"), nlohmann::json(true)}) {
SCOPED_TRACE(type->ToString() + " " + json.dump());
EXPECT_THAT(LiteralFromJson(json, type.get()),
HasErrorMessage("Cannot parse " + json.dump() + " as a " +
type->ToString() + " value"));
}
}
}

struct SchemaAwarePredicateParam {
std::string name;
std::string field_name;
Expand Down
Loading