Warning:
This wiki has been archived and is now read-only.

Feature:ProjectExpressions

From SPARQL Working Group
Jump to: navigation, search


Feature: Project Expressions

Being able to project expressions from result bindings, rather than literal values in the store.

Feature description

In SPARQL/Query 1.0 (original SPARQL), projection queries (SELECT queries) may only project out variables bound in the query. Because variables can only be bound via triple pattern matching, there is no way to project out values that are not matched in the underlying RDF data set. Projecting expressions represents the ability for SPARQL SELECT queries to project any SPARQL expression, rather than only variables. A projected expression might be a variable, a constant URI, a constant literal, or an arbitrary expression (including function calls) on variables and constants. Functions could include both SPARQL built-in functions and extension functions supported by an implementation.

There are many use cases that motivate the ability to project expressions rather than just variables in SPARQL queries. In general, the motivation is to return values that do not occur in the graphs that comprise a query's RDF data set. Specific examples include:

  • Returning the total cost of an order's line item as the product of two variables: ?unit_cost * ?quantity
  • Use SPARQL accessors to find the languages used in a dataset: LANG(?o)
  • Returning computed values, such as the current day of the week: ex:dayOfTheWeek(ex:Today())
  • Performing simple string parsing: ex:substring(?url, 8, ex:length(?url))

This feature is necessary for projecting the results of aggregate functions.

Example

We wish to find names, and whether the person is over 18.

 SELECT ?name (?age > 18) AS over18
 WHERE {
   ?person :name ?name ;
           :age ?age .
 }

Another example, we wish to find the full name of everyone who is interested in trees.

 PREFIX foaf: <http://xmlns.com/foaf/0.1/>
 PREFIX : <http://www.example.org/>
 
 SELECT fn:concat(?givenName, ' ', ?surname) AS ?fullName
 WHERE {
   ?person foaf:givenname ?givenName ;
           foaf:surname ?surname ;
           foaf:interest :trees .
 }

This example has made use of a concatenation function from XPath-Functions. Which functions will be available for value construction in SPARQL is an open issue that will be dealt with on a time-permitting basis.

To return an RDF graph where the first and family names are concatenated to a full name such project expressions could be used as an alternative to complex expressions (without projection) directly in CONSTRUCT templates

 PREFIX foaf: <http://xmlns.com/foaf/0.1/>
 CONSTRUCT { ?x foaf:name { fn:concat(?gn, " ", ?sn) } }
 WHERE { foaf:givenname ?gn ; foaf:surname ?sn . }

TODO: complex expressions in CONSTRUCT templates are arguable, since they are difficult to parse.

Alternatively, analogously to the SELECT example from before, we can use a subquery with project expressions for this query

 PREFIX foaf: <http://xmlns.com/foaf/0.1/>
 CONSTRUCT { ?x foaf:name ?fullName }
 WHERE {
   { SELECT fn:concat(?gn, " ", ?sn) AS ?fullName
     WHERE { foaf:givenname ?gn ; foaf:surname ?sn . } }
 }

@@Check whether any implementations support both subqueries and fn:concat()@@

Existing Implementation(s)

  • Garlik's JXT (though doesn't do CONSTRUCT)
  • Dave Beckett's Redland-based storage engines
  • In ARQ with a slightly different syntax (AS is part of the () expression).
  • In Open Anzo's Glitter SPARQL engine, with the same syntax as ARQ
  • Virtuoso, syntax is (expression) AS ?alias, the clause "as ?alias" is optional so parentheses around expression are required.
  • XSPARQL allows XPath/XQuery functions to be used as expressions in CONSTRUCTs as in the example above.

Existing Specification / Documentation

None known.

Compatibility

The straw-man syntax is back compatible with SPARQL, but there are plenty of alternatives.

Links to postponed Issues

CountAggregate is a related postponed issue from DAWG.

Related Use Cases/Extensions

Related to Feature:AggregateFunctions.

Related to Feature:Assignment.

Champions

  • Garlik would be willing to push this.
  • OpenLink

References