zeroshade commented on code in PR #2797: URL: https://github.com/apache/iceberg-go/pull/2797#discussion_r4803840974
########## catalog/catalogtest/collisions.go: ########## @@ -0,0 +1,106 @@ +// 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 catalogtest + +import ( + "context" + "errors" + "testing" + + "github.com/apache/iceberg-go/catalog" + "github.com/apache/iceberg-go/table" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// assertAlreadyExists asserts that err reports a name collision. Either Review Comment: +1 to @badalprasadsingh: this doesn't hold for `RenameTable`, where SQL, Hive and REST all return `ErrTableAlreadyExists` even though the existing object is a view. Something like "the sentinel varies by catalog and operation, so either is accepted" would be accurate. Same for the PR description. ########## catalog/catalogtest/collisions.go: ########## @@ -0,0 +1,106 @@ +// 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 catalogtest + +import ( + "context" + "errors" + "testing" + + "github.com/apache/iceberg-go/catalog" + "github.com/apache/iceberg-go/table" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// assertAlreadyExists asserts that err reports a name collision. Either +// sentinel is accepted because catalogs disagree: SQL and Hive return the one +// for the object that exists, while REST maps a 409 to the one for the call. +func assertAlreadyExists(t *testing.T, err error) { + t.Helper() + + assert.True(t, errors.Is(err, catalog.ErrTableAlreadyExists) || errors.Is(err, catalog.ErrViewAlreadyExists), + "expected ErrTableAlreadyExists or ErrViewAlreadyExists, got %v", err) +} + +// testCreateViewOverExistingTable asserts that creating a view with the name +// of an existing table is rejected and leaves the table intact. +func testCreateViewOverExistingTable(t *testing.T, cfg Config) { + ctx := context.Background() + cat, views := newViewCatalog(t, cfg) + namespace, ident := newIdentifiers() + + require.NoError(t, cat.CreateNamespace(ctx, namespace, nil)) + t.Cleanup(func() { _ = cat.DropNamespace(ctx, namespace) }) + + _, err := cat.CreateTable(ctx, ident, Schema) + require.NoError(t, err) + t.Cleanup(func() { _ = cat.DropTable(ctx, ident) }) + + _, err = views.CreateView(ctx, ident, newViewVersion(t, namespace), TableSchema) + assertAlreadyExists(t, err) + + tbl, err := cat.LoadTable(ctx, ident) + require.NoError(t, err) + assert.Equal(t, TableSchema.AsStruct(), tbl.Schema().AsStruct(), "table should keep its schema") +} + +// testCreateTableOverExistingView asserts that creating a table with the name +// of an existing view is rejected and leaves the view intact. +func testCreateTableOverExistingView(t *testing.T, cfg Config) { + ctx := context.Background() + cat, views := newViewCatalog(t, cfg) + namespace, ident := newIdentifiers() + + require.NoError(t, cat.CreateNamespace(ctx, namespace, nil)) + t.Cleanup(func() { _ = cat.DropNamespace(ctx, namespace) }) + + createView(t, views, ident) + + _, err := cat.CreateTable(ctx, ident, OtherSchema) + assertAlreadyExists(t, err) + + v, err := views.LoadView(ctx, ident) + require.NoError(t, err) + assertConformanceView(t, ident, v) +} + +// testRenameTableOntoExistingView asserts that renaming a table onto the name +// of an existing view is rejected and leaves the view intact. +func testRenameTableOntoExistingView(t *testing.T, cfg Config) { + ctx := context.Background() + cat, views := newViewCatalog(t, cfg) + namespace, from := newIdentifiers() + to := table.Identifier{namespace[0], "other"} + + require.NoError(t, cat.CreateNamespace(ctx, namespace, nil)) + t.Cleanup(func() { _ = cat.DropNamespace(ctx, namespace) }) + + _, err := cat.CreateTable(ctx, from, Schema) + require.NoError(t, err) + t.Cleanup(func() { _ = cat.DropTable(ctx, from) }) + + createView(t, views, to) + + _, err = cat.RenameTable(ctx, from, to) Review Comment: Could this also check that the source table is still there after the failed rename? As written, a catalog that dropped `from` before failing at the destination would pass. Java's `renameTableTargetAlreadyExistsAsView` asserts `tableExists(tableIdentifier)` for this. Something like: ```go _, err = cat.LoadTable(ctx, from) require.NoError(t, err, "the source table should survive a rejected rename") ``` -- 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]
