In the answer to this question here, using SPARQL 1.1, I wanted to bind a new variable based on the value of an existing one. Specifically, I had calculated an ID for the day of the week such that 0 = Sunday, 1 = Monday, 2 = Tuesday, etc. Based on the binding for ?dayID, I wanted to bind ?dayName.

Essentially, I want to code some traditional "cases" structure into SPARQL. Factoring out the day ID calculation for brevity (here hardcoded as Sunday), I tried:

SELECT ?dayName
    BIND (0 AS ?dayID)

    FILTER (?dayIDCheck = ?dayID)
BINDINGS ?dayIDCheck ?dayName {
    (0 "Sunday"@en)
    (1 "Monday"@en)
    (2 "Tuesday"@en)
    (3 "Wednesday"@en)
    (4 "Thursday"@en)
    (5 "Friday"@en)
    (6 "Saturday"@en)

Expecting to get ?dayName bound to "Sunday"@en. However, over at sparql.org, I get no answers.

So, concrete questions:

  • Can anyone explain why the above method doesn't work?
  • Anyone got an alternative method to implementing "cases" in SPARQL 1.1?

asked 24 Mar '12, 23:40

Signified's gravatar image

Signified ♦
accept rate: 37%

edited 25 Mar '12, 11:57

BINDINGS defines a data table that is joined with the results of the query. It is not the substitution of the variables by the values.


  SELECT * { ... FILTER(?x = 23) }
  BINDINGS ?x { (1) (2) (3) }

execute the { ... FILTER(?x = 23) }, where ?x is unbound, then joins it to the data table. Being unbound the graph pattern evaluates to the empty table, and joining it to the BINDINGS is still empty.

(NB: This was not my preferred design - I preferred the substitution semantics you expected.)

permanent link

answered 25 Mar '12, 12:41

AndyS's gravatar image

AndyS ♦
accept rate: 33%

Thanks for the answer. That's a real pity.

(25 Mar '12, 12:48) Signified ♦ Signified's gravatar image

The WG is still open to comments. SPARQL is in the lucky position that there are several real and complete implementations so feedback from depoyed experience before formally completing the spec is possible.

(25 Mar '12, 12:54) AndyS ♦ AndyS's gravatar image

Here's what I know. That query wouldn't run at all against Virtuoso so I simplified the query slightly and set up a test case against a Jena in-memory model

public void testBinding() {
    Query q=QueryFactory.create(
        "SELECT ?dayName WHERE {"+ 
        "   FILTER (?dayIDCheck=0)"+
        "BINDINGS ?dayIDCheck ?dayName {"+
        "   (0 'Sunday'@en)"+
        "   (1 'Monday'@en)"+
        "   (2 'Tuesday'@en)"+
        "   (3 'Wednesday'@en)"+
        "   (4 'Thursday'@en)"+
        "   (5 'Friday'@en)"+
        "   (6 'Saturday'@en)" +
    Model m=ModelFactory.createDefaultModel();
    QueryExecution qe=QueryExecutionFactory.create(q,m);
    ResultSet r=qe.execSelect();
    QuerySolution qs=r.nextSolution();

This test case fails on the assertTrue() so that means no solutions are coming back.

I do get results if I (1) remove the FILTER or (2) write the following for the FILTER


so I guess that FILTER and BINDINGS don't work together in Jena the way you and I would expect them to. Perhaps this is a bug in Jena or perhaps we're not reading the SPARQL 1.1 spec correctly.

Note in a case like this, you can always write the old if-then-else ladder...

permanent link

answered 25 Mar '12, 12:16

database_animal's gravatar image

database_animal ♦
accept rate: 15%

Thanks for the details. It seems from @neumarcx's answer that the query works as we would expect in newer versions of Joseki, which tends to suggest that it's a bug with the implementations. I really can't see any reason why it wouldn't work wrt. the spec (unless maybe there was an issue with the FILTER scoping being mismatched with the BINDINGS scoping). Also, thanks for the if-then-else ladder. Hadn't seen that pattern before!

(25 Mar '12, 12:29) Signified ♦ Signified's gravatar image

Looks like your runtime sets ?dayIDCheck but doesn't bind it.

The query works just fine in my run-time environment and executes as expected.

permanent link

answered 25 Mar '12, 11:33

neumarcx's gravatar image

accept rate: 0%

edited 25 Mar '12, 11:44

Signified's gravatar image

Signified ♦

You mean a bug in the endpoint?

(25 Mar '12, 11:34) Signified ♦ Signified's gravatar image

yep looks like you run fuseki

(25 Mar '12, 11:40) neumarcx neumarcx's gravatar image

Cool! What SPARQL engine is lotico running?

(25 Mar '12, 11:47) Signified ♦ Signified's gravatar image

we run a couple of different engines but for production we have use ARQ on joseki 3.4.4

For the new stuff we play with fuseki, and virtuoso is pretty cool as well. we will also take look at stardog soon.

(25 Mar '12, 11:52) neumarcx neumarcx's gravatar image
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:


question asked: 24 Mar '12, 23:40

question was seen: 3,575 times

last updated: 25 Mar '12, 12:54