openlink / virtuoso-opensource

Virtuoso is a high-performance and scalable Multi-Model RDBMS, Data Integration Middleware, Linked Data Deployment, and HTTP Application Server Platform
https://vos.openlinksw.com
Other
856 stars 210 forks source link

HAVING unable to filter on aggregates #254

Open knoan opened 9 years ago

knoan commented 9 years ago
select ?s (count(?o) as ?count) where {

    ?s ?p ?o

} group by ?s having (?count > 3)

Fails unexpectedly with

Virtuoso 37000 Error SP031: SPARQL compiler: Variable ?count is used in the result set outside aggregate and not mentioned in GROUP BY clause
HughWilliams commented 9 years ago

I have been able to recreate this error locally and reported to development to look into ....

knoan commented 9 years ago

Not sure that my expectations are correct here… the ability to filter on projected aggregates is likely to be a (handy) extension of sesame/jena beyond what specified by the standard.

HughWilliams commented 9 years ago

SPARQL 1.1 paragraph "18.2.4.2 HAVING" states that "expressions projected by the SELECT clause are not visible to the HAVING clause". In other words, variable ?count gets its value after filtering of grouped solutions by HAVING.

The right variant of the query would be

select ?s (count(?o) as ?count) from virtrdf: where { ?s ?p ?o } group by ?s having (count(?o) > 3)

( "from virtrdf:" added to make the sample practical on big databases)

knoan commented 9 years ago

Spot on…