Skip to content

Commit

Permalink
API: Deprecate NestedType.of in favor of builder.
Browse files Browse the repository at this point in the history
  • Loading branch information
rdblue committed Feb 13, 2025
1 parent 3ea3e43 commit 48137ab
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 63 deletions.
8 changes: 8 additions & 0 deletions api/src/main/java/org/apache/iceberg/types/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -540,10 +540,18 @@ public static NestedField required(int id, String name, Type type, String doc) {
return new NestedField(false, id, name, type, doc, null, null);
}

/**
* @deprecated will be removed in 2.0.0; use builder() instead.
*/
@Deprecated
public static NestedField of(int id, boolean isOptional, String name, Type type) {
return new NestedField(isOptional, id, name, type, null, null, null);
}

/**
* @deprecated will be removed in 2.0.0; use builder() instead.
*/
@Deprecated
public static NestedField of(int id, boolean isOptional, String name, Type type, String doc) {
return new NestedField(isOptional, id, name, type, doc, null, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,7 @@ private static Types.NestedField getPhysicalType(
// Use FixedSizeBinaryVector for binary backed decimal
type = Types.FixedType.ofLength(primitive.getTypeLength());
}
physicalType =
Types.NestedField.of(
logicalType.fieldId(), logicalType.isOptional(), logicalType.name(), type);
physicalType = Types.NestedField.from(logicalType).ofType(type).build();
}

return physicalType;
Expand Down
27 changes: 15 additions & 12 deletions core/src/main/java/org/apache/iceberg/MetricsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -346,18 +346,21 @@ public static Schema readableMetricsSchema(Schema dataTableSchema, Schema metada
String colName = idToName.get(id);

fields.add(
Types.NestedField.of(
nextId.incrementAndGet(),
true,
colName,
Types.StructType.of(
READABLE_METRIC_COLS.stream()
.map(
m ->
optional(
nextId.incrementAndGet(), m.name(), m.colType(field), m.doc()))
.collect(Collectors.toList())),
String.format("Metrics for column %s", colName)));
Types.NestedField.optional(colName)
.withId(nextId.incrementAndGet())
.ofType(
Types.StructType.of(
READABLE_METRIC_COLS.stream()
.map(
m ->
optional(
nextId.incrementAndGet(),
m.name(),
m.colType(field),
m.doc()))
.collect(Collectors.toList())))
.withDoc(String.format("Metrics for column %s", colName))
.build());
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/test/java/org/apache/iceberg/TestDeleteFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ public void testDeleteFilesOnIndependentBranches() {

@TestTemplate
public void testDeleteWithCollision() {
Schema schema = new Schema(Types.NestedField.of(0, false, "x", Types.StringType.get()));
Schema schema = new Schema(Types.NestedField.required(0, "x", Types.StringType.get()));
PartitionSpec spec = PartitionSpec.builderFor(schema).identity("x").build();
Table collisionTable =
TestTables.create(tableDir, "hashcollision", schema, spec, formatVersion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,7 @@ class SortKeySerializer extends TypeSerializer<SortKey> {
Types.NestedField sourceField = schema.findField(sortField.sourceId());
Type resultType = sortField.transform().getResultType(sourceField.type());
Types.NestedField transformedField =
Types.NestedField.of(
sourceField.fieldId(),
sourceField.isOptional(),
sourceField.name(),
resultType,
sourceField.doc());
Types.NestedField.from(sourceField).ofType(resultType).build();
transformedFields[i] = transformedField;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,11 @@ static Schema sortKeySchema(Schema schema, SortOrder sortOrder) {
// case. To resolve the collision, field id is set to transform index and field name is set to
// sourceFieldName_transformIndex
Types.NestedField transformedField =
Types.NestedField.of(
i,
sourceField.isOptional(),
sourceField.name() + '_' + i,
transformedType,
sourceField.doc());
Types.NestedField.from(sourceField)
.withId(i)
.withName(sourceField.name() + '_' + i)
.ofType(transformedType)
.build();
transformedFields.add(transformedField);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,13 @@ Type toIcebergType(Schema valueSchema) {
valueSchema.fields().stream()
.map(
field ->
NestedField.of(
nextId(),
config.schemaForceOptional() || field.schema().isOptional(),
field.name(),
toIcebergType(field.schema())))
NestedField.builder()
.isOptional(
config.schemaForceOptional() || field.schema().isOptional())
.withId(nextId())
.ofType(toIcebergType(field.schema()))
.withName(field.name())
.build())
.collect(Collectors.toList());
return StructType.of(structFields);
case STRING:
Expand Down
69 changes: 39 additions & 30 deletions orc/src/main/java/org/apache/iceberg/orc/OrcToIcebergVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ public Optional<Types.NestedField> record(
.map(Optional::get)
.collect(Collectors.toList()));
return Optional.of(
Types.NestedField.of(icebergIdOpt.get(), isOptional, currentFieldName(), structType));
Types.NestedField.builder()
.withId(icebergIdOpt.get())
.isOptional(isOptional)
.withName(currentFieldName())
.ofType(structType)
.build());
}

@Override
Expand All @@ -63,7 +68,12 @@ public Optional<Types.NestedField> list(
: Types.ListType.ofRequired(foundElement.fieldId(), foundElement.type());

return Optional.of(
Types.NestedField.of(icebergIdOpt.get(), isOptional, currentFieldName(), listTypeWithElem));
Types.NestedField.builder()
.withId(icebergIdOpt.get())
.isOptional(isOptional)
.withName(currentFieldName())
.ofType(listTypeWithElem)
.build());
}

@Override
Expand All @@ -86,7 +96,12 @@ public Optional<Types.NestedField> map(
foundKey.fieldId(), foundValue.fieldId(), foundKey.type(), foundValue.type());

return Optional.of(
Types.NestedField.of(icebergIdOpt.get(), isOptional, currentFieldName(), mapTypeWithKV));
Types.NestedField.builder()
.withId(icebergIdOpt.get())
.isOptional(isOptional)
.withName(currentFieldName())
.ofType(mapTypeWithKV)
.build());
}

@Override
Expand All @@ -98,17 +113,19 @@ public Optional<Types.NestedField> primitive(TypeDescription primitive) {
return Optional.empty();
}

final Types.NestedField foundField;
int icebergID = icebergIdOpt.get();
String name = currentFieldName();
Types.NestedField.Builder builder =
Types.NestedField.builder()
.withId(icebergIdOpt.get())
.isOptional(isOptional)
.withName(currentFieldName());
switch (primitive.getCategory()) {
case BOOLEAN:
foundField = Types.NestedField.of(icebergID, isOptional, name, Types.BooleanType.get());
builder.ofType(Types.BooleanType.get());
break;
case BYTE:
case SHORT:
case INT:
foundField = Types.NestedField.of(icebergID, isOptional, name, Types.IntegerType.get());
builder.ofType(Types.IntegerType.get());
break;
case LONG:
String longAttributeValue =
Expand All @@ -119,25 +136,25 @@ public Optional<Types.NestedField> primitive(TypeDescription primitive) {
: ORCSchemaUtil.LongType.valueOf(longAttributeValue);
switch (longType) {
case TIME:
foundField = Types.NestedField.of(icebergID, isOptional, name, Types.TimeType.get());
builder.ofType(Types.TimeType.get());
break;
case LONG:
foundField = Types.NestedField.of(icebergID, isOptional, name, Types.LongType.get());
builder.ofType(Types.LongType.get());
break;
default:
throw new IllegalStateException("Invalid Long type found in ORC type attribute");
}
break;
case FLOAT:
foundField = Types.NestedField.of(icebergID, isOptional, name, Types.FloatType.get());
builder.ofType(Types.FloatType.get());
break;
case DOUBLE:
foundField = Types.NestedField.of(icebergID, isOptional, name, Types.DoubleType.get());
builder.ofType(Types.DoubleType.get());
break;
case STRING:
case CHAR:
case VARCHAR:
foundField = Types.NestedField.of(icebergID, isOptional, name, Types.StringType.get());
builder.ofType(Types.StringType.get());
break;
case BINARY:
String binaryAttributeValue =
Expand All @@ -148,44 +165,36 @@ public Optional<Types.NestedField> primitive(TypeDescription primitive) {
: ORCSchemaUtil.BinaryType.valueOf(binaryAttributeValue);
switch (binaryType) {
case UUID:
foundField = Types.NestedField.of(icebergID, isOptional, name, Types.UUIDType.get());
builder.ofType(Types.UUIDType.get());
break;
case FIXED:
int fixedLength =
Integer.parseInt(primitive.getAttributeValue(ORCSchemaUtil.ICEBERG_FIELD_LENGTH));
foundField =
Types.NestedField.of(
icebergID, isOptional, name, Types.FixedType.ofLength(fixedLength));
builder.ofType(Types.FixedType.ofLength(fixedLength));
break;
case BINARY:
foundField = Types.NestedField.of(icebergID, isOptional, name, Types.BinaryType.get());
builder.ofType(Types.BinaryType.get());
break;
default:
throw new IllegalStateException("Invalid Binary type found in ORC type attribute");
}
break;
case DATE:
foundField = Types.NestedField.of(icebergID, isOptional, name, Types.DateType.get());
builder.ofType(Types.DateType.get());
break;
case TIMESTAMP:
foundField =
Types.NestedField.of(icebergID, isOptional, name, Types.TimestampType.withoutZone());
builder.ofType(Types.TimestampType.withoutZone());
break;
case TIMESTAMP_INSTANT:
foundField =
Types.NestedField.of(icebergID, isOptional, name, Types.TimestampType.withZone());
builder.ofType(Types.TimestampType.withZone());
break;
case DECIMAL:
foundField =
Types.NestedField.of(
icebergID,
isOptional,
name,
Types.DecimalType.of(primitive.getPrecision(), primitive.getScale()));
builder.ofType(Types.DecimalType.of(primitive.getPrecision(), primitive.getScale()));
break;
default:
throw new IllegalArgumentException("Can't handle " + primitive);
}
return Optional.of(foundField);

return Optional.of(builder.build());
}
}

0 comments on commit 48137ab

Please sign in to comment.