Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/src/main/java/org/apache/iceberg/Schema.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ public Schema(int schemaId, List<NestedField> columns, Map<String, Integer> alia
static void validateIdentifierField(int fieldId, Map<Integer, Types.NestedField> idToField,
Map<Integer, Integer> idToParent) {
Types.NestedField field = idToField.get(fieldId);
Preconditions.checkArgument(field != null,
"Cannot add fieldId %s as an identifier field: field does not exist", fieldId);
Preconditions.checkArgument(field.type().isPrimitiveType(),
"Cannot add field %s as an identifier field: not a primitive type field", field.name());
Preconditions.checkArgument(field.isRequired(),
Expand Down
10 changes: 7 additions & 3 deletions core/src/test/java/org/apache/iceberg/TestSchemaUpdate.java
Original file line number Diff line number Diff line change
Expand Up @@ -1334,24 +1334,28 @@ public void testRemoveIdentifierFields() {

@Test
public void testSetIdentifierFieldsFails() {

Schema testSchema = new Schema(
optional(1, "id", Types.IntegerType.get()),
required(2, "float", Types.FloatType.get()),
required(3, "double", Types.DoubleType.get())
);

AssertHelpers.assertThrows("Creating schema with nonexistent identifier fieldId should fail",
IllegalArgumentException.class,
"Cannot add fieldId 999 as an identifier field: field does not exist",
() -> new Schema(testSchema.asStruct().fields(), ImmutableSet.of(999)));

AssertHelpers.assertThrows("Creating schema with optional identifier field should fail",
IllegalArgumentException.class,
"Cannot add field id as an identifier field: not a required field",
() -> new Schema(testSchema.asStruct().fields(), ImmutableSet.of(1)));

AssertHelpers.assertThrows("Creating schema with optional identifier field should fail",
AssertHelpers.assertThrows("Creating schema with float identifier field should fail",
IllegalArgumentException.class,
"Cannot add field float as an identifier field: must not be float or double field",
() -> new Schema(testSchema.asStruct().fields(), ImmutableSet.of(2)));

AssertHelpers.assertThrows("Creating schema with optional identifier field should fail",
AssertHelpers.assertThrows("Creating schema with double identifier field should fail",
IllegalArgumentException.class,
"Cannot add field double as an identifier field: must not be float or double field",
() -> new Schema(testSchema.asStruct().fields(), ImmutableSet.of(3)));
Expand Down