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

HAVING clause with variable comparison not correctly evaluated #936

Open
white-gecko opened this issue Oct 2, 2019 · 3 comments
Open

HAVING clause with variable comparison not correctly evaluated #936

white-gecko opened this issue Oct 2, 2019 · 3 comments
Labels
bug Something isn't working SPARQL

Comments

@white-gecko
Copy link
Member

white-gecko commented Oct 2, 2019

On the graph

<urn:a> <urn:p> 1 .
<urn:b> <urn:p> 3 .
<urn:c> <urn:q> 1 .

I would expect the query

SELECT ?p ?o
WHERE { ?s ?p ?o }
GROUP BY ?p HAVING (?p != <urn:foo> )

to result in

?p ?o
urn:p one of 1 or 3
urn:q 1

while it results in an empty result set.

This issue is demonstrated in pull request #935 .

data = """
<urn:a> <urn:p> 1 .
<urn:b> <urn:p> 3 .
<urn:c> <urn:q> 1 .
"""

def testHavingPrimaryExpressionVarNeqIri(self):
query = ("SELECT ?p ?o "
"WHERE { ?s ?p ?o } "
"GROUP BY ?p HAVING (?p != <urn:foo> )")
qres = self.g.query(query)
self.assertEqual(2, len(qres))

@white-gecko
Copy link
Member Author

white-gecko commented Oct 15, 2019

I think the problem is in the parser. When I look at the algebra, the RelationExpression: RelationalExpression_{'expr': rdflib.term.Variable('p'), 'op': '!=', 'other': rdflib.term.URIRef('urn:foo'), '_vars': set()} is comparing with rdflib.term.Variable('p') while it has to be rdflib.term.Variable('__agg_1__') because of the GROUP BY ?p as can be seen in the AggregateJoin_.

SelectQuery_SelectQuery_{'p': Project_{'p': Extend_{'p': Extend_{'p': Filter_{'expr': RelationalExpression_{'expr': rdflib.term.Variable('p'), 'op': '!=', 'other': rdflib.term.URIRef('urn:foo'), '_vars': set()}, 'p': AggregateJoin_{'A': [Aggregate_Sample_{'vars': rdflib.term.Variable('p'), 'res': rdflib.term.Variable('__agg_1__'), '_vars': {rdflib.term.Variable('p'), rdflib.term.Variable('__agg_1__')}}, Aggregate_Sample_{'vars': rdflib.term.Variable('o'), 'res': rdflib.term.Variable('__agg_2__'), '_vars': {rdflib.term.Variable('o'), rdflib.term.Variable('__agg_2__')}}], 'p': Group_{'p': BGP_{'triples': [(rdflib.term.Variable('s'), rdflib.term.Variable('p'), rdflib.term.Variable('o'))], '_vars': {rdflib.term.Variable('p'), rdflib.term.Variable('o'), rdflib.term.Variable('s')}}, 'expr': [rdflib.term.Variable('p')], '_vars': {rdflib.term.Variable('p'), rdflib.term.Variable('o'), rdflib.term.Variable('s')}}, '_vars': {rdflib.term.Variable('o'), rdflib.term.Variable('__agg_1__'), rdflib.term.Variable('__agg_2__'), rdflib.term.Variable('s'), rdflib.term.Variable('p')}}, '_vars': {rdflib.term.Variable('o'), rdflib.term.Variable('__agg_1__'), rdflib.term.Variable('__agg_2__'), rdflib.term.Variable('s'), rdflib.term.Variable('p')}}, 'expr': rdflib.term.Variable('__agg_1__'), 'var': rdflib.term.Variable('p'), '_vars': {rdflib.term.Variable('o'), rdflib.term.Variable('__agg_1__'), rdflib.term.Variable('__agg_2__'), rdflib.term.Variable('s'), rdflib.term.Variable('p')}}, 'expr': rdflib.term.Variable('__agg_2__'), 'var': rdflib.term.Variable('o'), '_vars': {rdflib.term.Variable('o'), rdflib.term.Variable('__agg_1__'), rdflib.term.Variable('__agg_2__'), rdflib.term.Variable('s'), rdflib.term.Variable('p')}}, 'PV': [rdflib.term.Variable('p'), rdflib.term.Variable('o')], '_vars': {rdflib.term.Variable('o'), rdflib.term.Variable('__agg_1__'), rdflib.term.Variable('__agg_2__'), rdflib.term.Variable('s'), rdflib.term.Variable('p')}}, 'datasetClause': None, 'PV': [rdflib.term.Variable('p'), rdflib.term.Variable('o')], '_vars': {rdflib.term.Variable('o'), rdflib.term.Variable('__agg_1__'), rdflib.term.Variable('__agg_2__'), rdflib.term.Variable('s'), rdflib.term.Variable('p')}}

@reeshabhranjan
Copy link

Me, @prince17080 and @guvi007 are inspecting this issue, we will get back to you if we find any solution.

devrb added a commit to devrb/rdflib that referenced this issue May 29, 2020
Modified the algebra.py file under sparql folder
@nicholascar
Copy link
Member

@white-gecko can you please see if PR #1093 actually fixes this issue, as claimed by the PR raiser @devrb?

@ghost ghost added the SPARQL label Dec 23, 2021
aucampia added a commit that referenced this issue Jun 5, 2022
…valuated (#1093)

Fixed condition when evaluating having.

Co-authored-by: Natanael Arndt <arndtn@gmail.com>
Co-authored-by: Iwan Aucamp <aucampia@gmail.com>
Co-authored-by: Graham Higgins <gjhiggins@users.noreply.github.com>
@aucampia aucampia added the bug Something isn't working label Aug 21, 2022
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

4 participants