3737import org .apache .paimon .utils .Filter ;
3838import org .apache .paimon .utils .ObjectsFile ;
3939import org .apache .paimon .utils .PathFactory ;
40+ import org .apache .paimon .utils .RowRangeIndex ;
4041import org .apache .paimon .utils .SegmentsCache ;
4142
4243import javax .annotation .Nullable ;
@@ -61,6 +62,7 @@ public class ManifestFile extends ObjectsFile<ManifestEntry> {
6162 private final AvroFileFormat avroFileFormat ;
6263 private final long suggestedFileSize ;
6364 private final CoreOptions options ;
65+ @ Nullable private final SegmentsCache <Path > sidecarCache ;
6466
6567 private ManifestFile (
6668 FileIO fileIO ,
@@ -72,6 +74,7 @@ private ManifestFile(
7274 PathFactory pathFactory ,
7375 long suggestedFileSize ,
7476 @ Nullable SegmentsCache <Path > cache ,
77+ @ Nullable SegmentsCache <Path > sidecarCache ,
7578 CoreOptions options ) {
7679 super (
7780 fileIO ,
@@ -89,6 +92,7 @@ private ManifestFile(
8992 this .avroFileFormat = avroFileFormat ;
9093 this .suggestedFileSize = suggestedFileSize ;
9194 this .options = options ;
95+ this .sidecarCache = sidecarCache == null ? cache : sidecarCache ;
9296 }
9397
9498 @ Override
@@ -140,9 +144,33 @@ public <T> List<T> read(
140144 Filter <InternalRow > readFilter ,
141145 Filter <ManifestEntry > readTFilter ,
142146 Function <ManifestEntry , T > convertor ) {
147+ return read (
148+ fileName ,
149+ fileSize ,
150+ partitionFilter ,
151+ bucketFilter ,
152+ readFilter ,
153+ readTFilter ,
154+ convertor ,
155+ null );
156+ }
157+
158+ public <T > List <T > read (
159+ String fileName ,
160+ @ Nullable Long fileSize ,
161+ @ Nullable PartitionPredicate partitionFilter ,
162+ @ Nullable BucketFilter bucketFilter ,
163+ Filter <InternalRow > readFilter ,
164+ Filter <ManifestEntry > readTFilter ,
165+ Function <ManifestEntry , T > convertor ,
166+ @ Nullable ManifestSidecar .Selection selected ) {
167+ if (selected != null && selected .blocks ().isEmpty ()) {
168+ return java .util .Collections .emptyList ();
169+ }
143170 try {
144171 Path path = pathFactory .toPath (fileName );
145- if (cache != null ) {
172+ // Sidecar selections use the block cache, even when every block is selected.
173+ if (cache != null && selected == null ) {
146174 ManifestEntryFilters filters =
147175 new ManifestEntryFilters (
148176 partitionFilter , bucketFilter , readFilter , readTFilter );
@@ -155,7 +183,9 @@ public <T> List<T> read(
155183 path ,
156184 ManifestEntry .MANIFEST_ROW_TYPE ,
157185 partitionFilter ,
158- bucketFilter );
186+ bucketFilter ,
187+ selected ,
188+ cache == null ? null : cache .segmentsCache ());
159189 return readFromIterator (iterator , serializer , readFilter , readTFilter , convertor );
160190 } catch (IOException e ) {
161191 throw new UncheckedIOException (e );
@@ -209,8 +239,23 @@ private static CloseableIterator<InternalRow> createManifestIterator(
209239 @ Nullable PartitionPredicate partitionFilter ,
210240 @ Nullable BucketFilter bucketFilter )
211241 throws IOException {
242+ return createManifestIterator (
243+ fileIO , path , projectedType , partitionFilter , bucketFilter , null , null );
244+ }
245+
246+ private static CloseableIterator <InternalRow > createManifestIterator (
247+ FileIO fileIO ,
248+ Path path ,
249+ RowType projectedType ,
250+ @ Nullable PartitionPredicate partitionFilter ,
251+ @ Nullable BucketFilter bucketFilter ,
252+ @ Nullable ManifestSidecar .Selection selected ,
253+ @ Nullable SegmentsCache <Object > cache )
254+ throws IOException {
212255 try {
213- ManifestAvroReader reader = new ManifestAvroReader (fileIO .newInputStream (path ));
256+ ManifestAvroReader reader =
257+ new ManifestAvroReader (
258+ ManifestSidecar .openManifest (fileIO , path , selected , cache ));
214259 return reader .read (projectedType , partitionFilter , bucketFilter );
215260 } catch (IOException e ) {
216261 FileUtils .checkExists (fileIO , path );
@@ -345,6 +390,32 @@ public Path toPath(String fileName) {
345390 };
346391 }
347392
393+ @ Nullable
394+ public ManifestSidecar .Selection selectBlocks (
395+ ManifestFileMeta manifest , @ Nullable RowRangeIndex query ) {
396+ return selectBlocks (manifest , query , null , null );
397+ }
398+
399+ @ Nullable
400+ public ManifestSidecar .Selection selectBlocks (
401+ ManifestFileMeta manifest ,
402+ @ Nullable RowRangeIndex query ,
403+ @ Nullable PartitionPredicate partitionFilter ,
404+ @ Nullable BucketFilter bucketFilter ) {
405+ return !options .manifestSidecarEnabled ()
406+ || (query == null && partitionFilter == null && bucketFilter == null )
407+ ? null
408+ : ManifestSidecar .read (
409+ fileIO ,
410+ pathFactory .toPath (manifest .fileName ()),
411+ manifest ,
412+ query ,
413+ partitionFilter ,
414+ partitionType ,
415+ bucketFilter == null ? null : bucketFilter ::mayContain ,
416+ sidecarCache );
417+ }
418+
348419 /** Deletes an unreferenced manifest and its explicitly referenced extra files. */
349420 public void delete (ManifestFileMeta manifest ) {
350421 delete (manifest .fileName ());
@@ -365,6 +436,7 @@ public static class Factory {
365436 private final long suggestedFileSize ;
366437 private final CoreOptions options ;
367438 @ Nullable private final SegmentsCache <Path > cache ;
439+ @ Nullable private final SegmentsCache <Path > sidecarCache ;
368440
369441 public Factory (
370442 FileIO fileIO ,
@@ -375,6 +447,7 @@ public Factory(
375447 FileStorePathFactory pathFactory ,
376448 long suggestedFileSize ,
377449 @ Nullable SegmentsCache <Path > cache ,
450+ @ Nullable SegmentsCache <Path > sidecarCache ,
378451 CoreOptions options ) {
379452 this .fileIO = fileIO ;
380453 this .schemaManager = schemaManager ;
@@ -384,13 +457,10 @@ public Factory(
384457 this .pathFactory = pathFactory ;
385458 this .suggestedFileSize = suggestedFileSize ;
386459 this .cache = cache ;
460+ this .sidecarCache = sidecarCache ;
387461 this .options = options ;
388462 }
389463
390- public boolean isCacheEnabled () {
391- return cache != null ;
392- }
393-
394464 public ManifestFile create () {
395465 return new ManifestFile (
396466 fileIO ,
@@ -402,6 +472,7 @@ public ManifestFile create() {
402472 pathFactory .manifestFileFactory (),
403473 suggestedFileSize ,
404474 cache ,
475+ sidecarCache ,
405476 options );
406477 }
407478 }
0 commit comments