This is an automated email from the ASF dual-hosted git repository. andy pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/jena.git
The following commit(s) were added to refs/heads/main by this push: new a6e218e477 gh-1397 OpAsQuery fix for BIND within GRAPH new 4fcd7c3e01 Merge pull request #1403 from Aklakan/gh-1397 a6e218e477 is described below commit a6e218e47713786b1fe319a6bd8371fc1a8f1fcb Author: Claus Stadler <ravenarka...@googlemail.com> AuthorDate: Thu Jun 23 19:57:27 2022 +0200 gh-1397 OpAsQuery fix for BIND within GRAPH --- .../ElementTransformCleanGroupsOfOne.java | 33 +++++++++++----------- .../apache/jena/sparql/algebra/TestOpAsQuery.java | 4 +++ 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/syntax/syntaxtransform/ElementTransformCleanGroupsOfOne.java b/jena-arq/src/main/java/org/apache/jena/sparql/syntax/syntaxtransform/ElementTransformCleanGroupsOfOne.java index 9faeb576b5..b9f7911021 100644 --- a/jena-arq/src/main/java/org/apache/jena/sparql/syntax/syntaxtransform/ElementTransformCleanGroupsOfOne.java +++ b/jena-arq/src/main/java/org/apache/jena/sparql/syntax/syntaxtransform/ElementTransformCleanGroupsOfOne.java @@ -22,7 +22,7 @@ import java.util.List ; import org.apache.jena.sparql.syntax.* ; -/** +/** * Clean a SPARQL and ARQ syntax. This applies after using OpAsQuery. * <ul> * <li>Unwrap groups of one where they do not matter. @@ -34,9 +34,9 @@ public class ElementTransformCleanGroupsOfOne extends ElementTransformCopyBase { // Improvements: scan group elements to work out for non-reduceable adjacents. // These ones may clash with an adjeact one in the group above. // ElementTransformCleanGroupsOfOne -> ElementTransformCleanGroups - + public ElementTransformCleanGroupsOfOne() {} - + @Override public Element transform(ElementGroup eltGroup, List<Element> elts) { if ( elts.size() != 1 ) @@ -44,7 +44,8 @@ public class ElementTransformCleanGroupsOfOne extends ElementTransformCopyBase { Element elt = elts.get(0) ; if ( ( elt instanceof ElementTriplesBlock ) || ( elt instanceof ElementPathBlock ) || - ( elt instanceof ElementFilter ) ) + ( elt instanceof ElementFilter ) || + ( elt instanceof ElementBind ) ) return super.transform(eltGroup, elts) ; // No transformation. return elt ; } @@ -63,11 +64,11 @@ public class ElementTransformCleanGroupsOfOne extends ElementTransformCopyBase { elg.addElement(el); el = elg ; } - el2.addElement(el); + el2.addElement(el); } return el2 ; } - + // Special case: If Optional, and the original had a {{}} protected filter, keep {{}} // transform/ElementGroup has already run so undo if necessary. @Override @@ -79,16 +80,16 @@ public class ElementTransformCleanGroupsOfOne extends ElementTransformCopyBase { protectedElt.addElement(transformedElt); transformedElt = protectedElt ; } - - // Step 1 : does the original eltOptional has a {{}} RHS? + + // Step 1 : does the original eltOptional has a {{}} RHS? Element x = eltOptional.getOptionalElement() ; - + if ( ! ( x instanceof ElementGroup ) ) - // No. But it is not possible in written query syntax to have a nongroup as the RHS. + // No. But it is not possible in written query syntax to have a nongroup as the RHS. return super.transform(eltOptional, transformedElt) ; // So far - {}-RHS. ElementGroup eGroup = (ElementGroup)x ; - + // Is it {{}}? //ElementGroup inner = getGroupInGroup(x) ; if ( eGroup.size() != 1 ) @@ -101,21 +102,21 @@ public class ElementTransformCleanGroupsOfOne extends ElementTransformCopyBase { // Unbundle multiple levels. innerGroup = unwrap(innerGroup) ; boolean mustProtect = containsFilter(innerGroup) ; - + if ( mustProtect ) { // No need to check for {{}} in elt1 as the transform(ElementGroup) will have processed it. ElementGroup protectedElt = new ElementGroup() ; protectedElt.addElement(transformedElt); return new ElementOptional(protectedElt) ; - } + } // No need to protect - process as usual. return super.transform(eltOptional, transformedElt) ; } - + private boolean containsFilter(ElementGroup eltGroup) { return eltGroup.getElements().stream().anyMatch(el2 ->( el2 instanceof ElementFilter ) ) ; } - + // Removed layers of groups of one. Return inner most group. private ElementGroup unwrap(ElementGroup eltGroup) { if ( eltGroup.size() != 1 ) @@ -124,7 +125,7 @@ public class ElementTransformCleanGroupsOfOne extends ElementTransformCopyBase { if ( ! ( el instanceof ElementGroup ) ) return eltGroup ; ElementGroup eltGroup2 = (ElementGroup)el ; - return unwrap(eltGroup2) ; + return unwrap(eltGroup2) ; } } diff --git a/jena-arq/src/test/java/org/apache/jena/sparql/algebra/TestOpAsQuery.java b/jena-arq/src/test/java/org/apache/jena/sparql/algebra/TestOpAsQuery.java index 2fa959bfbb..b9924c9e13 100644 --- a/jena-arq/src/test/java/org/apache/jena/sparql/algebra/TestOpAsQuery.java +++ b/jena-arq/src/test/java/org/apache/jena/sparql/algebra/TestOpAsQuery.java @@ -73,6 +73,10 @@ public class TestOpAsQuery { @Test public void testBind12() { test_roundTripQuery("SELECT ?w ('y' AS ?y) ?x { BIND('w' AS ?w) ?s ?p ?o BIND ('x' AS ?x) }"); } @Test public void testBind13() { test_roundTripQuery("SELECT ('x' AS ?x) (str(?x) AS ?y) (str(?x) AS ?z) {}"); } + // https://github.com/apache/jena/issues/1397 + @Test public void testBind14() { test_roundTripQuery("SELECT * { GRAPH ?g { BIND('x' AS ?x) } }"); } + @Test public void testBind15() { test_roundTripQuery("SELECT * { GRAPH ?g { BIND('x' AS ?x) BIND('y' AS ?y) } }"); } + @Test public void testOptional01() { test_roundTripQuery("SELECT * WHERE { ?s ?p ?o OPTIONAL { ?s ?q ?z FILTER (?foo) } }") ; }