Operator Mapping
The SPARQL grammar identifies a set of operators (for instance, && , * , isIRI
) used to construct constraints. The following table associates each of these grammatical
productions with the appropriate operands and an operator function defined by either
XQuery 1.0 and XPath 2.0 Functions and Operators [XQuery 1.0 and XPath 2.0 Functions and Operators] or the SPARQL operators specified in section 17.4. When selecting the operator definition for a given set of parameters, the definition
with the most specific parameters applies. For instance, when evaluating xsd:integer = xsd:signedInt, the definition for = with two numeric parameters applies, rather than the one with two RDF terms . The table is arranged
so that the upper-most viable candidate is the most specific. Operators invoked without
appropriate operands result in a type error.
SPARQL follows XPath's scheme for numeric type promotions and subtype substitution
for arguments to numeric operators. The XPath Operator Mapping rules for numeric operands (xsd:integer, xsd:decimal, xsd:float, xsd:double, and types derived from a numeric type) apply to SPARQL operators as well (see
XML Path Language (XPath) 2.0 [XML Path Language (XPath) 2.0] for definitions of numeric type promotions and subtype substitution). Some of the operators are associated with nested function expressions, e.g. fn:not(op:numeric-equal(A, B)). Note that per the XPath definitions, fn:not and op:numeric-equal produce an error if their argument is an error.
The collation for fn:compare is defined by XPath and identified by http://www.w3.org/2005/xpath-functions/collation/codepoint. This collation allows for string comparison based on code point values. Codepoint
string equivalence can be tested with RDF term equivalence.
| Operator | Type(A) | Function | Result type |
|---|---|---|---|
| XQuery Unary Operators | |||
| ! A | xsd:boolean (EBV) | fn:not(A) | xsd:boolean |
| + A | numeric | op:numeric-unary-plus(A) | numeric |
| - A | numeric | op:numeric-unary-minus(A) | numeric |
| Operator | Type(A) | Type(B) | Function | Result type |
|---|---|---|---|---|
| Logical Connectives | ||||
| A || B | xsd:boolean (EBV) | xsd:boolean (EBV) | logical-or(A, B) | xsd:boolean |
| A && B | xsd:boolean (EBV) | xsd:boolean (EBV) | logical-and(A, B) | xsd:boolean |
| XPath Tests | ||||
| A = B | numeric | numeric | op:numeric-equal(A, B) | xsd:boolean |
| A = B | simple literal | simple literal | op:numeric-equal(fn:compare(A, B), 0) | xsd:boolean |
| A = B | xsd:string | xsd:string | op:numeric-equal(fn:compare(STR(A), STR(B)), 0) | xsd:boolean |
| A = B | xsd:boolean | xsd:boolean | op:boolean-equal(A, B) | xsd:boolean |
| A = B | xsd:dateTime | xsd:dateTime | op:dateTime-equal(A, B) | xsd:boolean |
| A != B | numeric | numeric | fn:not(op:numeric-equal(A, B)) | xsd:boolean |
| A != B | simple literal | simple literal | fn:not(op:numeric-equal(fn:compare(A, B), 0)) | xsd:boolean |
| A != B | xsd:string | xsd:string | fn:not(op:numeric-equal(fn:compare(STR(A), STR(B)), 0)) | xsd:boolean |
| A != B | xsd:boolean | xsd:boolean | fn:not(op:boolean-equal(A, B)) | xsd:boolean |
| A != B | xsd:dateTime | xsd:dateTime | fn:not(op:dateTime-equal(A, B)) | xsd:boolean |
| A < B | numeric | numeric | op:numeric-less-than(A, B) | xsd:boolean |
| A < B | simple literal | simple literal | op:numeric-equal(fn:compare(A, B), -1) | xsd:boolean |
| A < B | xsd:string | xsd:string | op:numeric-equal(fn:compare(STR(A), STR(B)), -1) | xsd:boolean |
| A < B | xsd:boolean | xsd:boolean | op:boolean-less-than(A, B) | xsd:boolean |
| A < B | xsd:dateTime | xsd:dateTime | op:dateTime-less-than(A, B) | xsd:boolean |
| A > B | numeric | numeric | op:numeric-greater-than(A, B) | xsd:boolean |
| A > B | simple literal | simple literal | op:numeric-equal(fn:compare(A, B), 1) | xsd:boolean |
| A > B | xsd:string | xsd:string | op:numeric-equal(fn:compare(STR(A), STR(B)), 1) | xsd:boolean |
| A > B | xsd:boolean | xsd:boolean | op:boolean-greater-than(A, B) | xsd:boolean |
| A > B | xsd:dateTime | xsd:dateTime | op:dateTime-greater-than(A, B) | xsd:boolean |
| A <= B | numeric | numeric | logical-or(op:numeric-less-than(A, B), op:numeric-equal(A, B)) | xsd:boolean |
| A <= B | simple literal | simple literal | fn:not(op:numeric-equal(fn:compare(A, B), 1)) | xsd:boolean |
| A <= B | xsd:string | xsd:string | fn:not(op:numeric-equal(fn:compare(STR(A), STR(B)), 1)) | xsd:boolean |
| A <= B | xsd:boolean | xsd:boolean | fn:not(op:boolean-greater-than(A, B)) | xsd:boolean |
| A <= B | xsd:dateTime | xsd:dateTime | fn:not(op:dateTime-greater-than(A, B)) | xsd:boolean |
| A >= B | numeric | numeric | logical-or(op:numeric-greater-than(A, B), op:numeric-equal(A, B)) | xsd:boolean |
| A >= B | simple literal | simple literal | fn:not(op:numeric-equal(fn:compare(A, B), -1)) | xsd:boolean |
| A >= B | xsd:string | xsd:string | fn:not(op:numeric-equal(fn:compare(STR(A), STR(B)), -1)) | xsd:boolean |
| A >= B | xsd:boolean | xsd:boolean | fn:not(op:boolean-less-than(A, B)) | xsd:boolean |
| A >= B | xsd:dateTime | xsd:dateTime | fn:not(op:dateTime-less-than(A, B)) | xsd:boolean |
| XPath Arithmetic | ||||
| A * B | numeric | numeric | op:numeric-multiply(A, B) | numeric |
| A / B | numeric | numeric | op:numeric-divide(A, B) | numeric; but xsd:decimal if both operands are xsd:integer |
| A + B | numeric | numeric | op:numeric-add(A, B) | numeric |
| A - B | numeric | numeric | op:numeric-subtract(A, B) | numeric |
| SPARQL Tests | ||||
| A = B | RDF term | RDF term | RDFterm-equal(A, B) | xsd:boolean |
| A != B | RDF term | RDF term | fn:not(RDFterm-equal(A, B)) | xsd:boolean |
xsd:boolean function arguments marked with "(EBV)" are coerced to xsd:boolean by evaluating the effective boolean value of that argument.
Operator Extensibility
SPARQL language extensions may provide additional associations between operators and
operator functions; this amounts to adding rows to the table above. No additional
operator may yield a result that replaces any result other than a type error in the
semantics defined above. The consequence of this rule is that SPARQL FILTERs will produce at least the same intermediate bindings after applying a FILTER as an unextended implementation.
Additional mappings of the '<' operator are expected to control the relative ordering
of the operands, specifically, when used in an ORDER BY clause.
Copyright © 2013 W3C® (MIT, ERCIM, Keio, Beihang). This software or document includes material copied from or derived from SPARQL 1.1 Query.