Search in sources :

Example 1 with SortField

use of org.apache.iceberg.SortField in project iceberg by apache.

the class SortOrderVisitor method visit.

/**
 * Visit the fields of a {@link SortOrder}.
 *
 * @param sortOrder a sort order to visit
 * @param visitor a sort order visitor
 * @param <R> return type of the visitor
 * @return a list of the result produced by visiting each sort field
 */
@SuppressWarnings("checkstyle:CyclomaticComplexity")
static <R> List<R> visit(SortOrder sortOrder, SortOrderVisitor<R> visitor) {
    Schema schema = sortOrder.schema();
    List<R> results = Lists.newArrayListWithExpectedSize(sortOrder.fields().size());
    for (SortField field : sortOrder.fields()) {
        String sourceName = schema.findColumnName(field.sourceId());
        Transform<?, ?> transform = field.transform();
        if (transform == null || transform instanceof Identity) {
            results.add(visitor.field(sourceName, field.sourceId(), field.direction(), field.nullOrder()));
        } else if (transform instanceof Bucket) {
            int numBuckets = ((Bucket<?>) transform).numBuckets();
            results.add(visitor.bucket(sourceName, field.sourceId(), numBuckets, field.direction(), field.nullOrder()));
        } else if (transform instanceof Truncate) {
            int width = ((Truncate<?>) transform).width();
            results.add(visitor.truncate(sourceName, field.sourceId(), width, field.direction(), field.nullOrder()));
        } else if (transform == Dates.YEAR || transform == Timestamps.YEAR) {
            results.add(visitor.year(sourceName, field.sourceId(), field.direction(), field.nullOrder()));
        } else if (transform == Dates.MONTH || transform == Timestamps.MONTH) {
            results.add(visitor.month(sourceName, field.sourceId(), field.direction(), field.nullOrder()));
        } else if (transform == Dates.DAY || transform == Timestamps.DAY) {
            results.add(visitor.day(sourceName, field.sourceId(), field.direction(), field.nullOrder()));
        } else if (transform == Timestamps.HOUR) {
            results.add(visitor.hour(sourceName, field.sourceId(), field.direction(), field.nullOrder()));
        } else if (transform instanceof UnknownTransform) {
            results.add(visitor.unknown(sourceName, field.sourceId(), transform.toString(), field.direction(), field.nullOrder()));
        }
    }
    return results;
}
Also used : Schema(org.apache.iceberg.Schema) SortField(org.apache.iceberg.SortField)

Example 2 with SortField

use of org.apache.iceberg.SortField in project iceberg by apache.

the class SortOrderUtil method buildSortOrder.

public static SortOrder buildSortOrder(Schema schema, PartitionSpec spec, SortOrder sortOrder) {
    if (sortOrder.isUnsorted() && spec.isUnpartitioned()) {
        return SortOrder.unsorted();
    }
    Multimap<Integer, SortField> sortFieldIndex = Multimaps.index(sortOrder.fields(), SortField::sourceId);
    // build a sort prefix of partition fields that are not already in the sort order
    SortOrder.Builder builder = SortOrder.builderFor(schema);
    for (PartitionField field : spec.fields()) {
        Collection<SortField> sortFields = sortFieldIndex.get(field.sourceId());
        boolean isSorted = sortFields.stream().anyMatch(sortField -> field.transform().equals(sortField.transform()) || sortField.transform().satisfiesOrderOf(field.transform()));
        if (!isSorted) {
            String sourceName = schema.findColumnName(field.sourceId());
            builder.asc(Expressions.transform(sourceName, field.transform()));
        }
    }
    // add the configured sort to the partition spec prefix sort
    SortOrderVisitor.visit(sortOrder, new CopySortOrderFields(builder));
    return builder.build();
}
Also used : PartitionField(org.apache.iceberg.PartitionField) SortOrder(org.apache.iceberg.SortOrder) SortField(org.apache.iceberg.SortField)

Aggregations

SortField (org.apache.iceberg.SortField)2 PartitionField (org.apache.iceberg.PartitionField)1 Schema (org.apache.iceberg.Schema)1 SortOrder (org.apache.iceberg.SortOrder)1