Difference between revisions of "Feature:ProjectExpressions"

From SPARQL Working Group
Jump to: navigation, search
(Example)
(Example)
(4 intermediate revisions by one user not shown)
Line 30: Line 30:
 
Another example, we wish to find the full name of everyone who is interested in trees.
 
Another example, we wish to find the full name of everyone who is interested in trees.
  
   SELECT fn:concat(?givenName, ' ', ?surname) AS fullName
+
  PREFIX foaf: <http://xmlns.com/foaf/0.1/>
 +
  PREFIX : <http://www.example.org/>
 +
 
 +
   SELECT fn:concat(?givenName, ' ', ?surname) AS ?fullName
 
   WHERE {
 
   WHERE {
     ?person :givenName ?givenName ;
+
     ?person foaf:givenname ?givenName ;
             :surname ?surname ;
+
             foaf:surname ?surname ;
             :interest :trees .
+
             foaf:interest :trees .
 
   }
 
   }
  
This example has made use of a concatenation function from [http://www.w3.org/2009/sparql/docs/features/#xpathfo XPath-Functions]. Whether this function will be available in SPARQL is an open issue that will be dealt with on a time-permitting basis.
+
This example has made use of a concatenation function from [http://www.w3.org/2009/sparql/docs/features/#xpathfo 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:
+
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
  
   CONSTRUCT { ?x foaf:name ?name }
+
  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 [http://lists.w3.org/Archives/Public/public-rdf-dawg/2009AprJun/0365.html difficult to parse].'''
 +
 
 +
Alternatively, analogously to the SELECT example from before, we can use a [http://www.w3.org/2009/sparql/docs/features/#Subqueries subquery] with project expressions for this query
 +
 
 +
  PREFIX foaf: <http://xmlns.com/foaf/0.1/>
 +
  CONSTRUCT { ?x foaf:name ?fullName }
 
   WHERE {
 
   WHERE {
     { SELECT fn:concat(?fn, " ", ?sn) AS ?name
+
     { SELECT fn:concat(?gn, " ", ?sn) AS ?fullName
    WHERE { foaf:firstName ?fn ; foaf:family_name ?sn . } }
+
      WHERE { foaf:givenname ?gn ; foaf:surname ?sn . } }
 
   }
 
   }
  
using a [http://www.w3.org/2009/sparql/docs/features/#Subqueries subquery]. @@Check whether any implementations support both subqueries and fn:concat()@@
+
@@Check whether any implementations support both subqueries and fn:concat()@@
  
 
== Existing Implementation(s) ==
 
== Existing Implementation(s) ==
Line 53: Line 66:
 
* Garlic's JXT (though doesn't do CONSTRUCT)
 
* Garlic's JXT (though doesn't do CONSTRUCT)
 
* Dave Beckett's Redland-based storage engines
 
* Dave Beckett's Redland-based storage engines
* [http://jena.sourceforge.net/ARQ/select_expr.html In ARQ] with a slightly syntax (AS is part of the () expression).
+
* [http://jena.sourceforge.net/ARQ/select_expr.html In ARQ] with a slightly different syntax (AS is part of the () expression).
 
* In [http://openanzo.org Open Anzo's] Glitter SPARQL engine, with the same syntax as ARQ
 
* In [http://openanzo.org 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.
 
* Virtuoso, syntax is (expression) AS ?alias, the clause "as ?alias" is optional so parentheses around expression are required.
 +
* [http://xsparql.deri.org XSPARQL] allows XPath/XQuery functions to be used as expressions in CONSTRUCTs as in the example above.
  
 
== Existing Specification / Documentation ==
 
== Existing Specification / Documentation ==

Revision as of 13:32, 16 June 2009


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)

  • Garlic'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