SPARQL extensions reference¶
What’s in this document?
This page describes functions that are not part of the W3C SPARQL specification but that are available for use to use in GraphDB.
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 doubleargument 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 floatingpoint argument with the sign of the second floatingpoint 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 + \{10\} \times 2^{(n1)} + ... + \{10\} \times 2^0\), i.e., the power of the highest nonzero 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 floatingpoint number adjacent to the first argument in the direction of the second argument. See Math.nextAfter(double,double).


Returns the floatingpoint value adjacent to d in the direction of negative infinity. See Math.nextDown(double).


Returns the floatingpoint 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 floatingpoint 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 floatingpoint value and the double value next larger in magnitude. Note that for nonNaN x, ulp(x) == ulp(x). See Math.ulp(double).

GraphDB also supports several Jena ARQ simple mathematical function analogs. The prefix afn:
stands for the namespace <http://jena.apache.org/ARQ/function#>
. (See Jena SPARQL extensions for additional Jena functions supported by GraphDB.)
Function 
Description 


Return the minimum of two numbers. 

Return the maximum of two numbers. 

The value of pi as an XSD double. 

The value of e as an XSD double. 

The square root of num. 
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 
RDFstar extension functions¶
To avoid any parsing of an embedded triple, GraphDB introduces the following SPARQL functions:
Function 
Description 


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 RDFstar and SPARQLstar syntax in the Syntax and examples section of this documentation’s RDFstar and SPARQLstar page.
SPARQL functions vs magic predicates¶
Functions and magic predicates are denoted and used differently. Magic predicates are similar to how GraphDB plugins can interpret certain triple patterns, and unlike functions, they can return multiple values per call.
Function calls take the form
ex:function(arg1, arg2, ...)
, where all arguments must be bound. These are most commonly used inBIND
,SELECT
,FILTER
, andORDER BY
clauses but may be used in other places as well.Magic predicates take the form
subject ex:magicPredicate (arg1 arg2 ...)
. In some cases, the arguments are allowed to be unbound, and are then calculated from thesubject
. They are used as query triple patterns that have the magic predicate name in the middle, a parenthesized RDF list of arguments as the object, and the variable where the result should be bound (or a parenthesized list of such variables, in the case of certain OpenAI GPT extensions) as the subject.
SPARQL SPIN functions¶
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 0based indexes, unlike SPARQL’s functions, which use 1based indexes.
Function 
Description 


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 0based
indexes, unlike SPARQL’s functions, which use 1based
indexes.



Position of last occurrence of a substring.
Note that SPIN functions that work on text use 0based
indexes, unlike SPARQL’s functions, which use 1based
indexes.



Builds a literal from a template, e.g. “foo {?2} bar {?1}”
where 


Calls Java 


Converts camelcased string to noncamel 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 multiword 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
multiword 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 
There are three magic predicates: spif:split
, spif:for
, and spif:foreach
.
Predicate 
Description 


Takes two arguments: a string to split and a regex to split on. The current implementation uses Java’s The following query splits the PREFIX spif: <http://spinrdf.org/spif#>
SELECT ?name WHERE {
BIND(str("Marion,Suess,Green") as ?input)
?name spif:split (?input ",") .
}


Generates bindings from a given start integer value to another given end integer value. The following query returns three rows, with the variable PREFIX spif: <http://spinrdf.org/spif#>
SELECT ?result WHERE {
?result spif:for (6 8)
}


Generates bindings for the given arguments arg1, arg2 and so on. They can be of any type. The following query returns three rows, with each row showing PREFIX spif: <http://spinrdf.org/spif#>
PREFIX ex: <http://www.example.com/ns/>
SELECT ?result WHERE {
?result spif:foreach (ex:someuri "some string" 23.7)
}

RDF list function extensions¶
GraphDB supports the below Jena list function analogs.
The prefix list:
stands for the namespace <http://jena.apache.org/ARQ/list#>
.
Function 
Description 


Membership of an RDF List (RDF Collection). Currently in GraphDB, if 

Index of an RDF List (RDF Collection). Currently in GraphDB, if 

Length of an RDF List (RDF Collection). Currently in GraphDB, if 
Note
The Jena behavior is that if list
is not bound or a constant, the function finds and iterates all lists in the graph (can be slow). As mentioned above, currently, GraphDB does not provide support for unbound list
. Support for it will be added with the coming releases.
Aggregation function extensions¶
GraphDB supports the below Jena ARQ aggregate function analogs, which are modeled after the corresponding SQL aggregate functions.
The prefix agg:
stands for the namespace <http://jena.apache.org/ARQ/function/aggregate#>
.
agg:stdev
agg:stdev_samp
agg:stdev_pop
agg:variance
agg:var_samp
agg:var_pop
The stdev_pop()
and stdev_samp()
functions compute the population standard deviation and sample standard deviation, respectively, of the input values. (stdev()
is an alias for stdev_samp()
) Both functions evaluate all input rows matched by the query. The difference is that stdev_samp()
is scaled by 1/(N1) while stdev_pop()
is scaled by 1/N.
The var_samp()
and var_pop()
functions compute the sample variance and population variance, respectively, of the input values. (variance()
is an alias for variance_samp()
) Both functions evaluate all input rows matched by the query. The difference is that variance_samp()
is scaled by 1/(N1) while variance_pop()
is scaled by 1/N.
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. GraphDB supports WKT (datatype geo:wktLiteral
) and GML (datatype geo:gmlLiteral
).
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. See the list following this table for potential unit values to pass as the 

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 patternmatrix. Otherwise, this function returns false. Patternmatrix represents a DE9IM intersection pattern consisting of T (true) and F (false) values. The spatial reference system for geom1 is used for spatial calculations. 

DE9IM intersection pattern: 

DE9IM intersection pattern: 

DE9IM intersection pattern: 

DE9IM intersection pattern: 

DE9IM intersection pattern: 

DE9IM intersection pattern: 

DE9IM intersection pattern: 

DE9IM intersection pattern: 

DE9IM intersection pattern: 

DE9IM intersection pattern: 

DE9IM intersection pattern: 

DE9IM intersection pattern: 

DE9IM intersection pattern: 

DE9IM intersection pattern: 

DE9IM intersection pattern: 

DE9IM intersection pattern: 

DE9IM intersection pattern: 

DE9IM intersection pattern: 

DE9IM intersection pattern: 

DE9IM intersection pattern: 

DE9IM intersection pattern: 

DE9IM intersection pattern: 

DE9IM intersection pattern: 

DE9IM intersection pattern: 
The following are potential geof:distance
unit values:
http://www.opengis.net/def/uom/OGC/1.0/degree
http://www.opengis.net/def/uom/OGC/1.0/GridSpacing
http://www.opengis.net/def/uom/OGC/1.0/metre
http://www.opengis.net/def/uom/OGC/1.0/radian
http://www.opengis.net/def/uom/OGC/1.0/unity
GeoSPARQL extension functions¶
On top of the standard GeoSPARQL functions, GraphDB adds a few useful extensions based on the USeekM library. The prefix geoext:
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 in the GeoSPARQL support section of this documentation.
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 DouglasPeuker algorithm. 

Given a maximum deviation from the curve, computes a simplified version of the given geometry using the DouglasPeuker algorithm. Will avoid creating derived geometries (polygons in particular) that are invalid. 

Checks whether the input geometry is a valid geometry. 
Geospatial extension 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 extension functions.
Jena SPARQL extensions¶
Below are several Jena function analogs supported by GraphDB. (See Mathematical function extensions for additional Jena math functions that GraphDB supports.)
The prefix afn:
stands for the namespace <http://jena.apache.org/ARQ/function#>
.
Function 
Description 

iri apf:splitIRI (namespace localname) iri apf:splitURI (namespace localname) 
Split the IRI or URI into namespace (an IRI) and local name (a string). Compare if given values or bound variables, otherwise set the variable. The object is a list with 2 elements. 

Concatenate the arguments in the object list as strings, and assign to 

Split a string and return a binding for each result. The subject variable should be unbound. The first argument to the object list is the string to be split. The second argument to the object list is a regular expression by which to split the string. The subject 

Return the blank node label if 

The local name of 

The namespace of 

Make a string from the format string and the RDF terms. 

Substring, Java style using 

Synonym for 

Concatenate given strings, using 

Calculate the SHA1 checksum of a literal or URI. 

Current time. (Actually, a fixed moment of the current query execution — see the standard function 
Time functions extensions¶
Beside the standard SPARQL functions related to time, GraphDB offers several additional functions, allowing users to do more with their time data. Those are implemented within the same namespace as standard math functions, <http://www.ontotext.com/sparql/functions/>
. The default prefix for the functions is ofn
.
Period extraction functions¶
The first group of functions is related to accessing particular parts of standard duration literals. For example, the expression 20190324T22:12:29.183+02:00"  "20190419T02:42:28.182+02:00"
will produce the following duration literal: P0Y0M25DT4H29M58.999S
. It is possible to parse the result and obtain the proper parts of it  for example, “25 days”, “4” hours, or more discrete time units. However, instead of having to do this manually, GraphDB offers functions that perform the computations at the engine level. The functions take a period as input and output xsd:long
.
Note
The functions described here perform simple extractions, rather than computing the periods. For example, if you have 40 days in the duration literal, but no months, i.e., P0Y0M40DT4H29M58.999S
, a monthsfromduration
extraction will not return 1 months.
The following table describes the functions that are implemented and gives example results, assuming the literal P0Y0M25DT4H29M58.999S
is passed to them:
Function  Description 
Expected return value 



0 


Returns the “months” part of the duration literal 
0 

Returns the “days” part of the duration literal 
25 

Returns the “hours” part of the duration literal 
4 

Returns the “minutes” part of the duration literal 
29 

Returns the “seconds” part of the duration literal 
58 

Returns the “milliseconds” part of the duration literal 
999 
An example query using a function from this group would be:
PREFIX xsd:<http://www.w3.org/2001/XMLSchema#>
PREFIX ofn:<http://www.ontotext.com/sparql/functions/>
SELECT ?result {
bind (ofn:millisfromduration("P0Y0M25DT4H29M58.999S"^^xsd:dayTimeDuration) as ?result)
}
Period transformation functions¶
The second group of functions is related to transforming a standard duration literal. This reduces the need for performing mathematical transformations on the input date. The functions take a period as input and output xsd:long
.
Note
The transformation is performed with no fractional components. For example, if transformed, the duration literal we used previously, P0Y0M25DT4H29M58.999S
will yield 25 days, rather than 25.19 days.
The following table describes the functions that are implemented and gives example results, assuming the literal P0Y0M25DT4H29M58.999S
is passed to them. Note that the return values are negative since the period is negative:
Function 
Description 
Expected return value 


Returns the duration of the period as weeks 
3 

Returns the duration of the period as days 
25 

Returns the duration of the period as hours 
604 

Returns the duration of the period as minutes 
36269 

Returns the duration of the period as seconds 
2176198 

Returns the duration of the period as milliseconds 
2176198999 
An example query using a function from this group would be:
PREFIX xsd:<http://www.w3.org/2001/XMLSchema#>
PREFIX ofn:<http://www.ontotext.com/sparql/functions/>
SELECT ?result {
bind (ofn:asMillis("P0Y0M25DT4H29M58.999S"^^xsd:dayTimeDuration) as ?result)
}
Durations expressed in certain units¶
The third group of functions eliminates the need for computing a difference between two dates when a transformation will be necessary, essentially combining the mathematical operation of subtracting two dates with a transformation. It is more efficient than performing an explicit mathematical operation between two date literals, for example: "20190324T22:12:29.183+02:00"  "20190419T02:42:28.182+02:00"
and then using a transformation function. The functions take two dates as input and output integer literals.
Note
Regular SPARQL subtraction can return negative values, as evidenced by the negative duration literal used in the example. However, comparisons are only positive. So, comparison isn’t an exact match for a subtraction followed by transformation. If one of the timestamps has timezone but the other does not, the result is illdefined.
The following table describes the functions that are implemented and gives example results, assuming the date literals 20190324T22:12:29.183+02:00"
and "20190419T02:42:28.182+02:00"
are passed to them. Note that the return values are positive:
Function 
Description 
Expected return value 


Returns the duration between the two dates as weeks 
3 

Returns the duration between the two dates as days 
25 

Returns the duration between the two dates as hours 
604 

Returns the duration between the two dates as minutes 
36269 

Returns the duration between the two dates as seconds 
2176198 

Returns the duration between the two dates as milliseconds 
2176198999 
An example query using a function from this group would be:
PREFIX xsd:<http://www.w3.org/2001/XMLSchema#>
PREFIX ofn:<http://www.ontotext.com/sparql/functions/>
SELECT ?result {
bind (ofn:millisBetween("20190324T22:12:29.183+02:00"^^xsd:dateTime, "20190419T02:42:28.182+02:00"^^xsd:dateTime) as ?result)
}
Arithmetic operations¶
The fourth group of functions includes operations such as: adding duration to a date; adding dayTimeDuration to a dateTime; adding time duration to a time; comparing durations. This is done via the SPARQL operator extensibility.
OpenAI GPT extensions¶
These magic predicates send queries to an OpenAI GPT model and return the answer as part of your SPARQL query results. Before using any of them, you must configure your access to the model as described in Configuring your use of GPT models.
See GPT functions for more information on these magic predicates.
Function 
Description 


This magic predicate passes one or more messages with instructions to the OpenAI GPT. The results (unlike with gpt:list) are stored in a single binding. The last message passed can be a real number between 0 and 2 to set the temperature of the response. If passing more than one argument in the triple pattern’s object position, enclose them in parentheses. See gpt:ask() — Retrieve a single answer for more information on this magic predicate. 

This magic predicate passes one or more messages with instructions to the OpenAI GPT. These instructions should include a number that indicates how many results that you would like. The results (unlike with gpt:ask) can then be returned as multiple bindings of the specified variable, which will be displayed as separate result set rows. The last message passed can be a real number between 0 and 2 to set the temperature of the response. If passing more than one argument in the triple pattern’s object position, enclose them in parentheses. See gpt:list() — Retrieve a list of answers for more information on this magic predicate. 

This sends one or more requests to create multiple bindings with multiple values — in other words, a table. The last message passed can be a real number between 0 and 2 to set the temperature of the response. Because See gpt:table() — Retrieve a table of answers for more information on this magic predicate. 
List manipulation extension functions¶
Function 
Description 


Combines all of its arguments into an internal list that you can reference using a blank node connected to the list members. You can use this as both a function and as a magic predicate. The helper:iterate() function lets you access individual members of the list. See helper:tuple() — Combine values into a list for more on this function. 

Similar to the helper:tuple function but only takes one argument. If the argument is bound to multiple values, it will aggregate those values into a list that can be accessed by a blank node connected to the list members. You can access individual members of the list using the helper:iterate() function. See helper:tupleAggr() — Aggregate values into a list for more on this function. 

Takes a subject, predicate, and object and combines them into an internal triple connected by a blank node. You can provide the triple components directly or combine using the helper:tuple function. If you do use the See helper:rdf() — Combine values into an RDF triple for more on this function. 

Serializes RDF stored internally with a function such as helper:rdf so that you can see the contents of the triples. The default format is Turtlestar, but you can request a different one by naming the MIME type as an optional second argument. See helper:serializeRDF() — Convert internal RDF to readable triples for more on this function. 

Iterates over the elements of an internal list created by the 