rdsr commented on a change in pull request #207: Add external schema mappings for files written with name-based schemas #40 URL: https://github.com/apache/incubator-iceberg/pull/207#discussion_r296503036
########## File path: core/src/test/java/org/apache/iceberg/avro/TestExtSchemaMapping.java ########## @@ -0,0 +1,111 @@ +/* + * 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.avro; + +import com.google.common.base.Splitter; +import com.google.common.collect.Iterables; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; +import java.io.File; +import java.io.IOException; +import java.util.Map; +import java.util.Set; +import org.apache.avro.file.DataFileWriter; +import org.apache.avro.generic.GenericData; +import org.apache.avro.generic.GenericDatumWriter; +import org.apache.avro.generic.GenericRecord; +import org.apache.iceberg.Files; +import org.apache.iceberg.Schema; +import org.apache.iceberg.types.TypeUtil; +import org.junit.Assert; +import org.junit.Ignore; +import org.junit.Test; + +public class TestExtSchemaMapping extends TestReadProjection { + private static final Splitter DOT = Splitter.on("."); + + @Override + protected GenericData.Record writeAndRead(String desc, + Schema writeSchema, + Schema readSchema, + GenericData.Record record) throws IOException { + + // We should get this external mapping from table properties, but for now. I'm building this using file schema. + Map<String, Integer> namesToId = nameToIds(writeSchema); + org.apache.avro.Schema fileSchema = removeIds(writeSchema); + // Write data without field ids, not using Iceberg writers as + // they would inject field ids as part of the schema header + File file = writeRecord(fileSchema, record, desc); + return readRecord(file, readSchema, namesToId); + } + + private static GenericData.Record readRecord(File file, Schema readSchema, Map<String, Integer> namesToId) { + AvroIterable<GenericData.Record> records = Avro.read(Files.localInput(file)) + .project(readSchema) + .extNameToId(namesToId) + .build(); + return Iterables.getOnlyElement(records); + } + + @Test + @Ignore Review comment: Let me know if it would be simpler to have a standalone test suite for external schema mapping instead of the current state where I extend `TestReadProjection` and ignore a couple tests ---------------------------------------------------------------- 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]
