RDFLib/rdflibissueplayground — Issue #4: >= and <= operators return erroneous results in sparql.query()
Author: gjhiggins
Created: 2012-02-20 06:12:53+00:00
State: open

gjhigg...@gmail.com, 2010-09-14T18:09:54.000Z
Migrated from http://code.google.com/p/rdflib/issues/detail?id=30 as it remains an issue.

# 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 = """

2007-12-31

2008-01-06

2008-01-04
2008-01-05

2008-01-07
2008-01-08

2008-01-02
2008-01-03

2008-01-09
2008-01-10

2008-01-01
2008-01-11

"""

g.load(StringIO(testgraph))

print "rdflib version: %s" % rdflib.__version__
r = g.query("""
PREFIX ex:
PREFIX loc:
PREFIX rdfs:
PREFIX xsd:

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

Comment 1 by gjhigg...@gmail.com
In rdflib 3.0.0 w. ccfsparql (for volubility):

rdflib version: 3.0.0
= xsd:date(2007-12-31)),
(xsd:date(date) <= xsd:date(2008-01-11))]>

sparql-p operator(s):
lambda(i):
sparqlOperators.ge(
sparqlOperators.XSDCast("?date",'http://www.w3.org/2001/XMLSchema#date')(i),
sparqlOperators.XSDCast(rdflib.term.Literal(u'2007-12-31'),'http://www.w3.org/2001/XMLSchema#date')(i))(i)
and
sparqlOperators.le(
sparqlOperators.XSDCast("?date",'http://www.w3.org/2001/XMLSchema#date')(i),
sparqlOperators.XSDCast(rdflib.term.Literal(u'2008-01-11'),'http://www.w3.org/2001/XMLSchema#date')(i))(i)
= xsd:date(2008-01-02)),
(xsd:date(end) <= xsd:date(2008-01-10))]>

sparql-p operator(s):
lambda(i):
sparqlOperators.ge(
sparqlOperators.XSDCast("?start",'http://www.w3.org/2001/XMLSchema#date')(i),
sparqlOperators.XSDCast(rdflib.term.Literal(u'2008-01-02'),'http://www.w3.org/2001/XMLSchema#date')(i))(i)
and
sparqlOperators.le(
sparqlOperators.XSDCast("?end",'http://www.w3.org/2001/XMLSchema#date')(i),
sparqlOperators.XSDCast(rdflib.term.Literal(u'2008-01-10'),'http://www.w3.org/2001/XMLSchema#date')(i))(i)
## Full SPARQL Algebra expression ##
Union(Filter(.. a filter ..,BGP(?event,ex:date,?date)),
Filter(.. a filter ..,BGP(?event,ex:starts,?start,?event,ex:finishes,?end)))
###################################
## Full SPARQL Algebra expression ##
Union(Filter(.. a filter ..,BGP(?event,ex:date,?date)),
Filter(.. a filter ..,BGP(?event,ex:starts,?start,?event,ex:finishes,?end)))
###################################
eval(Union(Filter(.. a filter ..,BGP(?event,ex:date,?date)),
Filter(.. a filter ..,BGP(?event,ex:starts,?start,?event,ex:finishes,?end))
),
{},
[a rdflib:ConjunctiveGraph;
rdflib:storage [a rdflib:Store;
rdfs:label 'IOMemory']])

# Debug prints...
('type(selection)', , 'selection', [])
('type(selection)', , 'selection', None)