I've also discovered that a SELECT query doesn't work either.
PREFIX example: <
http://www.example.org/test#> select ?s {{?s example:p1 example:o1} UNION {?s example:p2 example:o2}}
The query returns one result where "s" is <null> instead of an empty result set.
I did a bit of investigation on this issue.
FederationConnection.optimize(query, ...) applies EmptyPatternOptimizer which replaces statement patterns by empty sets if they do not match any triples.
A given query AST Union (SP1 SP2) is replaced by Union (EmptySet EmptySet) if SP1 and SP2 do not match any triples.
QueryModelPruner then transforms Union (EmptySet EmptySet) to SingletonSet. The method meet(Union union) removes each union argument that is an EmptySet but does not check if union is empty afterwards. Hence the node Union is replaced by SingletonSet.
Proposed fix for QueryModelPruner.meet() is:
@Override
public void meet(Union union) {
super.meet(union);
for (TupleExpr arg : union.getArgs()) {
if (arg instanceof EmptySet) {
union.removeArg(arg);
}
}
int numberOfArguments = union.getNumberOfArguments();
if (numberOfArguments == 0) {
union.replaceWith(new EmptySet());
} else if (numberOfArguments == 1) {
union.replaceWith(union.getArg(0));
}
else {
for (TupleExpr arg : union.getArgs()) {
if (!(arg instanceof SingletonSet)) {
return;
}
}
union.replaceWith(new SingletonSet());
}
}
FederationQueryTest can be extended by the following test:
public void testUnionUnsatisfiable()
throws Exception
{
assertQuery("{ {?person a:nonExistentProperty ?child} UNION {?person a:nonExistentProperty one:teacher}}");
}
Patches are below:
### Eclipse Workspace Patch 1.0
#P sesame-store-compliance
Index: src/test/java/org/openrdf/sail/federation/FederationQueryTest.java
===================================================================
--- src/test/java/org/openrdf/sail/federation/FederationQueryTest.java (revision 9594)
+++ src/test/java/org/openrdf/sail/federation/FederationQueryTest.java (working copy)
@@ -165,6 +165,12 @@
{
assertQuery("{ {?person a:parentOf ?child} UNION {?person c:job one:teacher}}");
}
+
+ public void testUnionUnsatisfiable()
+ throws Exception
+ {
+ assertQuery("{ {?person a:nonExistentProperty ?child} UNION {?person a:nonExistentProperty one:teacher}}");
+ }
private void assertQuery(String qry)
throws Exception
#P sesame-queryalgebra-evaluation
Index: src/main/java/org/openrdf/query/algebra/evaluation/impl/QueryModelPruner.java
===================================================================
--- src/main/java/org/openrdf/query/algebra/evaluation/impl/QueryModelPruner.java (revision 9594)
+++ src/main/java/org/openrdf/query/algebra/evaluation/impl/QueryModelPruner.java (working copy)
@@ -108,7 +108,10 @@
}
}
- if (union.getNumberOfArguments() == 1) {
+ int numberOfArguments = union.getNumberOfArguments();
+ if (numberOfArguments == 0) {
+ union.replaceWith(new EmptySet());
+ } else if (numberOfArguments == 1) {
union.replaceWith(union.getArg(0));
}
else {
Patch has been applied. Also, similar "corner case"(?) issues with Join and Intersection have been fixed.