Updated SpanSubspanQuery for empty length.
diff --git a/src/main/java/de/ids_mannheim/korap/KorapQuery.java b/src/main/java/de/ids_mannheim/korap/KorapQuery.java
index f7e30c5..89f4492 100644
--- a/src/main/java/de/ids_mannheim/korap/KorapQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/KorapQuery.java
@@ -251,7 +251,7 @@
 			762,
 		        "Span references are currently not supported"
                     );
-		};
+		}; 
 
 		return new SpanMatchModifyQueryWrapper(
 		    this.fromJson(operands.get(0)), number
@@ -521,9 +521,17 @@
 	    }
 	    else if (json.has("spanRef")) {
 	        JsonNode spanRef = json.get("spanRef");
+	        int length=0;
+	        if (!spanRef.isArray() || spanRef.size() == 0)
+	            throw new QueryException(714, "Span reference needs a start position and a length parameters.");
+	        	        
+	        if (!spanRef.get(1).isMissingNode()){
+	            length = spanRef.get(1).asInt();
+	        }
+	            
             return new SpanSubspanQueryWrapper(
                     fromJson(operands.get(0)), spanRef.get(0).asInt(),
-                    spanRef.get(1).asInt());
+                    length); 
 //		throw new QueryException(
 //		    762,
 //		    "Span references are currently not supported"
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/SubSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/SubSpans.java
index 7af989a..c95afcc 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/SubSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/SubSpans.java
@@ -11,8 +11,10 @@
 import de.ids_mannheim.korap.query.SpanSubspanQuery;
 
 /**
- * Enumeration of SubSpans, which are parts of another Spans. The SubSpans are
- * specified with a start offset relative to the original span and a length.
+ * Enumeration of SubSpans, which are parts of another Spans. The
+ * SubSpans are specified with a start offset relative to the original
+ * span and a length. If the length is unspecified or 0, the end
+ * position of the subspans is the same as that of the original spans.
  * 
  * @author margaretha
  * 
@@ -22,18 +24,19 @@
     private int startOffset, length;
 
     /**
-     * Constructs SubSpans for the given {@link SpanSubspanQuery} specifiying
-     * the start offset and the length of the subspans.
+     * Constructs SubSpans for the given {@link SpanSubspanQuery}
+     * specifiying the start offset and the length of the subspans.
      * 
-     * @param subspanQuery a SpanSubspanQuery
+     * @param subspanQuery
+     *            a SpanSubspanQuery
      * @param context
      * @param acceptDocs
      * @param termContexts
      * @throws IOException
      */
-    public SubSpans(SpanSubspanQuery subspanQuery, AtomicReaderContext context,
-            Bits acceptDocs, Map<Term, TermContext> termContexts)
-            throws IOException {
+    public SubSpans (SpanSubspanQuery subspanQuery,
+            AtomicReaderContext context, Bits acceptDocs,
+            Map<Term, TermContext> termContexts) throws IOException {
         super(subspanQuery, context, acceptDocs, termContexts);
         this.startOffset = subspanQuery.getStartOffset();
         this.length = subspanQuery.getLength();
@@ -49,8 +52,8 @@
     /**
      * Advances the SubSpans to the next match.
      * 
-     * @return <code>true</code> if a match is found, <code>false</code>
-     *         otherwise.
+     * @return <code>true</code> if a match is found,
+     *         <code>false</code> otherwise.
      * @throws IOException
      */
     private boolean advance() throws IOException {
@@ -83,11 +86,15 @@
             }
         }
 
-        matchEndPosition = matchStartPosition + this.length;
-        if (matchEndPosition > firstSpans.end()) {
+        if (this.length > 0) {
+            matchEndPosition = matchStartPosition + this.length;
+            if (matchEndPosition > firstSpans.end()) {
+                matchEndPosition = firstSpans.end();
+            }
+        }
+        else {
             matchEndPosition = firstSpans.end();
         }
-
         matchPayload = firstSpans.getPayload();
         matchDocNumber = firstSpans.doc();
         return true;
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestSubSpanIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestSubSpanIndex.java
index 58f88f5..4aff0bf 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestSubSpanIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestSubSpanIndex.java
@@ -9,7 +9,6 @@
 import org.junit.Test;
 
 import de.ids_mannheim.korap.KorapIndex;
-import de.ids_mannheim.korap.KorapMatch;
 import de.ids_mannheim.korap.KorapResult;
 import de.ids_mannheim.korap.query.DistanceConstraint;
 import de.ids_mannheim.korap.query.SpanDistanceQuery;
@@ -20,7 +19,7 @@
     KorapResult kr;
     KorapIndex ki;
 
-    public TestSubSpanIndex() throws IOException {
+    public TestSubSpanIndex () throws IOException {
         ki = new KorapIndex();
         ki.addDocFile(getClass().getResource("/wiki/00001.json.gz").getFile(),
                 true);
@@ -29,10 +28,10 @@
 
     @Test
     public void testCase1() throws IOException {
-        SpanDistanceQuery sdq = new SpanDistanceQuery(
-                new SpanTermQuery(new Term("tokens", "tt/p:NN")), 
-                new SpanTermQuery(new Term("tokens", "tt/p:VAFIN")), 
-                new DistanceConstraint(5, 5, true,false), true);
+        SpanDistanceQuery sdq = new SpanDistanceQuery(new SpanTermQuery(
+                new Term("tokens", "tt/p:NN")), new SpanTermQuery(new Term(
+                "tokens", "tt/p:VAFIN")), new DistanceConstraint(5, 5, true,
+                false), true);
 
         SpanSubspanQuery ssq = new SpanSubspanQuery(sdq, 0, 2, true);
         kr = ki.search(ssq, (short) 10);
@@ -60,11 +59,12 @@
 
     @Test
     public void testCase2() {
-        SpanDistanceQuery sdq = new SpanDistanceQuery(
-                new SpanTermQuery(new Term("tokens", "tt/p:NN")), 
-                new SpanTermQuery(new Term("tokens", "tt/p:VAFIN")), 
-                new DistanceConstraint(5, 5, true,false), true);
-        
+        SpanDistanceQuery sdq = new SpanDistanceQuery(new SpanTermQuery(
+                new Term("tokens", "tt/p:NN")), new SpanTermQuery(new Term(
+                "tokens", "tt/p:VAFIN")), new DistanceConstraint(5, 5, true,
+                false), true);
+
+        // the subspan length is longer than the span length
         SpanSubspanQuery ssq = new SpanSubspanQuery(sdq, 0, 7, true);
         kr = ki.search(ssq, (short) 10);
 
@@ -72,20 +72,47 @@
         assertEquals(41, kr.getMatch(0).getEndPos());
         assertEquals(179, kr.getMatch(1).getStartPos());
         assertEquals(185, kr.getMatch(1).getEndPos());
-        
+
+        // the subspan start is before the span start
         ssq = new SpanSubspanQuery(sdq, -7, 4, true);
         kr = ki.search(ssq, (short) 10);
-        
+
         assertEquals((long) 8, kr.getTotalResults());
         assertEquals(35, kr.getMatch(0).getStartPos());
         assertEquals(39, kr.getMatch(0).getEndPos());
         assertEquals(179, kr.getMatch(1).getStartPos());
         assertEquals(183, kr.getMatch(1).getEndPos());
-        
-        /* for (KorapMatch km : kr.getMatches()){
-         System.out.println(km.getStartPos() +","+km.getEndPos()
-         +km.getSnippetBrackets()); }*/
-         
+
+    }
+
+    // Length 0
+    @Test
+    public void testCase3() {
+        SpanDistanceQuery sdq = new SpanDistanceQuery(new SpanTermQuery(
+                new Term("tokens", "tt/p:NN")), new SpanTermQuery(new Term(
+                "tokens", "tt/p:VAFIN")), new DistanceConstraint(5, 5, true,
+                false), true);
+
+        SpanSubspanQuery ssq = new SpanSubspanQuery(sdq, 3, 0, true);
+        kr = ki.search(ssq, (short) 10);
+
+        assertEquals(38, kr.getMatch(0).getStartPos());
+        assertEquals(41, kr.getMatch(0).getEndPos());
+        assertEquals(182, kr.getMatch(1).getStartPos());
+        assertEquals(185, kr.getMatch(1).getEndPos());
+
+        ssq = new SpanSubspanQuery(sdq, -2, 0, true);
+        kr = ki.search(ssq, (short) 10);
+
+        assertEquals(39, kr.getMatch(0).getStartPos());
+        assertEquals(41, kr.getMatch(0).getEndPos());
+        assertEquals(183, kr.getMatch(1).getStartPos());
+        assertEquals(185, kr.getMatch(1).getEndPos());
+
+        // for (KorapMatch km : kr.getMatches()) {
+        // System.out.println(km.getStartPos() + "," + km.getEndPos()
+        // + km.getSnippetBrackets());
+        // }
     }
 
 }
diff --git a/src/test/java/de/ids_mannheim/korap/query/TestSpanSubspanQueryJSON.java b/src/test/java/de/ids_mannheim/korap/query/TestSpanSubspanQueryJSON.java
index e1e473d..c9f6d56 100644
--- a/src/test/java/de/ids_mannheim/korap/query/TestSpanSubspanQueryJSON.java
+++ b/src/test/java/de/ids_mannheim/korap/query/TestSpanSubspanQueryJSON.java
@@ -21,6 +21,26 @@
                 .getFile();
         SpanQueryWrapper sqwi = getJSONQuery(filepath);
         SpanQuery sq = sqwi.toQuery();
+        assertEquals(sq.toString(),
+                "subspan(spanContain(<tokens:s />, tokens:tt/l:Haus),1,4)");
+    }
+
+    @Test
+    public void testCase2() throws QueryException {
+        String filepath = getClass().getResource("/queries/submatch2.jsonld")
+                .getFile();
+        SpanQueryWrapper sqwi = getJSONQuery(filepath);
+        SpanQuery sq = sqwi.toQuery();
         assertEquals(sq.toString(), "subspan(<tokens:s />,1,4)");
     }
+
+    public void testCase3() throws QueryException {
+        String filepath = getClass().getResource("/queries/submatch3.jsonld")
+                .getFile();
+        SpanQueryWrapper sqwi = getJSONQuery(filepath);
+        SpanQuery sq = sqwi.toQuery();
+        assertEquals(sq.toString(), "subspan(<tokens:s />,1,0)");
+
+    }
+
 }
diff --git a/src/test/resources/queries/submatch.jsonld b/src/test/resources/queries/submatch.jsonld
index 36153eb..4baa073 100644
--- a/src/test/resources/queries/submatch.jsonld
+++ b/src/test/resources/queries/submatch.jsonld
@@ -11,15 +11,35 @@
    ],
    "collection":null,
    "query":{
-      "@type":"korap:reference",
-      "operation":"operation:focus",
-      "operands":[
+      "@type" : "korap:reference",
+      "operands" : [
          {
-            "@type":"korap:span",
-            "key":"s"
+            "@type" : "korap:group",
+            "frame" : "frame:contains",
+            "frames" : [
+               "frames:contains"
+            ],
+            "operands" : [
+               {
+                  "@type" : "korap:span",
+                  "key" : "s"
+               },
+               {
+                  "@type" : "korap:token",
+                  "wrap" : {
+                     "@type" : "korap:term",
+                     "foundry" : "tt",
+                     "key" : "Haus",
+                     "layer" : "lemma",
+                     "match" : "match:eq"
+                  }
+               }
+            ],
+            "operation" : "operation:position"
          }
       ],
-      "spanRef":[
+      "operation" : "operation:focus",
+      "spanRef" : [
          1,
          4
       ]
diff --git a/src/test/resources/queries/submatch2.jsonld b/src/test/resources/queries/submatch2.jsonld
new file mode 100644
index 0000000..45a3155
--- /dev/null
+++ b/src/test/resources/queries/submatch2.jsonld
@@ -0,0 +1,30 @@
+{
+   "@context":"http://ids-mannheim.de/ns/KorAP/json-ld/v0.2/context.jsonld",
+   "errors":[
+
+   ],
+   "warnings":[
+
+   ],
+   "messages":[
+
+   ],
+   "collection":null,
+   "query":{
+      "@type" : "korap:reference",
+      "operands" : [
+         {
+            "@type" : "korap:span",
+            "key" : "s"
+         }
+      ],
+      "operation" : "operation:focus",
+      "spanRef" : [
+         1,
+         4
+      ]
+   },
+   "meta":{
+
+   }
+}
diff --git a/src/test/resources/queries/submatch3.jsonld b/src/test/resources/queries/submatch3.jsonld
new file mode 100644
index 0000000..6363de6
--- /dev/null
+++ b/src/test/resources/queries/submatch3.jsonld
@@ -0,0 +1,29 @@
+{
+   "@context":"http://ids-mannheim.de/ns/KorAP/json-ld/v0.2/context.jsonld",
+   "errors":[
+
+   ],
+   "warnings":[
+
+   ],
+   "messages":[
+
+   ],
+   "collection":null,
+   "query":{
+      "@type" : "korap:reference",
+      "operands" : [
+         {
+            "@type" : "korap:span",
+            "key" : "s"
+         }
+      ],
+      "operation" : "operation:focus",
+      "spanRef" : [
+         1
+      ]
+   },
+   "meta":{
+
+   }
+}