Letractively / rdflib

Automatically exported from code.google.com/p/rdflib
Other
0 stars 0 forks source link

>= and <= operators return erroneous results in sparql.query() #30

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
# There seems to be a problem with the >= and <= operators.
# Running on Mac OS X 10.5.4 python 2.5.1
# This code:

#!/usr/bin/python
import sys
sys.path.insert(0, 
'/Library/Python/2.5/site-packages/rdflib-2.4.0-py2.5-macosx-10.3-fat.egg')

import rdflib
from rdflib import ConjunctiveGraph
from rdflib import BNode, Literal, Namespace, RDF, URIRef, RDFS, sparql, 
QueryResult
from StringIO import StringIO
from pprint import pprint
g = ConjunctiveGraph()
testgraph = """<rdf:RDF  xmlns:ex="http://temp.example.org/terms/"
    xmlns:loc="http://simile.mit.edu/2005/05/ontologies/location#"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">

   <ex:Event rdf:about="http://temp.example.org/terms/Event#case0">
      <ex:date rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2007-12-31</ex:date>
      <loc:place rdf:resource="http://temp.example.org/terms/Place#case1_place" />
   </ex:Event>
   <ex:Event rdf:about="http://temp.example.org/terms/Event#case1">
      <ex:date rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-06</ex:date>
      <loc:place rdf:resource="http://temp.example.org/terms/Place#case1_place" />
   </ex:Event>
   <ex:Event rdf:about="http://temp.example.org/terms/Event#case2">
      <ex:starts rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-04</ex:starts>
      <ex:finishes rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-05</ex:finishes>
      <loc:place rdf:resource="http://temp.example.org/terms/Place#case2_place" />
   </ex:Event>
   <ex:Event rdf:about="http://temp.example.org/terms/Event#case3">
      <ex:starts rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-07</ex:starts>
      <ex:finishes rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-08</ex:finishes>
      <loc:place rdf:resource="http://temp.example.org/terms/Place#case4_place" />
   </ex:Event>
   <ex:Event rdf:about="http://temp.example.org/terms/Event#case4">
      <ex:starts rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-02</ex:starts>
      <ex:finishes rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-03</ex:finishes>
      <loc:place rdf:resource="http://temp.example.org/terms/Place#case3_place" />
   </ex:Event>
   <ex:Event rdf:about="http://temp.example.org/terms/Event#case5">
      <ex:starts rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-09</ex:starts>
      <ex:finishes rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-10</ex:finishes>
      <loc:place rdf:resource="http://temp.example.org/terms/Place#case5_place" />
   </ex:Event>
   <ex:Event rdf:about="http://temp.example.org/terms/Event#case6">
      <ex:starts rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-01</ex:starts>
      <ex:finishes rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-11</ex:finishes>
      <loc:place rdf:resource="http://temp.example.org/terms/Place#case6_place" />
   </ex:Event>

</rdf:RDF>"""

g.load(StringIO(testgraph))

print "rdflib version: %s" % rdflib.__version__
r = g.query("""
PREFIX ex: <http://temp.example.org/terms/>
PREFIX loc: <http://simile.mit.edu/2005/05/ontologies/location#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

SELECT *
WHERE {{?event ex:date ?date .
    FILTER (xsd:date(?date) >= xsd:date("2007-12-31") && xsd:date(?date) <= xsd:date("2008-01-11"))}
    UNION {?event ex:starts ?start; ex:finishes ?end.
        FILTER (xsd:date(?start) >= xsd:date("2008-01-02") && xsd:date(?end) <= xsd:date("2008-01-10"))}
}
ORDER BY ?event
""")
print "Selected: " 
pprint(r.selected)

# returns:

rdflib version: 2.4.0
Selected: 
[(rdflib.URIRef('http://temp.example.org/terms/Event#case1'),
  rdflib.Literal('2008-01-06', language=None, datatype=rdflib.URIRef('http://www.w3.org/2001/XMLSchema#date'))),
 (rdflib.URIRef('http://temp.example.org/terms/Event#case2'),
  rdflib.Literal('2008-01-05', language=None, datatype=rdflib.URIRef('http://www.w3.org/2001/XMLSchema#date')),
  rdflib.Literal('2008-01-04', language=None, datatype=rdflib.URIRef('http://www.w3.org/2001/XMLSchema#date'))),
 (rdflib.URIRef('http://temp.example.org/terms/Event#case3'),
  rdflib.Literal('2008-01-08', language=None, datatype=rdflib.URIRef('http://www.w3.org/2001/XMLSchema#date')),
  rdflib.Literal('2008-01-07', language=None, datatype=rdflib.URIRef('http://www.w3.org/2001/XMLSchema#date')))]

# where it should select cases 1-5. 
# The same code under rdflib version 2.4.1 (with sys.path.insert() commented 
out) returns:

rdflib version: 2.4.1
  File "build/bdist.macosx-10.5-i386/egg/rdflib/Graph.py", line 738, in query
  File "build/bdist.macosx-10.5-i386/egg/rdflib/sparql/bison/Processor.py", line 50, in query
  File "build/bdist.macosx-10.5-i386/egg/rdflib/sparql/Algebra.py", line 389, in TopEvaluate
  File "build/bdist.macosx-10.5-i386/egg/rdflib/sparql/Query.py", line 1015, in select
  File "build/bdist.macosx-10.5-i386/egg/rdflib/sparql/Query.py", line 914, in _orderedSelect
  File "build/bdist.macosx-10.5-i386/egg/rdflib/sparql/Query.py", line 871, in _getFullBinding
  File "build/bdist.macosx-10.5-i386/egg/rdflib/sparql/Query.py", line 1005, in select
  File "build/bdist.macosx-10.5-i386/egg/rdflib/sparql/Query.py", line 81, in _variablesToArray
rdflib.sparql.SPARQLError: SPARQL Error: 'selection' argument must be a string, 
a Variable, or a list of those.

# Similarly, if the first FILTER statement is modified thus:
FILTER (xsd:date(?date) >= xsd:date("2007-12-30") && xsd:date(?date) <= 
xsd:date("2008-01-11"))
# under version 2.4.0 it selects case 0

# if the second FILTER statement is modified thus:
FILTER (xsd:date(?start) >= xsd:date("2008-01-01") && xsd:date(?end) <= 
xsd:date("2008-01-11"))
# under version 2.4.0 it returns cases 1-5

Original issue reported on code.google.com by crumpj...@gmail.com on 16 Oct 2008 at 6:17

GoogleCodeExporter commented 9 years ago

Original comment by eik...@gmail.com on 10 Feb 2009 at 8:26

GoogleCodeExporter commented 9 years ago

Original comment by eik...@gmail.com on 1 Feb 2010 at 8:27

GoogleCodeExporter commented 9 years ago
These issues involve bits that have been moved out of rdflib proper for now. We 
will re-open them 
or move them to rdfextas as appropriate.

Original comment by eik...@gmail.com on 11 Feb 2010 at 6:06