Skip to content
Draft
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
5 changes: 4 additions & 1 deletion be/src/exec/sink/writer/iceberg/partition_transformers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#include "exec/sink/writer/iceberg/partition_transformers.h"

#include <fmt/format.h>

#include <any>
#include <cmath>

Expand All @@ -35,7 +37,8 @@ std::string floating_point_partition_value_to_string(T value) {
if (std::isinf(value)) {
return value > 0 ? "Infinity" : "-Infinity";
}
return std::to_string(value);
// Distinct partitions such as 1.0000001 and 1.0000002 must round-trip without rounding.
return fmt::format("{}", value);
}

} // namespace
Expand Down
48 changes: 48 additions & 0 deletions be/test/exec/sink/writer/iceberg/partition_transformers_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@

#include "exec/sink/writer/iceberg/partition_transformers.h"

#include <fast_float/fast_float.h>
#include <gtest/gtest.h>

#include <bit>
#include <cmath>
#include <limits>
#include <system_error>
#include <type_traits>

#include "core/data_type/data_type_date_or_datetime_v2.h"
#include "core/data_type/data_type_timestamp_ns.h"
Expand All @@ -34,6 +39,41 @@ class PartitionTransformersTest : public testing::Test {
public:
PartitionTransformersTest() = default;
virtual ~PartitionTransformersTest() = default;

template <typename T>
void check_floating_point_partition_round_trip(PrimitiveType primitive_type) {
auto type = DataTypeFactory::instance().create_data_type(primitive_type, false);
IdentityPartitionColumnTransform transform(type);
const std::vector<T> values = {T(1.0000001),
T(1.0000002),
std::nextafter(T(1), T(2)),
T(-1.0000001),
T(1e-8),
T(-1e-8),
std::numeric_limits<T>::min(),
-std::numeric_limits<T>::min(),
std::numeric_limits<T>::max(),
-std::numeric_limits<T>::max(),
std::numeric_limits<T>::denorm_min(),
-std::numeric_limits<T>::denorm_min(),
T(0.0),
T(-0.0)};
using Bits = std::conditional_t<sizeof(T) == sizeof(UInt32), UInt32, UInt64>;
for (T value : values) {
const auto serialized = transform.get_partition_value(type, value);
SCOPED_TRACE(serialized);
T parsed;
const auto result = fast_float::from_chars(
serialized.data(), serialized.data() + serialized.size(), parsed);
ASSERT_EQ(std::errc {}, result.ec);
ASSERT_EQ(serialized.data() + serialized.size(), result.ptr);
EXPECT_EQ(std::bit_cast<Bits>(value), std::bit_cast<Bits>(parsed));
EXPECT_EQ(serialized, transform.to_human_string(type, value));
}

EXPECT_NE(transform.to_human_string(type, T(1.0000001)),
transform.to_human_string(type, T(1.0000002)));
}
};

TEST_F(PartitionTransformersTest, human_hour_floors_negative_ordinals) {
Expand Down Expand Up @@ -301,6 +341,14 @@ TEST_F(PartitionTransformersTest, test_string_truncate_transform) {
}
}

TEST_F(PartitionTransformersTest, test_float_partition_value_round_trip) {
check_floating_point_partition_round_trip<Float32>(TYPE_FLOAT);
}

TEST_F(PartitionTransformersTest, test_double_partition_value_round_trip) {
check_floating_point_partition_round_trip<Float64>(TYPE_DOUBLE);
}

TEST_F(PartitionTransformersTest, test_floating_point_special_partition_value) {
auto float_type =
DataTypeFactory::instance().create_data_type(PrimitiveType::TYPE_FLOAT, false);
Expand Down
Loading