rdblue commented on a change in pull request #227: ORC column map fix URL: https://github.com/apache/incubator-iceberg/pull/227#discussion_r300058882
########## File path: orc/src/main/java/org/apache/iceberg/orc/ORCSchemaUtil.java ########## @@ -0,0 +1,89 @@ +/* + * 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.orc; + +import com.google.common.base.Splitter; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.util.IdentityHashMap; +import java.util.Map; +import java.util.stream.Collectors; +import org.apache.orc.TypeDescription; + +/** + * Utilities for mapping Iceberg to ORC schemas. + */ +final class ORCSchemaUtil { + + private ORCSchemaUtil() {} + + static Map<TypeDescription, IcebergColumn> newColumMap() { + return new IdentityHashMap<>(); + } + + static <K, V> Map<K, V> inverse(Map<V, K> mapToFlip) { + return mapToFlip.entrySet() + .stream() + .collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey)); + } + + /** + * Serializes the column mapping into a comma separated list of values in the form: + * <code> + * orc_id:iceberg_id:(required|optional),orc_id:iceberg_id:(required|optional),... + * </code> + * For example: + * <code> + * 0:2:required,1:3:optional + * </code> + * The above example defines two column mappings: + * - ORC column 0 maps to Iceberg column 2 and it's required. + * - ORC column 1 maps to Iceberg column 3 and it's optiona. + * @param idMap the Map with the column mapping of ORC type to Iceberg column. + * @return A ByteBuffer with the string serialization in UTF-8. + */ + static ByteBuffer serialize(Map<TypeDescription, IcebergColumn> idMap) { + StringBuilder buffer = new StringBuilder(); + boolean needComma = false; + for (TypeDescription key : idMap.keySet()) { Review comment: Normally, we'd use Guava's `Joiner` to join a list and `Splitter` to parse one. Like this: ```java private static final Joiner COMMA = Joiner.on(','); private static final Joiner COLON = Joiner.on(','); ... String asString = COMMA.join(Iterables.transform(idMap.entrySet(), entry -> COLON.join(entry.getKey().getId(), entry.getValue().toString()) )); return ByteBuffer.wrap(asString.getBytes(StandardCharsets.UTF_8)); ``` ---------------------------------------------------------------- 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] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
