Hello all, I would like to ask a question about the OPTIONAL clause.

Suppose we have this graph pattern:

dbpedia:name_1  predicate_1  ?place1 .
dbpedia:name_2  predicate_2  ?movie .
dbpedia:name_3  predicate_3  ?email .

Even in this case, where the variables are not the same, so they are not "joined", if there are no triples that match for example the third triple pattern, then the whole query is going to give me nothing as result. How is it possible that the triples are connected, even though they have nothing in common?

I would imagine that even if there is nothing in the ?email variable I would get a cross product for the three variables that would contain something like NULL or empty string for the ?email.

This problem happens also when I am not SELECTing the ?email variable. If there are no triples that match, I get no answer for all the SELECTed variables.

Of course, one would say to use OPTIONAL for every triple pattern so that to be sure that I get results back. And yes, when I am using it I get results, even if the last triple pattern match no triples. However, I would prefer to find a way not to use the OPTIONAL clause. Any ideas?

Thank you very much for your time!

asked 07 Dec '12, 19:14

Christos's gravatar image

accept rate: 33%

edited 08 Dec '12, 03:51

Antoine%20Zimmermann's gravatar image

Antoine Zimm... ♦

Can you clarify why you don't want to use OPTIONAL in your question?

(08 Dec '12, 02:53) jindrichm jindrichm's gravatar image

I just want to remark that it seems to me that the way in which you are going about querying is very odd (and bound to cause you performance problems). Why are you formulating a single query that is about three completely separate subjects?

(09 Dec '12, 20:41) Jeen Broekstra ♦ Jeen%20Broekstra's gravatar image

If you want to get result whether or not a triple pattern matches, that means that you accept that it possibly do not match. "Possibly" is synonymous with "optionally", so you really want the triple to be OPTIONAL. End of the story.

However, if you want all the places and all the movies and all the emails in the same query without using OPTIONAL, you can also use a union:

SELECT ?place ?movie ?email WHERE {
   { dbpedia:name_1  predicate_1  ?place . }
   { dbpedia:name_2  predicate_2  ?movie . }
   { dbpedia:name_3  predicate_3  ?email . }

but this will give you back a table where each line has exactly two NULL values.

PS: there is nothing extraordinary here. You mention NULL in your question, so I guess you are familiar with SQL. In SQL, it is the same. Consider the query SELECT tab1.foo, tab2.bar FROM tab1 tab2. If tab2 is empty, then tab2.bar does not match anything, and therefore the query does not give any result, no matter how tab1.foo matches.

permanent link

answered 08 Dec '12, 04:18

Antoine%20Zimmermann's gravatar image

Antoine Zimm... ♦
accept rate: 34%

Thank you Antoine for your answer! I was aware of the UNION solution, but I thought I should ask since someone whould have a good alternative to OPTIONAL. UNION gives me 3 different columns (if I am SELECTing ?place, ?movie, ?email) and as you said on each row we have 2 empty values.

The problem with OPTIONAL is that it takes a really big amount of time to evaluate 20 sequential OPTIONAL, one after the other (20 is an example number).

I will continue searching for a better solution, there might be I guess.


(08 Dec '12, 10:15) Christos Christos'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: 07 Dec '12, 19:14

question was seen: 892 times

last updated: 09 Dec '12, 20:41