-
Notifications
You must be signed in to change notification settings - Fork 5.5k
[Iceberg] Add Iceberg metadata table $ref #23503
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,103 @@ | ||
| /* | ||
| * 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.Page; | ||
| import com.facebook.presto.common.predicate.TupleDomain; | ||
| import com.facebook.presto.iceberg.util.PageListBuilder; | ||
| import com.facebook.presto.spi.ColumnMetadata; | ||
| import com.facebook.presto.spi.ConnectorPageSource; | ||
| import com.facebook.presto.spi.ConnectorSession; | ||
| import com.facebook.presto.spi.ConnectorTableMetadata; | ||
| import com.facebook.presto.spi.FixedPageSource; | ||
| 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.Table; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static com.facebook.presto.common.type.BigintType.BIGINT; | ||
| import static com.facebook.presto.common.type.VarcharType.VARCHAR; | ||
| import static com.facebook.presto.spi.SystemTable.Distribution.SINGLE_COORDINATOR; | ||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| public class RefsTable | ||
| implements SystemTable | ||
| { | ||
| private final ConnectorTableMetadata tableMetadata; | ||
| private final Table icebergTable; | ||
|
|
||
| private static final List<ColumnMetadata> COLUMNS = ImmutableList.<ColumnMetadata>builder() | ||
| .add(new ColumnMetadata("name", VARCHAR)) | ||
| .add(new ColumnMetadata("type", VARCHAR)) | ||
| .add(new ColumnMetadata("snapshot_id", BIGINT)) | ||
| .add(new ColumnMetadata("max_reference_age_in_ms", BIGINT)) | ||
| .add(new ColumnMetadata("min_snapshots_to_keep", BIGINT)) | ||
| .add(new ColumnMetadata("max_snapshot_age_in_ms", BIGINT)) | ||
| .build(); | ||
|
|
||
| public RefsTable(SchemaTableName tableName, Table icebergTable) | ||
| { | ||
| tableMetadata = new ConnectorTableMetadata(requireNonNull(tableName, "tableName is null"), COLUMNS); | ||
| this.icebergTable = requireNonNull(icebergTable, "icebergTable is null"); | ||
| } | ||
|
|
||
| @Override | ||
| public Distribution getDistribution() | ||
| { | ||
| return SINGLE_COORDINATOR; | ||
| } | ||
|
|
||
| @Override | ||
| public ConnectorTableMetadata getTableMetadata() | ||
| { | ||
| return tableMetadata; | ||
| } | ||
|
|
||
| @Override | ||
| public ConnectorPageSource pageSource(ConnectorTransactionHandle transactionHandle, ConnectorSession session, TupleDomain<Integer> constraint) | ||
| { | ||
| return new FixedPageSource(buildPages(tableMetadata, icebergTable)); | ||
| } | ||
|
|
||
| private static void appendValue(Long value, PageListBuilder pagesBuilder) | ||
| { | ||
| if (value == null) { | ||
| pagesBuilder.appendNull(); | ||
| } | ||
| else { | ||
| pagesBuilder.appendBigint(value); | ||
| } | ||
| } | ||
|
|
||
| private static List<Page> buildPages(ConnectorTableMetadata tableMetadata, Table icebergTable) | ||
| { | ||
| PageListBuilder pagesBuilder = PageListBuilder.forTable(tableMetadata); | ||
|
|
||
| icebergTable.refs().forEach((key, value) -> { | ||
| pagesBuilder.beginRow(); | ||
| pagesBuilder.appendVarchar(key); | ||
| pagesBuilder.appendVarchar(String.valueOf(value.type())); | ||
| pagesBuilder.appendBigint(value.snapshotId()); | ||
| appendValue(value.maxRefAgeMs(), pagesBuilder); | ||
| appendValue(value.minSnapshotsToKeep() != null ? Long.valueOf(value.minSnapshotsToKeep()) : null, pagesBuilder); | ||
| appendValue(value.maxSnapshotAgeMs(), pagesBuilder); | ||
| pagesBuilder.endRow(); | ||
| }); | ||
|
|
||
| return pagesBuilder.build(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -203,6 +203,25 @@ public void testFilesTable() | |
| assertQuerySucceeds("SELECT * FROM test_schema.\"test_table$files\""); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRefsTable() | ||
| { | ||
| assertQuery("SHOW COLUMNS FROM test_schema.\"test_table$refs\"", | ||
| "VALUES ('name', 'varchar', '', '')," + | ||
| "('type', 'varchar', '', '')," + | ||
| "('snapshot_id', 'bigint', '', '')," + | ||
| "('max_reference_age_in_ms', 'bigint', '', '')," + | ||
| "('min_snapshots_to_keep', 'bigint', '', '')," + | ||
| "('max_snapshot_age_in_ms', 'bigint', '', '')"); | ||
| assertQuerySucceeds("SELECT * FROM test_schema.\"test_table$refs\""); | ||
|
|
||
| // Check main branch entry | ||
| assertQuery("SELECT count(*) FROM test_schema.\"test_table$refs\"", "VALUES 1"); | ||
| assertQuery("SELECT name FROM test_schema.\"test_table$refs\"", "VALUES 'main'"); | ||
|
Contributor
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. I think it would also be prudent to add assertions on values in the row of the table matching the You could add or refactor the
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. Added tests in |
||
|
|
||
| assertQuerySucceeds("SELECT * FROM test_schema.\"test_table_multilevel_partitions$refs\""); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSessionPropertiesInManuallyStartedTransaction() | ||
| { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can add some more detailed assertions like follows:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I have added detailed assertions for testTag & testBranch instead of the main branch. Please LMK if that's fine