You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by bl...@apache.org on 2016/03/15 16:48:06 UTC

avro git commit: AVRO-1667: Fix parser grammar flattening for recursive cases. Contributed by Zoltan Farkas.

Repository: avro
Updated Branches:
  refs/heads/master 62ad11dcf -> 482adcfa1


AVRO-1667: Fix parser grammar flattening for recursive cases. Contributed by Zoltan Farkas.

Recursive records use a Fixup class to copy sequences of parser Symbols
because the final sequence isn't yet known. But these weren't being
copied when the sequences being fixed up were copied, which caused the
final grammar to have nulls in some cases.


Project: http://git-wip-us.apache.org/repos/asf/avro/repo
Commit: http://git-wip-us.apache.org/repos/asf/avro/commit/482adcfa
Tree: http://git-wip-us.apache.org/repos/asf/avro/tree/482adcfa
Diff: http://git-wip-us.apache.org/repos/asf/avro/diff/482adcfa

Branch: refs/heads/master
Commit: 482adcfa1857033f2a76b07b429a266b0231f433
Parents: 62ad11d
Author: Ryan Blue <bl...@apache.org>
Authored: Sun Mar 6 19:09:58 2016 -0800
Committer: Ryan Blue <bl...@apache.org>
Committed: Tue Mar 15 08:47:31 2016 -0700

----------------------------------------------------------------------
 CHANGES.txt                                     |  3 +
 .../java/org/apache/avro/io/parsing/Symbol.java | 16 ++++-
 .../org/apache/avro/io/parsing/SymbolTest.java  | 75 ++++++++++++++++++++
 3 files changed, 93 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/avro/blob/482adcfa/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 00d9deb..e1a2611 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -22,6 +22,9 @@ Trunk (not yet released)
 
     AVRO-1799: Fix GenericRecord#toString ByteBuffer handling. (blue)
 
+    AVRO-1667: Fix parser grammar flattening for recursive cases.
+    (Zoltan Farkas via blue)
+
 Avro 1.8.0 (22 January 2016)
 
   INCOMPATIBLE CHANGES

http://git-wip-us.apache.org/repos/asf/avro/blob/482adcfa/lang/java/avro/src/main/java/org/apache/avro/io/parsing/Symbol.java
----------------------------------------------------------------------
diff --git a/lang/java/avro/src/main/java/org/apache/avro/io/parsing/Symbol.java b/lang/java/avro/src/main/java/org/apache/avro/io/parsing/Symbol.java
index 08a9d14..80ae644 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/io/parsing/Symbol.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/io/parsing/Symbol.java
@@ -68,7 +68,7 @@ public abstract class Symbol {
    * problem, we initialize the symbol with an array of nulls. Later we
    * fill the symbols. Not clean, but works. The other option is to not have
    * this field a final. But keeping it final and thus keeping symbol immutable
-   * gives some confort. See various generators how we generate records.
+   * gives some comfort. See various generators how we generate records.
    */
   public final Symbol[] production;
   /**
@@ -190,6 +190,10 @@ public abstract class Symbol {
         List<Fixup> l = map2.get(s);
         if (l == null) {
           System.arraycopy(p, 0, out, j, p.length);
+          // Copy any fixups that will be applied to p to add missing symbols
+          for (List<Fixup> fixups : map2.values()) {
+            copyFixups(fixups, out, j, p);
+          }
         } else {
           l.add(new Fixup(out, j));
         }
@@ -200,6 +204,16 @@ public abstract class Symbol {
     }
   }
 
+  private static void copyFixups(List<Fixup> fixups, Symbol[] out, int outPos,
+                                 Symbol[] toCopy) {
+    for (int i = 0, n = fixups.size(); i < n; i += 1) {
+      Fixup fixup = fixups.get(i);
+      if (fixup.symbols == toCopy) {
+        fixups.add(new Fixup(out, fixup.pos + outPos));
+      }
+    }
+  }
+
   /**
    * Returns the amount of space required to flatten the given
    * sub-array of symbols.

http://git-wip-us.apache.org/repos/asf/avro/blob/482adcfa/lang/java/avro/src/test/java/org/apache/avro/io/parsing/SymbolTest.java
----------------------------------------------------------------------
diff --git a/lang/java/avro/src/test/java/org/apache/avro/io/parsing/SymbolTest.java b/lang/java/avro/src/test/java/org/apache/avro/io/parsing/SymbolTest.java
new file mode 100644
index 0000000..ac372bb
--- /dev/null
+++ b/lang/java/avro/src/test/java/org/apache/avro/io/parsing/SymbolTest.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright 2016 The Apache Software Foundation.
+ *
+ * Licensed 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.avro.io.parsing;
+
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Set;
+import junit.framework.Assert;
+import org.apache.avro.Schema;
+import org.junit.Test;
+
+/**
+ * Unit test to verify that recursive schemas are flattened correctly.
+ * See AVRO-1667.
+ */
+public class SymbolTest {
+
+  private static final String SCHEMA = "{\"type\":\"record\",\"name\":\"SampleNode\","
+      + "\"namespace\":\"org.spf4j.ssdump2.avro\",\n" +
+      " \"fields\":[\n" +
+      "    {\"name\":\"count\",\"type\":\"int\",\"default\":0},\n" +
+      "    {\"name\":\"subNodes\",\"type\":\n" +
+      "       {\"type\":\"array\",\"items\":{\n" +
+      "           \"type\":\"record\",\"name\":\"SamplePair\",\n" +
+      "           \"fields\":[\n" +
+      "              {\"name\":\"method\",\"type\":\n" +
+      "                  {\"type\":\"record\",\"name\":\"Method\",\n" +
+      "                  \"fields\":[\n" +
+      "                     {\"name\":\"declaringClass\",\"type\":{\"type\":\"string\",\"avro.java.string\":\"String\"}},\n" +
+      "                     {\"name\":\"methodName\",\"type\":{\"type\":\"string\",\"avro.java.string\":\"String\"}}\n" +
+      "                  ]}},\n" +
+      "              {\"name\":\"node\",\"type\":\"SampleNode\"}]}}}]}";
+
+  @Test
+  public void testSomeMethod() throws IOException {
+    Schema schema = new Schema.Parser().parse(SCHEMA);
+
+    Symbol root = Symbol.root(new ResolvingGrammarGenerator()
+        .generate(schema, schema, new HashMap<ValidatingGrammarGenerator.LitS, Symbol>()));
+    validateNonNull(root, new HashSet<Symbol>());
+  }
+
+  private static void validateNonNull(final Symbol symb, Set<Symbol> seen) {
+    if (seen.contains(symb)) {
+      return;
+    } else {
+      seen.add(symb);
+    }
+    if (symb.production != null) {
+      for (Symbol s : symb.production) {
+        if (s == null) {
+          Assert.fail("invalid parsing tree should not contain nulls");
+        }
+        if (s.kind != Symbol.Kind.ROOT) {
+          validateNonNull(s, seen);
+        }
+      }
+    }
+  }
+}