SPARQL Functions Reference¶
What’s in this document?
This section lists all supported SPARQL functions in GraphDB. The function specifications include the types of the arguments and the output. Types from XML Schema should be readily recognizable as they start with the xsd:
prefix. In addition, the following more generic types are used:
rdfTerm
Any RDF value: a literal, a blank node or an IRI.
iri
An IRI.
bnode
A blank node.
literal
A literal regardless of its datatype or the presence of a language tag.
string
A plain literal or a literal with a language tag. Note that plain literals have the implicit datatype
xsd:string
.numeric
A literal with a numeric XSD datatype, e.g.
xsd:double
andxsd:long
.variable
A SPARQL variable.
expression
A SPARQL expression that may use any constants and variables to compute a value.
SPARQL 1.1 functions¶
Function |
Description |
---|---|
|
Returns |
|
The IF function form evaluates the first argument, interprets it as a effective boolean value, then returns the value of e2 if the EBV is true, otherwise it returns the value of e3. Only one of e2 and e3 is evaluated. If evaluating the first argument raises an error, then an error is raised for the evaluation of the IF expression. More |
|
The COALESCE function form returns the RDF term value of the first expression that evaluates without error. In SPARQL, evaluating an unbound variable raises an error. If none of the arguments evaluates to an RDF term, an error is raised. If no expressions are evaluated without error, an error is raised. More |
xsd:boolean NOT EXISTS { pattern } xsd:boolean EXISTS { pattern } |
There is a filter operator EXISTS that takes a graph pattern. EXISTS returns |
|
Returns a logical OR of left and right. Note that logical-or operates on the effective boolean value of its arguments. More |
|
Returns a logical AND of left and right. Note that logical-and operates on the effective boolean value of its arguments. More |
|
Returns |
|
Returns |
|
The IN operator tests whether the RDF term on the left-hand side is found in the values of list of expressions on the right-hand side. The test is done with |
|
The NOT IN operator tests whether the RDF term on the left-hand side is not found in the values of list of expressions on the right-hand side. The test is done with |
xsd:boolean isIRI(rdfTerm term) xsd:boolean isURI(rdfTerm term) |
Returns |
|
Returns |
|
Returns |
|
Returns |
xsd:string STR(literal ltrl) xsd:string STR(iri rsrc) |
Returns the lexical form of ltrl (a literal); returns the codepoint representation of rsrc (an IRI). This is useful for examining parts of an IRI, for instance, the hostname. More |
|
Returns the language tag of the literal ltrl, if it has one. It returns “” if ltrl has no language tag. Note that the RDF data model does not include literals with an empty language tag. More |
|
Returns the datatype IRI of the literal ltrl. More |
iri IRI(string str) iri IRI(iri rsrc) iri URI(string str) iri URI(iri rsrc) |
The IRI function constructs an IRI by resolving the string argument str. The IRI is resolved against the base IRI of the query and must result in an absolute IRI. If the function is passed an IRI rsrc, it returns the IRI unchanged. More |
bnode BNODE() bnode BNODE(string str) |
The BNODE function constructs a blank node that is distinct from all blank nodes in the dataset being queried and distinct from all blank nodes created by calls to this constructor for other query solutions. If the no argument form is used, every call results in a distinct blank node. If the form with the string str is used, every call results in distinct blank nodes for different strings, and the same blank node for calls with the same string within expressions for one solution mapping. More |
|
The STRDT function constructs a literal with lexical form and type as specified by the arguments. More |
|
The STRLANG function constructs a literal with lexical form and language tag as specified by the arguments. More |
|
Return a fresh IRI from the UUID URN scheme. Each call of UUID() returns a different UUID. More |
|
Return a string that is the scheme specific part of UUID. That is, as a string literal, the result of generating a UUID, converting to a string literal and removing the initial |
|
The STRLEN function corresponds to the XPath |
string SUBSTR(string source, xsd:integer startingLoc) string SUBSTR(string source, xsd:integer startingLoc, xsd:integer length) |
The SUBSTR function corresponds to the XPath |
|
The UCASE function corresponds to the XPath |
|
The LCASE function corresponds to the XPath |
|
The STRSTARTS function corresponds to the XPath |
|
The STRENDS function corresponds to the XPath |
|
The CONTAINS function corresponds to the XPath |
|
The STRBEFORE function corresponds to the XPath |
|
The STRAFTER function corresponds to the XPath |
|
The ENCODE_FOR_URI function corresponds to the XPath |
|
The CONCAT function corresponds to the XPath The lexical form of the returned literal is obtained by concatenating the lexical forms of its inputs. If all input literals are literals with identical language tag, then the returned literal is a literal with the same language tag, in all other cases, the returned literal is a simple literal. More |
|
Returns |
xsd:boolean REGEX(string text, xsd:string pattern) xsd:boolean REGEX(string text, xsd:string pattern, xsd:string flags) |
Invokes the XPath |
string REPLACE(string arg, xsd:string pattern, xsd:string replacement) string REPLACE(string arg, xsd:string pattern, xsd:string replacement, xsd:string flags) |
The REPLACE function corresponds to the XPath |
|
Returns the absolute value of num. An error is raised if the argument is not a numeric value. |
|
Returns the number with no fractional part that is closest to num. If there are two such numbers, then the one that is closest to positive infinity is returned. An error is raised if the argument is not a numeric value. |
|
Returns the smallest (closest to negative infinity) number with no fractional part that is not less than the value of num. An error is raised if the argument is not a numeric value. |
|
Returns the largest (closest to positive infinity) number with no fractional part that is not greater than the value of num. An error is raised if the argument is not a numeric value. |
|
Returns a pseudo-random number between 0 (inclusive) and 1.0 (exclusive). Different numbers can be produced every time this function is invoked. Numbers should be produced with approximately equal probability. More |
|
Returns an XSD dateTime value for the current query execution. All calls to this function in any one query execution will return the same value. The exact moment returned is not specified. More |
|
Returns the year part of arg as an integer.
|
|
Returns the month part of arg as an integer.
|
|
Returns the day part of arg as an integer.
|
|
Returns the hours part of arg as an integer. The value is as given in the lexical form of the XSD dateTime.
|
|
Returns the minutes part of the lexical form of arg. The value is as given in the lexical form of the XSD dateTime.
|
|
Returns the seconds part of the lexical form of arg.
|
|
Returns the timezone part of arg as an xsd:dayTimeDuration. Raises an error if there is no timezone.
|
|
Returns the timezone part of |
xsd:string MD5(xsd:string arg) |
Returns the MD5 checksum, as a hex digit string, calculated on the UTF-8 representation of the lexical form of the argument. More |
xsd:string SHA1(xsd:string arg) |
Returns the SHA1 checksum, as a hex digit string, calculated on the UTF-8 representation of the lexical form of the argument. More |
xsd:string SHA256(xsd:string arg) |
Returns the SHA256 checksum, as a hex digit string, calculated on the UTF-8 representation of the lexical form of the argument. More |
xsd:string SHA512(xsd:string arg) |
Returns the SHA512 checksum, as a hex digit string, calculated on the UTF-8 representation of the lexical form of the argument. More |
SPARQL 1.1 constructor functions¶
Casting in SPARQL 1.1 is performed by calling a constructor function for the target type on an operand of the source type. The standard includes the following constructor functions:
Note
Note that SPARQL 1.1 does not have an xsd:date
constructor.
Constructor function |
Description |
---|---|
|
Casts value to |
|
Casts value to |
|
Casts value to |
|
Casts value to |
|
Casts value to |
|
Casts value to |
|
Casts value to |
|
Casts value to |
|
Casts value to |
|
Casts value to |
|
Casts value to |
|
Casts value to |
|
Casts value to |
|
Casts value to |
|
Casts value to |
|
Casts value to |
|
Casts value to |
|
Casts value to |
|
Casts value to |
Mathematical function extensions¶
Beside the standard SPARQL functions operating on numbers, GraphDB offers several additional functions, allowing users to do more mathematical operations. These are implemented using Java’s Math class.
The prefix ofn:
stands for the namespace <http://www.ontotext.com/sparql/functions/>
.
Function |
Description |
---|---|
|
The arccosine function. The input is in the range \([-1, +1]\). The output is in the range \([0, \pi]\) radians. See Math.acos(double).
|
|
The arcsine function. The input is in the range \([-1, +1]\). The output is in the range \([-\pi/2, \pi/2]\) radians. See Math.asin(double).
|
|
The arctangent function. The output is in the range \((-\pi/2, \pi/2)\) radians. See Math.atan(double).
|
|
The double-argument arctangent function (the angle component of the conversion from rectangular coordinates to polar coordinates). The output is in the range \([-\pi/2, \pi/2]\) radians. See Math.atan2(double,double).
|
|
The cubic root function. See Math.cbrt(double).
|
|
Returns the first floating-point argument with the sign of the second floating-point argument. See Math.copySign(double,double).
|
|
The cosine function. The argument is in radians. See Math.cos(double).
|
|
The hyperbolic cosine function. See Math.cosh(double).
|
|
Returns the double value that is closer than any other to \(e\), the base of the natural logarithms. See Math.E.
|
|
The exponent function, \(e^x\). See Math.exp(double).
|
|
Returns \(e^x - 1\). See Math.expm1(double).
|
|
Returns the largest (closest to positive infinity) int value (as a double number) that is less than or equal to the algebraic quotient. The arguments are implicitly cast to long. See Math.floorDiv(long,long).
|
|
Returns the floor modulus (as a double number) of the arguments. The arguments are implicitly cast to long. See Math.floorMod(long,long).
|
|
Returns the unbiased exponent used in the representation of a double. This means that we take n from the binary representation of x: \(x = 1 \times 2^n + \{1|0\} \times 2^{(n-1)} + ... + \{1|0\} \times 2^0\), i.e., the power of the highest non-zero bit of the binary form of x. See Math.getExponent(double).
|
|
Returns \(sqrt(x^2 +y^2)\) without intermediate overflow or underflow. See Math.hypot(double,double).
|
|
Computes the remainder operation on two arguments as prescribed by the IEEE 754 standard. See Math.IEEEremainder(double,double).
|
|
The natural logarithm function. See Math.log(double).
|
|
The common (decimal) logarithm function. See Math.log10(double).
|
|
Returns the natural logarithm of the sum of the argument and 1. See Math.log1p(double).
|
|
The greater of two numbers. See Math.max(double,double).
|
|
The smaller of two numbers. See Math.min(double,double).
|
|
Returns the floating-point number adjacent to the first argument in the direction of the second argument. See Math.nextAfter(double,double).
|
|
Returns the floating-point value adjacent to d in the direction of negative infinity. See Math.nextDown(double).
|
|
Returns the floating-point value adjacent to d in the direction of positive infinity. See Math.nextUp(double).
|
|
Returns the double value that is closer than any other to \(\pi\), the ratio of the circumference of a circle to its diameter. See Math.PI.
|
|
The power function. See Math.pow(double,double).
|
|
Returns the double value that is closest in value to the argument and is equal to a mathematical integer. See Math.rint(double).
|
|
Returns \(d \times 2^{scaleFactor}\) rounded as if performed by a single correctly rounded floating-point multiply to a member of the double value set. See Math.scalb(double,int).
|
|
Returns the signum function of the argument; zero if the argument is zero, 1.0 if the argument is greater than zero, -1.0 if the argument is less than zero. See Math.signum(double).
|
|
The sine function. The argument is in radians. See Math.sin(double).
|
|
The hyperbolic sine function. See Math.sinh(double).
|
|
The square root function. See Math.sqrt(double).
|
|
The tangent function. The argument is in radians. See Math.tan(double).
|
|
The hyperbolic tangent function. See Math.tanh(double).
|
|
Converts an angle measured in radians to an approximately equivalent angle measured in degrees. See Math.toDegrees(double).
|
|
Converts an angle measured in degrees to an approximately equivalent angle measured in radians. See Math.toRadians(double).
|
|
Returns the size of an ulp of the argument. An ulp, unit in the last place, of a double value is the positive distance between this floating-point value and the double value next larger in magnitude. Note that for non-NaN x, ulp(-x) == ulp(x). See Math.ulp(double).
|
Date and time function extensions¶
Beside the standard SPARQL functions related to date and time, GraphDB offers several additional functions, allowing users to do more with their temporal data.
The prefix ofn:
stands for the namespace <http://www.ontotext.com/sparql/functions/>
. For more information, refer to Time Functions Extensions.
Function |
Description |
---|---|
|
Return the “years” part of the duration literal |
|
Returns the “months” part of the duration literal |
|
Returns the “days” part of the duration literal |
|
Returns the “hours” part of the duration literal |
|
Returns the “minutes” part of the duration literal |
|
Returns the “seconds” part of the duration literal |
|
Returns the “milliseconds” part of the duration literal |
|
Returns the duration of the period as weeks |
|
Returns the duration of the period as days |
|
Returns the duration of the period as hours |
|
Returns the duration of the period as minutes |
|
Returns the duration of the period as seconds |
|
Returns the duration of the period as milliseconds |
|
Returns the duration between the two dates as weeks |
|
Returns the duration between the two dates as days |
|
Returns the duration between the two dates as hours |
|
Returns the duration between the two dates as minutes |
|
Returns the duration between the two dates as seconds |
|
Returns the duration between the two dates as milliseconds |
SPARQL SPIN functions and magic predicates¶
The following SPIN SPARQL functions and magic predicates are available in GraphDB. The prefix spif:
stands for the namespace <http://spinrdf.org/spif#>
.
SPIN functions that work on text use 0-based indexes, unlike SPARQL’s functions, which use 1-based indexes.
SPARQL functions¶
Function |
Definition |
|
---|---|---|
Dates |
|
Parses date using format with Java’s SimpleDateFormat |
|
Formats date using format with Java SimpleDateFormat |
|
|
The current time in milliseconds since the epoch |
|
|
The time in milliseconds since the epoch for the provided argument |
|
Numbers |
|
Remainder from integer division |
|
Formats number using format with Java’s DecimalFormat |
|
|
Calls Java’s Math.random() |
|
Strings |
|
Calls |
|
UUID generation as a literal.
Same as SPARQL’s |
|
|
Same as SPARQL’s |
|
|
Position of first occurrence of a substring.
Note that SPIN functions that work on text use 0-based
indexes, unlike SPARQL’s functions, which use 1-based
indexes.
|
|
|
Position of last occurrence of a substring.
Note that SPIN functions that work on text use 0-based
indexes, unlike SPARQL’s functions, which use 1-based
indexes.
|
|
|
Builds a literal from a template, e.g. “foo {?2} bar {?1}”
where |
|
|
Calls Java |
|
|
Converts camel-cased string to non-camel case string with spaces |
|
|
Converts to upper case, similar to SPARQL’s |
|
|
Converts to lower case, similar to SPARQL’s |
|
|
Converts to title case (each word starts with a capital).
You can use this function to convert a multi-word string to
string to CamelCase as suitable for a class name as follows:
|
|
|
Converts to lower title case (each but the first word starts
with a capital. You can use this function to convert a
multi-word string to camelCase as suitable for a property
as follows: |
|
IRIs |
|
Calls |
|
Calls |
|
|
Builds a URI from a template, e.g. “<urn:{?1}/{?2}>”
where |
|
|
According to some standards, note that it allows spaces too |
|
Functions |
|
Invokes a function indirectly (supplied as an IRI) with the provided arguments |
|
Checks if the given function can be invoked with the given arguments |
Magic predicates¶
Magic predicates are similar to how GraphDB plugins can interpret certain triple patterns. Unlike a function, they can return multiple values per call.
The magic predicates are used as triple patterns like this: ?result :magicPredicate (“arg1” “arg2” …)
There are three magic predicates: spif:split
, spif:for
, and spif:foreach
.
Predicate |
Descriptionn |
---|---|
|
Takes two arguments: a string to split and a regex to split on. The current implementation uses Java’s |
|
Generates bindings from a given start integer value to another given end integer value. |
|
Generates bindings for the given arguments arg1, arg2 and so on. |
RDF-star extension functions¶
To avoid any parsing of an embedded triple, GraphDB introduces the following SPARQL functions:
Function |
Definition |
---|---|
|
Checks if the variable var is bound to an embedded triple |
iri rdf:subject(variable var) iri rdf:predicate(variable var) rdfTerm rdf:object(variable var) |
Extracts the subject, predicate, or object from a variable bound to an embedded triple |
|
Creates a new embedded statement with the provided values |
See more about RDF-star/SPARQL-star syntax here.
GeoSPARQL functions¶
The following functions are defined by the GeoSPARQL standard. For more information, refer to OGC GeoSPARQL - A Geographic Query Language for RDF Data. The prefix geof:
stands for the namespace <http://www.opengis.net/def/function/geosparql/>
.
The type geomLiteral
serves as a placeholder for any GeoSPARQL literal that describes a geometry.
Function |
Description |
---|---|
|
Returns the shortest distance in units between any two Points in the two geometric objects as calculated in the spatial reference system of geom1. |
|
This function returns a geometric object that represents all Points whose distance from geom1 is less than or equal to the radius measured in units. Calculations are in the spatial reference system of geom1. |
|
This function returns a geometric object that represents all Points in the convex hull of geom1. Calculations are in the spatial reference system of geom1. |
|
This function returns a geometric object that represents all Points in the intersection of geom1 with geom2. Calculations are in the spatial reference system of geom1. |
|
This function returns a geometric object that represents all Points in the union of geom1 with geom2. Calculations are in the spatial reference system of geom1. |
|
This function returns a geometric object that represents all Points in the set difference of geom1 with geom2. Calculations are in the spatial reference system of geom1. |
|
This function returns a geometric object that represents all Points in the set symmetric difference of geom1 with geom2. Calculations are in the spatial reference system of geom1. |
|
This function returns the minimum bounding box of geom1. Calculations are in the spatial reference system of geom1. |
|
This function returns the closure of the boundary of geom1. Calculations are in the spatial reference system of geom1. |
|
Returns the spatial reference system URI for geom. |
|
Returns true if the spatial relationship between geom1 and geom2 corresponds to one with acceptable values for the specified pattern-matrix. Otherwise, this function returns false. Pattern-matrix represents a DE-9IM intersection pattern consisting of T (true) and F (false) values. The spatial reference system for geom1 is used for spatial calculations. |
|
DE-9IM intersection pattern: |
|
DE-9IM intersection pattern: |
|
DE-9IM intersection pattern: |
|
DE-9IM intersection pattern: |
|
DE-9IM intersection pattern: |
|
DE-9IM intersection pattern: |
|
DE-9IM intersection pattern: |
|
DE-9IM intersection pattern: |
|
DE-9IM intersection pattern: |
|
DE-9IM intersection pattern: |
|
DE-9IM intersection pattern: |
|
DE-9IM intersection pattern: |
|
DE-9IM intersection pattern: |
|
DE-9IM intersection pattern: |
|
DE-9IM intersection pattern: |
|
DE-9IM intersection pattern: |
|
DE-9IM intersection pattern: |
|
DE-9IM intersection pattern: |
|
DE-9IM intersection pattern: |
|
DE-9IM intersection pattern: |
|
DE-9IM intersection pattern: |
|
DE-9IM intersection pattern: |
|
DE-9IM intersection pattern: |
|
DE-9IM intersection pattern: |
GeoSPARQL extension functions¶
On top of the standard GeoSPARQL functions, GraphDB adds a few useful extensions based on the USeekM library. The prefix ext:
stands for the namespace <http://rdf.useekm.com/ext#>
.
The types geo:Geometry
, geo:Point
, etc. refer to GeoSPARQL types in the http://www.opengis.net/ont/geosparql#
namespace.
See more about GraphDB’s GeoSPARQL extensions here.
Function |
Description |
|
Calculates the area of the surface of the geometry. |
|
For two given geometries, computes the point on the first geometry that is closest to the second geometry. |
|
Tests if the first geometry properly contains the second geometry. Geom1 contains properly geom2 if all geom1 contains geom2 and the boundaries of the two geometries do not intersect. |
|
Tests if the first geometry is covered by the second geometry. Geom1 is covered by geom2 if every point of geom1 is a point of geom2. |
|
Tests if the first geometry covers the second geometry. Geom1 covers geom2 if every point of geom2 is a point of geom1. |
|
Measures the degree of similarity between two geometries. The measure is normalized to lie in the range [0, 1]. Higher measures indicate a greater degree of similarity. |
|
Computes the shortest line between two geometries. Returns it as a LineString object. |
|
Given a maximum deviation from the curve, computes a simplified version of the given geometry using the Douglas-Peuker algorithm. |
|
Given a maximum deviation from the curve, computes a simplified version of the given geometry using the Douglas-Peuker algorithm. Will avoid creating derived geometries (polygons in particular) that are invalid. |
|
Checks whether the input geometry is a valid geometry. |
Geospatial extension query functions¶
At present, there is just one SPARQL extension function. The prefix omgeo:
stands for the namespace <http://www.ontotext.com/owlim/geo#>
.
Function |
Description |
---|---|
|
Computes the distance between two points
in kilometers and can be used in Latitude is limited to the range -90 (South) to +90 (North). Longitude is limited to the range -180 (West) to +180 (East). |
See more about GraphDB’s geospatial extensions here.