Return-Path: X-Original-To: apmail-avro-commits-archive@www.apache.org Delivered-To: apmail-avro-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id B2AFF19400 for ; Tue, 15 Mar 2016 15:48:06 +0000 (UTC) Received: (qmail 78826 invoked by uid 500); 15 Mar 2016 15:48:06 -0000 Delivered-To: apmail-avro-commits-archive@avro.apache.org Received: (qmail 78772 invoked by uid 500); 15 Mar 2016 15:48:06 -0000 Mailing-List: contact commits-help@avro.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@avro.apache.org Delivered-To: mailing list commits@avro.apache.org Received: (qmail 78727 invoked by uid 99); 15 Mar 2016 15:48:06 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 15 Mar 2016 15:48:06 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 0D6D3DFD43; Tue, 15 Mar 2016 15:48:06 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: blue@apache.org To: commits@avro.apache.org Message-Id: <1272bd2feb8d4a0d8f77f07c413af5fb@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: avro git commit: AVRO-1667: Fix parser grammar flattening for recursive cases. Contributed by Zoltan Farkas. Date: Tue, 15 Mar 2016 15:48:06 +0000 (UTC) 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 Authored: Sun Mar 6 19:09:58 2016 -0800 Committer: Ryan Blue 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 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 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 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())); + validateNonNull(root, new HashSet()); + } + + private static void validateNonNull(final Symbol symb, Set 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); + } + } + } + } +}