|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.iceberg; |
| 21 | + |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.List; |
| 24 | +import org.apache.iceberg.relocated.com.google.common.collect.Lists; |
| 25 | + |
| 26 | +public class UnboundSortOrder { |
| 27 | + private static final UnboundSortOrder UNSORTED_ORDER = new UnboundSortOrder(0, Collections.emptyList()); |
| 28 | + |
| 29 | + private final int orderId; |
| 30 | + private final List<UnboundSortField> fields; |
| 31 | + |
| 32 | + private UnboundSortOrder(int orderId, List<UnboundSortField> fields) { |
| 33 | + this.orderId = orderId; |
| 34 | + this.fields = fields; |
| 35 | + } |
| 36 | + |
| 37 | + public SortOrder bind(Schema schema) { |
| 38 | + SortOrder.Builder builder = SortOrder.builderFor(schema).withOrderId(orderId); |
| 39 | + |
| 40 | + for (UnboundSortField field : fields) { |
| 41 | + builder.addSortField(field.transformAsString, field.sourceId, field.direction, field.nullOrder); |
| 42 | + } |
| 43 | + |
| 44 | + return builder.build(); |
| 45 | + } |
| 46 | + |
| 47 | + SortOrder bindUnchecked(Schema schema) { |
| 48 | + SortOrder.Builder builder = SortOrder.builderFor(schema).withOrderId(orderId); |
| 49 | + |
| 50 | + for (UnboundSortField field : fields) { |
| 51 | + builder.addSortField(field.transformAsString, field.sourceId, field.direction, field.nullOrder); |
| 52 | + } |
| 53 | + |
| 54 | + return builder.buildUnchecked(); |
| 55 | + } |
| 56 | + |
| 57 | + int orderId() { |
| 58 | + return orderId; |
| 59 | + } |
| 60 | + |
| 61 | + List<UnboundSortField> fields() { |
| 62 | + return fields; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Creates a new {@link SortOrder.Builder sort order builder} for unbound sort orders. |
| 67 | + * |
| 68 | + * @return a sort order builder |
| 69 | + */ |
| 70 | + static Builder builder() { |
| 71 | + return new Builder(); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * A builder used to create {@link UnboundSortOrder unbound sort orders}. |
| 76 | + * <p> |
| 77 | + * Call {@link #builder()} to create a new builder. |
| 78 | + */ |
| 79 | + static class Builder { |
| 80 | + private final List<UnboundSortField> fields = Lists.newArrayList(); |
| 81 | + private Integer orderId = null; |
| 82 | + |
| 83 | + private Builder() { |
| 84 | + } |
| 85 | + |
| 86 | + Builder withOrderId(int newOrderId) { |
| 87 | + this.orderId = newOrderId; |
| 88 | + return this; |
| 89 | + } |
| 90 | + |
| 91 | + Builder addSortField(String transformAsString, int sourceId, SortDirection direction, NullOrder nullOrder) { |
| 92 | + fields.add(new UnboundSortField(transformAsString, sourceId, direction, nullOrder)); |
| 93 | + return this; |
| 94 | + } |
| 95 | + |
| 96 | + UnboundSortOrder build() { |
| 97 | + if (fields.isEmpty()) { |
| 98 | + if (orderId != null && orderId != 0) { |
| 99 | + throw new IllegalArgumentException("Unsorted order ID must be 0"); |
| 100 | + } |
| 101 | + return UNSORTED_ORDER; |
| 102 | + } |
| 103 | + |
| 104 | + if (orderId != null && orderId == 0) { |
| 105 | + throw new IllegalArgumentException("Sort order ID 0 is reserved for unsorted order"); |
| 106 | + } |
| 107 | + |
| 108 | + // default ID to 1 as 0 is reserved for unsorted order |
| 109 | + int actualOrderId = orderId != null ? orderId : 1; |
| 110 | + return new UnboundSortOrder(actualOrderId, fields); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + static class UnboundSortField { |
| 115 | + private final String transformAsString; |
| 116 | + private final int sourceId; |
| 117 | + private final SortDirection direction; |
| 118 | + private final NullOrder nullOrder; |
| 119 | + |
| 120 | + private UnboundSortField(String transformAsString, int sourceId, SortDirection direction, NullOrder nullOrder) { |
| 121 | + this.transformAsString = transformAsString; |
| 122 | + this.sourceId = sourceId; |
| 123 | + this.direction = direction; |
| 124 | + this.nullOrder = nullOrder; |
| 125 | + } |
| 126 | + |
| 127 | + public String transformAsString() { |
| 128 | + return transformAsString; |
| 129 | + } |
| 130 | + |
| 131 | + public int sourceId() { |
| 132 | + return sourceId; |
| 133 | + } |
| 134 | + |
| 135 | + public SortDirection direction() { |
| 136 | + return direction; |
| 137 | + } |
| 138 | + |
| 139 | + public NullOrder nullOrder() { |
| 140 | + return nullOrder; |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments