-
Notifications
You must be signed in to change notification settings - Fork 5.5k
feat(plugin-iceberg): Add Iceberg metadata table $metadata_log_entries #24302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| /* | ||
| * Licensed 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 com.facebook.presto.iceberg; | ||
|
|
||
| import com.facebook.presto.common.predicate.TupleDomain; | ||
| import com.facebook.presto.common.type.Type; | ||
| import com.facebook.presto.spi.ColumnMetadata; | ||
| import com.facebook.presto.spi.ConnectorSession; | ||
| import com.facebook.presto.spi.ConnectorTableMetadata; | ||
| import com.facebook.presto.spi.InMemoryRecordSet; | ||
| import com.facebook.presto.spi.RecordCursor; | ||
| import com.facebook.presto.spi.SchemaTableName; | ||
| import com.facebook.presto.spi.SystemTable; | ||
| import com.facebook.presto.spi.connector.ConnectorTransactionHandle; | ||
| import com.google.common.collect.ImmutableList; | ||
| import org.apache.iceberg.BaseTable; | ||
| import org.apache.iceberg.Snapshot; | ||
| import org.apache.iceberg.Table; | ||
| import org.apache.iceberg.TableMetadata; | ||
| import org.apache.iceberg.TableMetadata.MetadataLogEntry; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.NoSuchElementException; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import static com.facebook.presto.common.type.BigintType.BIGINT; | ||
| import static com.facebook.presto.common.type.DateTimeEncoding.packDateTimeWithZone; | ||
| import static com.facebook.presto.common.type.IntegerType.INTEGER; | ||
| import static com.facebook.presto.common.type.TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE; | ||
| import static com.facebook.presto.common.type.VarcharType.VARCHAR; | ||
| import static java.util.Objects.requireNonNull; | ||
| import static org.apache.iceberg.util.SnapshotUtil.snapshotIdAsOfTime; | ||
|
|
||
| public class MetadataLogTable | ||
| implements SystemTable | ||
| { | ||
| private final ConnectorTableMetadata tableMetadata; | ||
| private final Table icebergTable; | ||
|
|
||
| private static final List<ColumnMetadata> COLUMN_DEFINITIONS = ImmutableList.<ColumnMetadata>builder() | ||
| .add(ColumnMetadata.builder().setName("timestamp").setType(TIMESTAMP_WITH_TIME_ZONE).build()) | ||
| .add(ColumnMetadata.builder().setName("file").setType(VARCHAR).build()) | ||
| .add(ColumnMetadata.builder().setName("latest_snapshot_id").setType(BIGINT).build()) | ||
| .add(ColumnMetadata.builder().setName("latest_schema_id").setType(INTEGER).build()) | ||
| .add(ColumnMetadata.builder().setName("latest_sequence_number").setType(BIGINT).build()) | ||
| .build(); | ||
|
|
||
| private static final List<Type> COLUMN_TYPES = COLUMN_DEFINITIONS.stream() | ||
| .map(ColumnMetadata::getType) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| public MetadataLogTable(SchemaTableName tableName, Table icebergTable) | ||
| { | ||
| tableMetadata = new ConnectorTableMetadata(requireNonNull(tableName, "tableName is null"), COLUMN_DEFINITIONS); | ||
| this.icebergTable = requireNonNull(icebergTable, "icebergTable is null"); | ||
| } | ||
|
|
||
| @Override | ||
| public Distribution getDistribution() | ||
| { | ||
| return Distribution.SINGLE_COORDINATOR; | ||
| } | ||
|
|
||
| @Override | ||
| public ConnectorTableMetadata getTableMetadata() | ||
| { | ||
| return tableMetadata; | ||
| } | ||
|
|
||
| @Override | ||
| public RecordCursor cursor(ConnectorTransactionHandle transactionHandle, ConnectorSession session, TupleDomain<Integer> constraint) | ||
| { | ||
| Iterable<List<?>> rowIterable = () -> new Iterator<List<?>>() | ||
| { | ||
| private final Iterator<MetadataLogEntry> metadataLogEntriesIterator = ((BaseTable) icebergTable).operations().current().previousFiles().iterator(); | ||
| private boolean addedLatestEntry; | ||
|
|
||
| @Override | ||
| public boolean hasNext() | ||
| { | ||
| return metadataLogEntriesIterator.hasNext() || !addedLatestEntry; | ||
| } | ||
|
|
||
| @Override | ||
| public List<?> next() | ||
| { | ||
| if (metadataLogEntriesIterator.hasNext()) { | ||
| return processMetadataLogEntries(session, metadataLogEntriesIterator.next()); | ||
| } | ||
| if (!addedLatestEntry) { | ||
| addedLatestEntry = true; | ||
| TableMetadata currentMetadata = ((BaseTable) icebergTable).operations().current(); | ||
| return buildLatestMetadataRow(session, currentMetadata); | ||
| } | ||
| throw new NoSuchElementException(); | ||
| } | ||
| }; | ||
| return new InMemoryRecordSet(COLUMN_TYPES, rowIterable).cursor(); | ||
| } | ||
|
|
||
| private List<?> processMetadataLogEntries(ConnectorSession session, MetadataLogEntry metadataLogEntry) | ||
| { | ||
| Long snapshotId = null; | ||
| Snapshot snapshot = null; | ||
| try { | ||
| snapshotId = snapshotIdAsOfTime(icebergTable, metadataLogEntry.timestampMillis()); | ||
| snapshot = icebergTable.snapshot(snapshotId); | ||
| } | ||
| catch (IllegalArgumentException ignored) { | ||
| // Implies this metadata file was created during table creation | ||
| } | ||
| return addRow(session, metadataLogEntry.timestampMillis(), metadataLogEntry.file(), snapshotId, snapshot); | ||
| } | ||
|
|
||
| private List<?> buildLatestMetadataRow(ConnectorSession session, TableMetadata metadata) | ||
| { | ||
| Snapshot latestSnapshot = icebergTable.currentSnapshot(); | ||
| Long latestSnapshotId = (latestSnapshot != null) ? latestSnapshot.snapshotId() : null; | ||
|
|
||
| return addRow(session, metadata.lastUpdatedMillis(), metadata.metadataFileLocation(), latestSnapshotId, latestSnapshot); | ||
| } | ||
|
|
||
| private List<?> addRow(ConnectorSession session, long timestampMillis, String fileLocation, Long snapshotId, Snapshot snapshot) | ||
| { | ||
| return Arrays.asList( | ||
| packDateTimeWithZone(timestampMillis, session.getSqlFunctionProperties().getTimeZoneKey()), | ||
| fileLocation, | ||
| snapshotId, | ||
| snapshot != null ? snapshot.schemaId() : null, | ||
| snapshot != null ? snapshot.sequenceNumber() : null); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -115,8 +115,11 @@ | |
| import java.lang.reflect.Field; | ||
| import java.net.URI; | ||
| import java.nio.ByteBuffer; | ||
| import java.time.Instant; | ||
| import java.time.LocalDateTime; | ||
| import java.time.LocalTime; | ||
| import java.time.ZoneId; | ||
| import java.time.ZonedDateTime; | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
|
|
@@ -145,9 +148,11 @@ | |
| import static com.facebook.presto.common.type.IntegerType.INTEGER; | ||
| import static com.facebook.presto.common.type.RealType.REAL; | ||
| import static com.facebook.presto.common.type.TimeZoneKey.UTC_KEY; | ||
| import static com.facebook.presto.common.type.TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE; | ||
| import static com.facebook.presto.common.type.VarcharType.VARCHAR; | ||
| import static com.facebook.presto.hive.BaseHiveColumnHandle.ColumnType.SYNTHESIZED; | ||
| import static com.facebook.presto.hive.HiveCommonSessionProperties.PARQUET_BATCH_READ_OPTIMIZATION_ENABLED; | ||
| import static com.facebook.presto.iceberg.CatalogType.HADOOP; | ||
| import static com.facebook.presto.iceberg.FileContent.EQUALITY_DELETES; | ||
| import static com.facebook.presto.iceberg.FileContent.POSITION_DELETES; | ||
| import static com.facebook.presto.iceberg.FileFormat.ORC; | ||
|
|
@@ -186,6 +191,7 @@ | |
| import static org.apache.iceberg.types.Type.TypeID.TIME; | ||
| import static org.apache.parquet.column.ParquetProperties.WriterVersion.PARQUET_1_0; | ||
| import static org.apache.parquet.column.ParquetProperties.WriterVersion.PARQUET_2_0; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.testng.Assert.assertFalse; | ||
| import static org.testng.Assert.assertNotEquals; | ||
| import static org.testng.Assert.assertNotNull; | ||
|
|
@@ -2205,6 +2211,75 @@ public void testRefsTable() | |
| assertQuery("SELECT * FROM test_table_references FOR SYSTEM_VERSION AS OF 'testTag' where id1=1", "VALUES(1, NULL)"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testMetadataLogTable() | ||
| { | ||
| try { | ||
| assertUpdate("CREATE TABLE test_table_metadatalog (id1 BIGINT, id2 BIGINT)"); | ||
| assertQuery("SELECT count(*) FROM \"test_table_metadatalog$metadata_log_entries\"", "VALUES 1"); | ||
| //metadata file created at table creation | ||
| assertQuery("SELECT latest_snapshot_id FROM \"test_table_metadatalog$metadata_log_entries\"", "VALUES NULL"); | ||
|
|
||
| assertUpdate("INSERT INTO test_table_metadatalog VALUES (0, 00), (1, 10), (2, 20)", 3); | ||
| Table icebergTable = loadTable("test_table_metadatalog"); | ||
| Snapshot latestSnapshot = icebergTable.currentSnapshot(); | ||
| assertQuery("SELECT count(*) FROM \"test_table_metadatalog$metadata_log_entries\"", "VALUES 2"); | ||
| assertQuery("SELECT latest_snapshot_id FROM \"test_table_metadatalog$metadata_log_entries\" order by timestamp DESC limit 1", "values " + latestSnapshot.snapshotId()); | ||
| } | ||
| finally { | ||
| assertUpdate("DROP TABLE IF EXISTS test_table_metadatalog"); | ||
| } | ||
| } | ||
|
Comment on lines
+2214
to
+2232
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it convenient to add some test cases considering different
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hantangwangd Could you please provide me some example around which type of testcases would fit in here considering different timezone?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refer to Iceberg's test case, I think we can add some tests similar with the following code: And test it under different zoneIds. |
||
|
|
||
| @DataProvider(name = "timezoneId") | ||
| public Object[][] getTimezonesId() | ||
| { | ||
| return new Object[][]{{"UTC"}, {"America/Los_Angeles"}, {"Asia/Shanghai"}, {"Asia/Kolkata"}, {"America/Bahia_Banderas"}, {"Europe/Brussels"}}; | ||
| } | ||
|
|
||
| @Test(dataProvider = "timezoneId") | ||
| public void testMetadataLogTableWithTimeZoneId(String zoneId) | ||
| { | ||
| try { | ||
| Session sessionForTimeZone = Session.builder(getSession()) | ||
| .setTimeZoneKey(TimeZoneKey.getTimeZoneKey(zoneId)).build(); | ||
|
|
||
| assertUpdate(sessionForTimeZone, "CREATE TABLE test_table_metadatalog_tz_id (id1 BIGINT, id2 BIGINT)"); | ||
| assertQuery(sessionForTimeZone, "SELECT count(*) FROM \"test_table_metadatalog_tz_id$metadata_log_entries\"", "VALUES 1"); | ||
| assertQuery(sessionForTimeZone, "SELECT latest_snapshot_id FROM \"test_table_metadatalog_tz_id$metadata_log_entries\"", "VALUES NULL"); | ||
| Table icebergTable = loadTable("test_table_metadatalog_tz_id"); | ||
| TableMetadata tableMetadata = ((BaseTable) icebergTable).operations().current(); | ||
| ZonedDateTime zonedDateTime1 = Instant.ofEpochMilli(tableMetadata.lastUpdatedMillis()) | ||
| .atZone(ZoneId.of(zoneId)); | ||
| //metadata file created at table creation | ||
| String metadataFileLocation1 = tableMetadata.metadataFileLocation(); | ||
|
|
||
| assertUpdate("INSERT INTO test_table_metadatalog_tz_id VALUES (0, 00), (1, 10), (2, 20)", 3); | ||
| icebergTable = loadTable("test_table_metadatalog_tz_id"); | ||
| tableMetadata = ((BaseTable) icebergTable).operations().current(); | ||
| ZonedDateTime zonedDateTime2 = Instant.ofEpochMilli(tableMetadata.lastUpdatedMillis()) | ||
| .atZone(ZoneId.of(zoneId)); | ||
| //metadata file created after table insertion | ||
| String metadataFileLocation2 = tableMetadata.metadataFileLocation(); | ||
|
|
||
| Snapshot latestSnapshot = icebergTable.currentSnapshot(); | ||
| assertQuery("SELECT count(*) FROM \"test_table_metadatalog_tz_id$metadata_log_entries\"", "VALUES 2"); | ||
| assertQuery("SELECT latest_snapshot_id FROM \"test_table_metadatalog_tz_id$metadata_log_entries\" order by timestamp DESC limit 1", "values " + latestSnapshot.snapshotId()); | ||
|
|
||
| MaterializedResult actual = getQueryRunner().execute(sessionForTimeZone, "SELECT * FROM \"test_table_metadatalog_tz_id$metadata_log_entries\""); | ||
| assertThat(actual).hasSize(2); | ||
| MaterializedResult expected = resultBuilder(getSession(), TIMESTAMP_WITH_TIME_ZONE, VARCHAR, BIGINT, INTEGER, BIGINT) | ||
| .row(zonedDateTime1, metadataFileLocation1, null, null, null) | ||
| .row(zonedDateTime2, metadataFileLocation2, latestSnapshot.snapshotId(), latestSnapshot.schemaId(), latestSnapshot.sequenceNumber()) | ||
| .build(); | ||
|
|
||
| assertEquals(actual, expected); | ||
| } | ||
| finally { | ||
| assertUpdate("DROP TABLE IF EXISTS test_table_metadatalog_tz_id"); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testAllIcebergType() | ||
| { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.