Skip to content
Open
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
22 changes: 21 additions & 1 deletion core/src/main/java/org/apache/iceberg/MetadataTableType.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
*/
package org.apache.iceberg;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;

public enum MetadataTableType {
ENTRIES,
Expand All @@ -36,7 +39,14 @@ public enum MetadataTableType {
ALL_FILES,
ALL_MANIFESTS,
ALL_ENTRIES,
POSITION_DELETES;
POSITION_DELETES,
METADATA_TABLES;

private final String lower;

MetadataTableType() {
this.lower = name().toLowerCase(Locale.ROOT);
}

public static MetadataTableType from(String name) {
try {
Expand All @@ -45,4 +55,14 @@ public static MetadataTableType from(String name) {
return null;
}
}

public String tableName() {
return this.lower;
}

public static List<String> lowerNames() {
return Arrays.stream(values())
.map(MetadataTableType::tableName)
.collect(ImmutableList.toImmutableList());
}
}
2 changes: 2 additions & 0 deletions core/src/main/java/org/apache/iceberg/MetadataTableUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ private static Table createMetadataTableInstance(
return new AllEntriesTable(baseTable, metadataTableName);
case POSITION_DELETES:
return new PositionDeletesTable(baseTable, metadataTableName);
case METADATA_TABLES:
return new MetadataTables(baseTable, metadataTableName);
default:
throw new NoSuchTableException(
"Unknown metadata table type: %s for %s", type, metadataTableName);
Expand Down
78 changes: 78 additions & 0 deletions core/src/main/java/org/apache/iceberg/MetadataTables.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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;

import java.util.Arrays;
import org.apache.iceberg.types.Types;

/** A {@link Table} implementation that exposes a table's metadata tables as rows. */
public class MetadataTables extends BaseMetadataTable {
MetadataTables(Table table) {
this(table, table.name() + "." + MetadataTableType.METADATA_TABLES.tableName());
}

MetadataTables(Table table, String name) {
super(table, name);
}

private static final Schema METADATA_TABLES_SCHEMA =
new Schema(Types.NestedField.required(1, "metadata_table_name", Types.StringType.get()));

@Override
public TableScan newScan() {
return new MetadataTablesScan(table());
}

@Override
public Schema schema() {
return METADATA_TABLES_SCHEMA;
}

@Override
MetadataTableType metadataTableType() {
return MetadataTableType.METADATA_TABLES;
}

private DataTask task(BaseTableScan scan) {
StaticDataTask.Row[] rows =

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.

Checking RefsTable instead of constructing StaticDataTask.Row[]', we can simply use Collectionwith a transform function to convert it to row viaStaticDataTask.Row.of()`

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Hi @gaborkaszab same reason with the usage of empty Datafile, as constructor accept parameter Row[]

StaticDataTask(
      DataFile metadataFile, 
      Schema tableSchema, 
      Schema projectedSchema, 
      StructLike[] rows)

Arrays.stream(MetadataTableType.values())
.map(metadataType -> StaticDataTask.Row.of(metadataType.tableName()))
.toArray(StaticDataTask.Row[]::new);

DataFile metadataFile =

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.

I randomly checked other metadata tables and they seem to use the following InputFile:
table().io().newInputFile(table().operations().current().metadataFileLocation()),

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

That’s a good point. For listing the available metadata tables, at least at this stage, we don’t actually need to read any physical file.

Using a directly constructed empty DataFile has a couple of benefits here:

  1. It avoids unnecessary file operations.
  2. It makes the intent clearer in the code, since otherwise it may look like the metadata table information is stored in or read from the table metadata file.

So I think using an empty DataFile is more explicit for this particular metadata table.

DataFiles.builder(PartitionSpec.unpartitioned())
.withPath(table().location() + "#" + MetadataTableType.METADATA_TABLES.tableName())
.withFormat(FileFormat.METADATA)
.withRecordCount(MetadataTableType.values().length)
.withFileSizeInBytes(0)
.build();

return new StaticDataTask(metadataFile, schema(), scan.schema(), rows);

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.

Other metadata tables use StaticDataTask.of()

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

same reason as the above one

}

private class MetadataTablesScan extends StaticTableScan {
MetadataTablesScan(Table table) {
super(
table,
METADATA_TABLES_SCHEMA,
MetadataTableType.METADATA_TABLES,
MetadataTables.this::task);
}
}

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.

After reding this Scan class and comparing to others can't we simply do what PartitionsScan does?

PartitionsScan(Table table) {
      super(
          table,
          PartitionsTable.this.schema(),
          MetadataTableType.PARTITIONS,
          PartitionsTable.this::task);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

make sense, fixed

}
22 changes: 22 additions & 0 deletions core/src/test/java/org/apache/iceberg/TestMetadataTableScans.java
Original file line number Diff line number Diff line change
Expand Up @@ -1833,6 +1833,28 @@ public void testDeleteFilesTableScanOnBranch() throws IOException {
.isEqualTo(2);
}

@TestTemplate
public void testMetadataTablesScan() throws IOException {
table.newFastAppend().appendFile(FILE_A).commit();
Table metadataTable = new MetadataTables(table);

TableScan scan = metadataTable.newScan();
List<ScanTask> tasks = Lists.newArrayList(scan.planFiles());
assertThat(tasks).hasSize(1);

List<String> expected = MetadataTableType.lowerNames();

List<String> actual = Lists.newArrayList();
try (CloseableIterable<StructLike> rs = tasks.get(0).asDataTask().rows()) {
for (StructLike r : rs) {
actual.add(r.get(0, String.class));
}
}
assertThat(actual)
.as("MetadataTables should return all metadata table names")
.containsExactlyInAnyOrderElementsOf(expected);
}

private int rowCount(TableScan scan) throws IOException {
int count = 0;
try (CloseableIterable<FileScanTask> tasks = scan.planFiles()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,11 @@ public void testSerializableMetadataTablesPlanning(boolean fromSerialized) throw
Set<CharSequence> newFiles = getFiles(getMetaDataTable(table, type));

// Expect that the new data is changed in the meantime
assertThat(deserializedFiles).isNotEqualTo(newFiles);
if (type == MetadataTableType.METADATA_TABLES) {

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.

not entirely sure why the new table is any different than the other in this test.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Since reading the metadata tables does not require reading any actual files, the file path would remain unchanged regardless of how many commits are made. Therefore, this case needs to be handled separately.

assertThat(deserializedFiles).isEqualTo(newFiles);
} else {
assertThat(deserializedFiles).isNotEqualTo(newFiles);
}
}
}

Expand Down
26 changes: 26 additions & 0 deletions docs/docs/spark-queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,32 @@ To inspect a table's history, snapshots, and other metadata, Iceberg supports me

Metadata tables are identified by adding the metadata table name after the original table name. For example, history for `db.table` is read using `db.table.history`.

### Metadata Tables
To list the metadata tables supported by a table:

```sql
SELECT * FROM prod.db.table.metadata_tables;
```

| metadata_table_name |
|----------------------|
| entries |
| files |
| data_files |
| history |
| metadata_log_entries |
| snapshots |
| refs |
| manifests |
| partitions |
| all_data_files |
| all_delete_files |
| all_files |
| all_manifests |
| all_entries |
| position_deletes |
| metadata_tables |

### History

To show table history:
Expand Down
Loading