Link Search Menu Expand Document
Start for Free

Stored Functions

This page discusses stored functions in Stardog - the ability to resuse common expressions when writing queries.

Page Contents
  1. Overview
  2. Creating and Using Functions
  3. Stored Function Syntax
  4. Additional Function Management
  5. Dependencies Across Stored Functions


Stored functions provide the ability to reuse expressions. This avoids duplication and ensures consistency across instances of the same logic. Stored functions are treated similarly to built-in and user-defined functions in that they can be used in FILTER constraints and BIND assignments in SPARQL queries, path queries and rules.

Creating and Using Functions

Functions are useful to encapsulate computational or business logic for reuse. We can create a new function to compute the permutation using the function add on the CLI:

$ stardog-admin function add "function permutation(?n, ?r) { factorial(?n) / factorial(?n - ?r) }"

We can use this function in a SPARQL query and see that the function is expanded in the query plan:

Explaining Query:

select * where { ?x :p :q. filter(permutation(?x, 3) > 1) }

The Query Plan:

Projection(?x) [#1]
`─ Filter((factorial(?x) / factorial((?x - "3"^^xsd:integer))) > "1"^^xsd:integer) [#1]
   `─ Scan[POS](?x, :p, :q) [#1]

Stored functions are persisted in the system database. The system database should be backed up properly to avoid loss of functions.

Stored Function Syntax

Function definitions provided to the add command must adhere to the following grammar:


FUNCTION ::= 'function' FUNC_NAME '(' ARGS ')' '{' Expression '}'


ARGS ::= [Var [',' Var]* ]?

Prolog ::= // BASE and PREFIX declarations as defined by SPARQL 1.1
Expression ::= // as defined by SPARQL 1.1
Var ::= // as defined by SPARQL 1.1

We can use IRIs or prefixed names as function names and include several functions in one add call:

$ stardog-admin function add "prefix ex: <http://example/> \
     function ex:permutation(?n, ?r) { factorial(?n) / factorial(?n - ?r) } \
     function <http://example/combination>(?n, ?r) { permutation(?n, ?r) / factorial(?r) }"

Stored 2 functions successfully

Additional Function Management

The admin commands cover adding, listing and removing functions.

To list stored functions:

$ stardog-admin function list
FUNCTION combination(?n,?r) {
   ((factorial(?n) / factorial((?n - ?r))) / factorial(?r))

FUNCTION permutation(?n,?r) {
   (factorial(?n) / factorial((?n - ?r)))

To remove stored functions

$ stardog-admin function remove permutation
Removed stored function successfully

See the function command group page in the Stardog Admin CLI Reference to view the details of all function subcommands.

HTTP APIs are also provided to add, list and remove stored functions.

Dependencies Across Stored Functions

Stored functions are compiled at creation time in a way that guarantees they will work indefinitely, even if other functions are removed or changed in ways that would affect them. For this reason, dependent functions need to be reloaded when their dependencies are changed.