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;
}
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();
}
Aggregations