aokolnychyi commented on a change in pull request #1373: URL: https://github.com/apache/iceberg/pull/1373#discussion_r477606935
########## File path: api/src/main/java/org/apache/iceberg/SortOrder.java ########## @@ -0,0 +1,212 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.iceberg; + +import java.io.Serializable; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.stream.IntStream; +import org.apache.iceberg.exceptions.ValidationException; +import org.apache.iceberg.expressions.BoundReference; +import org.apache.iceberg.expressions.BoundTerm; +import org.apache.iceberg.expressions.BoundTransform; +import org.apache.iceberg.expressions.Expressions; +import org.apache.iceberg.expressions.Term; +import org.apache.iceberg.expressions.UnboundTerm; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.apache.iceberg.transforms.Transform; +import org.apache.iceberg.transforms.Transforms; +import org.apache.iceberg.types.Types; + +public class SortOrder implements Serializable { + // TODO: shall we reserve 0 for the unsorted order? PartitionSpec does not guarantee it? + private static final SortOrder UNSORTED_ORDER = new SortOrder(new Schema(), 0, Collections.emptyList()); + + private final Schema schema; + private final int orderId; + private final SortField[] fields; + + private transient volatile List<SortField> fieldList; + + private SortOrder(Schema schema, int orderId, List<SortField> fields) { + this.schema = schema; + this.orderId = orderId; + this.fields = fields.toArray(new SortField[0]); + } + + public Schema schema() { + return schema; + } + + public int orderId() { + return orderId; + } + + public List<SortField> fields() { + return lazyFieldList(); + } + + public boolean isUnsorted() { + return fields.length < 1; + } + + public boolean satisfies(SortOrder anotherSortOrder) { + // any ordering satisfies an unsorted ordering + if (anotherSortOrder.isUnsorted()) { + return true; + } + + // this ordering cannot satisfy an ordering with more sort fields + if (anotherSortOrder.fields.length > fields.length) { + return false; + } + + // this ordering has either more or the same number of sort fields + return IntStream.range(0, anotherSortOrder.fields.length) + .allMatch(index -> fields[index].equals(anotherSortOrder.fields[index])); + } + + public boolean sameOrder(SortOrder anotherSortOrder) { + return Arrays.equals(fields, anotherSortOrder.fields); + } + + private List<SortField> lazyFieldList() { + if (fieldList == null) { + synchronized (this) { + if (fieldList == null) { + this.fieldList = ImmutableList.copyOf(fields); + } + } + } + return fieldList; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("["); + for (SortField field : fields) { + sb.append("\n"); + sb.append(" ").append(field); + } + if (fields.length > 0) { + sb.append("\n"); + } + sb.append("]"); + return sb.toString(); + } + + @Override + public boolean equals(Object other) { + if (this == other) { + return true; + } else if (other == null || getClass() != other.getClass()) { + return false; + } + + SortOrder that = (SortOrder) other; + return orderId == that.orderId && Arrays.equals(fields, that.fields); + } + + @Override + public int hashCode() { + return 31 * Integer.hashCode(orderId) + Arrays.hashCode(fields); + } + + public static SortOrder unsorted() { + return UNSORTED_ORDER; + } + + public static Builder builderFor(Schema schema) { + return new Builder(schema); + } + + public static class Builder { + private final Schema schema; + private final List<SortField> fields = Lists.newArrayList(); + private int orderId = 0; + private boolean caseSensitive = true; + + private Builder(Schema schema) { + this.schema = schema; + } + + public Builder asc(String name, NullOrder nullOrder) { Review comment: Yeah, I'll be fine with picking a default value ourselves. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
