World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#14432] fix(iceberg-rest): validate create-table transforms before dispatch (#14935)
roryqi91c0d7e1c377
diff --git a/iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/IcebergRESTUtils.java b/iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/IcebergRESTUtils.javaindex f648b9e0ce..c0edcd6f99 100644--- a/iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/IcebergRESTUtils.java+++ b/iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/IcebergRESTUtils.java@@ -64,6 +64,7 @@ import org.apache.iceberg.io.StorageCredential;import org.apache.iceberg.io.SupportsStorageCredentials;import org.apache.iceberg.rest.RESTRequest;import org.apache.iceberg.rest.RESTUtil;+import org.apache.iceberg.rest.requests.CreateTableRequest;import org.apache.iceberg.rest.responses.BaseScanTaskResponse;import org.apache.iceberg.rest.responses.ErrorResponse;import org.apache.iceberg.rest.responses.ImmutableLoadCredentialsResponse;@@ -528,6 +529,7 @@ public class IcebergRESTUtils {/*** Checks that a request body was sent and passes the request's own validation, so a missing or* incomplete body is answered with 400 instead of failing later with an internal error.+ * Create-table partition and sort transforms are also checked against the source column types.** @param request the deserialized request body, which is null when no body was sent* @param requestType the request type named in the error message@@ -540,7 +542,17 @@ public class IcebergRESTUtils {if (request instanceof RESTRequest) {try {((RESTRequest) request).validate();- } catch (IllegalArgumentException | IllegalStateException | NullPointerException e) {+ if (request instanceof CreateTableRequest) {+ // validate() only checks required fields. Bind transforms before the request is rebuilt+ // to set its owner; binding failures would otherwise become 406.+ CreateTableRequest create = (CreateTableRequest) request;+ create.spec();+ create.writeOrder();+ }+ } catch (IllegalArgumentException+ | IllegalStateException+ | UnsupportedOperationException+ | NullPointerException e) {throw new BadRequestException("Invalid %s: %s",requestType, e.getMessage() == null ? "a required field is missing" : e.getMessage());diff --git a/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/service/TestIcebergRESTUtils.java b/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/service/TestIcebergRESTUtils.javaindex 10a76eaa84..834c8cee71 100644--- a/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/service/TestIcebergRESTUtils.java+++ b/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/service/TestIcebergRESTUtils.java@@ -24,6 +24,7 @@ import static org.mockito.Mockito.when;import static org.mockito.Mockito.withSettings;import com.google.common.collect.ImmutableMap;+import java.io.IOException;import java.util.Collections;import java.util.List;import java.util.Map;@@ -39,6 +40,7 @@ import org.apache.gravitino.iceberg.service.authorization.IcebergRESTServerConteimport org.apache.gravitino.iceberg.service.provider.IcebergConfigProvider;import org.apache.iceberg.PartitionSpec;import org.apache.iceberg.Schema;+import org.apache.iceberg.SchemaParser;import org.apache.iceberg.SortOrder;import org.apache.iceberg.TableMetadata;import org.apache.iceberg.catalog.Namespace;@@ -55,6 +57,7 @@ import org.apache.iceberg.rest.responses.ErrorResponse;import org.apache.iceberg.rest.responses.ImmutableLoadCredentialsResponse;import org.apache.iceberg.rest.responses.LoadCredentialsResponse;import org.apache.iceberg.rest.responses.LoadTableResponse;+import org.apache.iceberg.types.Types;import org.apache.iceberg.types.Types.IntegerType;import org.apache.iceberg.types.Types.NestedField;import org.apache.iceberg.types.Types.StringType;@@ -64,6 +67,7 @@ import org.junit.jupiter.api.Test;import org.junit.jupiter.api.TestInstance;import org.junit.jupiter.params.ParameterizedTest;import org.junit.jupiter.params.provider.MethodSource;+import org.junit.jupiter.params.provider.ValueSource;import org.mockito.Mockito;@SuppressWarnings("deprecation")@@ -540,6 +544,68 @@ public class TestIcebergRESTUtils {Assertions.assertDoesNotThrow(() -> IcebergRESTUtils.validateRequest(valid, "X"));}+ @ParameterizedTest+ @ValueSource(+ strings = {+ "boolean",+ "float",+ "double",+ "date",+ "time",+ "timestamp",+ "timestamptz",+ "uuid",+ "fixed[16]",+ "timestamp_ns",+ "timestamptz_ns"+ })+ void testValidateRequestRejectsInvalidTruncate(String type) throws IOException {+ for (boolean nested : new boolean[] {false, true}) {+ for (boolean sort : new boolean[] {false, true}) {+ CreateTableRequest request = truncateRequest(type, nested, sort);+ BadRequestException error =+ Assertions.assertThrows(+ BadRequestException.class,+ () -> IcebergRESTUtils.validateRequest(request, "CreateTableRequest"));+ Assertions.assertTrue(error.getMessage().contains("Invalid CreateTableRequest"));+ Assertions.assertTrue(error.getMessage().contains("Cannot truncate type: " + type));+ }+ }+ }++ @ParameterizedTest+ @ValueSource(strings = {"int", "long", "decimal(9, 2)", "string", "binary"})+ void testValidateRequestAcceptsValidTruncate(String type) throws IOException {+ for (boolean nested : new boolean[] {false, true}) {+ for (boolean sort : new boolean[] {false, true}) {+ CreateTableRequest request = truncateRequest(type, nested, sort);+ Assertions.assertDoesNotThrow(+ () -> IcebergRESTUtils.validateRequest(request, "CreateTableRequest"));+ }+ }+ }++ private static CreateTableRequest truncateRequest(String type, boolean nested, boolean sort)+ throws IOException {+ NestedField field = NestedField.optional(1, "c", Types.fromPrimitiveString(type));+ Schema schema =+ nested+ ? new Schema(NestedField.optional(2, "parent", Types.StructType.of(field)))+ : new Schema(field);+ String definition =+ sort+ ? "\"write-order\": {\"order-id\": 1, \"fields\": [{\"source-id\": 1,"+ + " \"transform\": \"truncate[4]\", \"direction\": \"asc\","+ + " \"null-order\": \"nulls-first\"}]}"+ : "\"partition-spec\": {\"spec-id\": 0, \"fields\": [{\"source-id\": 1,"+ + " \"field-id\": 1000, \"name\": \"p\", \"transform\": \"truncate[4]\"}]}";+ return IcebergObjectMapper.getInstance()+ .readValue(+ String.format(+ "{\"name\": \"t\", \"schema\": %s, %s}", SchemaParser.toJson(schema), definition),+ CreateTableRequest.class);+ }+private static List<Namespace> invalidNamespaces() {return List.of(Namespace.of("a.b"),diff --git a/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/service/rest/TestIcebergTableOperations.java b/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/service/rest/TestIcebergTableOperations.javaindex 6fe62062bd..7545bdf210 100644--- a/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/service/rest/TestIcebergTableOperations.java+++ b/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/service/rest/TestIcebergTableOperations.java@@ -110,6 +110,7 @@ import org.apache.iceberg.rest.requests.UpdateTableRequest;import org.apache.iceberg.rest.responses.ErrorResponse;import org.apache.iceberg.rest.responses.ListTablesResponse;import org.apache.iceberg.rest.responses.LoadTableResponse;+import org.apache.iceberg.types.Types.BooleanType;import org.apache.iceberg.types.Types.NestedField;import org.apache.iceberg.types.Types.StringType;import org.apache.iceberg.util.JsonUtil;@@ -721,6 +722,78 @@ public class TestIcebergTableOperations extends IcebergNamespaceTestBase {getTableClientBuilder(namespace, Optional.of("bad_default_tbl")).get().getStatus());}+ @ParameterizedTest+ @MethodSource("org.apache.gravitino.iceberg.service.rest.IcebergRestTestUtil#testNamespaces")+ void testCreateWithInvalidTruncateReturnsBadRequest(Namespace namespace) {+ verifyCreateNamespaceSucc(namespace);+ for (boolean staged : new boolean[] {false, true}) {+ for (String version : new String[] {null, "2", "3"}) {+ for (String definition :+ new String[] {+ "\"partition-spec\": {\"spec-id\": 0, \"fields\": [{\"source-id\": 1,"+ + " \"field-id\": 1000, \"name\": \"p\", \"transform\": \"truncate[4]\"}]}",+ "\"write-order\": {\"order-id\": 1, \"fields\": [{\"source-id\": 1,"+ + " \"transform\": \"truncate[4]\", \"direction\": \"asc\","+ + " \"null-order\": \"nulls-first\"}]}"+ }) {+ String request =+ String.format(+ "{\"name\": \"bad_truncate_tbl\", \"schema\": {\"type\": \"struct\","+ + " \"schema-id\": 0, \"fields\": [{\"id\": 1, \"name\": \"b\","+ + " \"required\": false, \"type\": \"boolean\"}]}, %s,"+ + " \"stage-create\": %b, \"properties\": %s}",+ definition,+ staged,+ version == null ? "{}" : "{\"format-version\": \"" + version + "\"}");+ try (Response response =+ getTableClientBuilder(namespace, Optional.empty())+ .post(Entity.entity(request, MediaType.APPLICATION_JSON_TYPE))) {+ assertBadRequest(response, "Cannot truncate type: boolean");+ }+ verifyLoadTableFail(namespace, "bad_truncate_tbl", 404);+ }+ }+ }+ }++ @ParameterizedTest+ @MethodSource("org.apache.gravitino.iceberg.service.rest.IcebergRestTestUtil#testNamespaces")+ void testInvalidTruncateAndUnknownCommitTypesDoNotChangeMetadata(Namespace namespace) {+ verifyCreateNamespaceSucc(namespace);+ String tableName = "bad_commit_tbl";+ CreateTableRequest create =+ CreateTableRequest.builder()+ .withName(tableName)+ .withSchema(new Schema(NestedField.optional(1, "b", BooleanType.get())))+ .build();+ try (Response response =+ getTableClientBuilder(namespace, Optional.empty())+ .post(Entity.entity(create, MediaType.APPLICATION_JSON_TYPE))) {+ Assertions.assertEquals(Status.OK.getStatusCode(), response.getStatus());+ }+ String metadataLocation = getTableMeta(namespace, tableName).metadataFileLocation();+ Assertions.assertNotNull(metadataLocation);+ for (String request :+ new String[] {+ "{\"requirements\": [], \"updates\": [{\"action\": \"add-spec\", \"spec\":"+ + " {\"spec-id\": 0, \"fields\": [{\"source-id\": 1, \"field-id\": 1000,"+ + " \"name\": \"p\", \"transform\": \"truncate[4]\"}]}}]}",+ "{\"requirements\": [], \"updates\": [{\"action\": \"add-sort-order\", \"sort-order\":"+ + " {\"order-id\": 1, \"fields\": [{\"source-id\": 1, \"transform\": \"truncate[4]\","+ + " \"direction\": \"asc\", \"null-order\": \"nulls-first\"}]}}]}",+ "{\"requirements\": [], \"updates\": [{\"action\": \"frob\"}]}",+ "{\"requirements\": [{\"type\": \"assert-frob\"}], \"updates\": []}"+ }) {+ try (Response response =+ getTableClientBuilder(namespace, Optional.of(tableName))+ .post(Entity.entity(request, MediaType.APPLICATION_JSON_TYPE))) {+ assertBadRequest(response, null);+ }+ Assertions.assertEquals(+ metadataLocation, getTableMeta(namespace, tableName).metadataFileLocation());+ }+ }+private static void assertBadRequest(Response response, String expectedInMessage) {Assertions.assertTrue(response.getMediaType().isCompatible(MediaType.APPLICATION_JSON_TYPE),