timothyw553 commented on code in PR #18954: URL: https://github.com/apache/iceberg/pull/18954#discussion_r4726362785
########## core/src/main/java/org/apache/iceberg/io/CanonicalUriFileIO.java: ########## @@ -0,0 +1,181 @@ +/* + * 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.iceberg.io; + +import java.util.List; +import java.util.Map; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; + +public class CanonicalUriFileIO implements DelegateFileIO { + + private final FileIO delegate; + private final String canonicalPrefix; + private final String physicalPrefix; + + public CanonicalUriFileIO(FileIO delegate, String canonicalPrefix, String physicalPrefix) { + if (canonicalPrefix == null || canonicalPrefix.isEmpty()) { + throw new IllegalArgumentException("Canonical URI prefix must not be null or empty"); + } + + if (physicalPrefix == null || physicalPrefix.isEmpty()) { + throw new IllegalArgumentException("Physical URI prefix must not be null or empty"); + } + + if (!canonicalPrefix.endsWith("/") || !physicalPrefix.endsWith("/")) { + throw new IllegalArgumentException("URI mapping prefixes must end with '/'"); + } + + this.delegate = delegate; + this.canonicalPrefix = canonicalPrefix; + this.physicalPrefix = physicalPrefix; + } + + private String resolve(String location) { + if (location.startsWith(canonicalPrefix)) { + return physicalPrefix + location.substring(canonicalPrefix.length()); + } + + return location; + } + + @Override + public InputFile newInputFile(String location) { + return wrapInput(delegate.newInputFile(resolve(location)), location); + } + + @Override + public InputFile newInputFile(String location, long length) { + return wrapInput(delegate.newInputFile(resolve(location), length), location); + } + + private InputFile wrapInput(InputFile physical, String canonical) { + return new InputFile() { + @Override + public long getLength() { + return physical.getLength(); + } + + @Override + public SeekableInputStream newStream() { + return physical.newStream(); + } + + @Override + public String location() { + return canonical; + } + + @Override + public boolean exists() { + return physical.exists(); + } + }; + } + + @Override + public OutputFile newOutputFile(String location) { + OutputFile physical = delegate.newOutputFile(resolve(location)); + + return new OutputFile() { + @Override + public PositionOutputStream create() { + return physical.create(); + } + + @Override + public PositionOutputStream createOrOverwrite() { + return physical.createOrOverwrite(); + } + + @Override + public String location() { + return location; + } + + @Override + public InputFile toInputFile() { + return wrapInput(physical.toInputFile(), location); + } + }; + } + + @Override + public void deleteFile(String location) { + delegate.deleteFile(resolve(location)); + } + + private String canonicalize(String location) { + if (location.startsWith(physicalPrefix)) { Review Comment: comparing URIs with raw `startsWith` misses equivalent spellings, like a configured `file:///tmp/.../` prefix vs `HadoopFileIO` returning `file:/tmp/...`. could we compare parsed URI components under a defined normalization rule that handles empty authority and preserves path boundaries, and add a test using real `HadoopFileIO` listings? ########## core/src/main/java/org/apache/iceberg/io/CanonicalUriFileIO.java: ########## @@ -0,0 +1,181 @@ +/* + * 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.iceberg.io; + +import java.util.List; +import java.util.Map; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; + +public class CanonicalUriFileIO implements DelegateFileIO { + + private final FileIO delegate; + private final String canonicalPrefix; + private final String physicalPrefix; + + public CanonicalUriFileIO(FileIO delegate, String canonicalPrefix, String physicalPrefix) { + if (canonicalPrefix == null || canonicalPrefix.isEmpty()) { + throw new IllegalArgumentException("Canonical URI prefix must not be null or empty"); + } + + if (physicalPrefix == null || physicalPrefix.isEmpty()) { + throw new IllegalArgumentException("Physical URI prefix must not be null or empty"); + } + + if (!canonicalPrefix.endsWith("/") || !physicalPrefix.endsWith("/")) { + throw new IllegalArgumentException("URI mapping prefixes must end with '/'"); + } + + this.delegate = delegate; + this.canonicalPrefix = canonicalPrefix; + this.physicalPrefix = physicalPrefix; + } + + private String resolve(String location) { + if (location.startsWith(canonicalPrefix)) { + return physicalPrefix + location.substring(canonicalPrefix.length()); + } + + return location; + } + + @Override + public InputFile newInputFile(String location) { + return wrapInput(delegate.newInputFile(resolve(location)), location); + } + + @Override + public InputFile newInputFile(String location, long length) { + return wrapInput(delegate.newInputFile(resolve(location), length), location); + } + + private InputFile wrapInput(InputFile physical, String canonical) { + return new InputFile() { + @Override + public long getLength() { + return physical.getLength(); + } + + @Override + public SeekableInputStream newStream() { + return physical.newStream(); + } + + @Override + public String location() { + return canonical; + } + + @Override + public boolean exists() { + return physical.exists(); + } + }; + } + + @Override + public OutputFile newOutputFile(String location) { + OutputFile physical = delegate.newOutputFile(resolve(location)); + + return new OutputFile() { + @Override + public PositionOutputStream create() { + return physical.create(); + } + + @Override + public PositionOutputStream createOrOverwrite() { + return physical.createOrOverwrite(); + } + + @Override + public String location() { + return location; + } + + @Override + public InputFile toInputFile() { + return wrapInput(physical.toInputFile(), location); + } + }; + } + + @Override + public void deleteFile(String location) { + delegate.deleteFile(resolve(location)); + } + + private String canonicalize(String location) { + if (location.startsWith(physicalPrefix)) { + return canonicalPrefix + location.substring(physicalPrefix.length()); + } + + return location; + } + + @Override + public void deleteFiles(Iterable<String> pathsToDelete) throws BulkDeletionFailureException { + List<String> physicalPaths = Lists.newArrayList(); + + for (String path : pathsToDelete) { + physicalPaths.add(resolve(path)); + } + + if (delegate instanceof SupportsBulkOperations) { + ((SupportsBulkOperations) delegate).deleteFiles(physicalPaths); + } else { + for (String path : physicalPaths) { + delegate.deleteFile(path); + } + } + } + + @Override + public Iterable<FileInfo> listPrefix(String prefix) { + if (!(delegate instanceof SupportsPrefixOperations)) { + throw new UnsupportedOperationException("Delegate does not support prefix listing"); + } + + List<FileInfo> results = Lists.newArrayList(); + + for (FileInfo file : ((SupportsPrefixOperations) delegate).listPrefix(resolve(prefix))) { + results.add(new FileInfo(canonicalize(file.location()), file.size(), file.createdAtMillis())); Review Comment: listing a physical prefix converts every returned location to a canonical name, which also bypasses hidden-directory filters and could make live files look like orphans during cleanup. should we keep the physical location when the requested prefix isn't canonical? we'll still need a clear rule if mixed canonical and physical metadata is allowed. ```diff - results.add(new FileInfo(canonicalize(file.location()), file.size(), file.createdAtMillis())); + results.add( + new FileInfo( + prefix.startsWith(canonicalPrefix) + ? canonicalize(file.location()) + : file.location(), + file.size(), + file.createdAtMillis())); ``` ########## core/src/main/java/org/apache/iceberg/io/CanonicalUriFileIO.java: ########## @@ -0,0 +1,181 @@ +/* + * 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.iceberg.io; + +import java.util.List; +import java.util.Map; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; + +public class CanonicalUriFileIO implements DelegateFileIO { + + private final FileIO delegate; + private final String canonicalPrefix; + private final String physicalPrefix; + + public CanonicalUriFileIO(FileIO delegate, String canonicalPrefix, String physicalPrefix) { Review Comment: implementing `DelegateFileIO` advertises bulk and prefix support even when `delegate` is a plain `FileIO`, so prefix calls can throw `UnsupportedOperationException` and the `deleteFiles` fallback stops on the first failure. should we require `DelegateFileIO` in the constructor and drop the fallback loop? ```diff - private final FileIO delegate; + private final DelegateFileIO delegate; - public CanonicalUriFileIO(FileIO delegate, String canonicalPrefix, String physicalPrefix) { + public CanonicalUriFileIO( + DelegateFileIO delegate, String canonicalPrefix, String physicalPrefix) { ``` ########## core/src/main/java/org/apache/iceberg/io/CanonicalUriFileIO.java: ########## @@ -0,0 +1,181 @@ +/* + * 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.iceberg.io; + +import java.util.List; +import java.util.Map; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; + +public class CanonicalUriFileIO implements DelegateFileIO { + + private final FileIO delegate; + private final String canonicalPrefix; + private final String physicalPrefix; + + public CanonicalUriFileIO(FileIO delegate, String canonicalPrefix, String physicalPrefix) { + if (canonicalPrefix == null || canonicalPrefix.isEmpty()) { + throw new IllegalArgumentException("Canonical URI prefix must not be null or empty"); + } + + if (physicalPrefix == null || physicalPrefix.isEmpty()) { + throw new IllegalArgumentException("Physical URI prefix must not be null or empty"); + } + + if (!canonicalPrefix.endsWith("/") || !physicalPrefix.endsWith("/")) { + throw new IllegalArgumentException("URI mapping prefixes must end with '/'"); + } + + this.delegate = delegate; + this.canonicalPrefix = canonicalPrefix; + this.physicalPrefix = physicalPrefix; + } + + private String resolve(String location) { + if (location.startsWith(canonicalPrefix)) { + return physicalPrefix + location.substring(canonicalPrefix.length()); + } + + return location; + } + + @Override + public InputFile newInputFile(String location) { + return wrapInput(delegate.newInputFile(resolve(location)), location); + } + + @Override + public InputFile newInputFile(String location, long length) { + return wrapInput(delegate.newInputFile(resolve(location), length), location); + } + + private InputFile wrapInput(InputFile physical, String canonical) { + return new InputFile() { + @Override + public long getLength() { + return physical.getLength(); + } + + @Override + public SeekableInputStream newStream() { + return physical.newStream(); + } + + @Override + public String location() { + return canonical; + } + + @Override + public boolean exists() { + return physical.exists(); + } + }; + } + + @Override + public OutputFile newOutputFile(String location) { + OutputFile physical = delegate.newOutputFile(resolve(location)); + + return new OutputFile() { + @Override + public PositionOutputStream create() { + return physical.create(); + } + + @Override + public PositionOutputStream createOrOverwrite() { + return physical.createOrOverwrite(); + } + + @Override + public String location() { + return location; + } + + @Override + public InputFile toInputFile() { + return wrapInput(physical.toInputFile(), location); + } + }; + } + + @Override + public void deleteFile(String location) { + delegate.deleteFile(resolve(location)); + } + + private String canonicalize(String location) { + if (location.startsWith(physicalPrefix)) { + return canonicalPrefix + location.substring(physicalPrefix.length()); + } + + return location; + } + + @Override + public void deleteFiles(Iterable<String> pathsToDelete) throws BulkDeletionFailureException { + List<String> physicalPaths = Lists.newArrayList(); + + for (String path : pathsToDelete) { + physicalPaths.add(resolve(path)); + } + + if (delegate instanceof SupportsBulkOperations) { + ((SupportsBulkOperations) delegate).deleteFiles(physicalPaths); + } else { + for (String path : physicalPaths) { + delegate.deleteFile(path); + } + } + } + + @Override + public Iterable<FileInfo> listPrefix(String prefix) { + if (!(delegate instanceof SupportsPrefixOperations)) { + throw new UnsupportedOperationException("Delegate does not support prefix listing"); + } + + List<FileInfo> results = Lists.newArrayList(); Review Comment: `listPrefix()` materializes the whole listing into a list up front, which breaks lazy iteration and holds every entry in memory. could we transform the delegate's iterable lazily during iteration instead, with a test checking that calling `listPrefix()` doesn't consume it? ########## core/src/main/java/org/apache/iceberg/io/CanonicalUriFileIO.java: ########## @@ -0,0 +1,181 @@ +/* + * 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.iceberg.io; + +import java.util.List; +import java.util.Map; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; + +public class CanonicalUriFileIO implements DelegateFileIO { + + private final FileIO delegate; + private final String canonicalPrefix; + private final String physicalPrefix; + + public CanonicalUriFileIO(FileIO delegate, String canonicalPrefix, String physicalPrefix) { + if (canonicalPrefix == null || canonicalPrefix.isEmpty()) { + throw new IllegalArgumentException("Canonical URI prefix must not be null or empty"); + } + + if (physicalPrefix == null || physicalPrefix.isEmpty()) { + throw new IllegalArgumentException("Physical URI prefix must not be null or empty"); + } + + if (!canonicalPrefix.endsWith("/") || !physicalPrefix.endsWith("/")) { + throw new IllegalArgumentException("URI mapping prefixes must end with '/'"); + } + + this.delegate = delegate; + this.canonicalPrefix = canonicalPrefix; + this.physicalPrefix = physicalPrefix; + } + + private String resolve(String location) { + if (location.startsWith(canonicalPrefix)) { Review Comment: if prefixes overlap, like `s3://bucket/` -> `s3://bucket/data/`, an existing physical path `s3://bucket/data/x.parquet` matches `canonicalPrefix` and gets translated twice into `s3://bucket/data/data/x.parquet`. should we reject overlapping prefixes in the constructor for now and add tests for both overlap directions? ########## core/src/main/java/org/apache/iceberg/io/CanonicalUriFileIO.java: ########## @@ -0,0 +1,181 @@ +/* + * 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.iceberg.io; + +import java.util.List; +import java.util.Map; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; + +public class CanonicalUriFileIO implements DelegateFileIO { + + private final FileIO delegate; + private final String canonicalPrefix; + private final String physicalPrefix; + + public CanonicalUriFileIO(FileIO delegate, String canonicalPrefix, String physicalPrefix) { + if (canonicalPrefix == null || canonicalPrefix.isEmpty()) { + throw new IllegalArgumentException("Canonical URI prefix must not be null or empty"); + } + + if (physicalPrefix == null || physicalPrefix.isEmpty()) { + throw new IllegalArgumentException("Physical URI prefix must not be null or empty"); + } + + if (!canonicalPrefix.endsWith("/") || !physicalPrefix.endsWith("/")) { + throw new IllegalArgumentException("URI mapping prefixes must end with '/'"); + } + + this.delegate = delegate; + this.canonicalPrefix = canonicalPrefix; + this.physicalPrefix = physicalPrefix; + } + + private String resolve(String location) { + if (location.startsWith(canonicalPrefix)) { + return physicalPrefix + location.substring(canonicalPrefix.length()); + } + + return location; + } + + @Override + public InputFile newInputFile(String location) { + return wrapInput(delegate.newInputFile(resolve(location)), location); + } + + @Override + public InputFile newInputFile(String location, long length) { + return wrapInput(delegate.newInputFile(resolve(location), length), location); + } + + private InputFile wrapInput(InputFile physical, String canonical) { + return new InputFile() { + @Override + public long getLength() { + return physical.getLength(); + } + + @Override + public SeekableInputStream newStream() { + return physical.newStream(); + } + + @Override + public String location() { + return canonical; + } + + @Override + public boolean exists() { + return physical.exists(); + } + }; + } + + @Override + public OutputFile newOutputFile(String location) { + OutputFile physical = delegate.newOutputFile(resolve(location)); + + return new OutputFile() { + @Override + public PositionOutputStream create() { + return physical.create(); + } + + @Override + public PositionOutputStream createOrOverwrite() { + return physical.createOrOverwrite(); + } + + @Override + public String location() { + return location; + } + + @Override + public InputFile toInputFile() { + return wrapInput(physical.toInputFile(), location); + } + }; + } + + @Override + public void deleteFile(String location) { + delegate.deleteFile(resolve(location)); + } + + private String canonicalize(String location) { + if (location.startsWith(physicalPrefix)) { + return canonicalPrefix + location.substring(physicalPrefix.length()); + } + + return location; + } + + @Override + public void deleteFiles(Iterable<String> pathsToDelete) throws BulkDeletionFailureException { + List<String> physicalPaths = Lists.newArrayList(); + + for (String path : pathsToDelete) { + physicalPaths.add(resolve(path)); + } + + if (delegate instanceof SupportsBulkOperations) { + ((SupportsBulkOperations) delegate).deleteFiles(physicalPaths); + } else { + for (String path : physicalPaths) { + delegate.deleteFile(path); + } + } + } + + @Override + public Iterable<FileInfo> listPrefix(String prefix) { + if (!(delegate instanceof SupportsPrefixOperations)) { + throw new UnsupportedOperationException("Delegate does not support prefix listing"); + } + + List<FileInfo> results = Lists.newArrayList(); + + for (FileInfo file : ((SupportsPrefixOperations) delegate).listPrefix(resolve(prefix))) { + results.add(new FileInfo(canonicalize(file.location()), file.size(), file.createdAtMillis())); + } + + return results; + } + + @Override + public void deletePrefix(String prefix) { + if (!(delegate instanceof SupportsPrefixOperations)) { + throw new UnsupportedOperationException("Delegate does not support prefix deletion"); + } + + ((SupportsPrefixOperations) delegate).deletePrefix(resolve(prefix)); + } + + @Override + public Map<String, String> properties() { + return delegate.properties(); Review Comment: `properties()` omits the mapping prefixes and delegate identity, and `CanonicalUriFileIO` lacks the init wiring `FileIOParser` needs to reload it from JSON. should we implement full reconstruction or throw here for now so `FileIOParser` fails loudly instead of emitting unloadable JSON? ```diff public Map<String, String> properties() { - return delegate.properties(); + throw new UnsupportedOperationException( + "Canonical URI configuration serialization is not supported"); } ``` -- 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]
