match creation with spanExpansion fails

Change-Id: Ibfcdf6c5911360e5e2b0984060909a87c5dc9a4f
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRepetitionQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRepetitionQueryWrapper.java
index 8a8a008..3f78fab 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRepetitionQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRepetitionQueryWrapper.java
@@ -62,7 +62,7 @@
             this.isEmpty = true;
 
         // Subquery may be an empty token
-        if (this.subquery.isNull()) {
+        if (subquery.isNull()) {
             this.isNull = true;
             return;
         }
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestSpanExpansionIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestSpanExpansionIndex.java
index 7869a7c..c618521 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestSpanExpansionIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestSpanExpansionIndex.java
@@ -17,11 +17,13 @@
 
 import de.ids_mannheim.korap.KrillIndex;
 import de.ids_mannheim.korap.KrillQuery;
+import de.ids_mannheim.korap.query.QueryBuilder;
 import de.ids_mannheim.korap.query.SpanElementQuery;
 import de.ids_mannheim.korap.query.SpanExpansionQuery;
 import de.ids_mannheim.korap.query.SpanRepetitionQuery;
 import de.ids_mannheim.korap.query.wrap.SpanQueryWrapper;
 import de.ids_mannheim.korap.response.Result;
+import de.ids_mannheim.korap.response.Match;
 import de.ids_mannheim.korap.util.QueryException;
 
 public class TestSpanExpansionIndex {
@@ -349,6 +351,47 @@
     }
 
 
+
+
+    /**
+     * Query rewrite bug
+     * 
+     * @throws IOException
+     * */
+    @Test
+    public void testExpansionQueryBug3 () throws IOException, QueryException {
+        KrillIndex ki = new KrillIndex();
+        ki.addDoc(createFieldDoc0()); // same doc
+        ki.addDoc(createFieldDoc1()); // only not clause
+        ki.addDoc(createFieldDoc2()); // only main clause
+        ki.commit();
+
+        String json = readFile(getClass().getResource(
+            "/queries/bugs/expansion_bug_3.jsonld").getFile()
+        );
+        KrillQuery kq = new KrillQuery("base");
+        SpanQuery sq = kq.fromJson(json).toQuery();
+        /*
+        assertEquals(sq.toString(),
+                     "spanExpansion(base:s:a, []{0, 100}, right)");
+        */
+        kr = ki.search(sq, (short) 11);
+        assertEquals((long) 1111, kr.getTotalResults());
+
+        assertEquals("[c]eccecd ...",kr.getMatch(0).getSnippetBrackets());
+        assertEquals("[ce]ccecde ...",kr.getMatch(1).getSnippetBrackets());
+        assertEquals("",kr.getMatch(10).getSnippetBrackets());
+        assertEquals("",kr.getMatch(11).getSnippetBrackets());
+        /*
+        assertEquals("[ce]cceecde",kr.getMatch(1).getSnippetBrackets());
+        assertEquals("[cecceecde]",kr.getMatch(10).getSnippetBrackets());
+        assertEquals("[bb]ccdd",kr.getMatch(11).getSnippetBrackets());
+        */
+    }
+
+
+
+
     private FieldDocument createFieldDoc0 () {
         FieldDocument fd = new FieldDocument();
         fd.addString("ID", "doc-0");
diff --git a/src/test/java/de/ids_mannheim/korap/query/TestKrillQuery.java b/src/test/java/de/ids_mannheim/korap/query/TestKrillQuery.java
index e4cd6ac..e379301 100644
--- a/src/test/java/de/ids_mannheim/korap/query/TestKrillQuery.java
+++ b/src/test/java/de/ids_mannheim/korap/query/TestKrillQuery.java
@@ -20,6 +20,8 @@
 @RunWith(JUnit4.class)
 public class TestKrillQuery {
 
+    // TODO: Better rename this to Builder
+
     @Test
     public void korapQuerySegment () throws QueryException {
         SpanQuery sq = new QueryBuilder("field1").seg("a").with("b").toQuery();
@@ -304,6 +306,15 @@
 
 
     @Test
+    public void KorapSequenceWithEmptyRepetitionQuery () throws QueryException {
+        QueryBuilder kq = new QueryBuilder("field");
+        SpanQuery sq = kq.seq(kq.seg("try")).append(kq.repeat(kq.empty(),0,100)).toQuery();
+        assertEquals("spanExpansion(field:try, []{0, 100}, right)",sq.toString());
+    };
+
+
+
+    @Test
     public void KorapWithinQuery1 () throws QueryException {
         QueryBuilder kq = new QueryBuilder("field");
         SpanQuery sq = kq.contains(kq.seg("test"), kq.seg("test2")).toQuery();
diff --git a/src/test/java/de/ids_mannheim/korap/query/TestKrillQueryJSON.java b/src/test/java/de/ids_mannheim/korap/query/TestKrillQueryJSON.java
index 22c6134..d44a260 100644
--- a/src/test/java/de/ids_mannheim/korap/query/TestKrillQueryJSON.java
+++ b/src/test/java/de/ids_mannheim/korap/query/TestKrillQueryJSON.java
@@ -537,6 +537,23 @@
     };
 
 
+    @Test
+    public void queryJSONinfiniteExpansion () throws QueryException {
+        // der []*
+        try {
+            String json = getString(getClass().getResource(
+                    "/queries/bugs/expansion_bug_3.jsonld").getFile());
+            KrillQuery kq = new KrillQuery("tokens");
+
+            assertEquals(kq.fromJson(json).toQuery().toString(),
+                    "spanExpansion(tokens:s:c, []{0, 100}, right)");
+        }
+        catch (QueryException e) {
+            fail(e.getMessage());
+        };
+    };
+
+
     public static String getString (String path) {
         StringBuilder contentBuilder = new StringBuilder();
         try {
diff --git a/src/test/resources/queries/bugs/expansion_bug_3.jsonld b/src/test/resources/queries/bugs/expansion_bug_3.jsonld
new file mode 100644
index 0000000..966dbd8
--- /dev/null
+++ b/src/test/resources/queries/bugs/expansion_bug_3.jsonld
@@ -0,0 +1,41 @@
+{
+  "@context":"http://korap.ids-mannheim.de/ns/koral/0.3/context.jsonld",
+  "query":{
+    "@type":"koral:group",
+    "operation":"operation:sequence",
+    "operands":[
+      {
+	"@type":"koral:token",
+	"wrap":{
+	  "@type":"koral:term",
+	  "layer":"s",
+	  "key":"c",
+	  "match":"match:eq"
+	}
+      },
+      {
+	"@type":"koral:group",
+	"operation":"operation:repetition",
+	"operands":[
+	  {
+	    "@type":"koral:token"
+	  }
+	],
+	"boundary":{
+	  "@type":"koral:boundary",
+	  "min":0
+	},
+	"min":0
+      }
+    ]
+  },
+  "meta":{
+    "startPage":1,
+    "count":25,
+    "cutOff":true,
+    "context":{
+      "left":["t",40],
+      "right":["t",40]
+    }
+  }
+}