Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SPARQL: COUNT DISTINCT not working properly #404

Closed
pchampin opened this issue Jun 17, 2014 · 2 comments
Closed

SPARQL: COUNT DISTINCT not working properly #404

pchampin opened this issue Jun 17, 2014 · 2 comments
Labels
bug Something isn't working SPARQL
Milestone

Comments

@pchampin
Copy link
Contributor

The combination of COUNT with DISTINCT is not working propetry in the RDFLib implementation of SPARQL.
Consider the following code:

from rdflib import *

g = Graph()
g.parse(format="turtle", publicID="http://example.org/", data="""
@prefix : <> .

<#a>
  :knows <#b>, <#c> ;
  :age 42 .

<#b>
  :knows <#a>, <#c> ;
  :age 36 .

<#c>
  :knows <#b>, <#c> ;
  :age 20 .

""")


print "Query 1: people knowing someone younger"
results = g.query("""
PREFIX : <http://example.org/>

SELECT DISTINCT ?x {
  ?x :age ?ax ; :knows [ :age ?ay ].
  FILTER( ?ax > ?ay )
}
""")
for i in results:
    print str(i[0])


print "\nQuery 2: count people knowing someone younger"
results = g.query("""
PREFIX : <http://example.org/>

SELECT (COUNT(DISTINCT ?x) as ?cx) {
  ?x :age ?ax ; :knows [ :age ?ay ].
  FILTER( ?ax > ?ay )
}
""")


for i in results:
    print str(i[0])

It produces the following output:

Query 1: people knowing someone younger
http://example.org/#a
http://example.org/#b

Query 2: count people knowing someone younger
3

while the result of Query 2 should obvioulsy be 2. It seems that the DISTINCT in Query 2 has no effect (indeed, <#a> matches the query twice, as she knows 2 people younger than herself).

@pchampin
Copy link
Contributor Author

Actually, the same problem exists with SUM(DISTINCT ?x), AVG(DISTINCT ?x) or GROUP_CONCAT(DISTINCT ?x).

@gromgull
Copy link
Member

This works in master now, presumably fixed by the refactoring of the aggregation code here: #678

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working SPARQL
Projects
None yet
Development

No branches or pull requests

2 participants