Skip to content

Commit

Permalink
Fix Iceberg table creation with location when schema location inacces…
Browse files Browse the repository at this point in the history
…sible
  • Loading branch information
austenLacy authored and findepi committed Jan 5, 2022
1 parent eabdd82 commit f5298e1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Expand Up @@ -386,7 +386,7 @@ public static Transaction newCreateTableTransaction(TrinoCatalog catalog, Connec
Schema schema = toIcebergSchema(tableMetadata.getColumns());
PartitionSpec partitionSpec = parsePartitionFields(schema, getPartitioning(tableMetadata.getProperties()));
String targetPath = getTableLocation(tableMetadata.getProperties())
.orElse(catalog.defaultTableLocation(session, schemaTableName));
.orElseGet(() -> catalog.defaultTableLocation(session, schemaTableName));

ImmutableMap.Builder<String, String> propertiesBuilder = ImmutableMap.builderWithExpectedSize(2);
FileFormat fileFormat = IcebergTableProperties.getFileFormat(tableMetadata.getProperties());
Expand Down
Expand Up @@ -13,9 +13,11 @@
*/
package io.trino.tests.product.iceberg;

import com.google.inject.Inject;
import io.trino.tempto.AfterTestWithContext;
import io.trino.tempto.BeforeTestWithContext;
import io.trino.tempto.ProductTest;
import io.trino.tempto.hadoop.hdfs.HdfsClient;
import org.testng.annotations.Test;

import static io.trino.tempto.assertions.QueryAssert.Row.row;
Expand All @@ -25,10 +27,14 @@
import static io.trino.tests.product.TestGroups.STORAGE_FORMATS;
import static io.trino.tests.product.hive.util.TemporaryHiveTable.randomTableSuffix;
import static io.trino.tests.product.utils.QueryExecutors.onTrino;
import static java.lang.String.format;

public class TestIcebergCreateTable
extends ProductTest
{
@Inject
private HdfsClient hdfsClient;

@BeforeTestWithContext
public void setUp()
{
Expand Down Expand Up @@ -85,4 +91,35 @@ public void testCreateTableAsSelect()
onTrino().executeQuery("DROP TABLE " + tableName);
}
}

@Test(groups = {ICEBERG, STORAGE_FORMATS})
public void testCreateExternalTableWithInaccessibleSchemaLocation()
{
String schemaName = "schema_without_location";
String schemaLocation = "/tmp/" + schemaName;
hdfsClient.createDirectory(schemaLocation);

onTrino().executeQuery(format("CREATE SCHEMA iceberg.%s WITH (location = '%s')", schemaName, schemaLocation));

hdfsClient.delete(schemaLocation);

String tableName = "test_create_external";
String tableLocation = "/tmp/" + tableName;

String schemaAndTableName = format("iceberg.%s.%s", schemaName, tableName);
onTrino().executeQuery(format("CREATE TABLE %s (a bigint, b VARCHAR) WITH (location = '%s')", schemaAndTableName, tableLocation));

onTrino().executeQuery("INSERT INTO " + schemaAndTableName + "(a, b) VALUES " +
"(NULL, NULL), " +
"(-42, 'abc'), " +
"(9223372036854775807, 'abcdefghijklmnopqrstuvwxyz')");
assertThat(onTrino().executeQuery("SELECT * FROM " + schemaAndTableName))
.containsOnly(
row(null, null),
row(-42, "abc"),
row(9223372036854775807L, "abcdefghijklmnopqrstuvwxyz"));

onTrino().executeQuery(format("DROP TABLE %s", schemaAndTableName));
onTrino().executeQuery(format("DROP SCHEMA iceberg.%s", schemaName));
}
}

0 comments on commit f5298e1

Please sign in to comment.