You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@polaris.apache.org by "dimas-b (via GitHub)" <gi...@apache.org> on 2026/05/12 02:10:59 UTC

[PR] Make location overlap checks more robust [polaris]

dimas-b opened a new pull request, #4408:
URL: https://github.com/apache/polaris/pull/4408

   * Add time-limited retries to `validateNoLocationOverlap`
   
   * Add `SIBLING_TIMEOUT_MILLIS` to `FeatureConfiguration` to allow users to control the total retry duration.
   
   * Throw a `CommitFailedException` subclass if coherent data about all siblings cannot be resolved during the retry period.
   
   * Concurrent sibling removal now causes a retry instead of hard failure.
   
   * Final sibling resolution failures lead to 409 (Conflict) errors at the REST API level instead of 500 (Server Failure) now.
   
   * Sibling resolution can still fail under massive concurrent load of DELETE requests, but the probability of that should be reduced now and users can control it via the timeout parameter.
   
   Closes #4407
   
   <!--
   ๐Ÿ“ Describe what changes you're proposing, especially breaking or user-facing changes. 
   ๐Ÿ“– See https://github.com/apache/polaris/blob/main/CONTRIBUTING.md for more.
   -->
   
   ## Checklist
   - [ ] ๐Ÿ›ก๏ธ Don't disclose security issues! (contact security@apache.org)
   - [ ] ๐Ÿ”— Clearly explained why the changes are needed, or linked related issues: Fixes #
   - [ ] ๐Ÿงช Added/updated tests with good coverage, or manually tested (and explained how)
   - [ ] ๐Ÿ’ก Added comments for complex logic
   - [ ] ๐Ÿงพ Updated `CHANGELOG.md` (if needed)
   - [ ] ๐Ÿ“š Updated documentation in `site/content/in-dev/unreleased` (if needed)
   


-- 
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: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make location overlap checks more robust [polaris]

Posted by "flyrain (via GitHub)" <gi...@apache.org>.
flyrain commented on code in PR #4408:
URL: https://github.com/apache/polaris/pull/4408#discussion_r3238415071


##########
runtime/service/src/test/java/org/apache/polaris/service/catalog/iceberg/IcebergCatalogTest.java:
##########
@@ -0,0 +1,142 @@
+/*
+ * 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.polaris.service.catalog.iceberg;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.when;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.polaris.core.PolarisDiagnostics;
+import org.apache.polaris.core.auth.PolarisPrincipal;
+import org.apache.polaris.core.context.CallContext;
+import org.apache.polaris.core.entity.CatalogEntity;
+import org.apache.polaris.core.entity.PolarisEntity;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.core.exceptions.CommitConflictException;
+import org.apache.polaris.core.persistence.ResolvedPolarisEntity;
+import org.apache.polaris.core.persistence.resolver.PolarisResolutionManifestCatalogView;
+import org.apache.polaris.core.persistence.resolver.ResolvedPathKey;
+import org.apache.polaris.core.persistence.resolver.Resolver;
+import org.apache.polaris.core.persistence.resolver.ResolverFactory;
+import org.apache.polaris.core.persistence.resolver.ResolverPath;
+import org.apache.polaris.core.persistence.resolver.ResolverStatus;
+import org.assertj.core.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+@ExtendWith(MockitoExtension.class)
+class IcebergCatalogTest {
+
+  @Mock ResolverFactory resolverFactory;
+  @Mock CallContext callContext;
+  @Mock PolarisResolutionManifestCatalogView resolvedEntityView;
+  @Mock CatalogEntity catalogEntity;
+  @Mock PolarisDiagnostics diagnostics;
+  @Mock Resolver resolver;
+
+  @Mock PolarisEntity ns1;
+  @Mock PolarisEntity ns2;
+  @Mock PolarisEntity table3;
+
+  private final PolarisPrincipal principal = PolarisPrincipal.of("test", Map.of(), Set.of());
+
+  private IcebergCatalog catalog;
+  private ResolvedPolarisEntity rns1 = new ResolvedPolarisEntity(ns1, List.of(), List.of());
+  private ResolvedPolarisEntity rns2 = new ResolvedPolarisEntity(ns2, List.of(), List.of());
+  private ResolvedPolarisEntity rt3 = new ResolvedPolarisEntity(table3, List.of(), List.of());
+
+  @BeforeEach
+  void initMocks() {
+    when(resolvedEntityView.getResolvedCatalogEntity()).thenReturn(catalogEntity);
+    when(resolverFactory.createResolver(any(), any())).thenReturn(resolver);
+    when(resolver.resolveAll()).thenReturn(new ResolverStatus(ResolverStatus.StatusEnum.SUCCESS));
+
+    rns1 = new ResolvedPolarisEntity(ns1, List.of(), List.of());
+    rns2 = new ResolvedPolarisEntity(ns2, List.of(), List.of());
+    rt3 = new ResolvedPolarisEntity(table3, List.of(), List.of());
+
+    catalog =
+        new IcebergCatalog(
+            diagnostics,
+            resolverFactory,
+            null,
+            callContext,
+            resolvedEntityView,
+            principal,
+            null,
+            null,
+            null,
+            null,
+            null);
+  }
+
+  @Test
+  void testFailedLocationSiblingResolutionException() {
+    when(resolver.resolveAll())
+        .thenReturn(new ResolverStatus(PolarisEntityType.TABLE_LIKE, "test-name"));
+    Assertions.assertThatThrownBy(() -> catalog.resolveOptionalPaths(List.of(), "test"))
+        .isInstanceOf(CommitConflictException.class)
+        .hasMessageContaining("Unable to resolve sibling entities to validate location")
+        .hasMessageContaining("test-name")
+        .hasMessageContaining(ResolverStatus.StatusEnum.ENTITY_COULD_NOT_BE_RESOLVED.name());
+
+    when(resolver.resolveAll())
+        .thenReturn(
+            new ResolverStatus(
+                new ResolverPath(
+                    List.of("ns1", "ns2", "table3"), PolarisEntityType.TABLE_LIKE, true),
+                123));
+    Assertions.assertThatThrownBy(() -> catalog.resolveOptionalPaths(List.of(), "test"))
+        .isInstanceOf(CommitConflictException.class)
+        .hasMessageContaining("Unable to resolve sibling entities to validate location")
+        .hasMessageContaining("ns1.ns2.table3")
+        .hasMessageContaining("failed index: 123")
+        .hasMessageContaining(ResolverStatus.StatusEnum.PATH_COULD_NOT_BE_FULLY_RESOLVED.name());
+  }
+
+  @Test
+  void testLocationSiblingResolution() {
+    ResolvedPathKey k1 = ResolvedPathKey.ofTableLike(TableIdentifier.of("ns1", "ns2", "t3"));
+    ResolvedPathKey k2 = ResolvedPathKey.ofTableLike(TableIdentifier.of("missing"));
+    ResolvedPathKey k3 = ResolvedPathKey.ofNamespace(Namespace.of("ns1", "ns2"));
+    ResolvedPathKey k4 = ResolvedPathKey.ofNamespace(Namespace.of("ns1", "ns2", "partial"));
+
+    List<List<ResolvedPolarisEntity>> resolved = new ArrayList<>();
+    resolved.add(List.of(rns1, rns2, rt3));
+    resolved.add(List.of());
+    resolved.add(List.of(rns1, rns2));
+    resolved.add(List.of(rns1, rns2));
+
+    when(resolver.getResolvedPaths()).thenReturn(resolved);
+    List<PolarisEntity> entities = catalog.resolveOptionalPaths(List.of(k1, k2, k3, k4), "test");
+    assertThat(entities).hasSize(2);
+    assertThat(entities.get(0)).isSameAs(table3);
+    assertThat(entities.get(1)).isSameAs(ns2);
+  }
+}

Review Comment:
   Worth adding a case that drives `validateNoLocationOverlap` end-to-end with a generic-table sibling โ€” would have surfaced the `getPropertiesAsMap` vs `getInternalPropertiesAsMap` issue.



-- 
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: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make location overlap checks more robust [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #4408:
URL: https://github.com/apache/polaris/pull/4408#discussion_r3230512828


##########
runtime/service/src/main/java/org/apache/polaris/service/catalog/iceberg/IcebergCatalog.java:
##########
@@ -2725,4 +2736,37 @@ private Page<TableIdentifier> listTableLike(
   private int getMaxMetadataRefreshRetries() {
     return realmConfig.getConfig(FeatureConfiguration.MAX_METADATA_REFRESH_RETRIES);
   }
+
+  /**
+   * Sub-classing {@link CommitFailedException} to cause Conflict (409) errors at the REST API level
+   * if this exceptio escaped, instead of Server Error (500).
+   */
+  @VisibleForTesting
+  static class UnableToCheckSiblingLocationsException extends CommitFailedException {

Review Comment:
   not really, reties were server-side, but I refactored that part completely.
   
   `CommitFailedException` was used only for the 409 error code.



-- 
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: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make location overlap checks more robust [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #4408:
URL: https://github.com/apache/polaris/pull/4408#discussion_r3243294921


##########
runtime/service/src/main/java/org/apache/polaris/service/catalog/iceberg/IcebergCatalog.java:
##########
@@ -1242,70 +1241,87 @@ private <T extends PolarisEntity & LocationBasedEntity> void validateNoLocationO
                   return Namespace.of(newLevels);
                 })
             .toList();
-    LOGGER.debug(
-        "Resolving {} sibling entities to validate location",
-        siblingTables.size() + siblingNamespaces.size());
-    PolarisResolutionManifest resolutionManifest =
-        new PolarisResolutionManifest(
-            diagnostics,
-            callContext.getRealmContext(),
-            resolverFactory,
-            principal,
-            parentPath.getFirst().getName());
+    List<ResolvedPathKey> pathsToResolve =
+        new ArrayList<>(siblingTables.size() + siblingNamespaces.size());
     siblingTables.forEach(
-        tbl ->
-            resolutionManifest.addPath(
-                new ResolverPath(
-                    PolarisCatalogHelpers.tableIdentifierToList(tbl),
-                    PolarisEntityType.TABLE_LIKE)));
+        tbl -> {
+          if (!tbl.name().equals(name)) {
+            pathsToResolve.add(ResolvedPathKey.ofTableLike(tbl));
+          }
+        });
     siblingNamespaces.forEach(
-        ns ->
-            resolutionManifest.addPath(
-                new ResolverPath(Arrays.asList(ns.levels()), PolarisEntityType.NAMESPACE)));
+        ns -> {
+          if (!ns.level(ns.length() - 1).equals(name)) {
+            pathsToResolve.add(ResolvedPathKey.ofNamespace(ns));
+          }
+        });
+
+    StorageLocation targetLocation = StorageLocation.of(location);
+    for (PolarisEntity entityToCheck :
+        resolveOptionalPaths(pathsToResolve, parentPath.getFirst().getName())) {
+      String loc =
+          entityToCheck.getPropertiesAsMap().get(PolarisEntityConstants.ENTITY_BASE_LOCATION);
+      if (loc == null) {
+        continue;
+      }
+
+      StorageLocation siblingLocation = StorageLocation.of(loc);
+
+      if (targetLocation.isChildOf(siblingLocation) || siblingLocation.isChildOf(targetLocation)) {
+        throw new ForbiddenException(
+            "Unable to create table at location '%s' because it conflicts with existing table or namespace at "

Review Comment:
   The message is the same as before. I can adjust it if you prefer.



-- 
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: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make location overlap checks more robust [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #4408:
URL: https://github.com/apache/polaris/pull/4408#discussion_r3230512828


##########
runtime/service/src/main/java/org/apache/polaris/service/catalog/iceberg/IcebergCatalog.java:
##########
@@ -2725,4 +2736,37 @@ private Page<TableIdentifier> listTableLike(
   private int getMaxMetadataRefreshRetries() {
     return realmConfig.getConfig(FeatureConfiguration.MAX_METADATA_REFRESH_RETRIES);
   }
+
+  /**
+   * Sub-classing {@link CommitFailedException} to cause Conflict (409) errors at the REST API level
+   * if this exceptio escaped, instead of Server Error (500).
+   */
+  @VisibleForTesting
+  static class UnableToCheckSiblingLocationsException extends CommitFailedException {

Review Comment:
   no longer used



-- 
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: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make location overlap checks more robust [polaris]

Posted by "flyrain (via GitHub)" <gi...@apache.org>.
flyrain commented on code in PR #4408:
URL: https://github.com/apache/polaris/pull/4408#discussion_r3238415065


##########
runtime/service/src/main/java/org/apache/polaris/service/catalog/iceberg/IcebergCatalog.java:
##########
@@ -1242,70 +1241,87 @@ private <T extends PolarisEntity & LocationBasedEntity> void validateNoLocationO
                   return Namespace.of(newLevels);
                 })
             .toList();
-    LOGGER.debug(
-        "Resolving {} sibling entities to validate location",
-        siblingTables.size() + siblingNamespaces.size());
-    PolarisResolutionManifest resolutionManifest =
-        new PolarisResolutionManifest(
-            diagnostics,
-            callContext.getRealmContext(),
-            resolverFactory,
-            principal,
-            parentPath.getFirst().getName());
+    List<ResolvedPathKey> pathsToResolve =
+        new ArrayList<>(siblingTables.size() + siblingNamespaces.size());
     siblingTables.forEach(
-        tbl ->
-            resolutionManifest.addPath(
-                new ResolverPath(
-                    PolarisCatalogHelpers.tableIdentifierToList(tbl),
-                    PolarisEntityType.TABLE_LIKE)));
+        tbl -> {
+          if (!tbl.name().equals(name)) {
+            pathsToResolve.add(ResolvedPathKey.ofTableLike(tbl));
+          }
+        });
     siblingNamespaces.forEach(
-        ns ->
-            resolutionManifest.addPath(
-                new ResolverPath(Arrays.asList(ns.levels()), PolarisEntityType.NAMESPACE)));
+        ns -> {
+          if (!ns.level(ns.length() - 1).equals(name)) {
+            pathsToResolve.add(ResolvedPathKey.ofNamespace(ns));
+          }
+        });
+
+    StorageLocation targetLocation = StorageLocation.of(location);
+    for (PolarisEntity entityToCheck :
+        resolveOptionalPaths(pathsToResolve, parentPath.getFirst().getName())) {
+      String loc =
+          entityToCheck.getPropertiesAsMap().get(PolarisEntityConstants.ENTITY_BASE_LOCATION);
+      if (loc == null) {
+        continue;
+      }
+
+      StorageLocation siblingLocation = StorageLocation.of(loc);
+
+      if (targetLocation.isChildOf(siblingLocation) || siblingLocation.isChildOf(targetLocation)) {
+        throw new ForbiddenException(
+            "Unable to create table at location '%s' because it conflicts with existing table or namespace at "

Review Comment:
   This method also runs for namespaces, message reads weird in that case. "Unable to create entity" or similar.



-- 
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: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make location overlap checks more robust [polaris]

Posted by "flyrain (via GitHub)" <gi...@apache.org>.
flyrain commented on code in PR #4408:
URL: https://github.com/apache/polaris/pull/4408#discussion_r3270636877


##########
polaris-core/src/main/java/org/apache/polaris/core/entity/PolarisEntityUtils.java:
##########
@@ -75,4 +78,20 @@ public static Namespace decodeNamespace(String encodedNs) {
     }
     return Namespace.of(levels);
   }
+
+  public static Optional<LocationBasedEntity> asLocationBasedEntity(PolarisEntity entity) {

Review Comment:
   +1  nice solution, thanks for adding it



-- 
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: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make location overlap checks more robust [polaris]

Posted by "flyrain (via GitHub)" <gi...@apache.org>.
flyrain commented on code in PR #4408:
URL: https://github.com/apache/polaris/pull/4408#discussion_r3238415065


##########
runtime/service/src/main/java/org/apache/polaris/service/catalog/iceberg/IcebergCatalog.java:
##########
@@ -1242,70 +1241,87 @@ private <T extends PolarisEntity & LocationBasedEntity> void validateNoLocationO
                   return Namespace.of(newLevels);
                 })
             .toList();
-    LOGGER.debug(
-        "Resolving {} sibling entities to validate location",
-        siblingTables.size() + siblingNamespaces.size());
-    PolarisResolutionManifest resolutionManifest =
-        new PolarisResolutionManifest(
-            diagnostics,
-            callContext.getRealmContext(),
-            resolverFactory,
-            principal,
-            parentPath.getFirst().getName());
+    List<ResolvedPathKey> pathsToResolve =
+        new ArrayList<>(siblingTables.size() + siblingNamespaces.size());
     siblingTables.forEach(
-        tbl ->
-            resolutionManifest.addPath(
-                new ResolverPath(
-                    PolarisCatalogHelpers.tableIdentifierToList(tbl),
-                    PolarisEntityType.TABLE_LIKE)));
+        tbl -> {
+          if (!tbl.name().equals(name)) {
+            pathsToResolve.add(ResolvedPathKey.ofTableLike(tbl));
+          }
+        });
     siblingNamespaces.forEach(
-        ns ->
-            resolutionManifest.addPath(
-                new ResolverPath(Arrays.asList(ns.levels()), PolarisEntityType.NAMESPACE)));
+        ns -> {
+          if (!ns.level(ns.length() - 1).equals(name)) {
+            pathsToResolve.add(ResolvedPathKey.ofNamespace(ns));
+          }
+        });
+
+    StorageLocation targetLocation = StorageLocation.of(location);
+    for (PolarisEntity entityToCheck :
+        resolveOptionalPaths(pathsToResolve, parentPath.getFirst().getName())) {
+      String loc =
+          entityToCheck.getPropertiesAsMap().get(PolarisEntityConstants.ENTITY_BASE_LOCATION);
+      if (loc == null) {
+        continue;
+      }
+
+      StorageLocation siblingLocation = StorageLocation.of(loc);
+
+      if (targetLocation.isChildOf(siblingLocation) || siblingLocation.isChildOf(targetLocation)) {
+        throw new ForbiddenException(
+            "Unable to create table at location '%s' because it conflicts with existing table or namespace at "

Review Comment:
   This method also runs for namespaces โ€” message reads weird in that case. "Unable to create entity" or similar.



##########
runtime/service/src/test/java/org/apache/polaris/service/catalog/iceberg/IcebergCatalogTest.java:
##########
@@ -0,0 +1,142 @@
+/*
+ * 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.polaris.service.catalog.iceberg;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.when;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.polaris.core.PolarisDiagnostics;
+import org.apache.polaris.core.auth.PolarisPrincipal;
+import org.apache.polaris.core.context.CallContext;
+import org.apache.polaris.core.entity.CatalogEntity;
+import org.apache.polaris.core.entity.PolarisEntity;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.core.exceptions.CommitConflictException;
+import org.apache.polaris.core.persistence.ResolvedPolarisEntity;
+import org.apache.polaris.core.persistence.resolver.PolarisResolutionManifestCatalogView;
+import org.apache.polaris.core.persistence.resolver.ResolvedPathKey;
+import org.apache.polaris.core.persistence.resolver.Resolver;
+import org.apache.polaris.core.persistence.resolver.ResolverFactory;
+import org.apache.polaris.core.persistence.resolver.ResolverPath;
+import org.apache.polaris.core.persistence.resolver.ResolverStatus;
+import org.assertj.core.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+@ExtendWith(MockitoExtension.class)
+class IcebergCatalogTest {
+
+  @Mock ResolverFactory resolverFactory;
+  @Mock CallContext callContext;
+  @Mock PolarisResolutionManifestCatalogView resolvedEntityView;
+  @Mock CatalogEntity catalogEntity;
+  @Mock PolarisDiagnostics diagnostics;
+  @Mock Resolver resolver;
+
+  @Mock PolarisEntity ns1;
+  @Mock PolarisEntity ns2;
+  @Mock PolarisEntity table3;
+
+  private final PolarisPrincipal principal = PolarisPrincipal.of("test", Map.of(), Set.of());
+
+  private IcebergCatalog catalog;
+  private ResolvedPolarisEntity rns1 = new ResolvedPolarisEntity(ns1, List.of(), List.of());
+  private ResolvedPolarisEntity rns2 = new ResolvedPolarisEntity(ns2, List.of(), List.of());
+  private ResolvedPolarisEntity rt3 = new ResolvedPolarisEntity(table3, List.of(), List.of());
+
+  @BeforeEach
+  void initMocks() {
+    when(resolvedEntityView.getResolvedCatalogEntity()).thenReturn(catalogEntity);
+    when(resolverFactory.createResolver(any(), any())).thenReturn(resolver);
+    when(resolver.resolveAll()).thenReturn(new ResolverStatus(ResolverStatus.StatusEnum.SUCCESS));
+
+    rns1 = new ResolvedPolarisEntity(ns1, List.of(), List.of());
+    rns2 = new ResolvedPolarisEntity(ns2, List.of(), List.of());
+    rt3 = new ResolvedPolarisEntity(table3, List.of(), List.of());
+
+    catalog =
+        new IcebergCatalog(
+            diagnostics,
+            resolverFactory,
+            null,
+            callContext,
+            resolvedEntityView,
+            principal,
+            null,
+            null,
+            null,
+            null,
+            null);
+  }
+
+  @Test
+  void testFailedLocationSiblingResolutionException() {
+    when(resolver.resolveAll())
+        .thenReturn(new ResolverStatus(PolarisEntityType.TABLE_LIKE, "test-name"));
+    Assertions.assertThatThrownBy(() -> catalog.resolveOptionalPaths(List.of(), "test"))
+        .isInstanceOf(CommitConflictException.class)
+        .hasMessageContaining("Unable to resolve sibling entities to validate location")
+        .hasMessageContaining("test-name")
+        .hasMessageContaining(ResolverStatus.StatusEnum.ENTITY_COULD_NOT_BE_RESOLVED.name());
+
+    when(resolver.resolveAll())
+        .thenReturn(
+            new ResolverStatus(
+                new ResolverPath(
+                    List.of("ns1", "ns2", "table3"), PolarisEntityType.TABLE_LIKE, true),
+                123));
+    Assertions.assertThatThrownBy(() -> catalog.resolveOptionalPaths(List.of(), "test"))
+        .isInstanceOf(CommitConflictException.class)
+        .hasMessageContaining("Unable to resolve sibling entities to validate location")
+        .hasMessageContaining("ns1.ns2.table3")
+        .hasMessageContaining("failed index: 123")
+        .hasMessageContaining(ResolverStatus.StatusEnum.PATH_COULD_NOT_BE_FULLY_RESOLVED.name());
+  }
+
+  @Test
+  void testLocationSiblingResolution() {
+    ResolvedPathKey k1 = ResolvedPathKey.ofTableLike(TableIdentifier.of("ns1", "ns2", "t3"));
+    ResolvedPathKey k2 = ResolvedPathKey.ofTableLike(TableIdentifier.of("missing"));
+    ResolvedPathKey k3 = ResolvedPathKey.ofNamespace(Namespace.of("ns1", "ns2"));
+    ResolvedPathKey k4 = ResolvedPathKey.ofNamespace(Namespace.of("ns1", "ns2", "partial"));
+
+    List<List<ResolvedPolarisEntity>> resolved = new ArrayList<>();
+    resolved.add(List.of(rns1, rns2, rt3));
+    resolved.add(List.of());
+    resolved.add(List.of(rns1, rns2));
+    resolved.add(List.of(rns1, rns2));
+
+    when(resolver.getResolvedPaths()).thenReturn(resolved);
+    List<PolarisEntity> entities = catalog.resolveOptionalPaths(List.of(k1, k2, k3, k4), "test");
+    assertThat(entities).hasSize(2);
+    assertThat(entities.get(0)).isSameAs(table3);
+    assertThat(entities.get(1)).isSameAs(ns2);
+  }
+}

Review Comment:
   Worth adding a case that drives `validateNoLocationOverlap` end-to-end with a generic-table sibling โ€” would have surfaced the `getPropertiesAsMap` vs `getInternalPropertiesAsMap` issue.



##########
runtime/service/src/main/java/org/apache/polaris/service/catalog/iceberg/IcebergCatalog.java:
##########
@@ -1242,70 +1241,87 @@ private <T extends PolarisEntity & LocationBasedEntity> void validateNoLocationO
                   return Namespace.of(newLevels);
                 })
             .toList();
-    LOGGER.debug(
-        "Resolving {} sibling entities to validate location",
-        siblingTables.size() + siblingNamespaces.size());
-    PolarisResolutionManifest resolutionManifest =
-        new PolarisResolutionManifest(
-            diagnostics,
-            callContext.getRealmContext(),
-            resolverFactory,
-            principal,
-            parentPath.getFirst().getName());
+    List<ResolvedPathKey> pathsToResolve =
+        new ArrayList<>(siblingTables.size() + siblingNamespaces.size());
     siblingTables.forEach(
-        tbl ->
-            resolutionManifest.addPath(
-                new ResolverPath(
-                    PolarisCatalogHelpers.tableIdentifierToList(tbl),
-                    PolarisEntityType.TABLE_LIKE)));
+        tbl -> {
+          if (!tbl.name().equals(name)) {
+            pathsToResolve.add(ResolvedPathKey.ofTableLike(tbl));
+          }
+        });
     siblingNamespaces.forEach(
-        ns ->
-            resolutionManifest.addPath(
-                new ResolverPath(Arrays.asList(ns.levels()), PolarisEntityType.NAMESPACE)));
+        ns -> {
+          if (!ns.level(ns.length() - 1).equals(name)) {
+            pathsToResolve.add(ResolvedPathKey.ofNamespace(ns));
+          }
+        });
+
+    StorageLocation targetLocation = StorageLocation.of(location);
+    for (PolarisEntity entityToCheck :
+        resolveOptionalPaths(pathsToResolve, parentPath.getFirst().getName())) {
+      String loc =
+          entityToCheck.getPropertiesAsMap().get(PolarisEntityConstants.ENTITY_BASE_LOCATION);
+      if (loc == null) {
+        continue;
+      }
+
+      StorageLocation siblingLocation = StorageLocation.of(loc);
+
+      if (targetLocation.isChildOf(siblingLocation) || siblingLocation.isChildOf(targetLocation)) {
+        throw new ForbiddenException(
+            "Unable to create table at location '%s' because it conflicts with existing table or namespace at "
+                + "location '%s'",
+            targetLocation, siblingLocation);
+      }
+    }
+  }
+
+  @VisibleForTesting
+  List<PolarisEntity> resolveOptionalPaths(List<ResolvedPathKey> keys, String catalogName) {
+    LOGGER.debug("Resolving {} sibling entities to validate location", keys.size());
+
+    PolarisResolutionManifest resolutionManifest =
+        new PolarisResolutionManifest(
+            diagnostics, callContext.getRealmContext(), resolverFactory, principal, catalogName);
+
+    keys.forEach(
+        k -> {
+          resolutionManifest.addPath(new ResolverPath(k, true)); // optional path
+        });
+
     ResolverStatus status = resolutionManifest.resolveAll();
-    if (!status.getStatus().equals(ResolverStatus.StatusEnum.SUCCESS)) {
+
+    if (status.getStatus() != ResolverStatus.StatusEnum.SUCCESS) {
       String message =
           "Unable to resolve sibling entities to validate location - " + status.getStatus();
       if (status.getStatus().equals(ResolverStatus.StatusEnum.ENTITY_COULD_NOT_BE_RESOLVED)) {
         message += ". Could not resolve entity: " + status.getFailedToResolvedEntityName();
       }
-      throw new IllegalStateException(message);
+
+      if (status.getStatus().equals(ResolverStatus.StatusEnum.PATH_COULD_NOT_BE_FULLY_RESOLVED)) {
+        ResolverPath path = status.getFailedToResolvePath();
+        if (path != null) {
+          message += ". path: " + String.join(".", path.entityNames());

Review Comment:
   Two sequential `if (status.getStatus().equals(...))` on the same enum โ€” switch or `else if` reads cleaner.



##########
runtime/service/src/test/java/org/apache/polaris/service/catalog/iceberg/IcebergCatalogTest.java:
##########
@@ -0,0 +1,142 @@
+/*
+ * 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.polaris.service.catalog.iceberg;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.when;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.polaris.core.PolarisDiagnostics;
+import org.apache.polaris.core.auth.PolarisPrincipal;
+import org.apache.polaris.core.context.CallContext;
+import org.apache.polaris.core.entity.CatalogEntity;
+import org.apache.polaris.core.entity.PolarisEntity;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.core.exceptions.CommitConflictException;
+import org.apache.polaris.core.persistence.ResolvedPolarisEntity;
+import org.apache.polaris.core.persistence.resolver.PolarisResolutionManifestCatalogView;
+import org.apache.polaris.core.persistence.resolver.ResolvedPathKey;
+import org.apache.polaris.core.persistence.resolver.Resolver;
+import org.apache.polaris.core.persistence.resolver.ResolverFactory;
+import org.apache.polaris.core.persistence.resolver.ResolverPath;
+import org.apache.polaris.core.persistence.resolver.ResolverStatus;
+import org.assertj.core.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+@ExtendWith(MockitoExtension.class)
+class IcebergCatalogTest {

Review Comment:
   Can we rename it to something specific like `IcebergCatalogSiblingResolutionTest`. The current name IcebergCatalogTest reads like the canonical test for the whole class and collides with the `AbstractIcebergCatalogTest` hierarchy.



##########
runtime/service/src/main/java/org/apache/polaris/service/catalog/iceberg/IcebergCatalog.java:
##########
@@ -1242,70 +1241,87 @@ private <T extends PolarisEntity & LocationBasedEntity> void validateNoLocationO
                   return Namespace.of(newLevels);
                 })
             .toList();
-    LOGGER.debug(
-        "Resolving {} sibling entities to validate location",
-        siblingTables.size() + siblingNamespaces.size());
-    PolarisResolutionManifest resolutionManifest =
-        new PolarisResolutionManifest(
-            diagnostics,
-            callContext.getRealmContext(),
-            resolverFactory,
-            principal,
-            parentPath.getFirst().getName());
+    List<ResolvedPathKey> pathsToResolve =
+        new ArrayList<>(siblingTables.size() + siblingNamespaces.size());
     siblingTables.forEach(
-        tbl ->
-            resolutionManifest.addPath(
-                new ResolverPath(
-                    PolarisCatalogHelpers.tableIdentifierToList(tbl),
-                    PolarisEntityType.TABLE_LIKE)));
+        tbl -> {
+          if (!tbl.name().equals(name)) {
+            pathsToResolve.add(ResolvedPathKey.ofTableLike(tbl));
+          }
+        });
     siblingNamespaces.forEach(
-        ns ->
-            resolutionManifest.addPath(
-                new ResolverPath(Arrays.asList(ns.levels()), PolarisEntityType.NAMESPACE)));
+        ns -> {
+          if (!ns.level(ns.length() - 1).equals(name)) {
+            pathsToResolve.add(ResolvedPathKey.ofNamespace(ns));
+          }
+        });
+
+    StorageLocation targetLocation = StorageLocation.of(location);
+    for (PolarisEntity entityToCheck :
+        resolveOptionalPaths(pathsToResolve, parentPath.getFirst().getName())) {
+      String loc =
+          entityToCheck.getPropertiesAsMap().get(PolarisEntityConstants.ENTITY_BASE_LOCATION);

Review Comment:
   Regression for generic tables: `GenericTableEntity.getBaseLocation()` reads from `getInternalPropertiesAsMap()`, not `getPropertiesAsMap()`. So this lookup returns null for any generic-table sibling, the `continue` below skips it, and overlap is silently no longer enforced for generic tables. The pre-PR code had a `subType == GENERIC_TABLE` branch that handled this.



-- 
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: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make location overlap checks more robust [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #4408:
URL: https://github.com/apache/polaris/pull/4408#discussion_r3230511840


##########
runtime/service/src/main/java/org/apache/polaris/service/catalog/iceberg/IcebergCatalog.java:
##########
@@ -2725,4 +2736,37 @@ private Page<TableIdentifier> listTableLike(
   private int getMaxMetadataRefreshRetries() {
     return realmConfig.getConfig(FeatureConfiguration.MAX_METADATA_REFRESH_RETRIES);
   }
+
+  /**
+   * Sub-classing {@link CommitFailedException} to cause Conflict (409) errors at the REST API level
+   * if this exceptio escaped, instead of Server Error (500).
+   */
+  @VisibleForTesting
+  static class UnableToCheckSiblingLocationsException extends CommitFailedException {
+
+    private final ResolverStatus status;
+
+    UnableToCheckSiblingLocationsException(ResolverStatus status) {
+      super("");
+      this.status = status;
+    }
+
+    @Override
+    public String getMessage() {

Review Comment:
   no longer used



-- 
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: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make location overlap checks more robust [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on PR #4408:
URL: https://github.com/apache/polaris/pull/4408#issuecomment-4431061788

   > [...] the cleaner approach would be to avoid the second resolution step and load sibling entities directly once we already have the sibling ids / names
   
   I was thinking about that too, but it would be a major change to the current `Resolver` logic and might alter expectations other Persistence implementations have on the end-to-end check/save process.
   
   However, since @snazy prefers this approach too, I'm going to make this change.
   
   CC: @dennishuo 


-- 
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: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make location overlap checks more robust [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b merged PR #4408:
URL: https://github.com/apache/polaris/pull/4408


-- 
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: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make location overlap checks more robust [polaris]

Posted by "gh-yzou (via GitHub)" <gi...@apache.org>.
gh-yzou commented on code in PR #4408:
URL: https://github.com/apache/polaris/pull/4408#discussion_r3307287931


##########
runtime/service/src/main/java/org/apache/polaris/service/catalog/iceberg/IcebergCatalog.java:
##########
@@ -1242,70 +1241,87 @@ private <T extends PolarisEntity & LocationBasedEntity> void validateNoLocationO
                   return Namespace.of(newLevels);
                 })
             .toList();
-    LOGGER.debug(
-        "Resolving {} sibling entities to validate location",
-        siblingTables.size() + siblingNamespaces.size());
-    PolarisResolutionManifest resolutionManifest =
-        new PolarisResolutionManifest(
-            diagnostics,
-            callContext.getRealmContext(),
-            resolverFactory,
-            principal,
-            parentPath.getFirst().getName());
+    List<ResolvedPathKey> pathsToResolve =
+        new ArrayList<>(siblingTables.size() + siblingNamespaces.size());
     siblingTables.forEach(
-        tbl ->
-            resolutionManifest.addPath(
-                new ResolverPath(
-                    PolarisCatalogHelpers.tableIdentifierToList(tbl),
-                    PolarisEntityType.TABLE_LIKE)));
+        tbl -> {
+          if (!tbl.name().equals(name)) {
+            pathsToResolve.add(ResolvedPathKey.ofTableLike(tbl));
+          }
+        });
     siblingNamespaces.forEach(
-        ns ->
-            resolutionManifest.addPath(
-                new ResolverPath(Arrays.asList(ns.levels()), PolarisEntityType.NAMESPACE)));
+        ns -> {
+          if (!ns.level(ns.length() - 1).equals(name)) {
+            pathsToResolve.add(ResolvedPathKey.ofNamespace(ns));
+          }
+        });
+
+    StorageLocation targetLocation = StorageLocation.of(location);
+    for (PolarisEntity entityToCheck :
+        resolveOptionalPaths(pathsToResolve, parentPath.getFirst().getName())) {
+      String loc =
+          entityToCheck.getPropertiesAsMap().get(PolarisEntityConstants.ENTITY_BASE_LOCATION);

Review Comment:
   I think that was purely based on whether the property is set through the API spec or not. i always had the confusion that the internal property is not for user facing properties, but Eric was pointing out that it is the opposite way.



-- 
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: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make location overlap checks more robust [polaris]

Posted by "gh-yzou (via GitHub)" <gi...@apache.org>.
gh-yzou commented on code in PR #4408:
URL: https://github.com/apache/polaris/pull/4408#discussion_r3307287931


##########
runtime/service/src/main/java/org/apache/polaris/service/catalog/iceberg/IcebergCatalog.java:
##########
@@ -1242,70 +1241,87 @@ private <T extends PolarisEntity & LocationBasedEntity> void validateNoLocationO
                   return Namespace.of(newLevels);
                 })
             .toList();
-    LOGGER.debug(
-        "Resolving {} sibling entities to validate location",
-        siblingTables.size() + siblingNamespaces.size());
-    PolarisResolutionManifest resolutionManifest =
-        new PolarisResolutionManifest(
-            diagnostics,
-            callContext.getRealmContext(),
-            resolverFactory,
-            principal,
-            parentPath.getFirst().getName());
+    List<ResolvedPathKey> pathsToResolve =
+        new ArrayList<>(siblingTables.size() + siblingNamespaces.size());
     siblingTables.forEach(
-        tbl ->
-            resolutionManifest.addPath(
-                new ResolverPath(
-                    PolarisCatalogHelpers.tableIdentifierToList(tbl),
-                    PolarisEntityType.TABLE_LIKE)));
+        tbl -> {
+          if (!tbl.name().equals(name)) {
+            pathsToResolve.add(ResolvedPathKey.ofTableLike(tbl));
+          }
+        });
     siblingNamespaces.forEach(
-        ns ->
-            resolutionManifest.addPath(
-                new ResolverPath(Arrays.asList(ns.levels()), PolarisEntityType.NAMESPACE)));
+        ns -> {
+          if (!ns.level(ns.length() - 1).equals(name)) {
+            pathsToResolve.add(ResolvedPathKey.ofNamespace(ns));
+          }
+        });
+
+    StorageLocation targetLocation = StorageLocation.of(location);
+    for (PolarisEntity entityToCheck :
+        resolveOptionalPaths(pathsToResolve, parentPath.getFirst().getName())) {
+      String loc =
+          entityToCheck.getPropertiesAsMap().get(PolarisEntityConstants.ENTITY_BASE_LOCATION);

Review Comment:
   I think that was purely based on whether the property is set through the API spec or not. i always had the confusion that the internal property is not for user facing properties, but Eric was pointing out that it is the opposite way. The location property is configured through spec API, and user facing, and therefore it is under internal property.



##########
runtime/service/src/main/java/org/apache/polaris/service/catalog/iceberg/IcebergCatalog.java:
##########
@@ -1242,70 +1241,87 @@ private <T extends PolarisEntity & LocationBasedEntity> void validateNoLocationO
                   return Namespace.of(newLevels);
                 })
             .toList();
-    LOGGER.debug(
-        "Resolving {} sibling entities to validate location",
-        siblingTables.size() + siblingNamespaces.size());
-    PolarisResolutionManifest resolutionManifest =
-        new PolarisResolutionManifest(
-            diagnostics,
-            callContext.getRealmContext(),
-            resolverFactory,
-            principal,
-            parentPath.getFirst().getName());
+    List<ResolvedPathKey> pathsToResolve =
+        new ArrayList<>(siblingTables.size() + siblingNamespaces.size());
     siblingTables.forEach(
-        tbl ->
-            resolutionManifest.addPath(
-                new ResolverPath(
-                    PolarisCatalogHelpers.tableIdentifierToList(tbl),
-                    PolarisEntityType.TABLE_LIKE)));
+        tbl -> {
+          if (!tbl.name().equals(name)) {
+            pathsToResolve.add(ResolvedPathKey.ofTableLike(tbl));
+          }
+        });
     siblingNamespaces.forEach(
-        ns ->
-            resolutionManifest.addPath(
-                new ResolverPath(Arrays.asList(ns.levels()), PolarisEntityType.NAMESPACE)));
+        ns -> {
+          if (!ns.level(ns.length() - 1).equals(name)) {
+            pathsToResolve.add(ResolvedPathKey.ofNamespace(ns));
+          }
+        });
+
+    StorageLocation targetLocation = StorageLocation.of(location);
+    for (PolarisEntity entityToCheck :
+        resolveOptionalPaths(pathsToResolve, parentPath.getFirst().getName())) {
+      String loc =
+          entityToCheck.getPropertiesAsMap().get(PolarisEntityConstants.ENTITY_BASE_LOCATION);

Review Comment:
   I think that was purely based on whether the property is set through the API spec or not. i always had the confusion that the internal property is not for user facing properties, but Eric was pointing out that it is the opposite way. The location property is configured through spec API, and user facing, and therefore it is under internal property based on guideline



-- 
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: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make location overlap checks more robust [polaris]

Posted by "XN137 (via GitHub)" <gi...@apache.org>.
XN137 commented on code in PR #4408:
URL: https://github.com/apache/polaris/pull/4408#discussion_r3223977511


##########
runtime/service/src/test/java/org/apache/polaris/service/catalog/iceberg/UnableToCheckSiblingLocationsExceptionTest.java:
##########
@@ -0,0 +1,55 @@
+/*
+ * 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.polaris.service.catalog.iceberg;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.List;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.core.persistence.resolver.ResolverPath;
+import org.apache.polaris.core.persistence.resolver.ResolverStatus;
+import org.apache.polaris.service.catalog.iceberg.IcebergCatalog.UnableToCheckSiblingLocationsException;
+import org.junit.jupiter.api.Test;
+
+class UnableToCheckSiblingLocationsExceptionTest {
+
+  @Test
+  void testEntityNotResolvedMessage() {
+    ResolverStatus s = new ResolverStatus(PolarisEntityType.TABLE_LIKE, "test-name");
+    UnableToCheckSiblingLocationsException e = new UnableToCheckSiblingLocationsException(s);
+    assertThat(e).hasMessageContaining("Unable to resolve sibling entities to validate location");
+    assertThat(e).hasMessageContaining("test-name");
+    assertThat(e)
+        .hasMessageContaining(ResolverStatus.StatusEnum.ENTITY_COULD_NOT_BE_RESOLVED.name());
+  }
+
+  @Test
+  void testPathNotResolvedMessage() {
+    ResolverPath path =
+        new ResolverPath(List.of("ns1", "ns2", "table3"), PolarisEntityType.TABLE_LIKE, true);
+    ResolverStatus s = new ResolverStatus(path, 123);
+    UnableToCheckSiblingLocationsException e = new UnableToCheckSiblingLocationsException(s);
+    assertThat(e).hasMessageContaining("Unable to resolve sibling entities to validate location");
+    assertThat(e).hasMessageContaining("ns1.ns2.table3");
+    assertThat(e).hasMessageContaining("failed index: 123");
+    assertThat(e)
+        .hasMessageContaining(ResolverStatus.StatusEnum.PATH_COULD_NOT_BE_FULLY_RESOLVED.name());

Review Comment:
   nit: chain the `.hasMessageContaining` calls



##########
runtime/service/src/main/java/org/apache/polaris/service/catalog/iceberg/IcebergCatalog.java:
##########
@@ -2725,4 +2736,37 @@ private Page<TableIdentifier> listTableLike(
   private int getMaxMetadataRefreshRetries() {
     return realmConfig.getConfig(FeatureConfiguration.MAX_METADATA_REFRESH_RETRIES);
   }
+
+  /**
+   * Sub-classing {@link CommitFailedException} to cause Conflict (409) errors at the REST API level
+   * if this exceptio escaped, instead of Server Error (500).

Review Comment:
   typo: exceptio



##########
polaris-core/src/main/java/org/apache/polaris/core/config/FeatureConfiguration.java:
##########
@@ -466,6 +466,16 @@ public static void enforceFeatureEnabledOrThrow(
           .defaultValue(false)
           .buildFeatureConfiguration();
 
+  public static final FeatureConfiguration<Long> SIBLING_TIMEOUT_MILLIS =
+      PolarisConfiguration.<Long>builder()
+          .key("SIBLING_TIMEOUT_MILLIS")

Review Comment:
   `SIBLING_CHECK_TIMEOUT_MILLIS` for consistency with `OPTIMIZED_SIBLING_CHECK` above?



##########
runtime/service/src/main/java/org/apache/polaris/service/catalog/iceberg/IcebergCatalog.java:
##########
@@ -2725,4 +2736,37 @@ private Page<TableIdentifier> listTableLike(
   private int getMaxMetadataRefreshRetries() {
     return realmConfig.getConfig(FeatureConfiguration.MAX_METADATA_REFRESH_RETRIES);
   }
+
+  /**
+   * Sub-classing {@link CommitFailedException} to cause Conflict (409) errors at the REST API level
+   * if this exceptio escaped, instead of Server Error (500).
+   */
+  @VisibleForTesting
+  static class UnableToCheckSiblingLocationsException extends CommitFailedException {

Review Comment:
   CommitFailedException javadoc reads:
   
   > Exception raised when a commit fails because of out of date metadata
   
   do i assume correctly that we want to trigger retry behavior on the client side for this case and this makes it the best choice still?



##########
runtime/service/src/main/java/org/apache/polaris/service/catalog/iceberg/IcebergCatalog.java:
##########
@@ -2725,4 +2736,37 @@ private Page<TableIdentifier> listTableLike(
   private int getMaxMetadataRefreshRetries() {
     return realmConfig.getConfig(FeatureConfiguration.MAX_METADATA_REFRESH_RETRIES);
   }
+
+  /**
+   * Sub-classing {@link CommitFailedException} to cause Conflict (409) errors at the REST API level
+   * if this exceptio escaped, instead of Server Error (500).
+   */
+  @VisibleForTesting
+  static class UnableToCheckSiblingLocationsException extends CommitFailedException {
+
+    private final ResolverStatus status;
+
+    UnableToCheckSiblingLocationsException(ResolverStatus status) {
+      super("");
+      this.status = status;
+    }
+
+    @Override
+    public String getMessage() {

Review Comment:
   is it good idea to recompute this every time instead of building it once?
   it seems to get called at least once by `Tasks.runTaskWithRetry` for logging.



-- 
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: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make location overlap checks more robust [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #4408:
URL: https://github.com/apache/polaris/pull/4408#discussion_r3244491109


##########
runtime/service/src/main/java/org/apache/polaris/service/catalog/iceberg/IcebergCatalog.java:
##########
@@ -1242,70 +1241,87 @@ private <T extends PolarisEntity & LocationBasedEntity> void validateNoLocationO
                   return Namespace.of(newLevels);
                 })
             .toList();
-    LOGGER.debug(
-        "Resolving {} sibling entities to validate location",
-        siblingTables.size() + siblingNamespaces.size());
-    PolarisResolutionManifest resolutionManifest =
-        new PolarisResolutionManifest(
-            diagnostics,
-            callContext.getRealmContext(),
-            resolverFactory,
-            principal,
-            parentPath.getFirst().getName());
+    List<ResolvedPathKey> pathsToResolve =
+        new ArrayList<>(siblingTables.size() + siblingNamespaces.size());
     siblingTables.forEach(
-        tbl ->
-            resolutionManifest.addPath(
-                new ResolverPath(
-                    PolarisCatalogHelpers.tableIdentifierToList(tbl),
-                    PolarisEntityType.TABLE_LIKE)));
+        tbl -> {
+          if (!tbl.name().equals(name)) {
+            pathsToResolve.add(ResolvedPathKey.ofTableLike(tbl));
+          }
+        });
     siblingNamespaces.forEach(
-        ns ->
-            resolutionManifest.addPath(
-                new ResolverPath(Arrays.asList(ns.levels()), PolarisEntityType.NAMESPACE)));
+        ns -> {
+          if (!ns.level(ns.length() - 1).equals(name)) {
+            pathsToResolve.add(ResolvedPathKey.ofNamespace(ns));
+          }
+        });
+
+    StorageLocation targetLocation = StorageLocation.of(location);
+    for (PolarisEntity entityToCheck :
+        resolveOptionalPaths(pathsToResolve, parentPath.getFirst().getName())) {
+      String loc =
+          entityToCheck.getPropertiesAsMap().get(PolarisEntityConstants.ENTITY_BASE_LOCATION);
+      if (loc == null) {
+        continue;
+      }
+
+      StorageLocation siblingLocation = StorageLocation.of(loc);
+
+      if (targetLocation.isChildOf(siblingLocation) || siblingLocation.isChildOf(targetLocation)) {
+        throw new ForbiddenException(
+            "Unable to create table at location '%s' because it conflicts with existing table or namespace at "
+                + "location '%s'",
+            targetLocation, siblingLocation);
+      }
+    }
+  }
+
+  @VisibleForTesting
+  List<PolarisEntity> resolveOptionalPaths(List<ResolvedPathKey> keys, String catalogName) {
+    LOGGER.debug("Resolving {} sibling entities to validate location", keys.size());
+
+    PolarisResolutionManifest resolutionManifest =
+        new PolarisResolutionManifest(
+            diagnostics, callContext.getRealmContext(), resolverFactory, principal, catalogName);
+
+    keys.forEach(
+        k -> {
+          resolutionManifest.addPath(new ResolverPath(k, true)); // optional path
+        });
+
     ResolverStatus status = resolutionManifest.resolveAll();
-    if (!status.getStatus().equals(ResolverStatus.StatusEnum.SUCCESS)) {
+
+    if (status.getStatus() != ResolverStatus.StatusEnum.SUCCESS) {
       String message =
           "Unable to resolve sibling entities to validate location - " + status.getStatus();
       if (status.getStatus().equals(ResolverStatus.StatusEnum.ENTITY_COULD_NOT_BE_RESOLVED)) {
         message += ". Could not resolve entity: " + status.getFailedToResolvedEntityName();
       }
-      throw new IllegalStateException(message);
+
+      if (status.getStatus().equals(ResolverStatus.StatusEnum.PATH_COULD_NOT_BE_FULLY_RESOLVED)) {
+        ResolverPath path = status.getFailedToResolvePath();
+        if (path != null) {
+          message += ". path: " + String.join(".", path.entityNames());

Review Comment:
   fixed



##########
runtime/service/src/main/java/org/apache/polaris/service/catalog/iceberg/IcebergCatalog.java:
##########
@@ -1242,70 +1241,87 @@ private <T extends PolarisEntity & LocationBasedEntity> void validateNoLocationO
                   return Namespace.of(newLevels);
                 })
             .toList();
-    LOGGER.debug(
-        "Resolving {} sibling entities to validate location",
-        siblingTables.size() + siblingNamespaces.size());
-    PolarisResolutionManifest resolutionManifest =
-        new PolarisResolutionManifest(
-            diagnostics,
-            callContext.getRealmContext(),
-            resolverFactory,
-            principal,
-            parentPath.getFirst().getName());
+    List<ResolvedPathKey> pathsToResolve =
+        new ArrayList<>(siblingTables.size() + siblingNamespaces.size());
     siblingTables.forEach(
-        tbl ->
-            resolutionManifest.addPath(
-                new ResolverPath(
-                    PolarisCatalogHelpers.tableIdentifierToList(tbl),
-                    PolarisEntityType.TABLE_LIKE)));
+        tbl -> {
+          if (!tbl.name().equals(name)) {
+            pathsToResolve.add(ResolvedPathKey.ofTableLike(tbl));
+          }
+        });
     siblingNamespaces.forEach(
-        ns ->
-            resolutionManifest.addPath(
-                new ResolverPath(Arrays.asList(ns.levels()), PolarisEntityType.NAMESPACE)));
+        ns -> {
+          if (!ns.level(ns.length() - 1).equals(name)) {
+            pathsToResolve.add(ResolvedPathKey.ofNamespace(ns));
+          }
+        });
+
+    StorageLocation targetLocation = StorageLocation.of(location);
+    for (PolarisEntity entityToCheck :
+        resolveOptionalPaths(pathsToResolve, parentPath.getFirst().getName())) {
+      String loc =
+          entityToCheck.getPropertiesAsMap().get(PolarisEntityConstants.ENTITY_BASE_LOCATION);
+      if (loc == null) {
+        continue;
+      }
+
+      StorageLocation siblingLocation = StorageLocation.of(loc);
+
+      if (targetLocation.isChildOf(siblingLocation) || siblingLocation.isChildOf(targetLocation)) {
+        throw new ForbiddenException(
+            "Unable to create table at location '%s' because it conflicts with existing table or namespace at "

Review Comment:
   fixed



##########
runtime/service/src/main/java/org/apache/polaris/service/catalog/iceberg/IcebergCatalog.java:
##########
@@ -1242,70 +1241,87 @@ private <T extends PolarisEntity & LocationBasedEntity> void validateNoLocationO
                   return Namespace.of(newLevels);
                 })
             .toList();
-    LOGGER.debug(
-        "Resolving {} sibling entities to validate location",
-        siblingTables.size() + siblingNamespaces.size());
-    PolarisResolutionManifest resolutionManifest =
-        new PolarisResolutionManifest(
-            diagnostics,
-            callContext.getRealmContext(),
-            resolverFactory,
-            principal,
-            parentPath.getFirst().getName());
+    List<ResolvedPathKey> pathsToResolve =
+        new ArrayList<>(siblingTables.size() + siblingNamespaces.size());
     siblingTables.forEach(
-        tbl ->
-            resolutionManifest.addPath(
-                new ResolverPath(
-                    PolarisCatalogHelpers.tableIdentifierToList(tbl),
-                    PolarisEntityType.TABLE_LIKE)));
+        tbl -> {
+          if (!tbl.name().equals(name)) {
+            pathsToResolve.add(ResolvedPathKey.ofTableLike(tbl));
+          }
+        });
     siblingNamespaces.forEach(
-        ns ->
-            resolutionManifest.addPath(
-                new ResolverPath(Arrays.asList(ns.levels()), PolarisEntityType.NAMESPACE)));
+        ns -> {
+          if (!ns.level(ns.length() - 1).equals(name)) {
+            pathsToResolve.add(ResolvedPathKey.ofNamespace(ns));
+          }
+        });
+
+    StorageLocation targetLocation = StorageLocation.of(location);
+    for (PolarisEntity entityToCheck :
+        resolveOptionalPaths(pathsToResolve, parentPath.getFirst().getName())) {
+      String loc =
+          entityToCheck.getPropertiesAsMap().get(PolarisEntityConstants.ENTITY_BASE_LOCATION);

Review Comment:
   fixed



-- 
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: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make location overlap checks more robust [polaris]

Posted by "flyrain (via GitHub)" <gi...@apache.org>.
flyrain commented on code in PR #4408:
URL: https://github.com/apache/polaris/pull/4408#discussion_r3243571305


##########
runtime/service/src/main/java/org/apache/polaris/service/catalog/iceberg/IcebergCatalog.java:
##########
@@ -1242,70 +1241,87 @@ private <T extends PolarisEntity & LocationBasedEntity> void validateNoLocationO
                   return Namespace.of(newLevels);
                 })
             .toList();
-    LOGGER.debug(
-        "Resolving {} sibling entities to validate location",
-        siblingTables.size() + siblingNamespaces.size());
-    PolarisResolutionManifest resolutionManifest =
-        new PolarisResolutionManifest(
-            diagnostics,
-            callContext.getRealmContext(),
-            resolverFactory,
-            principal,
-            parentPath.getFirst().getName());
+    List<ResolvedPathKey> pathsToResolve =
+        new ArrayList<>(siblingTables.size() + siblingNamespaces.size());
     siblingTables.forEach(
-        tbl ->
-            resolutionManifest.addPath(
-                new ResolverPath(
-                    PolarisCatalogHelpers.tableIdentifierToList(tbl),
-                    PolarisEntityType.TABLE_LIKE)));
+        tbl -> {
+          if (!tbl.name().equals(name)) {
+            pathsToResolve.add(ResolvedPathKey.ofTableLike(tbl));
+          }
+        });
     siblingNamespaces.forEach(
-        ns ->
-            resolutionManifest.addPath(
-                new ResolverPath(Arrays.asList(ns.levels()), PolarisEntityType.NAMESPACE)));
+        ns -> {
+          if (!ns.level(ns.length() - 1).equals(name)) {
+            pathsToResolve.add(ResolvedPathKey.ofNamespace(ns));
+          }
+        });
+
+    StorageLocation targetLocation = StorageLocation.of(location);
+    for (PolarisEntity entityToCheck :
+        resolveOptionalPaths(pathsToResolve, parentPath.getFirst().getName())) {
+      String loc =
+          entityToCheck.getPropertiesAsMap().get(PolarisEntityConstants.ENTITY_BASE_LOCATION);
+      if (loc == null) {
+        continue;
+      }
+
+      StorageLocation siblingLocation = StorageLocation.of(loc);
+
+      if (targetLocation.isChildOf(siblingLocation) || siblingLocation.isChildOf(targetLocation)) {
+        throw new ForbiddenException(
+            "Unable to create table at location '%s' because it conflicts with existing table or namespace at "

Review Comment:
   That'd be awesome! It's not a blocker for me.



-- 
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: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make location overlap checks more robust [polaris]

Posted by "flyrain (via GitHub)" <gi...@apache.org>.
flyrain commented on code in PR #4408:
URL: https://github.com/apache/polaris/pull/4408#discussion_r3238415059


##########
runtime/service/src/main/java/org/apache/polaris/service/catalog/iceberg/IcebergCatalog.java:
##########
@@ -1242,70 +1241,87 @@ private <T extends PolarisEntity & LocationBasedEntity> void validateNoLocationO
                   return Namespace.of(newLevels);
                 })
             .toList();
-    LOGGER.debug(
-        "Resolving {} sibling entities to validate location",
-        siblingTables.size() + siblingNamespaces.size());
-    PolarisResolutionManifest resolutionManifest =
-        new PolarisResolutionManifest(
-            diagnostics,
-            callContext.getRealmContext(),
-            resolverFactory,
-            principal,
-            parentPath.getFirst().getName());
+    List<ResolvedPathKey> pathsToResolve =
+        new ArrayList<>(siblingTables.size() + siblingNamespaces.size());
     siblingTables.forEach(
-        tbl ->
-            resolutionManifest.addPath(
-                new ResolverPath(
-                    PolarisCatalogHelpers.tableIdentifierToList(tbl),
-                    PolarisEntityType.TABLE_LIKE)));
+        tbl -> {
+          if (!tbl.name().equals(name)) {
+            pathsToResolve.add(ResolvedPathKey.ofTableLike(tbl));
+          }
+        });
     siblingNamespaces.forEach(
-        ns ->
-            resolutionManifest.addPath(
-                new ResolverPath(Arrays.asList(ns.levels()), PolarisEntityType.NAMESPACE)));
+        ns -> {
+          if (!ns.level(ns.length() - 1).equals(name)) {
+            pathsToResolve.add(ResolvedPathKey.ofNamespace(ns));
+          }
+        });
+
+    StorageLocation targetLocation = StorageLocation.of(location);
+    for (PolarisEntity entityToCheck :
+        resolveOptionalPaths(pathsToResolve, parentPath.getFirst().getName())) {
+      String loc =
+          entityToCheck.getPropertiesAsMap().get(PolarisEntityConstants.ENTITY_BASE_LOCATION);

Review Comment:
   Regression for generic tables: `GenericTableEntity.getBaseLocation()` reads from `getInternalPropertiesAsMap()`, not `getPropertiesAsMap()`. So this lookup returns null for any generic-table sibling, the `continue` below skips it, and overlap is silently no longer enforced for generic tables. The pre-PR code had a `subType == GENERIC_TABLE` branch that handled this. I'd suggest to add a unit test to avoid future regression.



-- 
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: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make location overlap checks more robust [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #4408:
URL: https://github.com/apache/polaris/pull/4408#discussion_r3243288536


##########
runtime/service/src/main/java/org/apache/polaris/service/catalog/iceberg/IcebergCatalog.java:
##########
@@ -1242,70 +1241,87 @@ private <T extends PolarisEntity & LocationBasedEntity> void validateNoLocationO
                   return Namespace.of(newLevels);
                 })
             .toList();
-    LOGGER.debug(
-        "Resolving {} sibling entities to validate location",
-        siblingTables.size() + siblingNamespaces.size());
-    PolarisResolutionManifest resolutionManifest =
-        new PolarisResolutionManifest(
-            diagnostics,
-            callContext.getRealmContext(),
-            resolverFactory,
-            principal,
-            parentPath.getFirst().getName());
+    List<ResolvedPathKey> pathsToResolve =
+        new ArrayList<>(siblingTables.size() + siblingNamespaces.size());
     siblingTables.forEach(
-        tbl ->
-            resolutionManifest.addPath(
-                new ResolverPath(
-                    PolarisCatalogHelpers.tableIdentifierToList(tbl),
-                    PolarisEntityType.TABLE_LIKE)));
+        tbl -> {
+          if (!tbl.name().equals(name)) {
+            pathsToResolve.add(ResolvedPathKey.ofTableLike(tbl));
+          }
+        });
     siblingNamespaces.forEach(
-        ns ->
-            resolutionManifest.addPath(
-                new ResolverPath(Arrays.asList(ns.levels()), PolarisEntityType.NAMESPACE)));
+        ns -> {
+          if (!ns.level(ns.length() - 1).equals(name)) {
+            pathsToResolve.add(ResolvedPathKey.ofNamespace(ns));
+          }
+        });
+
+    StorageLocation targetLocation = StorageLocation.of(location);
+    for (PolarisEntity entityToCheck :
+        resolveOptionalPaths(pathsToResolve, parentPath.getFirst().getName())) {
+      String loc =
+          entityToCheck.getPropertiesAsMap().get(PolarisEntityConstants.ENTITY_BASE_LOCATION);

Review Comment:
   Good catch, @flyrain !
   
   What's the rationale for this difference in Generic Tables?



-- 
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: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make location overlap checks more robust [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on PR #4408:
URL: https://github.com/apache/polaris/pull/4408#issuecomment-4453371191

   @flyrain : an IT for those 500 errors is possible, but I do not think it's worth the effort and extra resource use during CI. Because of the racy nature of the issue, such a test cannot produce true positive results.


-- 
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: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make location overlap checks more robust [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #4408:
URL: https://github.com/apache/polaris/pull/4408#discussion_r3230507131


##########
polaris-core/src/main/java/org/apache/polaris/core/config/FeatureConfiguration.java:
##########
@@ -466,6 +466,16 @@ public static void enforceFeatureEnabledOrThrow(
           .defaultValue(false)
           .buildFeatureConfiguration();
 
+  public static final FeatureConfiguration<Long> SIBLING_TIMEOUT_MILLIS =
+      PolarisConfiguration.<Long>builder()
+          .key("SIBLING_TIMEOUT_MILLIS")

Review Comment:
   no longer used



##########
runtime/service/src/main/java/org/apache/polaris/service/catalog/iceberg/IcebergCatalog.java:
##########
@@ -2725,4 +2736,37 @@ private Page<TableIdentifier> listTableLike(
   private int getMaxMetadataRefreshRetries() {
     return realmConfig.getConfig(FeatureConfiguration.MAX_METADATA_REFRESH_RETRIES);
   }
+
+  /**
+   * Sub-classing {@link CommitFailedException} to cause Conflict (409) errors at the REST API level
+   * if this exceptio escaped, instead of Server Error (500).

Review Comment:
   no longer used



-- 
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: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make location overlap checks more robust [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #4408:
URL: https://github.com/apache/polaris/pull/4408#discussion_r3230510749


##########
runtime/service/src/test/java/org/apache/polaris/service/catalog/iceberg/UnableToCheckSiblingLocationsExceptionTest.java:
##########
@@ -0,0 +1,55 @@
+/*
+ * 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.polaris.service.catalog.iceberg;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.List;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.core.persistence.resolver.ResolverPath;
+import org.apache.polaris.core.persistence.resolver.ResolverStatus;
+import org.apache.polaris.service.catalog.iceberg.IcebergCatalog.UnableToCheckSiblingLocationsException;
+import org.junit.jupiter.api.Test;
+
+class UnableToCheckSiblingLocationsExceptionTest {
+
+  @Test
+  void testEntityNotResolvedMessage() {
+    ResolverStatus s = new ResolverStatus(PolarisEntityType.TABLE_LIKE, "test-name");
+    UnableToCheckSiblingLocationsException e = new UnableToCheckSiblingLocationsException(s);
+    assertThat(e).hasMessageContaining("Unable to resolve sibling entities to validate location");
+    assertThat(e).hasMessageContaining("test-name");
+    assertThat(e)
+        .hasMessageContaining(ResolverStatus.StatusEnum.ENTITY_COULD_NOT_BE_RESOLVED.name());
+  }
+
+  @Test
+  void testPathNotResolvedMessage() {
+    ResolverPath path =
+        new ResolverPath(List.of("ns1", "ns2", "table3"), PolarisEntityType.TABLE_LIKE, true);
+    ResolverStatus s = new ResolverStatus(path, 123);
+    UnableToCheckSiblingLocationsException e = new UnableToCheckSiblingLocationsException(s);
+    assertThat(e).hasMessageContaining("Unable to resolve sibling entities to validate location");
+    assertThat(e).hasMessageContaining("ns1.ns2.table3");
+    assertThat(e).hasMessageContaining("failed index: 123");
+    assertThat(e)
+        .hasMessageContaining(ResolverStatus.StatusEnum.PATH_COULD_NOT_BE_FULLY_RESOLVED.name());

Review Comment:
   done (in a new test class)



-- 
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: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make location overlap checks more robust [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on PR #4408:
URL: https://github.com/apache/polaris/pull/4408#issuecomment-4435745936

   I refactored for a composite approach: lookup is optional now, but still goes through `Resolver` for maximum backward compatibility.


-- 
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: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make location overlap checks more robust [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #4408:
URL: https://github.com/apache/polaris/pull/4408#discussion_r3243324724


##########
runtime/service/src/test/java/org/apache/polaris/service/catalog/iceberg/IcebergCatalogTest.java:
##########
@@ -0,0 +1,142 @@
+/*
+ * 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.polaris.service.catalog.iceberg;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.when;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.polaris.core.PolarisDiagnostics;
+import org.apache.polaris.core.auth.PolarisPrincipal;
+import org.apache.polaris.core.context.CallContext;
+import org.apache.polaris.core.entity.CatalogEntity;
+import org.apache.polaris.core.entity.PolarisEntity;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.core.exceptions.CommitConflictException;
+import org.apache.polaris.core.persistence.ResolvedPolarisEntity;
+import org.apache.polaris.core.persistence.resolver.PolarisResolutionManifestCatalogView;
+import org.apache.polaris.core.persistence.resolver.ResolvedPathKey;
+import org.apache.polaris.core.persistence.resolver.Resolver;
+import org.apache.polaris.core.persistence.resolver.ResolverFactory;
+import org.apache.polaris.core.persistence.resolver.ResolverPath;
+import org.apache.polaris.core.persistence.resolver.ResolverStatus;
+import org.assertj.core.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+@ExtendWith(MockitoExtension.class)
+class IcebergCatalogTest {

Review Comment:
   `IcebergCatalogTest` is a unit test for (some of) the code inside `IcebergCatalog`. Other test cases may be added later for other parts of the code. I think the name matches the usual JUnit naming conventions.



-- 
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: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make location overlap checks more robust [polaris]

Posted by "flyrain (via GitHub)" <gi...@apache.org>.
flyrain commented on code in PR #4408:
URL: https://github.com/apache/polaris/pull/4408#discussion_r3238415068


##########
runtime/service/src/main/java/org/apache/polaris/service/catalog/iceberg/IcebergCatalog.java:
##########
@@ -1242,70 +1241,87 @@ private <T extends PolarisEntity & LocationBasedEntity> void validateNoLocationO
                   return Namespace.of(newLevels);
                 })
             .toList();
-    LOGGER.debug(
-        "Resolving {} sibling entities to validate location",
-        siblingTables.size() + siblingNamespaces.size());
-    PolarisResolutionManifest resolutionManifest =
-        new PolarisResolutionManifest(
-            diagnostics,
-            callContext.getRealmContext(),
-            resolverFactory,
-            principal,
-            parentPath.getFirst().getName());
+    List<ResolvedPathKey> pathsToResolve =
+        new ArrayList<>(siblingTables.size() + siblingNamespaces.size());
     siblingTables.forEach(
-        tbl ->
-            resolutionManifest.addPath(
-                new ResolverPath(
-                    PolarisCatalogHelpers.tableIdentifierToList(tbl),
-                    PolarisEntityType.TABLE_LIKE)));
+        tbl -> {
+          if (!tbl.name().equals(name)) {
+            pathsToResolve.add(ResolvedPathKey.ofTableLike(tbl));
+          }
+        });
     siblingNamespaces.forEach(
-        ns ->
-            resolutionManifest.addPath(
-                new ResolverPath(Arrays.asList(ns.levels()), PolarisEntityType.NAMESPACE)));
+        ns -> {
+          if (!ns.level(ns.length() - 1).equals(name)) {
+            pathsToResolve.add(ResolvedPathKey.ofNamespace(ns));
+          }
+        });
+
+    StorageLocation targetLocation = StorageLocation.of(location);
+    for (PolarisEntity entityToCheck :
+        resolveOptionalPaths(pathsToResolve, parentPath.getFirst().getName())) {
+      String loc =
+          entityToCheck.getPropertiesAsMap().get(PolarisEntityConstants.ENTITY_BASE_LOCATION);
+      if (loc == null) {
+        continue;
+      }
+
+      StorageLocation siblingLocation = StorageLocation.of(loc);
+
+      if (targetLocation.isChildOf(siblingLocation) || siblingLocation.isChildOf(targetLocation)) {
+        throw new ForbiddenException(
+            "Unable to create table at location '%s' because it conflicts with existing table or namespace at "
+                + "location '%s'",
+            targetLocation, siblingLocation);
+      }
+    }
+  }
+
+  @VisibleForTesting
+  List<PolarisEntity> resolveOptionalPaths(List<ResolvedPathKey> keys, String catalogName) {
+    LOGGER.debug("Resolving {} sibling entities to validate location", keys.size());
+
+    PolarisResolutionManifest resolutionManifest =
+        new PolarisResolutionManifest(
+            diagnostics, callContext.getRealmContext(), resolverFactory, principal, catalogName);
+
+    keys.forEach(
+        k -> {
+          resolutionManifest.addPath(new ResolverPath(k, true)); // optional path
+        });
+
     ResolverStatus status = resolutionManifest.resolveAll();
-    if (!status.getStatus().equals(ResolverStatus.StatusEnum.SUCCESS)) {
+
+    if (status.getStatus() != ResolverStatus.StatusEnum.SUCCESS) {
       String message =
           "Unable to resolve sibling entities to validate location - " + status.getStatus();
       if (status.getStatus().equals(ResolverStatus.StatusEnum.ENTITY_COULD_NOT_BE_RESOLVED)) {
         message += ". Could not resolve entity: " + status.getFailedToResolvedEntityName();
       }
-      throw new IllegalStateException(message);
+
+      if (status.getStatus().equals(ResolverStatus.StatusEnum.PATH_COULD_NOT_BE_FULLY_RESOLVED)) {
+        ResolverPath path = status.getFailedToResolvePath();
+        if (path != null) {
+          message += ". path: " + String.join(".", path.entityNames());

Review Comment:
   Minor: two sequential `if (status.getStatus().equals(...))` on the same enum, switch or `else if` reads cleaner.



-- 
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: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make location overlap checks more robust [polaris]

Posted by "flyrain (via GitHub)" <gi...@apache.org>.
flyrain commented on code in PR #4408:
URL: https://github.com/apache/polaris/pull/4408#discussion_r3243567883


##########
runtime/service/src/main/java/org/apache/polaris/service/catalog/iceberg/IcebergCatalog.java:
##########
@@ -1242,70 +1241,87 @@ private <T extends PolarisEntity & LocationBasedEntity> void validateNoLocationO
                   return Namespace.of(newLevels);
                 })
             .toList();
-    LOGGER.debug(
-        "Resolving {} sibling entities to validate location",
-        siblingTables.size() + siblingNamespaces.size());
-    PolarisResolutionManifest resolutionManifest =
-        new PolarisResolutionManifest(
-            diagnostics,
-            callContext.getRealmContext(),
-            resolverFactory,
-            principal,
-            parentPath.getFirst().getName());
+    List<ResolvedPathKey> pathsToResolve =
+        new ArrayList<>(siblingTables.size() + siblingNamespaces.size());
     siblingTables.forEach(
-        tbl ->
-            resolutionManifest.addPath(
-                new ResolverPath(
-                    PolarisCatalogHelpers.tableIdentifierToList(tbl),
-                    PolarisEntityType.TABLE_LIKE)));
+        tbl -> {
+          if (!tbl.name().equals(name)) {
+            pathsToResolve.add(ResolvedPathKey.ofTableLike(tbl));
+          }
+        });
     siblingNamespaces.forEach(
-        ns ->
-            resolutionManifest.addPath(
-                new ResolverPath(Arrays.asList(ns.levels()), PolarisEntityType.NAMESPACE)));
+        ns -> {
+          if (!ns.level(ns.length() - 1).equals(name)) {
+            pathsToResolve.add(ResolvedPathKey.ofNamespace(ns));
+          }
+        });
+
+    StorageLocation targetLocation = StorageLocation.of(location);
+    for (PolarisEntity entityToCheck :
+        resolveOptionalPaths(pathsToResolve, parentPath.getFirst().getName())) {
+      String loc =
+          entityToCheck.getPropertiesAsMap().get(PolarisEntityConstants.ENTITY_BASE_LOCATION);

Review Comment:
   I remember there are some discussion before, but couldn't remember the specific reason. cc @gh-yzou 



-- 
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: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org