[redland-dev] Problems with SPARQL's OPTIONAL pattern matching

Arjan Wekking a.wekking at synantics.nl
Fri Mar 10 15:08:54 GMT 2006


On Mar 10, 2006, at 9:57 AM, Attilio Fiandrotti wrote:

> Hi
>
> i ran some tests on the OPTIONAL matching using latest librdf,  
> raptor and rasqal libraries from SVN with the two following SPARQL  
> queries
>
> Query 1)
> PREFIX rdf: <http://www.dajobe.org/foaf.rdf> PREFIX foaf: <http:// 
> xmlns.com/foaf/0.1/> PREFIX dc: <http://purl.org/dc/elements/1.1/>  
> SELECT ?name, ?depiction_title WHERE { ?x foaf:name ?name . ?x  
> foaf:depiction ?y . OPTIONAL { ?y dc:title ?depiction_title } }
>
> Query 2)
> PREFIX rdf: <http://www.dajobe.org/foaf.rdf> PREFIX foaf: <http:// 
> xmlns.com/foaf/0.1/> PREFIX dc: <http://purl.org/dc/elements/1.1/>  
> SELECT ?name, ?depiction_title WHERE { ?x foaf:name ?name . ?x  
> foaf:depiction ?y . OPTIONAL { ?y dc:bad_tag ?depiction_title } }
>
> (note the "bad_tag" in query 2, to simulate missing title predicate  
> for the depiction)
>
> And i executed both queries in different ways
>
> * Via roqet
> Query 1) Correct output, all variables assigned
> Query 2) Correct output, ?depiction_title=NULL
>
> * Via C# bindings
> Query 1) Correct output, all variables assigned
> Query 2) application crashed because ?depiction_title=NULL
>
> * Via Python bindings
> Query 1) Correct output, all variables assigned
> Query 2) application crashed because ?depiction_title=NULL
>
> Note that if the OPTIONAL keyword is removed, then Query 2 produces  
> empty sets of result, but i never get crashes.
> I suppose this is a bindings problem: is this analysis corect?

Hi Attilio,

You are correct, it is a bindings problem and I encountered it a  
while back in the Python bindings (also with unbound nodes in  
bindings results). I opened a bug back then (http://bugs.librdf.org/ 
mantis/view.php?id=59) and it has recently been fixed in CVS by Dave.  
I assume the same problem exists for the C# (and possibly other  
language) bindings, especially if I look at the relevant code (http:// 
cvs.librdf.org/cvsweb/redland/bindings/csharp/QueryResults.cs? 
rev=1.9&view=auto):

> 		private Hashtable MakeResultsHash ()
> 		{
> 			Hashtable h = new Hashtable ();
> 			int c = librdf_query_results_get_bindings_count (handle);
> 			for (int i = 0; i < c; i++) {
> 				IntPtr iname = librdf_query_results_get_binding_name (handle, i);
> 				String name = Util.UTF8PtrToString (iname);
> 				IntPtr v = librdf_query_results_get_binding_value (handle, i);
> 				h.Add (name, new Node (v));
> 			}
>
> 			return h;
> 		}

This is basically the same method as there is in the Python bindings,  
here there is no check whether IntPtr v is null or not, assuming an  
IntPtrcan be null in C# (never worked with it myself). What would be  
required here (and possibly in other places) is a null check on 'v'  
because most likely the Node constructor does not accept null values  
(same thing in Python). So to fix it something like the following has  
to be done:

> 		private Hashtable MakeResultsHash ()
> 		{
> 			Hashtable h = new Hashtable ();
> 			int c = librdf_query_results_get_bindings_count (handle);
> 			for (int i = 0; i < c; i++) {
> 				IntPtr iname = librdf_query_results_get_binding_name (handle, i);
> 				String name = Util.UTF8PtrToString (iname);
> 				IntPtr v = librdf_query_results_get_binding_value (handle, i);
> 				if (v == null) {
> 					h.Add (name, null);
> 				} else {
> 					h.Add (name, new Node (v));
> 				}
> 			}
>
> 			return h;
> 		}

I have no idea whether C# hashtables accept null values, and I have  
no idea whether v == null works at all, like I said, never worked  
with C# myself but I guess the solution is clear anyway. I'd check  
the other language bindings as well if I had the time for it, but I  
don't right now :/

Regards,
-Arjan

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.gnomehack.com/pipermail/redland-dev/attachments/20060310/9985b93d/attachment.html


More information about the redland-dev mailing list