@@ -79,6 +79,12 @@ public TableNotFoundException(TableIdentifier tableId) {
7979 }
8080 }
8181
82+ public static class NoSnapshotFoundException extends IOException {
83+ public NoSnapshotFoundException (TableIdentifier tableId ) {
84+ super ("No Snapshot found: '" + tableId + "'" );
85+ }
86+ }
87+
8288 @ Getter
8389 private final TableIdentifier tableId ;
8490 /** allow the {@link IcebergCatalog} creating this table to qualify its {@link DatasetDescriptor#getName()} used for lineage, etc. */
@@ -97,19 +103,22 @@ public TableNotFoundException(TableIdentifier tableId) {
97103 /** @return metadata info limited to the most recent (current) snapshot */
98104 public IcebergSnapshotInfo getCurrentSnapshotInfo () throws IOException {
99105 TableMetadata current = accessTableMetadata ();
100- return createSnapshotInfo (current .currentSnapshot (), Optional .of (current .metadataFileLocation ()), Optional .of (current ));
106+ Snapshot currentSnapshot = accessCurrentSnapshot (current );
107+ return createSnapshotInfo (currentSnapshot , Optional .of (current .metadataFileLocation ()), Optional .of (current ));
101108 }
102109
103110 /** @return metadata info for most recent snapshot, wherein manifests and their child data files ARE NOT listed */
104111 public IcebergSnapshotInfo getCurrentSnapshotInfoOverviewOnly () throws IOException {
105112 TableMetadata current = accessTableMetadata ();
106- return createSnapshotInfo (current .currentSnapshot (), Optional .of (current .metadataFileLocation ()), Optional .of (current ), true );
113+ Snapshot currentSnapshot = accessCurrentSnapshot (current );
114+ return createSnapshotInfo (currentSnapshot , Optional .of (current .metadataFileLocation ()), Optional .of (current ), true );
107115 }
108116
109117 /** @return metadata info for all known snapshots, ordered historically, with *most recent last* */
110118 public Iterator <IcebergSnapshotInfo > getAllSnapshotInfosIterator () throws IOException {
111119 TableMetadata current = accessTableMetadata ();
112- long currentSnapshotId = current .currentSnapshot ().snapshotId ();
120+ Snapshot currentSnapshot = accessCurrentSnapshot (current );
121+ long currentSnapshotId = currentSnapshot .snapshotId ();
113122 List <Snapshot > snapshots = current .snapshots ();
114123 return Iterators .transform (snapshots .iterator (), snapshot -> {
115124 try {
@@ -172,6 +181,12 @@ protected TableMetadata accessTableMetadata() throws TableNotFoundException {
172181 return Optional .ofNullable (current ).orElseThrow (() -> new TableNotFoundException (this .tableId ));
173182 }
174183
184+ /** @throws {@link IcebergTable.NoSnapshotFoundException} when table is empty i.e. table has zero snapshot */
185+ protected Snapshot accessCurrentSnapshot (TableMetadata tableMetadata ) throws NoSnapshotFoundException {
186+ Snapshot currentSnapshot = tableMetadata .currentSnapshot ();
187+ return Optional .ofNullable (currentSnapshot ).orElseThrow (() -> new NoSnapshotFoundException (this .tableId ));
188+ }
189+
175190 protected IcebergSnapshotInfo createSnapshotInfo (Snapshot snapshot , Optional <String > metadataFileLocation , Optional <TableMetadata > currentTableMetadata )
176191 throws IOException {
177192 return createSnapshotInfo (snapshot , metadataFileLocation , currentTableMetadata , false );
@@ -239,8 +254,7 @@ protected void registerIcebergTable(TableMetadata srcMetadata, TableMetadata dst
239254 public List <DataFile > getPartitionSpecificDataFiles (Predicate <StructLike > icebergPartitionFilterPredicate )
240255 throws IOException {
241256 TableMetadata tableMetadata = accessTableMetadata ();
242- Snapshot currentSnapshot = Optional .ofNullable (tableMetadata .currentSnapshot ())
243- .orElseThrow (() -> new IOException (String .format ("~%s~ No snapshots found" , tableId )));
257+ Snapshot currentSnapshot = accessCurrentSnapshot (tableMetadata );
244258 long currentSnapshotId = currentSnapshot .snapshotId ();
245259 List <DataFile > knownDataFiles = new ArrayList <>();
246260 GrowthMilestoneTracker growthMilestoneTracker = new GrowthMilestoneTracker ();
@@ -287,10 +301,10 @@ protected void overwritePartition(List<DataFile> dataFiles, String partitionColN
287301 return ;
288302 }
289303 TableMetadata tableMetadata = accessTableMetadata ();
290- Optional < Snapshot > currentSnapshot = Optional . ofNullable ( tableMetadata . currentSnapshot ());
291- if ( currentSnapshot . isPresent ()) {
292- log .info ("~{}~ SnapshotId before overwrite: {}" , tableId , currentSnapshot .get (). snapshotId ());
293- } else {
304+ try {
305+ Snapshot currentSnapshot = accessCurrentSnapshot ( tableMetadata );
306+ log .info ("~{}~ SnapshotId before overwrite: {}" , tableId , currentSnapshot .snapshotId ());
307+ } catch ( NoSnapshotFoundException e ) {
294308 log .warn ("~{}~ No current snapshot found before overwrite" , tableId );
295309 }
296310 OverwriteFiles overwriteFiles = this .table .newOverwrite ();
0 commit comments