Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
package org.apache.arrow.adapter.avro.consumers;

import java.io.IOException;
import java.nio.ByteBuffer;
import org.apache.arrow.vector.VarCharVector;
import org.apache.avro.io.Decoder;
import org.apache.avro.util.Utf8;

/**
* Consumer which consume string type values from avro decoder. Write the data to {@link
* VarCharVector}.
*/
public class AvroStringConsumer extends BaseAvroConsumer<VarCharVector> {

private ByteBuffer cacheBuffer;
private Utf8 cacheUtf8;

/** Instantiate a AvroStringConsumer. */
public AvroStringConsumer(VarCharVector vector) {
Expand All @@ -36,9 +36,9 @@ public AvroStringConsumer(VarCharVector vector) {

@Override
public void consume(Decoder decoder) throws IOException {
// cacheBuffer is initialized null and create in the first consume,
// if its capacity < size to read, decoder will create a new one with new capacity.
cacheBuffer = decoder.readBytes(cacheBuffer);
vector.setSafe(currentIndex++, cacheBuffer, 0, cacheBuffer.limit());
// cacheUtf8 starts null and is reused; the Utf8 grows its backing array
// when needed, so only getByteLength() bytes are valid.
cacheUtf8 = decoder.readString(cacheUtf8);
vector.setSafe(currentIndex++, cacheUtf8.getBytes(), 0, cacheUtf8.getByteLength());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.arrow.adapter.avro.consumers.AvroStringConsumer;
import org.apache.arrow.vector.FieldVector;
import org.apache.arrow.vector.VarCharVector;
import org.apache.arrow.vector.VectorSchemaRoot;
Expand All @@ -34,6 +36,11 @@
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.io.BinaryDecoder;
import org.apache.avro.io.BinaryEncoder;
import org.apache.avro.io.DecoderFactory;
import org.apache.avro.io.EncoderFactory;
import org.apache.avro.io.ResolvingDecoder;
import org.junit.jupiter.api.Test;

public class AvroToArrowTest extends AvroTestBase {
Expand All @@ -49,6 +56,42 @@ public void testStringType() throws Exception {
checkPrimitiveResult(data, vector);
}

/**
* Regression for issue 832. {@code AvroStringConsumer} used {@code Decoder.readBytes}, so a
* {@link ResolvingDecoder} whose reader schema is a string threw {@code AvroTypeException}:
* "Attempt to process a bytes when a string was expected." Values are written with {@code
* Encoder.writeString} and include an empty string and a non-ASCII literal so the reused {@code
* Utf8} buffer is copied with the correct byte length.
*/
@Test
public void testStringTypeWithResolvingDecoder() throws Exception {
Schema schema = getSchema("test_primitive_string.avsc");
List<String> data = Arrays.asList("v1", "", "你好世界");

ByteArrayOutputStream out = new ByteArrayOutputStream();
BinaryEncoder encoder = EncoderFactory.get().directBinaryEncoder(out, null);
for (String value : data) {
encoder.writeString(value);
}
encoder.flush();

DecoderFactory factory = DecoderFactory.get();
BinaryDecoder binaryDecoder = factory.binaryDecoder(out.toByteArray(), null);
ResolvingDecoder resolvingDecoder = factory.resolvingDecoder(schema, schema, binaryDecoder);

VarCharVector vector = new VarCharVector("str", config.getAllocator());
try (AvroStringConsumer consumer = new AvroStringConsumer(vector)) {
vector.allocateNew();
for (int i = 0; i < data.size(); i++) {
resolvingDecoder.configure(binaryDecoder);
consumer.consume(resolvingDecoder);
resolvingDecoder.drain();
}
vector.setValueCount(data.size());
checkPrimitiveResult(data, vector);
}
}

@Test
public void testNullableStringType() throws Exception {
Schema schema = getSchema("test_nullable_string.avsc");
Expand Down
Loading