This is an automated email from the ASF dual-hosted git repository.
roryqi pushed a commit to branch branch-1.3
in repository https://gitbox.apache.org/repos/asf/gravitino.git
The following commit(s) were added to refs/heads/branch-1.3 by this push:
new ec876b1089 [Cherry-pick to branch-1.3] [#14432] fix(iceberg-rest):
validate create-table transforms before dispatch (#14938)
ec876b1089 is described below
commit ec876b10895b227e5cf98727c9baa31cfd70b2c2
Author: roryqi <[email protected]>
AuthorDate: Thu Nov 12 16:23:26 2026 +0800
[Cherry-pick to branch-1.3] [#14432] fix(iceberg-rest): validate
create-table transforms before dispatch (#14938)
### What changes were proposed in this pull request?
Cherry-pick #14935 to `branch-1.3` to validate create-table partition
and sort transforms before dispatch. Resolve branch-specific import and
test context conflicts.
### Why are the changes needed?
Invalid transforms can fail while rebuilding an authenticated request to
set its owner and return 406 instead of 400. Follow-up to #14432.
### Does this PR introduce _any_ user-facing change?
Invalid create-table transforms return 400 with an Iceberg ErrorModel.
Unsupported operations continue to return 406.
### How was this patch tested?
All 129 focused regression tests passed across `TestIcebergRESTUtils`,
`TestIcebergTableOperations`, `TestIcebergExceptionMapper`, and
`TestIcebergRequestBodyReaderInterceptor`.
Module Spotless and `git diff --check` passed.
---
.../iceberg/service/IcebergRESTUtils.java | 14 ++++-
.../iceberg/service/TestIcebergRESTUtils.java | 67 ++++++++++++++++++++
.../service/rest/TestIcebergTableOperations.java | 73 ++++++++++++++++++++++
3 files changed, 153 insertions(+), 1 deletion(-)
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.java
index 1568696a52..e086e37a0d 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
@@ -58,6 +58,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.ErrorResponse;
import org.apache.iceberg.rest.responses.ImmutableLoadCredentialsResponse;
import org.apache.iceberg.rest.responses.LoadCredentialsResponse;
@@ -386,6 +387,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
@@ -398,7 +400,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.java
index 41973deebc..1d93d01da9 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.IcebergRESTServerConte
import 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;
@@ -54,6 +56,7 @@ import org.apache.iceberg.rest.requests.UpdateTableRequest;
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;
@@ -61,6 +64,8 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mockito;
@SuppressWarnings("deprecation")
@@ -498,4 +503,66 @@ public class TestIcebergRESTUtils {
RESTRequest valid = () -> {};
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);
+ }
}
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.java
index 09bf82c516..d55abb7596 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
@@ -100,6 +100,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;
@@ -641,6 +642,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),