AllManifestsTable.java org::apache::iceberg::AllManifestsTable org::apache::iceberg::AllManifestsTable::AllManifestsTableScan org::apache::iceberg::AllManifestsTable::ManifestListReadTask org::apache::iceberg::AllManifestsTable::SnapshotEvaluator org::apache::iceberg::AllManifestsTable::SnapshotEvaluator::SnapshotEvalVisitor org::apache::iceberg /* *LicensedtotheApacheSoftwareFoundation(ASF)underone *ormorecontributorlicenseagreements.SeetheNOTICEfile *distributedwiththisworkforadditionalinformation *regardingcopyrightownership.TheASFlicensesthisfile *toyouundertheApacheLicense,Version2.0(the *"License");youmaynotusethisfileexceptincompliance *withtheLicense.YoumayobtainacopyoftheLicenseat * *http://www.apache.org/licenses/LICENSE-2.0 * *Unlessrequiredbyapplicablelaworagreedtoinwriting, *softwaredistributedundertheLicenseisdistributedonan *"ASIS"BASIS,WITHOUTWARRANTIESORCONDITIONSOFANY *KIND,eitherexpressorimplied.SeetheLicenseforthe *specificlanguagegoverningpermissionsandlimitations *undertheLicense. */ packageorg.apache.iceberg; importjava.io.IOException; importjava.util.List; importjava.util.Map; importjava.util.Set; importjava.util.function.Function; importorg.apache.iceberg.avro.Avro; importorg.apache.iceberg.exceptions.RuntimeIOException; importorg.apache.iceberg.expressions.Binder; importorg.apache.iceberg.expressions.BoundReference; importorg.apache.iceberg.expressions.Expression; importorg.apache.iceberg.expressions.ExpressionVisitors; importorg.apache.iceberg.expressions.ExpressionVisitors.BoundExpressionVisitor; importorg.apache.iceberg.expressions.Expressions; importorg.apache.iceberg.expressions.Literal; importorg.apache.iceberg.io.CloseableIterable; importorg.apache.iceberg.io.FileIO; importorg.apache.iceberg.relocated.com.google.common.collect.ImmutableList; importorg.apache.iceberg.relocated.com.google.common.collect.Iterables; importorg.apache.iceberg.relocated.com.google.common.collect.Maps; importorg.apache.iceberg.types.Types; importorg.apache.iceberg.util.StructProjection; publicclassAllManifestsTableextendsBaseMetadataTable{ publicstaticfinalTypes.NestedFieldREF_SNAPSHOT_ID= Types.NestedField.required(18,"reference_snapshot_id",Types.LongType.get()); privatestaticfinalSchemaMANIFEST_FILE_SCHEMA= newSchema( Types.NestedField.required(14,"content",Types.IntegerType.get()), Types.NestedField.required(1,"path",Types.StringType.get()), Types.NestedField.required(2,"length",Types.LongType.get()), Types.NestedField.optional(3,"partition_spec_id",Types.IntegerType.get()), Types.NestedField.optional(4,"added_snapshot_id",Types.LongType.get()), Types.NestedField.optional(5,"added_data_files_count",Types.IntegerType.get()), Types.NestedField.optional(6,"existing_data_files_count",Types.IntegerType.get()), Types.NestedField.optional(7,"deleted_data_files_count",Types.IntegerType.get()), Types.NestedField.required(15,"added_delete_files_count",Types.IntegerType.get()), Types.NestedField.required(16,"existing_delete_files_count",Types.IntegerType.get()), Types.NestedField.required(17,"deleted_delete_files_count",Types.IntegerType.get()), Types.NestedField.optional( 8, "partition_summaries", Types.ListType.ofRequired( 9, Types.StructType.of( Types.NestedField.required(10,"contains_null",Types.BooleanType.get()), Types.NestedField.required(11,"contains_nan",Types.BooleanType.get()), Types.NestedField.optional(12,"lower_bound",Types.StringType.get()), Types.NestedField.optional(13,"upper_bound",Types.StringType.get())))), REF_SNAPSHOT_ID); AllManifestsTable(Tabletable){ this(table,table.name()+".all_manifests"); } AllManifestsTable(Tabletable,Stringname){ super(table,name); } @Override publicTableScannewScan(){ returnnewAllManifestsTableScan(table(),MANIFEST_FILE_SCHEMA); } @Override publicSchemaschema(){ returnMANIFEST_FILE_SCHEMA; } @Override MetadataTableTypemetadataTableType(){ returnMetadataTableType.ALL_MANIFESTS; } publicstaticclassAllManifestsTableScanextendsBaseAllMetadataTableScan{ AllManifestsTableScan(Tabletable,SchemafileSchema){ super(table,fileSchema,MetadataTableType.ALL_MANIFESTS); } privateAllManifestsTableScan(Tabletable,Schemaschema,TableScanContextcontext){ super(table,schema,MetadataTableType.ALL_MANIFESTS,context); } @Override protectedTableScannewRefinedScan(Tabletable,Schemaschema,TableScanContextcontext){ returnnewAllManifestsTableScan(table,schema,context); } @Override protectedCloseableIterable<FileScanTask>doPlanFiles(){ FileIOio=table().io(); Map<Integer,PartitionSpec>specs=Maps.newHashMap(table().specs()); Expressionfilter=shouldIgnoreResiduals()?Expressions.alwaysTrue():filter(); SnapshotEvaluatorsnapshotEvaluator= newSnapshotEvaluator(filter,MANIFEST_FILE_SCHEMA.asStruct(),isCaseSensitive()); Iterable<Snapshot>filteredSnapshots= Iterables.filter(table().snapshots(),snapshotEvaluator::eval); returnCloseableIterable.withNoopClose( Iterables.transform( filteredSnapshots, snap->{ if(snap.manifestListLocation()!=null){ returnnewManifestListReadTask( io,schema(),specs,snap.manifestListLocation(),filter,snap.snapshotId()); }else{ returnStaticDataTask.of( io.newInputFile( ((BaseTable)table()).operations().current().metadataFileLocation()), MANIFEST_FILE_SCHEMA, schema(), snap.allManifests(io), manifest-> manifestFileToRow( specs.get(manifest.partitionSpecId()),manifest,snap.snapshotId())); } })); } } staticclassManifestListReadTaskimplementsDataTask{ privatefinalFileIOio; privatefinalSchemaschema; privatefinalMap<Integer,PartitionSpec>specs; privatefinalStringmanifestListLocation; privatefinalExpressionresidual; privatefinallongreferenceSnapshotId; privateDataFilelazyDataFile=null; ManifestListReadTask( FileIOio, Schemaschema, Map<Integer,PartitionSpec>specs, StringmanifestListLocation, Expressionresidual, longreferenceSnapshotId){ this.io=io; this.schema=schema; this.specs=specs; this.manifestListLocation=manifestListLocation; this.residual=residual; this.referenceSnapshotId=referenceSnapshotId; } @Override publicList<DeleteFile>deletes(){ returnImmutableList.of(); } @Override publicCloseableIterable<StructLike>rows(){ try(CloseableIterable<ManifestFile>manifests= Avro.read(io.newInputFile(manifestListLocation)) .rename("manifest_file",GenericManifestFile.class.getName()) .rename("partitions",GenericPartitionFieldSummary.class.getName()) .rename("r508",GenericPartitionFieldSummary.class.getName()) .project(ManifestFile.schema()) .classLoader(GenericManifestFile.class.getClassLoader()) .reuseContainers(false) .build()){ CloseableIterable<StructLike>rowIterable= CloseableIterable.transform( manifests, manifest-> manifestFileToRow( specs.get(manifest.partitionSpecId()),manifest,referenceSnapshotId)); StructProjectionprojection=StructProjection.create(MANIFEST_FILE_SCHEMA,schema); returnCloseableIterable.transform(rowIterable,projection::wrap); }catch(IOExceptione){ thrownewRuntimeIOException(e,"Cannotreadmanifestlistfile:%s",manifestListLocation); } } @Override publicDataFilefile(){ if(lazyDataFile==null){ this.lazyDataFile= DataFiles.builder(PartitionSpec.unpartitioned()) .withInputFile(io.newInputFile(manifestListLocation)) .withRecordCount(1) .withFormat(FileFormat.AVRO) .build(); } returnlazyDataFile; } @Override publicPartitionSpecspec(){ returnPartitionSpec.unpartitioned(); } @Override publiclongstart(){ return0; } @Override publiclonglength(){ //returnagenericlengthtoavoidlookinguptheactuallength return8192; } @Override publicExpressionresidual(){ //thistableisunpartitionedsotheresidualisalwaysconstant returnresidual; } @Override publicIterable<FileScanTask>split(longsplitSize){ returnImmutableList.of(this);//don'tsplit } } staticStaticDataTask.RowmanifestFileToRow( PartitionSpecspec,ManifestFilemanifest,longreferenceSnapshotId){ returnStaticDataTask.Row.of( manifest.content().id(), manifest.path(), manifest.length(), manifest.partitionSpecId(), manifest.snapshotId(), manifest.content()==ManifestContent.DATA?manifest.addedFilesCount():0, manifest.content()==ManifestContent.DATA?manifest.existingFilesCount():0, manifest.content()==ManifestContent.DATA?manifest.deletedFilesCount():0, manifest.content()==ManifestContent.DELETES?manifest.addedFilesCount():0, manifest.content()==ManifestContent.DELETES?manifest.existingFilesCount():0, manifest.content()==ManifestContent.DELETES?manifest.deletedFilesCount():0, ManifestsTable.partitionSummariesToRows(spec,manifest.partitions()), referenceSnapshotId); } privatestaticclassSnapshotEvaluator{ privatefinalExpressionboundExpr; privateSnapshotEvaluator(Expressionexpr,Types.StructTypestructType,booleancaseSensitive){ this.boundExpr=Binder.bind(structType,expr,caseSensitive); } privatebooleaneval(Snapshotsnapshot){ returnnewSnapshotEvalVisitor().eval(snapshot); } privateclassSnapshotEvalVisitorextendsBoundExpressionVisitor<Boolean>{ privatelongsnapshotId; privatestaticfinalbooleanROWS_MIGHT_MATCH=true; privatestaticfinalbooleanROWS_CANNOT_MATCH=false; privatebooleaneval(Snapshotsnapshot){ this.snapshotId=snapshot.snapshotId(); returnExpressionVisitors.visitEvaluator(boundExpr,this); } @Override publicBooleanalwaysTrue(){ returnROWS_MIGHT_MATCH; } @Override publicBooleanalwaysFalse(){ returnROWS_CANNOT_MATCH; } @Override publicBooleannot(Booleanresult){ return!result; } @Override publicBooleanand(BooleanleftResult,BooleanrightResult){ returnleftResult&&rightResult; } @Override publicBooleanor(BooleanleftResult,BooleanrightResult){ returnleftResult||rightResult; } @Override public<T>BooleanisNull(BoundReference<T>ref){ if(isSnapshotRef(ref)){ returnROWS_CANNOT_MATCH;//reference_snapshot_idisnevernull }else{ returnROWS_MIGHT_MATCH; } } @Override public<T>BooleannotNull(BoundReference<T>ref){ returnROWS_MIGHT_MATCH; } @Override public<T>BooleanisNaN(BoundReference<T>ref){ if(isSnapshotRef(ref)){ returnROWS_CANNOT_MATCH;//reference_snapshot_idisnevernan }else{ returnROWS_MIGHT_MATCH; } } @Override public<T>BooleannotNaN(BoundReference<T>ref){ returnROWS_MIGHT_MATCH; } @Override public<T>Booleanlt(BoundReference<T>ref,Literal<T>lit){ returncompareSnapshotRef(ref,lit,compareResult->compareResult<0); } @Override public<T>BooleanltEq(BoundReference<T>ref,Literal<T>lit){ returncompareSnapshotRef(ref,lit,compareResult->compareResult<=0); } @Override public<T>Booleangt(BoundReference<T>ref,Literal<T>lit){ returncompareSnapshotRef(ref,lit,compareResult->compareResult>0); } @Override public<T>BooleangtEq(BoundReference<T>ref,Literal<T>lit){ returncompareSnapshotRef(ref,lit,compareResult->compareResult>=0); } @Override public<T>Booleaneq(BoundReference<T>ref,Literal<T>lit){ returncompareSnapshotRef(ref,lit,compareResult->compareResult==0); } @Override public<T>BooleannotEq(BoundReference<T>ref,Literal<T>lit){ returncompareSnapshotRef(ref,lit,compareResult->compareResult!=0); } @Override public<T>Booleanin(BoundReference<T>ref,Set<T>literalSet){ if(isSnapshotRef(ref)){ if(!literalSet.contains(snapshotId)){ returnROWS_CANNOT_MATCH; } } returnROWS_MIGHT_MATCH; } @Override public<T>BooleannotIn(BoundReference<T>ref,Set<T>literalSet){ if(isSnapshotRef(ref)){ if(literalSet.contains(snapshotId)){ returnROWS_CANNOT_MATCH; } } returnROWS_MIGHT_MATCH; } @Override public<T>BooleanstartsWith(BoundReference<T>ref,Literal<T>lit){ returnROWS_MIGHT_MATCH; } @Override public<T>BooleannotStartsWith(BoundReference<T>ref,Literal<T>lit){ returnROWS_MIGHT_MATCH; } private<T>BooleancompareSnapshotRef( BoundReference<T>ref,Literal<T>lit,Function<Integer,Boolean>desiredResult){ if(isSnapshotRef(ref)){ Literal<Long>longLit=lit.to(Types.LongType.get()); intcmp=longLit.comparator().compare(snapshotId,longLit.value()); if(!desiredResult.apply(cmp)){ returnROWS_CANNOT_MATCH; } } returnROWS_MIGHT_MATCH; } private<T>booleanisSnapshotRef(BoundReference<T>ref){ returnref.fieldId()==REF_SNAPSHOT_ID.fieldId(); } } } }