rdblue commented on code in PR #11919: URL: https://github.com/apache/iceberg/pull/11919#discussion_r1905975033
########## core/src/main/java/org/apache/iceberg/avro/InternalWriter.java: ########## @@ -0,0 +1,74 @@ +/* + * 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.IOException; +import java.util.List; +import java.util.stream.Stream; +import org.apache.avro.Schema; +import org.apache.avro.io.Encoder; +import org.apache.iceberg.FieldMetrics; +import org.apache.iceberg.types.Type; + +/** + * A Writer that consumes Iceberg's internal in-memory object model. + * + * <p>Iceberg's internal in-memory object model produces the types defined in {@link + * Type.TypeID#javaClass()}. + */ +public class InternalWriter<T> implements MetricsAwareDatumWriter<T> { + private ValueWriter<T> writer = null; + + public static <D> InternalWriter<D> create(Schema schema) { + return new InternalWriter<>(schema); + } + + InternalWriter(Schema schema) { + setSchema(schema); + } + + @Override + @SuppressWarnings("unchecked") + public void setSchema(Schema schema) { + this.writer = (ValueWriter<T>) AvroSchemaVisitor.visit(schema, new GenericAvroSchemaVisitor()); + } + + @Override + public void write(T datum, Encoder out) throws IOException { + writer.write(datum, out); + } + + @Override + public Stream<FieldMetrics> metrics() { + return writer.metrics(); + } + + private static class GenericAvroSchemaVisitor extends BaseAvroSchemaVisitor { + + @Override + protected ValueWriter<?> createRecordWriter(List<ValueWriter<?>> fields) { + return ValueWriters.struct(fields); + } + + @Override + protected ValueWriter<?> fixedWriter(int size) { + return ValueWriters.byteBuffers(); Review Comment: This isn't correct for the write path. In the read path, we want to broadly accept values and let validation happen later, so reading directly as `ByteBuffer` and not checking the fixed length is okay. However in the write path we do need to validate that the `ByteBuffer` being written is the expected fixed size, which `ValueWriters.byteBuffers()` (`ByteBufferWriter`) doesn't do. However, `FixedWriter` and `GenericFixedWriter` do check: ```java @Override public void write(byte[] bytes, Encoder encoder) throws IOException { Preconditions.checkArgument( bytes.length == length, "Cannot write byte array of length %s as fixed[%s]", bytes.length, length); encoder.writeFixed(bytes); } ``` I think this needs a new `ByteBufferWriter` for fixed buffers: ```java public static ValueWriter<ByteBuffer> fixedBuffers(int length) { return new FixedByteBufferWriter(length); } private static class FixedByteBufferWriter implements ValueWriter<ByteBuffer> { private final int length; private FixedByteBufferWriter(int length) { this.length = length; } @Override public void write(ByteBuffer bytes, Encoder encoder) throws IOException { Preconditions.checkArgument( bytes.remaining() == length, "Cannot write byte buffer of length %s as fixed[%s]", bytes.remaining(), length); encoder.writeBytes(bytes); } } ``` -- 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. To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org