Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.google.common.collect.ImmutableMap;
import io.airlift.slice.Slice;
import org.apache.iceberg.AppendFiles;
import org.apache.iceberg.BaseTable;
import org.apache.iceberg.DataFiles;
import org.apache.iceberg.PartitionSpecParser;
import org.apache.iceberg.Schema;
Expand All @@ -63,6 +64,7 @@
import static com.facebook.presto.common.type.BigintType.BIGINT;
import static com.facebook.presto.iceberg.IcebergColumnHandle.primitiveIcebergColumnHandle;
import static com.facebook.presto.iceberg.IcebergTableProperties.FILE_FORMAT_PROPERTY;
import static com.facebook.presto.iceberg.IcebergTableProperties.FORMAT_VERSION;
import static com.facebook.presto.iceberg.IcebergTableProperties.PARTITIONING_PROPERTY;
import static com.facebook.presto.iceberg.IcebergUtil.getColumns;
import static com.facebook.presto.iceberg.IcebergUtil.getFileFormat;
Expand Down Expand Up @@ -282,6 +284,10 @@ protected ImmutableMap<String, Object> createMetadataProperties(org.apache.icebe
{
ImmutableMap.Builder<String, Object> properties = ImmutableMap.builder();
properties.put(FILE_FORMAT_PROPERTY, getFileFormat(icebergTable));

int formatVersion = ((BaseTable) icebergTable).operations().current().formatVersion();
properties.put(FORMAT_VERSION, String.valueOf(formatVersion));

if (!icebergTable.spec().fields().isEmpty()) {
properties.put(PARTITIONING_PROPERTY, toPartitionFields(icebergTable.spec()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import static com.facebook.presto.hive.HiveMetadata.TABLE_COMMENT;
import static com.facebook.presto.iceberg.IcebergSchemaProperties.getSchemaLocation;
import static com.facebook.presto.iceberg.IcebergTableProperties.getFileFormat;
import static com.facebook.presto.iceberg.IcebergTableProperties.getFormatVersion;
import static com.facebook.presto.iceberg.IcebergTableProperties.getPartitioning;
import static com.facebook.presto.iceberg.IcebergTableProperties.getTableLocation;
import static com.facebook.presto.iceberg.IcebergUtil.getColumns;
Expand All @@ -79,6 +80,7 @@
import static java.util.stream.Collectors.toList;
import static org.apache.iceberg.TableMetadata.newTableMetadata;
import static org.apache.iceberg.TableProperties.DEFAULT_FILE_FORMAT;
import static org.apache.iceberg.TableProperties.FORMAT_VERSION;
import static org.apache.iceberg.TableProperties.OBJECT_STORE_PATH;
import static org.apache.iceberg.TableProperties.WRITE_DATA_LOCATION;
import static org.apache.iceberg.TableProperties.WRITE_METADATA_LOCATION;
Expand Down Expand Up @@ -263,13 +265,18 @@ public ConnectorOutputTableHandle beginCreateTable(ConnectorSession session, Con
throw new TableAlreadyExistsException(schemaTableName);
}

ImmutableMap.Builder<String, String> propertiesBuilder = ImmutableMap.builderWithExpectedSize(2);
ImmutableMap.Builder<String, String> propertiesBuilder = ImmutableMap.builderWithExpectedSize(3);
FileFormat fileFormat = getFileFormat(tableMetadata.getProperties());
propertiesBuilder.put(DEFAULT_FILE_FORMAT, fileFormat.toString());
if (tableMetadata.getComment().isPresent()) {
propertiesBuilder.put(TABLE_COMMENT, tableMetadata.getComment().get());
}

String formatVersion = getFormatVersion(tableMetadata.getProperties());
if (formatVersion != null) {
propertiesBuilder.put(FORMAT_VERSION, formatVersion);
}

TableMetadata metadata = newTableMetadata(schema, partitionSpec, targetPath, propertiesBuilder.build());

transaction = createTableTransaction(tableName, operations, metadata);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,24 @@ public void testDescribeTable()
MaterializedResult actualColumns = computeActual("DESCRIBE orders");
Assert.assertEquals(actualColumns, expectedColumns);
}

@Test
public void testShowCreateTable()
{
assertThat(computeActual("SHOW CREATE TABLE orders").getOnlyValue())
.isEqualTo("CREATE TABLE iceberg.tpch.orders (\n" +
" orderkey bigint,\n" +
" custkey bigint,\n" +
" orderstatus varchar,\n" +
" totalprice double,\n" +
" orderdate date,\n" +
" orderpriority varchar,\n" +
" clerk varchar,\n" +
" shippriority integer,\n" +
" comment varchar\n" +
" \"orderkey\" bigint,\n" +
" \"custkey\" bigint,\n" +
" \"orderstatus\" varchar,\n" +
" \"totalprice\" double,\n" +
" \"orderdate\" date,\n" +
" \"orderpriority\" varchar,\n" +
" \"clerk\" varchar,\n" +
" \"shippriority\" integer,\n" +
" \"comment\" varchar\n" +
")\n" +
"WITH (\n" +
" format = 'ORC'\n" +
" format = 'PARQUET',\n" +
" format_version = '1'\n" +
")");
}

Expand Down Expand Up @@ -348,6 +349,7 @@ private void testCreatePartitionedTableAs(Session session, FileFormat fileFormat
")\n" +
"WITH (\n" +
" format = '" + fileFormat + "',\n" +
" format_version = '1',\n" +
" partitioning = ARRAY['order_status','ship_priority','bucket(order_key, 9)']\n" +
")",
getSession().getCatalog().get(),
Expand Down Expand Up @@ -389,7 +391,8 @@ public void testTableComments()
")\n" +
"COMMENT '%s'\n" +
"WITH (\n" +
" format = 'ORC'\n" +
" format = 'ORC',\n" +
" format_version = '1'\n" +
")";
String createTableSql = format(createTableTemplate, "test table comment");
assertUpdate(createTableSql);
Expand Down Expand Up @@ -474,6 +477,7 @@ private void testCreateTableLike()
assertUpdate(session, "CREATE TABLE test_create_table_like_original (col1 INTEGER, aDate DATE) WITH(format = 'PARQUET', partitioning = ARRAY['aDate'])");
assertEquals(getTablePropertiesString("test_create_table_like_original"), "WITH (\n" +
" format = 'PARQUET',\n" +
" format_version = '1',\n" +
" partitioning = ARRAY['adate']\n" +
")");

Expand All @@ -484,33 +488,84 @@ private void testCreateTableLike()

assertUpdate(session, "CREATE TABLE test_create_table_like_copy1 (LIKE test_create_table_like_original)");
assertEquals(getTablePropertiesString("test_create_table_like_copy1"), "WITH (\n" +
" format = 'PARQUET'\n" +
" format = 'PARQUET',\n" +
" format_version = '1'\n" +
")");
dropTable(session, "test_create_table_like_copy1");

assertUpdate(session, "CREATE TABLE test_create_table_like_copy2 (LIKE test_create_table_like_original EXCLUDING PROPERTIES)");
assertEquals(getTablePropertiesString("test_create_table_like_copy2"), "WITH (\n" +
" format = 'PARQUET'\n" +
" format = 'PARQUET',\n" +
" format_version = '1'\n" +
")");
dropTable(session, "test_create_table_like_copy2");

assertUpdate(session, "CREATE TABLE test_create_table_like_copy3 (LIKE test_create_table_like_original INCLUDING PROPERTIES)");
assertEquals(getTablePropertiesString("test_create_table_like_copy3"), "WITH (\n" +
" format = 'PARQUET',\n" +
" format_version = '1',\n" +
" partitioning = ARRAY['adate']\n" +
")");
dropTable(session, "test_create_table_like_copy3");

assertUpdate(session, "CREATE TABLE test_create_table_like_copy4 (LIKE test_create_table_like_original INCLUDING PROPERTIES) WITH (format = 'ORC')");
assertEquals(getTablePropertiesString("test_create_table_like_copy4"), "WITH (\n" +
" format = 'ORC',\n" +
" format_version = '1',\n" +
" partitioning = ARRAY['adate']\n" +
")");
dropTable(session, "test_create_table_like_copy4");

dropTable(session, "test_create_table_like_original");
}

@Test
public void testCreateTableWithFormatVersion()
{
testWithAllFormatVersions(this::testCreateTableWithFormatVersion);
}

private void testCreateTableWithFormatVersion(Session session, String formatVersion)
{
@Language("SQL") String createTable = "" +
"CREATE TABLE test_create_table_with_format_version " +
"WITH (" +
"format = 'PARQUET', " +
"format_version = '" + formatVersion + "'" +
") " +
"AS " +
"SELECT orderkey AS order_key, shippriority AS ship_priority, orderstatus AS order_status " +
"FROM tpch.tiny.orders";

assertUpdate(session, createTable, "SELECT count(*) from orders");

String createTableSql = format("" +
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add "@language("SQL")"?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This annotation was not added here because code editors like IntelliJ are not able to detect the language in the case of format strings. So, even after using this annotation, it doesn't benefit as language injection doesn't take place.

"CREATE TABLE %s.%s.%s (\n" +
" \"order_key\" bigint,\n" +
" \"ship_priority\" integer,\n" +
" \"order_status\" varchar\n" +
")\n" +
"WITH (\n" +
" format = 'PARQUET',\n" +
" format_version = '%s'\n" +
")",
getSession().getCatalog().get(),
getSession().getSchema().get(),
"test_create_table_with_format_version",
formatVersion);

MaterializedResult actualResult = computeActual("SHOW CREATE TABLE test_create_table_with_format_version");
assertEquals(getOnlyElement(actualResult.getOnlyColumnAsSet()), createTableSql);

dropTable(session, "test_create_table_with_format_version");
}

private void testWithAllFormatVersions(BiConsumer<Session, String> test)
{
test.accept(getSession(), "1");
test.accept(getSession(), "2");
}

private String getTablePropertiesString(String tableName)
{
MaterializedResult showCreateTable = computeActual("SHOW CREATE TABLE " + tableName);
Expand Down