Skip to content

Commit da7a577

Browse files
committed
Fixing "ice describe" for tables with corrupted metadata
1 parent f54c3ac commit da7a577

File tree

1 file changed

+58
-38
lines changed

1 file changed

+58
-38
lines changed

ice/src/main/java/com/altinity/ice/cli/internal/cmd/Describe.java

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@
2323
import java.util.Map;
2424
import java.util.Set;
2525
import javax.annotation.Nullable;
26-
import org.apache.iceberg.DataFile;
27-
import org.apache.iceberg.FileScanTask;
28-
import org.apache.iceberg.Snapshot;
29-
import org.apache.iceberg.TableScan;
26+
27+
import org.apache.iceberg.*;
3028
import org.apache.iceberg.catalog.Namespace;
3129
import org.apache.iceberg.catalog.TableIdentifier;
3230
import org.apache.iceberg.io.CloseableIterable;
@@ -74,42 +72,18 @@ public static void run(RESTCatalog catalog, String target, boolean json, Option.
7472
if (targetTable != null && !targetTable.equals(tableId.name())) {
7573
continue;
7674
}
77-
org.apache.iceberg.Table table = catalog.loadTable(tableId);
78-
Snapshot snapshot = table.currentSnapshot();
79-
Table.Snapshot snapshotInfo = null;
80-
if (snapshot != null) {
81-
snapshotInfo =
82-
new Table.Snapshot(
83-
snapshot.sequenceNumber(),
84-
snapshot.snapshotId(),
85-
snapshot.parentId(),
86-
snapshot.timestampMillis(),
87-
Instant.ofEpochMilli(snapshot.timestampMillis()).toString(),
88-
Instant.ofEpochMilli(snapshot.timestampMillis())
89-
.atZone(ZoneId.systemDefault())
90-
.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME),
91-
snapshot.operation(),
92-
snapshot.summary(),
93-
snapshot.manifestListLocation());
94-
}
9575

96-
List<Table.Metrics> metrics = null;
97-
if (optionsSet.contains(Option.INCLUDE_METRICS)) {
98-
metrics = gatherTableMetrics(table);
76+
Describe.Table.Data tableData = null;
77+
Table.Metadata tableMetadata;
78+
try {
79+
tableData = gatherTableData(catalog, tableId, optionsSet);
80+
tableMetadata = new Table.Metadata(tableId.toString());
81+
} catch (Exception e) {
82+
e.printStackTrace(System.err);
83+
tableMetadata = new Table.Metadata(tableId.toString(), e.getMessage());
9984
}
10085

101-
boolean includeSchema = optionsSet.contains(Option.INCLUDE_SCHEMA);
102-
Table.Data tableData =
103-
new Table.Data(
104-
includeSchema ? table.schema().toString() : null,
105-
includeSchema ? table.spec().toString() : null,
106-
includeSchema ? table.sortOrder().toString() : null,
107-
optionsSet.contains(Option.INCLUDE_PROPERTIES) ? table.properties() : null,
108-
table.location(),
109-
snapshotInfo,
110-
metrics);
111-
112-
tablesMetadata.add(new Table("Table", new Table.Metadata(tableId.toString()), tableData));
86+
tablesMetadata.add(new Table("Table", tableMetadata, tableData));
11387
}
11488
}
11589

@@ -125,6 +99,46 @@ public static void run(RESTCatalog catalog, String target, boolean json, Option.
12599
}
126100
}
127101

102+
private static Table.Data gatherTableData(
103+
RESTCatalog catalog,
104+
TableIdentifier tableId,
105+
Set<Describe.Option> optionsSet) throws IOException {
106+
107+
org.apache.iceberg.Table table = catalog.loadTable(tableId);
108+
Snapshot snapshot = table.currentSnapshot();
109+
Table.Snapshot snapshotInfo = null;
110+
if (snapshot != null) {
111+
snapshotInfo =
112+
new Table.Snapshot(
113+
snapshot.sequenceNumber(),
114+
snapshot.snapshotId(),
115+
snapshot.parentId(),
116+
snapshot.timestampMillis(),
117+
Instant.ofEpochMilli(snapshot.timestampMillis()).toString(),
118+
Instant.ofEpochMilli(snapshot.timestampMillis())
119+
.atZone(ZoneId.systemDefault())
120+
.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME),
121+
snapshot.operation(),
122+
snapshot.summary(),
123+
snapshot.manifestListLocation());
124+
}
125+
126+
List<Table.Metrics> metrics = null;
127+
if (optionsSet.contains(Option.INCLUDE_METRICS)) {
128+
metrics = gatherTableMetrics(table);
129+
}
130+
131+
boolean includeSchema = optionsSet.contains(Option.INCLUDE_SCHEMA);
132+
return new Table.Data(
133+
includeSchema ? table.schema().toString() : null,
134+
includeSchema ? table.spec().toString() : null,
135+
includeSchema ? table.sortOrder().toString() : null,
136+
optionsSet.contains(Option.INCLUDE_PROPERTIES) ? table.properties() : null,
137+
table.location(),
138+
snapshotInfo,
139+
metrics);
140+
}
141+
128142
private static List<Table.Metrics> gatherTableMetrics(org.apache.iceberg.Table table)
129143
throws IOException {
130144
List<Table.Metrics> metricsList = new ArrayList<>();
@@ -218,7 +232,13 @@ record Table(String kind, Table.Metadata metadata, Table.Data data) {
218232
}
219233
}
220234

221-
record Metadata(String id) {}
235+
record Metadata(String id, String status) {
236+
static final String STATUS_OK = "OK";
237+
238+
Metadata(String id) {
239+
this(id, STATUS_OK);
240+
}
241+
}
222242

223243
record Data(
224244
String schemaRaw,

0 commit comments

Comments
 (0)