rdblue commented on a change in pull request #1373: URL: https://github.com/apache/iceberg/pull/1373#discussion_r479578801
########## File path: core/src/main/java/org/apache/iceberg/SortOrderParser.java ########## @@ -0,0 +1,98 @@ +/* + * 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 com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.JsonNode; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.util.Iterator; +import java.util.Locale; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.util.JsonUtil; + +public class SortOrderParser { + private static final String ORDER_ID = "order-id"; + private static final String FIELDS = "fields"; + private static final String DIRECTION = "direction"; + private static final String NULL_ORDERING = "null-order"; + private static final String TRANSFORM = "transform"; + private static final String SOURCE_ID = "source-id"; + + private SortOrderParser() { + } + + public static void toJson(SortOrder sortOrder, JsonGenerator generator) throws IOException { + generator.writeStartObject(); + generator.writeNumberField(ORDER_ID, sortOrder.orderId()); + generator.writeFieldName(FIELDS); + toJsonFields(sortOrder, generator); + generator.writeEndObject(); + } + + private static void toJsonFields(SortOrder sortOrder, JsonGenerator generator) throws IOException { + generator.writeStartArray(); + for (SortField field : sortOrder.fields()) { + generator.writeStartObject(); + generator.writeStringField(TRANSFORM, field.transform().toString()); + generator.writeNumberField(SOURCE_ID, field.sourceId()); + generator.writeStringField(DIRECTION, field.direction().toString().toLowerCase(Locale.ROOT)); + generator.writeStringField(NULL_ORDERING, field.nullOrder().jsonValue()); + generator.writeEndObject(); + } + generator.writeEndArray(); + } + + public static SortOrder fromJson(Schema schema, String json) { + try { + return fromJson(schema, JsonUtil.mapper().readValue(json, JsonNode.class)); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + + public static SortOrder fromJson(Schema schema, JsonNode json) { + Preconditions.checkArgument(json.isObject(), "Cannot parse sort order from non-object: %s", json); + int orderId = JsonUtil.getInt(ORDER_ID, json); + SortOrder.Builder builder = SortOrder.builderFor(schema).withOrderId(orderId); + buildFromJsonFields(builder, json.get(FIELDS)); + return builder.build(); + } + + private static void buildFromJsonFields(SortOrder.Builder builder, JsonNode json) { + Preconditions.checkArgument(json.isArray(), "Cannot parse partition order fields, not an array: %s", json); Review comment: I think this needs to check whether `json` is `null` as well. That's a bug here and in `PartitionSpecParser`. If it is null because the field doesn't exist, then this will throw a `NullPointerException` instead of a good error message. I'm good with either checking here (`json != null && json.isArray()`) or in the method that calls this using `json.has(FIELDS)`. ---------------------------------------------------------------- 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]
