This is an automated email from the ASF dual-hosted git repository.

laskoviymishka pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg-go.git


The following commit(s) were added to refs/heads/main by this push:
     new 20bef470a fix(transforms): zero-pad year partition values to match 
Java (#2688)
20bef470a is described below

commit 20bef470a0fd774cc77b2efbd96cd80d347e3ba1
Author: Edward Shen <[email protected]>
AuthorDate: Tue Nov 24 21:17:00 2026 +0800

    fix(transforms): zero-pad year partition values to match Java (#2688)
    
    YearTransform.ToHumanString rendered the year with strconv.Itoa, so
    years below 1000 lost their leading zeros: 0001-01-01 landed in
    ts_year=1/ and 0935-07-12 in ts_year=935/. Java's
    TransformUtil.humanYear and PyIceberg's to_human_year pad to four
    digits with the sign inside the width, so a table written by
    iceberg-go alongside Spark or PyIceberg gets two different
    directories for the same partition value.
    
    Format with %04d, matching both implementations, including year 0,
    negative years (-001) and years beyond 9999, which are not truncated.
    
    Fixes #2603
---
 table/partition_path_test.go | 19 ++++++++++++++++++-
 transforms.go                |  3 ++-
 transforms_test.go           | 34 ++++++++++++++++++++++++++++++++++
 website/src/releases.md      |  1 +
 4 files changed, 55 insertions(+), 2 deletions(-)

diff --git a/table/partition_path_test.go b/table/partition_path_test.go
index 358cecd36..2124a6fb2 100644
--- a/table/partition_path_test.go
+++ b/table/partition_path_test.go
@@ -56,6 +56,10 @@ func TestPartitionPathPlanMatchesPartitionToPath(t 
*testing.T) {
                        SourceIDs: []int{3}, FieldID: 1004,
                        Transform: unknown, Name: "unknown",
                },
+               iceberg.PartitionField{
+                       SourceIDs: []int{1}, FieldID: 1005,
+                       Transform: iceberg.YearTransform{}, Name: 
"created_year",
+               },
        )
 
        recordSchema := arrow.NewSchema([]arrow.Field{
@@ -72,10 +76,23 @@ func TestPartitionPathPlanMatchesPartitionToPath(t 
*testing.T) {
                int32(7),
                nil,
                int32(42),
+               int32(54),
        }
 
        assert.Equal(t, spec.PartitionToPath(record, schema), 
plan.pathPlan.format(record))
        assert.Equal(t,
-               
"created+ts%2Ftz=2024-01-15T10%3A30%3A00%2B00%3A00/str%23part=partition%2Fvalue/id_bucket=7/missing=null/unknown=42",
+               
"created+ts%2Ftz=2024-01-15T10%3A30%3A00%2B00%3A00/str%23part=partition%2Fvalue/id_bucket=7/missing=null/unknown=42/created_year=2024",
                plan.pathPlan.format(record))
+
+       // the sentinel date keeps its leading zeros on the writer path too
+       early := partitionRecord{
+               iceberg.Timestamp(-62135596800000000),
+               "partition/value",
+               int32(7),
+               nil,
+               int32(42),
+               int32(-1969), // year 1
+       }
+       assert.Equal(t, spec.PartitionToPath(early, schema), 
plan.pathPlan.format(early))
+       assert.Contains(t, plan.pathPlan.format(early), "/created_year=0001")
 }
diff --git a/transforms.go b/transforms.go
index a352e2870..e16347279 100644
--- a/transforms.go
+++ b/transforms.go
@@ -1200,7 +1200,8 @@ func (YearTransform) Apply(value Optional[Literal]) (out 
Optional[Literal]) {
 func (YearTransform) ToHumanString(_ Type, val any) string {
        switch v := val.(type) {
        case int32:
-               return strconv.Itoa(int(v) + epochTM.Year())
+               // %04d matches Java's TransformUtil.humanYear and PyIceberg's 
to_human_year.
+               return fmt.Sprintf("%04d", int(v)+epochTM.Year())
        default:
                return "null"
        }
diff --git a/transforms_test.go b/transforms_test.go
index 08aa3466d..7b1109d9c 100644
--- a/transforms_test.go
+++ b/transforms_test.go
@@ -234,6 +234,11 @@ func TestToHumanStringValues(t *testing.T) {
                {iceberg.HourTransform{}, iceberg.PrimitiveTypes.Int32, 
int32(4645896), "2500-01-01-00"},
                {iceberg.HourTransform{}, iceberg.PrimitiveTypes.Int32, 
int32(-4119936), "1500-01-01-00"},
                {iceberg.YearTransform{}, iceberg.PrimitiveTypes.Int32, 
int32(-1), "1969"},
+               {iceberg.YearTransform{}, iceberg.PrimitiveTypes.Int32, 
int32(-1035), "0935"},
+               {iceberg.YearTransform{}, iceberg.PrimitiveTypes.Int32, 
int32(-1969), "0001"},
+               {iceberg.YearTransform{}, iceberg.PrimitiveTypes.Int32, 
int32(-1970), "0000"},
+               {iceberg.YearTransform{}, iceberg.PrimitiveTypes.Int32, 
int32(-1971), "-001"},
+               {iceberg.YearTransform{}, iceberg.PrimitiveTypes.Int32, 
int32(10375), "12345"},
                {iceberg.MonthTransform{}, iceberg.PrimitiveTypes.Int32, 
int32(-1), "1969-12"},
                {iceberg.DayTransform{}, iceberg.PrimitiveTypes.Date, 
int32(-1), "1969-12-31"},
                {iceberg.HourTransform{}, iceberg.PrimitiveTypes.Int32, 
int32(-1), "1969-12-31-23"},
@@ -388,6 +393,35 @@ func TestPartitionToPath_TimestampTzIdentity(t *testing.T) 
{
        assert.Equal(t, expected, spec.PartitionToPath(record, schema))
 }
 
+func TestPartitionToPath_YearMatchesJava(t *testing.T) {
+       schema := iceberg.NewSchema(0,
+               iceberg.NestedField{ID: 1, Name: "ts", Type: 
iceberg.PrimitiveTypes.Date, Required: true},
+       )
+       spec := iceberg.NewPartitionSpecID(0,
+               iceberg.PartitionField{
+                       SourceIDs: []int{1}, FieldID: 1000,
+                       Transform: iceberg.YearTransform{}, Name: "ts_year",
+               },
+       )
+
+       tests := []struct {
+               name     string
+               year     int32
+               expected string
+       }{
+               {"four digit year", int32(47), "ts_year=2017"},
+               {"year below 1000 is zero padded", int32(-1035), 
"ts_year=0935"},
+               {"year 1 is zero padded", int32(-1969), "ts_year=0001"},
+               {"negative year keeps the sign in the padded width", 
int32(-1971), "ts_year=-001"},
+               {"five digit year is not truncated", int32(10375), 
"ts_year=12345"},
+       }
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       assert.Equal(t, tt.expected, 
spec.PartitionToPath(partitionRecord{tt.year}, schema))
+               })
+       }
+}
+
 func TestManifestPartitionVals(t *testing.T) {
        // Sanity checks that the source and result types of the transform are
        // compatible with their use to generate partition data in manifests.
diff --git a/website/src/releases.md b/website/src/releases.md
index 3c668bb41..5591a2978 100644
--- a/website/src/releases.md
+++ b/website/src/releases.md
@@ -130,6 +130,7 @@ Per-release notes (highlights, breaking changes, 
contributors) are published on
 - **`table/substrait` is now internal.** The package moved to 
`table/internal/substrait`, so `substrait.ConvertExpr`, 
`substrait.ConvertSchema` and `substrait.NewExtensionSet` are no longer 
exported. There is no replacement; scans and deletes still convert filters 
internally.
 
 - **Transform formatting requires the result type and uses the Java-aligned 
method name.** `Transform.ToHumanStr(value)` and 
`Transform.ToHumanStrType(resultType, value)` are removed. Use 
`Transform.ToHumanString(resultType, value)` to format partition values, 
including timestamps with time zones.
+- **Year partition paths are zero-padded to four digits.** 
`YearTransform.ToHumanString` now formats years below 1000 with leading zeros, 
as Java and PyIceberg do, so `0001-01-01` lands in `ts_year=0001/` instead of 
`ts_year=1/`. New files for such partitions are written to a different 
directory than earlier iceberg-go releases used. Reads are unaffected because 
partition values come from manifests, not paths. Tooling that matches on 
partition paths should use the padded form.
 - **Catalogs now require cleanup.** The `catalog.Catalog` interface includes 
`Close() error`. Custom catalog implementations must provide `Close`, and 
callers should close catalog instances after use. `catalog.Closer` is removed. 
Replace `cat.(catalog.Closer)` checks with `defer cat.Close()`; 
`catalog.Catalog` already implements `io.Closer`, so no type assertion is 
needed.
 
 - **`table.ErrDataFilesMissing` no longer matches `table.ErrCommitFailed`.** 
An operation that adds delete files referencing data files that are not live, 
for example because a concurrent commit removed them, fails with 
`ErrDataFilesMissing`. The error is now terminal, like 
`table.ErrCommitDiverged`, matching Java's `ValidationException`, so code that 
retries when `errors.Is(err, table.ErrCommitFailed)` no longer retries it. When 
`Transaction.Commit` returns it, the error also matches `t [...]

Reply via email to