You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iceberg.apache.org by ze...@apache.org on 2026/08/17 19:19:06 UTC

(iceberg-go) branch main updated: fix(catalog): reject invalid current metadata version before staging (#1719)

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

zeroshade 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 e68501067 fix(catalog): reject invalid current metadata version before staging (#1719)
e68501067 is described below

commit e68501067e37f39e33f90b1197fa7bb2c57f9d15
Author: Minh Vu <vu...@gmail.com>
AuthorDate: Mon Aug 17 21:19:01 2026 +0200

    fix(catalog): reject invalid current metadata version before staging (#1719)
    
    * fix(catalog): reject trailing metadata filename junk
    
    * fix(catalog): reject invalid metadata versions
    
    * style: format metadata filename regex vars
    
    * fix(catalog): validate metadata version before staging
    
    * fix(catalog): reject empty existing metadata locations
---
 catalog/internal/update_stage_test.go | 110 ++++++++++++++++++++++++++++++++++
 catalog/internal/utils.go             |  29 ++++++---
 2 files changed, 131 insertions(+), 8 deletions(-)

diff --git a/catalog/internal/update_stage_test.go b/catalog/internal/update_stage_test.go
new file mode 100644
index 000000000..8f0bf82f6
--- /dev/null
+++ b/catalog/internal/update_stage_test.go
@@ -0,0 +1,110 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package internal
+
+import (
+	"context"
+	"testing"
+
+	"github.com/apache/iceberg-go"
+	"github.com/apache/iceberg-go/table"
+	"github.com/stretchr/testify/require"
+)
+
+func TestUpdateAndStageTableRejectsInvalidMetadataLocation(t *testing.T) {
+	metadata, err := table.NewMetadata(
+		iceberg.NewSchema(0),
+		nil,
+		table.UnsortedSortOrder,
+		"file:///tmp/table",
+		nil,
+	)
+	require.NoError(t, err)
+
+	current := table.New(
+		[]string{"db", "tbl"},
+		metadata,
+		"file:///tmp/table/metadata/current.metadata.json",
+		nil,
+		nil,
+	)
+
+	staged, err := UpdateAndStageTable(
+		context.Background(),
+		nil,
+		current,
+		current.Identifier(),
+		nil,
+		nil,
+		nil,
+	)
+	require.Error(t, err)
+	require.Nil(t, staged)
+	require.Contains(t, err.Error(), "invalid metadata location")
+}
+
+func TestUpdateAndStageTableRejectsEmptyMetadataLocationForExistingTable(t *testing.T) {
+	metadata, err := table.NewMetadata(
+		iceberg.NewSchema(0),
+		nil,
+		table.UnsortedSortOrder,
+		"file:///tmp/table",
+		nil,
+	)
+	require.NoError(t, err)
+
+	current := table.New(
+		[]string{"db", "tbl"},
+		metadata,
+		"",
+		nil,
+		nil,
+	)
+
+	staged, err := UpdateAndStageTable(
+		context.Background(),
+		nil,
+		current,
+		current.Identifier(),
+		nil,
+		nil,
+		nil,
+	)
+	require.Error(t, err)
+	require.Nil(t, staged)
+	require.Contains(t, err.Error(), "invalid metadata location")
+}
+
+func TestParseMetadataVersionAcceptsLegacyMetadataLocation(t *testing.T) {
+	require.Equal(t, 1, ParseMetadataVersion("file:///tmp/table/metadata/v1.metadata.json"))
+	require.Equal(t, 2, ParseMetadataVersion("file:///tmp/table/metadata/v2.gz.metadata.json"))
+}
+
+func TestUpdateAndStageTableUsesVersionZeroForCreate(t *testing.T) {
+	staged, err := UpdateAndStageTable(
+		context.Background(),
+		nil,
+		nil,
+		[]string{"db", "tbl"},
+		nil,
+		[]table.Update{table.NewSetLocationUpdate("file:///tmp/table")},
+		nil,
+	)
+	require.NoError(t, err)
+	require.Equal(t, 0, ParseMetadataVersion(staged.MetadataLocation()))
+}
diff --git a/catalog/internal/utils.go b/catalog/internal/utils.go
index 39cb64679..fbb088e83 100644
--- a/catalog/internal/utils.go
+++ b/catalog/internal/utils.go
@@ -229,7 +229,10 @@ func getDefaultWarehouseLocation(namespaceKey, tablename string, nsprops, catpro
 // ([\w-]{36})      -> UUID (36 characters, including hyphens)
 // (?:\.\w+)?       -> optional codec name
 // \.metadata\.json -> file extension at the end of the filename
-var tableMetadataFileNameRegex = regexp.MustCompile(`^(\d+)-([\w-]{36})(?:\.\w+)?\.metadata\.json$`)
+var (
+	tableMetadataFileNameRegex       = regexp.MustCompile(`^(\d+)-([\w-]{36})(?:\.\w+)?\.metadata\.json$`)
+	legacyTableMetadataFileNameRegex = regexp.MustCompile(`^v(\d+)(?:\.gz)?\.metadata\.json$`)
+)
 
 func ParseMetadataVersion(location string) int {
 	if strings.HasSuffix(location, "/") {
@@ -239,12 +242,15 @@ func ParseMetadataVersion(location string) int {
 	fileName := path.Base(location)
 	matches := tableMetadataFileNameRegex.FindStringSubmatch(fileName)
 
-	if len(matches) != 3 {
-		return -1
-	}
-
-	if _, err := uuid.Parse(matches[2]); err != nil {
-		return -1
+	if len(matches) == 3 {
+		if _, err := uuid.Parse(matches[2]); err != nil {
+			return -1
+		}
+	} else {
+		matches = legacyTableMetadataFileNameRegex.FindStringSubmatch(fileName)
+		if len(matches) != 2 {
+			return -1
+		}
 	}
 
 	v, err := strconv.Atoi(matches[1])
@@ -273,6 +279,14 @@ func UpdateAndStageTable(ctx context.Context, catprops iceberg.Properties, curre
 			return nil, err
 		}
 	}
+	newVersion := 0
+	if current != nil {
+		metadataVersion := ParseMetadataVersion(metadataLoc)
+		if metadataVersion < 0 {
+			return nil, fmt.Errorf("invalid metadata location: %s", metadataLoc)
+		}
+		newVersion = metadataVersion + 1
+	}
 	for _, r := range reqs {
 		if err := r.Validate(currentMetadata); err != nil {
 			return nil, err
@@ -289,7 +303,6 @@ func UpdateAndStageTable(ctx context.Context, catprops iceberg.Properties, curre
 		return nil, err
 	}
 
-	newVersion := ParseMetadataVersion(metadataLoc) + 1
 	newLocation, err := provider.NewTableMetadataFileLocation(newVersion)
 	if err != nil {
 		return nil, err