public PartitionKey toIceberg()

in xtable-core/src/main/java/org/apache/xtable/iceberg/IcebergPartitionValueConverter.java [188:239]


  public PartitionKey toIceberg(
      PartitionSpec partitionSpec, Schema schema, List<PartitionValue> partitionValues) {
    if (partitionValues == null || partitionValues.isEmpty()) {
      return null;
    }
    Map<String, PartitionValue> nameToPartitionInfo =
        partitionValues.stream()
            .collect(
                Collectors.toMap(
                    entry -> entry.getPartitionField().getSourceField().getName(),
                    Function.identity()));
    PartitionKey partitionKey = new PartitionKey(partitionSpec, schema);
    for (int i = 0; i < partitionSpec.fields().size(); i++) {
      PartitionField icebergPartitionField = partitionSpec.fields().get(i);
      String sourceFieldName = schema.findField(icebergPartitionField.sourceId()).name();
      PartitionValue partitionValue = nameToPartitionInfo.get(sourceFieldName);
      Object value = partitionValue.getRange().getMaxValue();
      switch (partitionValue.getPartitionField().getTransformType()) {
        case YEAR:
          partitionKey.set(
              i,
              Transforms.year(Types.TimestampType.withoutZone())
                  .apply(millisToMicros((Long) value)));
          break;
        case MONTH:
          partitionKey.set(
              i,
              Transforms.month(Types.TimestampType.withoutZone())
                  .apply(millisToMicros((Long) value)));
          break;
        case DAY:
          partitionKey.set(
              i,
              Transforms.day(Types.TimestampType.withoutZone())
                  .apply(millisToMicros((Long) value)));
          break;
        case HOUR:
          partitionKey.set(
              i,
              Transforms.hour(Types.TimestampType.withoutZone())
                  .apply(millisToMicros((Long) value)));
          break;
        case VALUE:
          partitionKey.set(i, Transforms.identity(Types.StringType.get()).apply(value));
          break;
        default:
          throw new IllegalArgumentException(
              "Unsupported type: " + partitionValue.getPartitionField().getTransformType());
      }
    }
    return partitionKey;
  }