rdblue commented on a change in pull request #1633: URL: https://github.com/apache/iceberg/pull/1633#discussion_r521623982
########## File path: aws/src/integration/java/org/apache/iceberg/aws/glue/GlueCatalogTableTest.java ########## @@ -0,0 +1,241 @@ +/* + * 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 org.apache.iceberg.aws.glue; + +import java.util.List; +import java.util.Locale; +import java.util.Optional; +import org.apache.iceberg.AssertHelpers; +import org.apache.iceberg.BaseMetastoreTableOperations; +import org.apache.iceberg.DataFile; +import org.apache.iceberg.DataFiles; +import org.apache.iceberg.PartitionSpec; +import org.apache.iceberg.Schema; +import org.apache.iceberg.Table; +import org.apache.iceberg.TableMetadata; +import org.apache.iceberg.TableOperations; +import org.apache.iceberg.catalog.Namespace; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.iceberg.exceptions.AlreadyExistsException; +import org.apache.iceberg.exceptions.NoSuchTableException; +import org.apache.iceberg.types.Types; +import org.junit.Assert; +import org.junit.Test; +import software.amazon.awssdk.services.glue.model.GetTableRequest; +import software.amazon.awssdk.services.glue.model.GetTableResponse; +import software.amazon.awssdk.services.glue.model.GetTableVersionsRequest; +import software.amazon.awssdk.services.s3.model.HeadObjectRequest; +import software.amazon.awssdk.services.s3.model.ListObjectsV2Request; +import software.amazon.awssdk.services.s3.model.ListObjectsV2Response; +import software.amazon.awssdk.services.s3.model.S3Object; + +public class GlueCatalogTableTest extends GlueTestBase { + + @Test + public void testCreateTable() { + String namespace = createNamespace(); + String tableName = getRandomName(); + glueCatalog.createTable(TableIdentifier.of(namespace, tableName), schema, partitionSpec); + GetTableResponse response = glue.getTable(GetTableRequest.builder() + .databaseName(namespace).name(tableName).build()); + Assert.assertEquals(namespace, response.table().databaseName()); + Assert.assertEquals(tableName, response.table().name()); + Assert.assertEquals(BaseMetastoreTableOperations.ICEBERG_TABLE_TYPE_VALUE.toUpperCase(Locale.ENGLISH), + response.table().parameters().get(BaseMetastoreTableOperations.TABLE_TYPE_PROP)); + Assert.assertTrue(response.table().parameters().containsKey(BaseMetastoreTableOperations.METADATA_LOCATION_PROP)); + String metaLocation = response.table().parameters().get(BaseMetastoreTableOperations.METADATA_LOCATION_PROP); + String key = metaLocation.split(testBucketName, -1)[1].substring(1); + s3.headObject(HeadObjectRequest.builder().bucket(testBucketName).key(key).build()); + Table table = glueCatalog.loadTable(TableIdentifier.of(namespace, tableName)); + Assert.assertEquals(partitionSpec, table.spec()); + Assert.assertEquals(schema.toString(), table.schema().toString()); + } + + @Test + public void testCreateTableDuplicate() { + String namespace = createNamespace(); + String tableName = createTable(namespace); + AssertHelpers.assertThrows("should not create table with the same name", + AlreadyExistsException.class, + () -> glueCatalog.createTable(TableIdentifier.of(namespace, tableName), schema, partitionSpec)); + } + + @Test + public void testCreateTableBadName() { + String namespace = createNamespace(); + AssertHelpers.assertThrows("should not create table with bad name", + IllegalArgumentException.class, + () -> glueCatalog.createTable(TableIdentifier.of(namespace, "table-1"), schema, partitionSpec)); + } + + @Test + public void testListTables() { + String namespace = createNamespace(); + // list table should have nothing + Assert.assertTrue(glueCatalog.listTables(Namespace.of(namespace)).isEmpty()); + String tableName = createTable(namespace); + // list table should show + List<TableIdentifier> tables = glueCatalog.listTables(Namespace.of(namespace)); + Assert.assertEquals(1, tables.size()); + Assert.assertEquals(TableIdentifier.of(namespace, tableName), tables.get(0)); + } + + @Test + public void testTableExists() { + String namespace = createNamespace(); + String tableName = createTable(namespace); + Assert.assertTrue(glueCatalog.tableExists(TableIdentifier.of(namespace, tableName))); + } + + @Test + public void testUpdateTable() { + String namespace = createNamespace(); + String tableName = getRandomName(); + // current should be null + TableOperations ops = glueCatalog.newTableOps(TableIdentifier.of(namespace, tableName)); + TableMetadata current = ops.current(); + Assert.assertNull(current); + // create table, refresh should update + createTable(namespace, tableName); + current = ops.refresh(); + Assert.assertEquals(schema.toString(), current.schema().toString()); + Assert.assertEquals(partitionSpec, current.spec()); + Table table = glueCatalog.loadTable(TableIdentifier.of(namespace, tableName)); + Assert.assertTrue(table.history().isEmpty()); + // commit new version, should create a new snapshot + table = glueCatalog.loadTable(TableIdentifier.of(namespace, tableName)); + DataFile dataFile = DataFiles.builder(partitionSpec) + .withPath("/path/to/data-a.parquet") + .withFileSizeInBytes(10) + .withRecordCount(1) + .build(); + table.newAppend().appendFile(dataFile).commit(); + table = glueCatalog.loadTable(TableIdentifier.of(namespace, tableName)); + Assert.assertEquals(1, table.history().size()); + } + + @Test + public void testRenameTable() { + String namespace = createNamespace(); + String tableName = createTable(namespace); + Table table = glueCatalog.loadTable(TableIdentifier.of(namespace, tableName)); + // rename table + String newTableName = tableName + "_2"; + glueCatalog.renameTable(TableIdentifier.of(namespace, tableName), TableIdentifier.of(namespace, newTableName)); + Table renamedTable = glueCatalog.loadTable(TableIdentifier.of(namespace, newTableName)); + Assert.assertEquals(table.location(), renamedTable.location()); + Assert.assertEquals(table.schema().toString(), renamedTable.schema().toString()); + Assert.assertEquals(table.spec(), renamedTable.spec()); + Assert.assertEquals(table.currentSnapshot(), renamedTable.currentSnapshot()); + } + + @Test + public void testRenameTableRollback() { + String namespace = createNamespace(); + String tableName = createTable(namespace); + TableIdentifier id = TableIdentifier.of(namespace, tableName); + Table table = glueCatalog.loadTable(id); + // rename rollback + AssertHelpers.assertThrows("should not have table", + NoSuchTableException.class, + () -> glueCatalog.renameTable( + TableIdentifier.of(namespace, tableName), TableIdentifier.of(namespace, tableName + "-3"))); Review comment: What is a rename rollback? Why does this test fail? The table looks like it should exist. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org