Skip to content

Commit

Permalink
Nested arrays cause 'item-end when a 'x' was expected" error (#476)
Browse files Browse the repository at this point in the history
* Address issue with nested arrays

Some schemas with nested arrays ( e.g., NestedArray.avsc ) generate the following
error when deserialized with fastserde:

Attempt to process a item-end when a string was expected.

This PR adds a test case and addresses the issue

* Update test

* Fix fastserdetest.avsc

* Update fastserdetest.java

* Remove code to verify test pipeline fails

* Make name consistent

* Fix FastDeserializerGeneratorBase

* Use helper functions so test builds on all versions

* Fix case of ItemName

* Remove dependency of getSymbolPrintName

* Remove unnecessary whitespace change

* Add javadoc

---------

Co-authored-by: Andy Sautins <[email protected]>
  • Loading branch information
asautins and Andy Sautins authored Apr 18, 2023
1 parent 6be886d commit ca6496c
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"namespace": "com.linkedin.avro.fastserde.generated.avro",
"name": "NestedArrayTest",
"type": "record",
"fields": [
{
"name": "NestedArrayItems",
"type": {
"type": "array",
"items": {
"type": "array",
"items": {
"name": "NestedArrayItem",
"type": "record",
"fields": [
{
"name": "ItemName",
"type": "string"
}
]
}
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.linkedin.avro.fastserde;

import com.linkedin.avro.fastserde.generated.avro.NestedArrayItem;
import com.linkedin.avro.fastserde.generated.avro.NestedArrayTest;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.avro.io.Decoder;
import org.testng.Assert;
import org.testng.annotations.Test;


public class FastNestedArrayTest {

@Test
public void testExample() throws Exception {

NestedArrayTest nestedArrayTest = new NestedArrayTest();

List<NestedArrayItem> items = new ArrayList<>();
NestedArrayItem item = new NestedArrayItem();
FastSerdeTestsSupport.setField(item, "ItemName", "itemName");
FastSerdeTestsSupport.setField(nestedArrayTest, "NestedArrayItems", Collections.singletonList(items));

Decoder decoder = FastSerdeTestsSupport.specificDataAsDecoder(nestedArrayTest, NestedArrayTest.SCHEMA$);

FastSpecificDatumReader<NestedArrayTest> fastSpecificDatumReader =
new FastSpecificDatumReader<>(NestedArrayTest.SCHEMA$);
FastDeserializer<NestedArrayTest> fastDeserializer =
fastSpecificDatumReader.getFastDeserializer().get();

NestedArrayTest actual = fastDeserializer.deserialize(decoder);

Assert.assertEquals(actual, nestedArrayTest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.ListIterator;
import java.util.Optional;
import org.apache.avro.Schema;
import com.linkedin.avro.fastserde.backport.Symbol;
import org.apache.avro.util.Utf8;
Expand Down Expand Up @@ -71,6 +72,17 @@ protected static void assignBlockToBody(Object codeContainer, JBlock body) {

public abstract FastDeserializer<T> generateDeserializer();

/**
* Retrieve the symbols associated with the FieldAction necessary
* to generate code to populate the field.
*
* When processing a nested array ( an array directly inside
* another array ) return the symbol.production to navigate the
* nested array properly.
*
* @param action FieldAction being process
* @return list of symbols associated with the input FieldAction
*/
protected ListIterator<Symbol> actionIterator(FieldAction action) {
ListIterator<Symbol> actionIterator = null;

Expand All @@ -85,6 +97,14 @@ protected ListIterator<Symbol> actionIterator(FieldAction action) {
while (actionIterator.hasNext()) {
Symbol symbol = actionIterator.next();

if (Symbol.Kind.REPEATER.equals(symbol.kind) &&
Symbol.Kind.TERMINAL.equals(((Symbol.Repeater)symbol).end)
&& "array-end"
.equals(((Symbol.Repeater)symbol).end.toString())) {
actionIterator = Arrays.asList(reverseSymbolArray(symbol.production)).listIterator();
break;
}

if (symbol instanceof Symbol.ErrorAction) {
throw new FastDeserializerGeneratorException(((Symbol.ErrorAction) symbol).msg);
}
Expand Down

0 comments on commit ca6496c

Please sign in to comment.