Skip to content
Merged
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
52 changes: 52 additions & 0 deletions src/iceberg/test/partition_spec_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@

#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <nlohmann/json.hpp>

#include "iceberg/json_internal.h"
#include "iceberg/partition_field.h"
#include "iceberg/schema.h"
#include "iceberg/schema_field.h"
Expand Down Expand Up @@ -106,4 +108,54 @@ TEST(PartitionSpecTest, PartitionSchemaTest) {
EXPECT_EQ(pt_field2.name(), partition_schema.value()->fields()[1].name());
EXPECT_EQ(pt_field2.field_id(), partition_schema.value()->fields()[1].field_id());
}

TEST(PartitionSpecTest, PartitionTypeTest) {
nlohmann::json json = R"(
{
"spec-id": 1,
"fields": [ {
"source-id": 4,
"field-id": 1000,
"name": "ts_day",
"transform": "day"
}, {
"source-id": 1,
"field-id": 1001,
"name": "id_bucket",
"transform": "bucket[16]"
}, {
"source-id": 2,
"field-id": 1002,
"name": "id_truncate",
"transform": "truncate[4]"
} ]
})"_json;

SchemaField field1(1, "id", int32(), false);
SchemaField field2(2, "name", string(), false);
SchemaField field3(3, "ts", timestamp(), false);
SchemaField field4(4, "ts_day", timestamp(), false);
SchemaField field5(5, "id_bucket", int32(), false);
SchemaField field6(6, "id_truncate", int32(), false);
auto const schema = std::make_shared<Schema>(
std::vector<SchemaField>{field1, field2, field3, field4, field5, field6},
Schema::kInitialSchemaId);

auto parsed_spec_result = PartitionSpecFromJson(schema, json);
ASSERT_TRUE(parsed_spec_result.has_value()) << parsed_spec_result.error().message;

auto partition_schema = parsed_spec_result.value()->PartitionType();

SchemaField pt_field1(1000, "ts_day", date(), true);
SchemaField pt_field2(1001, "id_bucket", int32(), true);
SchemaField pt_field3(1002, "id_truncate", string(), true);

ASSERT_TRUE(partition_schema.has_value());
ASSERT_EQ(3, partition_schema.value()->fields().size());

EXPECT_EQ(pt_field1, partition_schema.value()->fields()[0]);
EXPECT_EQ(pt_field2, partition_schema.value()->fields()[1]);
EXPECT_EQ(pt_field3, partition_schema.value()->fields()[2]);
}

} // namespace iceberg
2 changes: 1 addition & 1 deletion src/iceberg/test/transform_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ TEST(TransformResultTypeTest, PositiveCases) {
.expected_result_type = iceberg::int32()},
{.str = "day",
.source_type = iceberg::timestamp(),
.expected_result_type = iceberg::int32()},
.expected_result_type = iceberg::date()},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding tests for Wire Format Compatibility would be better. The PR changes the return type from int32() to date() but doesn't verify:

  1. That date() values serialize to int32 on the wire
  2. That existing partitioned data files can still be read
  3. That this change is backward compatible with tables partitioned using the old int32 type

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid we don't have fully covered integration test yet. I added a test for PartitionSpec, though I'm not sure if that's sufficient.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SG

{.str = "hour",
.source_type = iceberg::timestamp(),
.expected_result_type = iceberg::int32()},
Expand Down
5 changes: 5 additions & 0 deletions src/iceberg/transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ class ICEBERG_EXPORT TransformFunction {
/// \brief Get the source type of transform function
const std::shared_ptr<Type>& source_type() const;
/// \brief Get the result type of transform function
///
/// Note: This method defines both the physical and display representation of the
/// partition field. The physical representation must conform to the Iceberg spec. The
/// display representation can deviate from the spec, such as by transforming the value
/// into a more human-readable format.
virtual std::shared_ptr<Type> ResultType() const = 0;

friend bool operator==(const TransformFunction& lhs, const TransformFunction& rhs) {
Expand Down
2 changes: 1 addition & 1 deletion src/iceberg/transform_function.cc
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ Result<Literal> DayTransform::Transform(const Literal& literal) {
return TemporalUtils::ExtractDay(literal);
}

std::shared_ptr<Type> DayTransform::ResultType() const { return int32(); }
std::shared_ptr<Type> DayTransform::ResultType() const { return date(); }

Result<std::unique_ptr<TransformFunction>> DayTransform::Make(
std::shared_ptr<Type> const& source_type) {
Expand Down
6 changes: 5 additions & 1 deletion src/iceberg/transform_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ class ICEBERG_EXPORT DayTransform : public TransformFunction {
/// \brief Extract a date or timestamp day, as days from 1970-01-01.
Result<Literal> Transform(const Literal& literal) override;

/// \brief Returns INT32 as the output type.
/// \brief Return the result type of a day transform.
///
/// Note: The physical representation conforms to the Iceberg spec as DateType is
/// internally converted to int. The DateType returned here provides a more
/// human-readable way to display the partition field.
std::shared_ptr<Type> ResultType() const override;

/// \brief Create a DayTransform.
Expand Down
Loading