jerryshao commented on code in PR #14894:
URL: https://github.com/apache/gravitino/pull/14894#discussion_r4538296433


##########
catalogs/catalog-lakehouse-generic/src/main/java/org/apache/gravitino/catalog/lakehouse/lance/LanceTableOperations.java:
##########
@@ -599,6 +650,282 @@ private SchemaRefreshMode schemaRefreshMode() {
         .orElse(SchemaRefreshMode.DECLARED_AND_EMPTY);
   }
 
+  /**
+   * Rejects a location that equals, contains or is inside the location of 
another table in the same
+   * catalog. Dataset deletion removes its whole location, so a table at a 
parent directory could
+   * delete the datasets of other tables, and a table at another table's 
location could change that
+   * table's dataset.
+   *
+   * <p>This compares stored metadata only. It does not see other catalogs, 
symbolic links or tables
+   * created concurrently. The other table is not named, because the caller 
may not be allowed to
+   * see it.
+   *
+   * @param ident the table being created; an existing table with this name is 
ignored, because an
+   *     overwrite replaces it
+   * @param location the requested table location
+   */
+  private void rejectOverlappingLocation(NameIdentifier ident, String 
location) {
+    Namespace schemaNamespace = ident.namespace();
+    if (schemaNamespace.length() < 3) {
+      // Only a metalake.catalog.schema namespace identifies the catalog to 
compare against.
+      return;
+    }
+    List<String> requested = locationSegments(location);
+    Namespace catalogNamespace = Namespace.of(schemaNamespace.level(0), 
schemaNamespace.level(1));
+    try {
+      for (SchemaEntity schema :
+          store().list(catalogNamespace, SchemaEntity.class, 
Entity.EntityType.SCHEMA)) {
+        Namespace tableNamespace =
+            Namespace.of(catalogNamespace.level(0), catalogNamespace.level(1), 
schema.name());
+        for (TableEntity table :
+            store().list(tableNamespace, TableEntity.class, 
Entity.EntityType.TABLE)) {

Review Comment:
   Every create scans all schemas and loads every table entity in the catalog, 
so cost grows with catalog size and happens on the create path. Also the check 
and the insert are not atomic, so concurrent creates can still overlap; 
consider a location index or at least document and test this.



##########
catalogs/catalog-lakehouse-generic/src/main/java/org/apache/gravitino/catalog/lakehouse/lance/LanceTableOperations.java:
##########
@@ -234,22 +243,37 @@ public Table createTable(
       }
     }
 
-    if (register) {
-      if (mode == CreationMode.CREATE) {
-        // Check metadata before accessing storage so an existing name 
consistently reports a
-        // conflict. The final strict insert still detects a concurrent 
registration.
-        try {
-          if (store().exists(ident, Entity.EntityType.TABLE)) {
-            throw new TableAlreadyExistsException("Table %s already exists", 
ident);
-          }
-        } catch (IOException e) {
-          throw ExceptionMessages.wrap("Failed to check existence of table " + 
ident, e);
+    if (mode == CreationMode.CREATE) {
+      // Check metadata before accessing storage so an existing name reports a 
conflict without
+      // writing a dataset. The final strict insert still detects a concurrent 
creation.
+      try {
+        if (store().exists(ident, Entity.EntityType.TABLE)) {
+          throw new TableAlreadyExistsException("Table %s already exists", 
ident);
         }
+      } catch (IOException e) {
+        throw ExceptionMessages.wrap("Failed to check existence of table " + 
ident, e);
       }
+    }
+
+    // Before Lance dataset access, so a location that holds another table's 
data is never written,
+    // adopted or later deleted through this table.
+    rejectOverlappingLocation(ident, location);

Review Comment:
   This also rejects creates and registers whose location equals an existing 
table, which was previously allowed, so existing workflows (for example 
re-registering a dataset under another name) now fail. Please call this out as 
a breaking change and add a note or migration path.



##########
catalogs/catalog-lakehouse-generic/src/main/java/org/apache/gravitino/catalog/lakehouse/lance/LanceTableOperations.java:
##########
@@ -292,6 +316,14 @@ public Table alterTable(NameIdentifier ident, 
TableChange... changes)
               + ident
               + " because its dataset schema is not initialized or could not 
be loaded");
     }
+    if (isAdoptedDataset(loadedTable.properties()) && 
!externalDatasetModificationAllowed()) {

Review Comment:
   Alter on registered or declared tables now fails unless the catalog opts in, 
which is a behavior change for current users. Is this intended to apply to 
already-existing tables, and is the renamed property 
LANCE_ALLOW_EXTERNAL_TABLE_PURGE supposed to still be honored for upgrades?



##########
catalogs/catalog-lakehouse-generic/src/main/java/org/apache/gravitino/catalog/lakehouse/lance/LanceTableOperations.java:
##########
@@ -599,6 +650,282 @@ private SchemaRefreshMode schemaRefreshMode() {
         .orElse(SchemaRefreshMode.DECLARED_AND_EMPTY);
   }
 
+  /**
+   * Rejects a location that equals, contains or is inside the location of 
another table in the same
+   * catalog. Dataset deletion removes its whole location, so a table at a 
parent directory could
+   * delete the datasets of other tables, and a table at another table's 
location could change that
+   * table's dataset.
+   *
+   * <p>This compares stored metadata only. It does not see other catalogs, 
symbolic links or tables
+   * created concurrently. The other table is not named, because the caller 
may not be allowed to
+   * see it.
+   *
+   * @param ident the table being created; an existing table with this name is 
ignored, because an
+   *     overwrite replaces it
+   * @param location the requested table location
+   */
+  private void rejectOverlappingLocation(NameIdentifier ident, String 
location) {
+    Namespace schemaNamespace = ident.namespace();
+    if (schemaNamespace.length() < 3) {
+      // Only a metalake.catalog.schema namespace identifies the catalog to 
compare against.
+      return;
+    }
+    List<String> requested = locationSegments(location);
+    Namespace catalogNamespace = Namespace.of(schemaNamespace.level(0), 
schemaNamespace.level(1));
+    try {
+      for (SchemaEntity schema :
+          store().list(catalogNamespace, SchemaEntity.class, 
Entity.EntityType.SCHEMA)) {
+        Namespace tableNamespace =
+            Namespace.of(catalogNamespace.level(0), catalogNamespace.level(1), 
schema.name());
+        for (TableEntity table :
+            store().list(tableNamespace, TableEntity.class, 
Entity.EntityType.TABLE)) {
+          if (table.nameIdentifier().equals(ident) || table.properties() == 
null) {
+            continue;
+          }
+          String other = table.properties().get(Table.PROPERTY_LOCATION);
+          if (StringUtils.isBlank(other)) {
+            continue;
+          }
+          final List<String> existing;
+          try {
+            existing = locationSegments(other);
+          } catch (IllegalArgumentException invalid) {
+            // Fail closed without exposing the other table's name or location 
to its peers.
+            LOG.warn("Invalid stored Lance location for table {}", 
table.nameIdentifier(), invalid);
+            // Main REST can serialize exception causes by default. Keep the 
private URI in server
+            // logs only, not in the throwable returned to a caller of another 
table.
+            throw new IllegalStateException(

Review Comment:
   One existing table with an unparsable stored location makes every create or 
register in the whole catalog fail with IllegalStateException. Skip that table 
with a warning, or fail only when the new location is also unparsable, so one 
bad row cannot block the catalog.



-- 
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]

Reply via email to