Heuristic:AKSW RDFUnit JENA 766 Group By Count Workaround
| Knowledge Sources | |
|---|---|
| Domains | Debugging, SPARQL, Apache_Jena |
| Last Updated | 2026-10-16 00:00 GMT |
Overview
Workaround for Apache Jena bug JENA-766 where COUNT with GROUP BY and HAVING miscalculates the aggregate by wrapping the query in a sub-SELECT.
Description
When generating COUNT aggregate queries for the `AggregatedTestExecutor`, RDFUnit encounters test cases whose WHERE clauses contain GROUP BY (and potentially HAVING) expressions. In Apache Jena, when a HAVING clause is present, the aggregate `COUNT(?this)` is calculated against the HAVING expression rather than the grouped results, producing incorrect violation counts. This is tracked as JENA-766.
RDFUnit works around this by detecting whether the parsed query `hasGroupBy()`. If it does, instead of using the simple `SELECT (COUNT(?this) AS ?total) WHERE ...` form, RDFUnit wraps the original WHERE clause in a sub-SELECT: `SELECT (COUNT(?this) AS ?total) WHERE { SELECT ?this WHERE ... }`. This forces Jena to first evaluate the inner SELECT (which applies GROUP BY/HAVING correctly), and then the outer COUNT aggregates over the filtered results.
Usage
Use this heuristic when generating COUNT aggregate queries that may contain GROUP BY or HAVING clauses. This applies specifically to the `QueryGenerationCountFactory` used by `AggregatedTestExecutor`. If upgrading to a Jena version where JENA-766 is fixed, the workaround can be removed but remains harmless.
The Insight (Rule of Thumb)
- Action: When a COUNT query has GROUP BY, wrap the WHERE clause in a sub-SELECT before applying the outer COUNT.
- Value: Use `SELECT (COUNT(?this) AS ?total) WHERE { SELECT ?this WHERE <original> }` instead of `SELECT (COUNT(?this) AS ?total) WHERE <original>`.
- Trade-off: Slightly more complex query structure and marginal parse overhead, but produces correct COUNT results.
- Compatibility: Required for Apache Jena 3.x. Test before removing with newer Jena versions.
Reasoning
The JENA-766 bug causes the `COUNT` aggregate to be evaluated against the `HAVING` expression rather than the grouped rows. By wrapping the query in a sub-SELECT, the GROUP BY and HAVING are evaluated in the inner query first, and the outer `COUNT` then correctly counts the rows returned by the sub-SELECT. This is a well-known SPARQL pattern for working around aggregate evaluation quirks.
Code Evidence
Workaround logic from `QueryGenerationCountFactory.java:33-56`:
@Override
public Query getSparqlQuery(TestCase testCase) {
String sparqlQuery = getPrefixDeclarations(testCase) +
SELECT_CLAUSE_SIMPLE + testCase.getSparqlWhere();
try {
Query query = QueryFactory.create(sparqlQuery);
if (!query.hasGroupBy()) {
return query;
}
} catch (QueryParseException e) {
throw new IllegalArgumentException("Illegal query: \n" + sparqlQuery, e);
}
// When we have HAVING the aggregate is calculated against the HAVING expression
// This way we enclose the query in a sub-select and calculate the count () correctly
// See https://issues.apache.org/jira/browse/JENA-766
Query query = QueryFactory.create(getPrefixDeclarations(testCase) +
SELECT_CLAUSE_GROUP_START
+ testCase.getSparqlWhere() +
SELECT_CLAUSE_GROUP_END
);
return query;
}
Sub-SELECT wrapper constants from `QueryGenerationCountFactory.java:20-23`:
private static final String SELECT_CLAUSE_GROUP_START =
" SELECT (count( ?this ) AS ?total ) WHERE {" +
" SELECT ?this WHERE ";
private static final String SELECT_CLAUSE_GROUP_END = "}";