Requirement: User-defined SPARQL functions

(This requirement is only relevant if SPARQL is chosen to play a role in 
the solution. However I mention it now because it may remove some of the 
prejudices against SPARQL being too complicating).

SPARQL functions are identified by their URI. A SPARQL engine could 
follow that URI to learn about functions that it hasn't encountered yet. 
There is no standard mechanism to declare new extension functions. This 
WG could fill this gap, because having user-defined SPARQL functions 
significantly improves the user experience and maintainability of SPARQL 
queries and constraints.

Example: Assuming we want to implement a cardinality check for a given 
property, to make sure that a ?subject has at most 10 values of 
ex:property. In vanilla SPARQL this could be written down with a nested 
SELECT as

SELECT *
WHERE {
     {
         SELECT (COUNT(?value) AS ?cardinality)
         WHERE {
             ?subject a ex:Class .
             ?subject ex:property ?value .
         } GROUP BY ?subject
     }
     FILTER (?cardinality <= 10)
}

A user-defined function would be like a stored procedure, and execute an 
encapsulated SPARQL query when called. With such a function 
spl:objectCount, the query above would become

SELECT *
WHERE {
     ?subject a ex:Class .
     FILTER (spl:objectCount(?subject, ex:property) <= 10) .
}

Which is far more readable and reusable. Furthermore it resolves an 
issue with the inside-out execution order of SPARQL (it's difficult to 
pass values into a nested SELECT).

SPIN provides an RDF vocabulary to represent such functions. In the 
example above, the body of that function would be

SELECT (COUNT(?object) AS ?result)
WHERE {
     ?arg1 ?arg2 ?object .
}

and ?arg1 is passed into the body from the subject, and ?arg2 is the 
property. Once we have agreed on a vocabulary to define such functions, 
SPARQL processors can follow the URI of a function to learn about the 
declared arguments, body and return type, and then execute the nested 
body query whenever the function is called.

There are also SPIN extensions that allow these bodies to be JavaScript 
snippets, and we could define extensions that call web services etc.

Thanks
Holger

Received on Thursday, 23 October 2014 00:30:29 UTC