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

TypeError: unorderable types: SequencePath() < SequencePath() #492

Closed
jpotoniec opened this issue Jul 3, 2015 · 8 comments
Closed

TypeError: unorderable types: SequencePath() < SequencePath() #492

jpotoniec opened this issue Jul 3, 2015 · 8 comments
Assignees
Labels
bug Something isn't working fix-in-progress SPARQL
Milestone

Comments

@jpotoniec
Copy link
Contributor

Hi,
I believe there is some error in the SPARQL parser during reordering triples with property paths
Minimal working example leading to the error:

#!/usr/bin/env python3

import rdflib

query = '''
prefix owl: <http://www.w3.org/2002/07/owl#> 
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
select ?x
where
{   
    ?x rdf:rest/rdf:first _:6.
    ?x rdf:rest/rdf:first _:5.
}
'''
print(rdflib.__version__)
g = rdflib.Graph()
result = g.query(query)

I am using rdflib installed with pip as described in README. The output of the above script is:

4.2.0
Traceback (most recent call last):
  File "./query.py", line 17, in <module>
    result = g.query(query)
  File "/home/smaug/tmp/venv/lib/python3.4/site-packages/rdflib/graph.py", line 1080, in query
    query_object, initBindings, initNs, **kwargs))
  File "/home/smaug/tmp/venv/lib/python3.4/site-packages/rdflib/plugins/sparql/processor.py", line 73, in query
    query = translateQuery(parsetree, base, initNs)
  File "/home/smaug/tmp/venv/lib/python3.4/site-packages/rdflib/plugins/sparql/algebra.py", line 744, in translateQuery
    P, PV = translate(q[1])
  File "/home/smaug/tmp/venv/lib/python3.4/site-packages/rdflib/plugins/sparql/algebra.py", line 533, in translate
    M = translateGroupGraphPattern(q.where)
  File "/home/smaug/tmp/venv/lib/python3.4/site-packages/rdflib/plugins/sparql/algebra.py", line 278, in translateGroupGraphPattern
    g[-1]["triples"] += triples(p.triples)
  File "/home/smaug/tmp/venv/lib/python3.4/site-packages/rdflib/plugins/sparql/algebra.py", line 131, in triples
    for x in range(0, len(l), 3))
  File "/home/smaug/tmp/venv/lib/python3.4/site-packages/rdflib/plugins/sparql/algebra.py", line 113, in reorderTriples
    1], varsknown, varscount), x[1]) for x in l[i:])
TypeError: unorderable types: SequencePath() < SequencePath()

I am to believe that the syntax of the SPARQL query is correct, as other SPARQL engines can deal with it (e.g. http://factforge.net/sparql?sample=prefix+owl%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2002%2F07%2Fowl%23%3E+%0D%0Aprefix+rdf%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F1999%2F02%2F22-rdf-syntax-ns%23%3E%0D%0Aselect+%3Fx%0D%0Awhere%0D%0A%7B+++%0D%0A++++%3Fx+rdf%3Arest%2Frdf%3Afirst+_%3A6.%0D%0A++++%3Fx+rdf%3Arest%2Frdf%3Afirst+_%3A5.%0D%0A%7D%0D%0Alimit+1)

@gromgull
Copy link
Member

gromgull commented Jul 3, 2015

Yep - someone has to make up some kind of ordering and implement __lt__ for the path objects.

@joernhees joernhees added bug Something isn't working SPARQL labels Jul 8, 2015
@joernhees joernhees added this to the rdflib 4.2.1 milestone Jul 8, 2015
@joernhees joernhees modified the milestones: rdflib 4.2.1, rdflib 4.2.2 Aug 11, 2015
@bollwyvl
Copy link
Contributor

Wow, this hit me like a train. How can I help?

@joernhees
Copy link
Member

just make a pull request? sorry, i didn't find any time to think about this yet

@bollwyvl
Copy link
Contributor

Is there another implementation i should look at for guidance?

On 09:04, Fri, Sep 18, 2015 Jörn Hees notifications@github.com wrote:

just make a pull request? sorry, i didn't find any time to think about
this yet


Reply to this email directly or view it on GitHub
#492 (comment).

@gromgull
Copy link
Member

No no - it's actually very easy. On py2 everything is orderable:

In [26]: RDF.type/RDF.first
Out[26]: Path(http://www.w3.org/1999/02/22-rdf-syntax-ns#type /
http://www.w3.org/1999/02/22-rdf-syntax-ns#first)

In [27]: p1 = RDF.type/RDF.first

In [28]: p2 = RDF.type|RDF.first

In [29]: p2
Out[29]: Path(http://www.w3.org/1999/02/22-rdf-syntax-ns#type |
http://www.w3.org/1999/02/22-rdf-syntax-ns#first)

In [30]: p1>p2
Out[30]: False

In [31]: p2<p1
Out[31]: False

BUT on py3 only explicitly comparable things are:

p1=RDF.type/RDF.type
p2=RDF.type|RDF.type
p1>p2
Traceback (most recent call last):
File "", line 1, in
TypeError: unorderable types: SequencePath() > AlternativePath()

To fix this you have to implement lt, gt, ge etc.
You can shortcut and only implement equals and lt and use
https://docs.python.org/2/library/functools.html#functools.total_ordering

The actual order doesn't matter, they just have to be sortable, just make
up something.
This is already done for instance for terms:

https://github.com/RDFLib/rdflib/blob/master/rdflib/term.py#L169
and
https://github.com/RDFLib/rdflib/blob/master/rdflib/term.py#L1587-L1593

  • Gunnar

On 23 September 2015 at 16:14, Jörn Hees notifications@github.com wrote:

i guess all of
https://github.com/RDFLib/rdflib/blob/master/rdflib/plugins/sparql/algebra.py
and
https://github.com/RDFLib/rdflib/tree/master/rdflib/plugins/sparql/operators.py


Reply to this email directly or view it on GitHub
#492 (comment).

http://gromgull.net

joernhees added a commit to joernhees/rdflib that referenced this issue Sep 23, 2015
@joernhees joernhees self-assigned this Sep 23, 2015
@joernhees
Copy link
Member

@gromgull thanks for the pointer...

@bollwyvl
Copy link
Contributor

Ah, didn't realize this was a 2/3 issue. I didn't think clause ordering
mattered. Thanks!

On Wed, Sep 23, 2015 at 12:40 PM Jörn Hees notifications@github.com wrote:

@gromgull https://github.com/gromgull thanks for the pointer...


Reply to this email directly or view it on GitHub
#492 (comment).

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

No branches or pull requests

4 participants