okumin commented on code in PR #6812: URL: https://github.com/apache/hive/pull/6812#discussion_r4164197274
########## iceberg/iceberg-catalog/src/main/java/org/apache/iceberg/hive/ScopedDeleteFileIO.java: ########## @@ -0,0 +1,104 @@ +/* + * 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.hive; + +import java.util.Map; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.hive.metastore.utils.FileUtils; +import org.apache.iceberg.io.FileIO; +import org.apache.iceberg.io.InputFile; +import org.apache.iceberg.io.OutputFile; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A {@link FileIO} decorator used by {@link HiveCatalog#dropTable(org.apache.iceberg.catalog.TableIdentifier, + * boolean)} to fence purge deletions to files under a table's own location, regardless of what a table's + * metadata or manifests actually reference. + * + * <p>This does not implement {@link org.apache.iceberg.io.SupportsBulkOperations} or + * {@link org.apache.iceberg.io.SupportsPrefixOperations} even when the delegate does, so that + * {@code CatalogUtil.dropTableData} is forced to route every deletion through {@link #deleteFile(String)}. + */ +class ScopedDeleteFileIO implements FileIO { + private static final Logger LOG = LoggerFactory.getLogger(ScopedDeleteFileIO.class); + + private final FileIO delegate; + private final Path location; + + ScopedDeleteFileIO(FileIO delegate, String location) { + this.delegate = delegate; + this.location = new Path(location); + } + + @Override + public InputFile newInputFile(String path) { + return delegate.newInputFile(path); + } + + @Override + public OutputFile newOutputFile(String path) { + return delegate.newOutputFile(path); + } + + @Override + public void deleteFile(String path) { + var candidate = new Path(path); + if (!FileUtils.isPathWithinSubtree(qualify(candidate, location), qualify(location, candidate))) { + LOG.debug("Skipping delete outside table location {}: {}", location, path); + return; + } + delegate.deleteFile(path); + } + + /** + * A path with no scheme refers to the same filesystem location as an otherwise-identical path + * that does carry one, whichever side that happens to be: manifests can hold data file paths + * written without a scheme, and a table's own location can equally be scheme-less (e.g. when a + * caller supplies one explicitly). {@link org.apache.hadoop.fs.Path#equals} is scheme-sensitive, + * so without this a schemeless path would never match its scheme-qualified counterpart in + * {@link FileUtils#isPathWithinSubtree} even when it is nested directly under it. When both (or + * neither) sides carry a scheme, {@code path} is returned unchanged, so a genuine mismatch (e.g. + * a different scheme or authority) is still correctly treated as outside the table location. + */ + private static Path qualify(Path path, Path other) { + var pathUri = path.toUri(); + var otherUri = other.toUri(); + if (pathUri.getScheme() == null && otherUri.getScheme() != null) { + return new Path(otherUri.getScheme(), otherUri.getAuthority(), pathUri.getPath()); Review Comment: I am checking which scheme and authority to use. I expect the current implementation is fine, but we might need to use the default filesystem specified in core-site.xml. I'm checking just in case. ########## standalone-metastore/metastore-common/src/main/java/org/apache/hadoop/hive/metastore/utils/FileUtils.java: ########## @@ -658,4 +658,28 @@ void findNext() throws IOException { nextFile = null; } } + + /** + * Checks whether {@code path} is {@code subtree} itself or nested under it, by walking up + * {@code path}'s ancestors looking for a match. Depth-based rather than a string-prefix + * comparison, so it isn't fooled by sibling paths that merely share a prefix (e.g. {@code /a/bc} + * is not within {@code /a/b}). + * + * @param path the path to check + * @param subtree the candidate ancestor, or exact match + * @return true if {@code path} equals {@code subtree} or is one of its descendants + */ + public static boolean isPathWithinSubtree(final Path path, final Path subtree) { Review Comment: Let's add these test cases, too. https://github.com/apache/hive/blob/744271738e2d89f94156c41bc307efaa57a06b7e/common/src/test/org/apache/hadoop/hive/common/TestFileUtils.java#L66-L93 -- 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]
