jerryshao commented on code in PR #14894:
URL: https://github.com/apache/gravitino/pull/14894#discussion_r4536035950
##########
catalogs/catalog-lakehouse-generic/src/main/java/org/apache/gravitino/catalog/lakehouse/lance/LanceTableOperations.java:
##########
@@ -599,6 +634,103 @@ 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. Dropping a table deletes 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.isNotBlank(other) && isPrefixOrSame(requested,
locationSegments(other))) {
+ throw new IllegalArgumentException(
+ String.format(
+ "Location %s of Lance table %s overlaps the location of
another table in"
+ + " this catalog",
+ location, ident));
+ }
+ }
+ }
+ } catch (IOException e) {
+ throw ExceptionMessages.wrap("Failed to check the table locations for "
+ ident, e);
+ }
+ }
+
+ /**
+ * Splits a location into scheme, authority and normalized path segments. A
path without a scheme
+ * is a local file path. Segments are compared whole, so {@code s3://b/v1}
does not contain {@code
+ * s3://b/v10}.
+ */
+ static List<String> locationSegments(String location) {
Review Comment:
Scheme and authority are only lower-cased, so equivalent locations such as
s3:// vs s3a://, a trailing-dot or default-port authority, or percent-encoded
segments compare as different and slip past the overlap guard. Normalize known
scheme aliases (s3/s3a/s3n, gs/gcs) and decode paths before comparing, and add
tests for them.
##########
docs/lakehouse-generic-catalog.md:
##########
@@ -50,7 +50,7 @@ For detailed information on available operations, see [Manage
Relational Metadat
| `location` | Root storage path for all schemas and tables
| `s3://bucket/lakehouse` | No |
| `table-location-provider` | Name of the [table location
provider](#pluggable-table-location-provider) that provisions and unprovisions
the locations of this catalog's tables. Defaults to `default`, which resolves
the location from the table, schema and catalog `location` properties as
described below. Immutable once the catalog is created. | `default`
| No |
| `lance.schema-refresh-mode` | Lance table schema refresh mode.
`DECLARED_AND_EMPTY` (default) refreshes declared tables and tables with empty
stored columns. `VERSION_CHECK` additionally refreshes when the Lance dataset
version changes.
| `DECLARED_AND_EMPTY` | No
|
-| `lance.allow-external-table-purge` | Allow physical deletion of external or
registered Lance tables, including Lance REST `DropTable` and create
`OVERWRITE`. Catalog-only; table properties cannot enable it. Does not validate
ownership of storage locations. | `false` | No |
+| `lance.allow-external-dataset-modification` | Allow physical deletion of
external or registered Lance tables, including Lance REST `DropTable` and
create `OVERWRITE`. Also allow column and index changes to the datasets of
registered or declared tables. Catalog-only; table properties cannot enable it.
Does not validate ownership of storage locations. Renamed from
`lance.allow-external-table-purge`. | `false` | No |
Review Comment:
Renaming lance.allow-external-table-purge without a deprecated alias means
catalogs that set it to true silently go back to refusing purge after upgrade.
Was the old name ever released, and if so should it still be read as a fallback?
##########
catalogs/catalog-lakehouse-generic/src/main/java/org/apache/gravitino/catalog/lakehouse/lance/LanceTableOperations.java:
##########
@@ -292,6 +309,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:
Declared tables created before this change have neither the adopted flag nor
lance.register (the declared flag is removed after schema load), so they stay
unguarded for alter. Is there a migration or fallback for them, and if not
should this be documented?
##########
catalogs/catalog-lakehouse-generic/src/main/java/org/apache/gravitino/catalog/lakehouse/lance/LanceTableOperations.java:
##########
@@ -234,22 +237,36 @@ 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 any storage 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 lists every schema and every table in the catalog on each create or
register, which is O(N) store reads and will get slow on large catalogs. Query
tables by location prefix in the store, or at least note the cost and cap 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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]