rdblue commented on a change in pull request #1477: URL: https://github.com/apache/iceberg/pull/1477#discussion_r513103811
########## File path: core/src/main/java/org/apache/iceberg/avro/AvroEncoderUtil.java ########## @@ -0,0 +1,105 @@ +/* + * 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 java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import org.apache.avro.LogicalTypes; +import org.apache.avro.Schema; +import org.apache.avro.io.BinaryDecoder; +import org.apache.avro.io.BinaryEncoder; +import org.apache.avro.io.DatumReader; +import org.apache.avro.io.DatumWriter; +import org.apache.avro.io.DecoderFactory; +import org.apache.avro.io.EncoderFactory; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; + +public class AvroEncoderUtil { + + private AvroEncoderUtil() { + } + + static { + LogicalTypes.register(LogicalMap.NAME, schema -> LogicalMap.get()); + } + + private static final int VERSION = 1; + private static final byte[] MAGIC_BYTES = new byte[] {'a', 'V', 'R', VERSION}; + + private static byte[] encodeInt(int value) { + return ByteBuffer.allocate(4).putInt(value).array(); + } + + private static int decodeInt(byte[] value) { + return ByteBuffer.wrap(value).getInt(); + } + + public static <T> byte[] encode(T datum, Schema avroSchema) throws IOException { Review comment: Thanks for reminding me about the issue with the generic reader/writer. I agree that requires us to make these public for now. > IcebergEncoder and IcebergDecoder, seems it don't encode the writeSchema into the binary These encode a schema fingerprint that is used to load the correct schema. What we would need to do is keep track of all of the schemas that have been used for `ManifestFile` and build a lookup table from fingerprint to schema. That keeps the size of the encoded record lower. I'm fine moving forward with what you have here. I think it is something that we can support for Flink jobs. And it is nice to encode the schema with the data. Then we don't have to supply the lookup by fingerprint. > Besides, it need a user-provided writeSchema to read the avro binary while in our flink states we only have the binary We will still need to pass in a read schema in the future. Avro reads using the index of a column in the read schema to set the value. So a schema with columns (a, b, c) will turn into something like this: ``` record = newRecord() record.set(0, binaryDecoder.readLong()) record.set(1, binaryDecoder.readString()) record.set(2, binaryDecoder.readDouble()) ``` If the column order in the current `ManifestFile` class doesn't match, then basing the indexes passed to `set` on the write schema will cause failures. A read schema allows us to build a resolver that translates to the correct positions. (That's done in [the StructReader](https://github.com/apache/iceberg/blob/master/core/src/main/java/org/apache/iceberg/avro/ValueReaders.java#L645-L649)). ---------------------------------------------------------------- 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]
