Dear all,

I'd like to introduce another improvement: (simplified) native support for
custom SPARQL functions.

What you can do with it: in case you want to implement a specialised custom
SPARQL function based on a function available in the underlying database,
you can now do so using the Java ServiceLoader as "plugin mechanism".

To implement a custom native function, you need to do the following things:
1. implement the
interface org.apache.marmotta.kiwi.sparql.function.NativeFunction
2. add a reference to your implementation class (FQN) to

  META-INF/services/org.apache.marmotta.kiwi.sparql.function.NativeFunction
3. add a reference to your implementation class (FQN) to
    META-INF/services/org.openrdf.query.algebra.evaluation.function.Function


The NativeFunction interface extends the basic Sesame functions with some
methods needed for the translation into SQL. Most importantly, these are
isSupported (return true if there is a mapping for the given dialect) and
getNative (return a string representing the function in SQL, taking the
passed string arguments). An example is given e.g. here:

https://git-wip-us.apache.org/repos/asf?p=marmotta.git;a=blob;f=libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/function/string/NReplace.java;h=cd1b848618a727f8a8b1206462d8d87c3cee86fb;hb=refs/heads/develop

This also works for aggregation functions. For example, I have a custom
implementation for the standard deviation:

https://git-wip-us.apache.org/repos/asf?p=marmotta.git;a=blob;f=libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/function/custom/Stddev.java;h=24898de35be95bd73fe44a0265e0bcde542bb5ec;hb=refs/heads/develop

When you are implementing custom SPARQL functions, you should provide also
a Java implementation of the call method so backends that are not supported
can evaluate the function in-memory. At the very least, the call method
should throw an UnsupportedOperationException.

I noticed that there are several people here who would be interested in
such extensions. If you encounter troubles with this, feel free to contact
me here.

Cheers,

Sebastian

Reply via email to