Gabriel39 commented on code in PR #70869: URL: https://github.com/apache/doris/pull/70869#discussion_r4663385384
########## fe/fe-iceberg-common/src/main/java/org/apache/doris/connector/iceberg/IcebergV4FileIO.java: ########## @@ -0,0 +1,163 @@ +// 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.doris.connector.iceberg; + +import com.github.benmanes.caffeine.cache.Caffeine; +import com.github.benmanes.caffeine.cache.LoadingCache; +import org.apache.iceberg.ContentFile; +import org.apache.iceberg.DataFile; +import org.apache.iceberg.DeleteFile; +import org.apache.iceberg.FileFormat; +import org.apache.iceberg.InternalData; +import org.apache.iceberg.ManifestFile; +import org.apache.iceberg.ManifestListFile; +import org.apache.iceberg.Schema; +import org.apache.iceberg.avro.AvroIterable; +import org.apache.iceberg.io.CloseableIterable; +import org.apache.iceberg.io.FileIO; +import org.apache.iceberg.io.InputFile; +import org.apache.iceberg.io.OutputFile; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.util.Map; +import java.util.Objects; +import java.util.regex.Pattern; + +/** Serializable, borrowed read view for table-relative format v4 locations. */ +public final class IcebergV4FileIO implements FileIO { + private static final long serialVersionUID = 1L; + private static final Pattern SCHEME = Pattern.compile("^[a-zA-Z][a-zA-Z0-9+.-]*:.*", Pattern.DOTALL); + private final FileIO delegate; + private final String tableLocation; + private transient LoadingCache<String, Integer> manifestVersions; + + public IcebergV4FileIO(FileIO delegate, String tableLocation) { + this.delegate = Objects.requireNonNull(delegate, "delegate"); + this.tableLocation = tableLocation; + } + + public FileIO delegate() { + return delegate; + } + + public static String resolve(String tableLocation, String path) { + Objects.requireNonNull(path, "path"); + if (isAbsolute(path)) { + return path; + } + if (tableLocation == null || tableLocation.isEmpty()) { + throw new IllegalArgumentException("Relative Iceberg v4 path requires a table location: " + path); + } + // Duplicate separators are part of an object-store key, including at the concatenation boundary. + return tableLocation + "/" + path; + } + + private static boolean isAbsolute(String path) { + return SCHEME.matcher(path).matches(); + } + + public String resolveLocation(String path) { + return resolve(tableLocation, path); + } + + public String resolveContentFile(ContentFile<?> file) { + return isAbsolute(file.location()) || isLegacyEntry(file) + ? file.location() : resolve(tableLocation, file.location()); + } + + private boolean isLegacyEntry(ContentFile<?> file) { + String manifest = file.manifestLocation(); + // Encoding alone is insufficient: the SDK can also write a V4 manifest using Avro. + return manifest != null && FileFormat.fromFileName(manifest) == FileFormat.AVRO + && versions().get(manifest) < 4; + } + + private synchronized LoadingCache<String, Integer> versions() { + if (manifestVersions == null) { + // Keep only small version numbers; never serialize a query's cache into every metadata task. + manifestVersions = Caffeine.newBuilder().maximumSize(256).build(this::readManifestVersion); + } + return manifestVersions; + } + + private int readManifestVersion(String manifest) { + try (CloseableIterable<?> header = InternalData.read(FileFormat.AVRO, + delegate.newInputFile(resolve(tableLocation, manifest))).project(new Schema()).build()) { + return Integer.parseInt(((AvroIterable<?>) header).getMetadata().getOrDefault("format-version", "1")); + } catch (IOException failure) { + throw new UncheckedIOException("Failed to read Iceberg manifest version", failure); + } + } + + @Override + public InputFile newInputFile(String path) { + return delegate.newInputFile(resolve(tableLocation, path)); + } + + @Override + public InputFile newInputFile(String path, long length) { + return delegate.newInputFile(resolve(tableLocation, path), length); + } + + // Absolute descriptors may carry encryption metadata understood by the delegate. Relative V4 encrypted + // descriptors retain the SDK's rejection rather than silently falling back to plaintext reads. + @Override + public InputFile newInputFile(DataFile file) { + return isAbsolute(file.location()) || isLegacyEntry(file) + ? delegate.newInputFile(file) : FileIO.super.newInputFile(file); + } + + @Override + public InputFile newInputFile(DeleteFile file) { + return isAbsolute(file.location()) || isLegacyEntry(file) + ? delegate.newInputFile(file) : FileIO.super.newInputFile(file); + } + + @Override + public InputFile newInputFile(ManifestFile file) { + return isAbsolute(file.path()) + ? delegate.newInputFile(file) : FileIO.super.newInputFile(file); + } + + @Override + public InputFile newInputFile(ManifestListFile file) { + return isAbsolute(file.location()) + ? delegate.newInputFile(file) : FileIO.super.newInputFile(file); Review Comment: Fixed in 3548d6ebd9b69c5c665722b4845eabaca31027b5. Manifest/list factories and the producing-version probe now preserve absolute filesystem metadata paths without a URI scheme. Native planning also reuses the existing bound V4 FileIO view; wrapping it again caused a second rebase during the manifest version probe. V4 content-file path rules remain unchanged, including literal doubled separators. Added SDK-backed V2/V3 upgrade cases with a qualified table location and absolute `write.metadata.path`. They discard snapshot caches and cover cold list/manifest reads, serialized FileIO, cached/uncached native planning, a historical snapshot and a later V4 append. Both cases fail on the previous head with the reported `NotFoundException` and pass with the fix. The full focused run passed 537 connector/JNI tests with zero failures, errors or skips; FE Checkstyle also passed. The packaged SQL regression runtime was not rerun for this follow-up. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
