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

Fix issue #936 HAVING clause with variable comparison not correctly evaluated #1093

Merged
merged 8 commits into from
Jun 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rdflib/plugins/sparql/algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ def translateAggregates(
v.expr = traverse(v.expr, functools.partial(_aggs, A=A))

# having clause
if traverse(q.having, _hasAggregate, complete=False):
if traverse(q.having, _hasAggregate, complete=True):
q.having = traverse(q.having, _sample)
traverse(q.having, functools.partial(_aggs, A=A))

Expand Down
35 changes: 35 additions & 0 deletions test/test_having.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from rdflib import Graph

g = Graph()

data = """
<urn:a> <urn:p> 1 .
<urn:b> <urn:p> 3 .
<urn:c> <urn:q> 1 .
"""
g.parse(data=data, format="turtle")


def test_group_by():
query = "SELECT ?p" "WHERE { ?s ?p ?o } " "GROUP BY ?p"
qres = g.query(query)

assert len(qres) == 2


def test_having_aggregate_eq_literal():
query = (
"SELECT ?p (avg(?o) as ?a) "
"WHERE { ?s ?p ?o } "
"GROUP BY ?p HAVING (avg(?o) = 2 )"
)
qres = g.query(query)

assert len(qres) == 1


def test_having_primary_expression_var_neq_iri():
query = "SELECT ?p " "WHERE { ?s ?p ?o } " "GROUP BY ?p HAVING (?p != <urn:foo> )"
qres = g.query(query)

assert len(qres) == 2