Java Code Examples for org.apache.iceberg.types.TypeUtil#assignFreshIds()

The following examples show how to use org.apache.iceberg.types.TypeUtil#assignFreshIds() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IcebergMetadata.java    From presto with Apache License 2.0 6 votes vote down vote up
private static Schema toIcebergSchema(List<ColumnMetadata> columns)
{
    List<NestedField> icebergColumns = new ArrayList<>();
    for (ColumnMetadata column : columns) {
        if (!column.isHidden()) {
            int index = icebergColumns.size();
            Type type = toIcebergType(column.getType());
            NestedField field = column.isNullable()
                    ? NestedField.optional(index, column.getName(), type, column.getComment())
                    : NestedField.required(index, column.getName(), type, column.getComment());
            icebergColumns.add(field);
        }
    }
    Schema schema = new Schema(icebergColumns);
    AtomicInteger nextFieldId = new AtomicInteger(1);
    return TypeUtil.assignFreshIds(schema, nextFieldId::getAndIncrement);
}
 
Example 2
Source File: TableMetadata.java    From iceberg with Apache License 2.0 5 votes vote down vote up
static TableMetadata newTableMetadata(Schema schema,
                                      PartitionSpec spec,
                                      String location,
                                      Map<String, String> properties,
                                      int formatVersion) {
  // reassign all column ids to ensure consistency
  AtomicInteger lastColumnId = new AtomicInteger(0);
  Schema freshSchema = TypeUtil.assignFreshIds(schema, lastColumnId::incrementAndGet);

  // rebuild the partition spec using the new column ids
  PartitionSpec.Builder specBuilder = PartitionSpec.builderFor(freshSchema)
      .withSpecId(INITIAL_SPEC_ID);
  for (PartitionField field : spec.fields()) {
    // look up the name of the source field in the old schema to get the new schema's id
    String sourceName = schema.findColumnName(field.sourceId());
    // reassign all partition fields with fresh partition field Ids to ensure consistency
    specBuilder.add(
        freshSchema.findField(sourceName).fieldId(),
        field.name(),
        field.transform().toString());
  }
  PartitionSpec freshSpec = specBuilder.build();

  return new TableMetadata(null, formatVersion, UUID.randomUUID().toString(), location,
      INITIAL_SEQUENCE_NUMBER, System.currentTimeMillis(),
      lastColumnId.get(), freshSchema, INITIAL_SPEC_ID, ImmutableList.of(freshSpec),
      ImmutableMap.copyOf(properties), -1, ImmutableList.of(),
      ImmutableList.of(), ImmutableList.of());
}
 
Example 3
Source File: TestReplaceTransaction.java    From iceberg with Apache License 2.0 4 votes vote down vote up
private static Schema assignFreshIds(Schema schema) {
  AtomicInteger lastColumnId = new AtomicInteger(0);
  return TypeUtil.assignFreshIds(schema, lastColumnId::incrementAndGet);
}