static Schema sortKeySchema(Schema schema, SortOrder sortOrder) {
    List<SortField> sortFields = sortOrder.fields();
    int size = sortFields.size();
    List<Types.NestedField> transformedFields = Lists.newArrayListWithCapacity(size);
    for (int i = 0; i < size; ++i) {
      int sourceFieldId = sortFields.get(i).sourceId();
      Types.NestedField sourceField = schema.findField(sourceFieldId);
      Preconditions.checkArgument(
          sourceField != null, "Cannot find source field: %s", sourceFieldId);
      Type transformedType = sortFields.get(i).transform().getResultType(sourceField.type());
      // There could be multiple transformations on the same source column, like in the PartitionKey
      // 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());
      transformedFields.add(transformedField);
    }

    return new Schema(transformedFields);
  }
@Test
  public void testResultSchema() {
    Schema schema =
        new Schema(
            Types.NestedField.required(1, "id", Types.StringType.get()),
            Types.NestedField.required(2, "ratio", Types.DoubleType.get()),
            Types.NestedField.optional(
                3,
                "user",
                Types.StructType.of(
                    Types.NestedField.required(11, "name", Types.StringType.get()),
                    Types.NestedField.required(12, "ts", Types.TimestampType.withoutZone()),
                    Types.NestedField.optional(13, "device_id", Types.UUIDType.get()),
                    Types.NestedField.optional(
                        14,
                        "location",
                        Types.StructType.of(
                            Types.NestedField.required(101, "lat", Types.FloatType.get()),
                            Types.NestedField.required(102, "long", Types.FloatType.get()),
                            Types.NestedField.required(103, "blob", Types.BinaryType.get()))))));

    SortOrder sortOrder =
        SortOrder.builderFor(schema)
            .asc("ratio")
            .sortBy(Expressions.hour("user.ts"), SortDirection.ASC, NullOrder.NULLS_FIRST)
            .sortBy(
                Expressions.bucket("user.device_id", 16), SortDirection.ASC, NullOrder.NULLS_FIRST)
            .sortBy(
                Expressions.truncate("user.location.blob", 16),
                SortDirection.ASC,
                NullOrder.NULLS_FIRST)
            .build();

    assertThat(SortKeyUtil.sortKeySchema(schema, sortOrder).asStruct())
        .isEqualTo(
            Types.StructType.of(
                Types.NestedField.required(0, "ratio_0", Types.DoubleType.get()),
                Types.NestedField.required(1, "ts_1", Types.IntegerType.get()),
                Types.NestedField.optional(2, "device_id_2", Types.IntegerType.get()),
                Types.NestedField.required(3, "blob_3", Types.BinaryType.get())));
  }