badalprasadsingh commented on code in PR #2435: URL: https://github.com/apache/iceberg-go/pull/2435#discussion_r4509847103
########## table/transaction_location.go: ########## @@ -0,0 +1,67 @@ +// 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 table + +import ( + "fmt" + "net/url" + "strings" + + "github.com/apache/iceberg-go" +) + +// SetLocation stages a change to the table's base location. Subsequent +// writes in the transaction use the new location; existing files are not moved. +// Trailing slashes are stripped so generated paths have no double separators. +// Changes across storage schemes or buckets require multi-location FileIO support. +// Call Commit to persist the change. +func (t *Transaction) SetLocation(location string) error { + location = strings.TrimRight(location, "/") + if strings.TrimSpace(location) == "" { + return fmt.Errorf("%w: table location must not be empty", iceberg.ErrInvalidArgument) + } + if _, err := t.txnMeta(); err != nil { + return err + } + + current, err := url.Parse(t.tbl.Location()) + if err != nil { + return fmt.Errorf("%w: invalid current table location: %v", iceberg.ErrInvalidArgument, err) + } + next, err := url.Parse(location) + if err != nil { + return fmt.Errorf("%w: invalid table location: %v", iceberg.ErrInvalidArgument, err) + } + if current.Scheme != next.Scheme || current.Host != next.Host { Review Comment: P1: azure keeps the container in the user part (`abfss://container@account...`), and the fileio is scoped to it: a move from container c1 to c2 passes this check, tghen reads fail after LoadTable please compare `current.User.String() != next.User.String()` and add an Azure case to the test? ########## table/transaction_location_test.go: ########## @@ -0,0 +1,346 @@ +// 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 table_test + +import ( + "encoding/json" + "path/filepath" + "strings" + "testing" + + "github.com/apache/arrow-go/v18/arrow/array" + "github.com/apache/arrow-go/v18/arrow/memory" + "github.com/apache/iceberg-go" + "github.com/apache/iceberg-go/table" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestTransactionSetLocationStagesMetadataUpdate(t *testing.T) { + tbl := newCommitTestTable(t) + original := tbl.Location() + tx := tbl.NewTransaction() + + newLocation := "s3://bucket/new-location///" + require.NoError(t, tx.SetLocation(newLocation)) + + staged, err := tx.StagedTable() + require.NoError(t, err) + assert.Equal(t, "s3://bucket/new-location", staged.Location()) + assert.Equal(t, original, tbl.Location(), "the original table stays unchanged until commit") + + commit, err := tx.TableCommit() + require.NoError(t, err) + require.Len(t, commit.Updates, 1) + assert.Equal(t, table.UpdateSetLocation, commit.Updates[0].Action()) + updateJSON, err := json.Marshal(commit.Updates[0]) + require.NoError(t, err) + assert.JSONEq(t, `{"action":"set-location","location":"s3://bucket/new-location"}`, string(updateJSON)) + require.Len(t, commit.Requirements, 2) + assert.Equal(t, "assert-ref-snapshot-id", commit.Requirements[0].GetType()) + assert.Equal(t, "assert-table-uuid", commit.Requirements[1].GetType()) +} + +func TestTransactionSetLocationNoOp(t *testing.T) { + tbl := newCommitTestTable(t) + tx := tbl.NewTransaction() + require.NoError(t, tx.SetLocation(tbl.Location())) + + commit, err := tx.TableCommit() + require.NoError(t, err) + assert.Empty(t, commit.Updates) +} + +func TestTransactionSetLocationRepeatedSameLocation(t *testing.T) { + tx := newCommitTestTable(t).NewTransaction() + require.NoError(t, tx.SetLocation("s3://bucket/new")) + require.NoError(t, tx.SetLocation("s3://bucket/new")) + + commit, err := tx.TableCommit() + require.NoError(t, err) + require.Len(t, commit.Updates, 1) +} + +func TestTransactionSetLocationStripsTrailingSlash(t *testing.T) { + tx := newCommitTestTable(t).NewTransaction() + require.NoError(t, tx.SetLocation("s3://bucket/new/")) + + staged, err := tx.StagedTable() + require.NoError(t, err) + assert.Equal(t, "s3://bucket/new", staged.Location()) +} + +func TestTransactionSetLocationRejectsEmptyLocation(t *testing.T) { + for _, location := range []string{"", " ", "\t\n", "///"} { + t.Run("location="+location, func(t *testing.T) { + tx := newCommitTestTable(t).NewTransaction() + err := tx.SetLocation(location) + require.ErrorIs(t, err, iceberg.ErrInvalidArgument) + + commit, err := tx.TableCommit() + require.NoError(t, err) + assert.Empty(t, commit.Updates) + }) + } +} + +func TestTransactionSetLocationTrailingSlashNoOp(t *testing.T) { + tbl := newCommitTestTable(t) + tx := tbl.NewTransaction() + require.NoError(t, tx.SetLocation(tbl.Location()+"///")) + + commit, err := tx.TableCommit() + require.NoError(t, err) + assert.Empty(t, commit.Updates) +} + +func TestTransactionSetLocationRejectsCrossStorageBoundary(t *testing.T) { + for _, location := range []string{ + "s3://another-bucket/relocated", + "gs://bucket/relocated", + "/tmp/relocated", + } { + t.Run(location, func(t *testing.T) { + tx := newCommitTestTable(t).NewTransaction() + require.ErrorIs(t, tx.SetLocation(location), iceberg.ErrNotImplemented) + + commit, err := tx.TableCommit() + require.NoError(t, err) + assert.Empty(t, commit.Updates) + }) + } +} + +func TestTransactionSetLocationPreservesOtherUpdates(t *testing.T) { + tx := newCommitTestTable(t).NewTransaction() + require.NoError(t, tx.SetProperties(iceberg.Properties{"test.property": "value"})) + require.NoError(t, tx.SetLocation("s3://bucket/relocated")) + + commit, err := tx.TableCommit() + require.NoError(t, err) + require.Len(t, commit.Updates, 2) + assert.Equal(t, table.UpdateSetProperties, commit.Updates[0].Action()) + assert.Equal(t, table.UpdateSetLocation, commit.Updates[1].Action()) +} + +func TestTransactionSetLocationAfterCommitIsRejected(t *testing.T) { + tx := newCommitTestTable(t).NewTransaction() + tx.MarkCommitted() + + err := tx.SetLocation("s3://bucket/new") + require.ErrorContains(t, err, "already been committed") +} + +func TestTransactionSetLocationWritesToStagedPaths(t *testing.T) { Review Comment: no test covers the overwrite, cow rewrite or pos-del call sites changed in this pr: please add them to this table test -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
