7.8 Functions Supported in SPARQL Queries through RDF Graph Support for Apache Jena
SPARQL queries through the support for Apache Jena can use the following kinds of functions.
-
Functions in the function library of the Jena ARQ query engine
-
Native Oracle Database functions for projected variables
-
User-defined functions
- Functions in the ARQ Function Library
- Native Oracle Database Functions for Projected Variables
- User-Defined Functions
Parent topic: RDF Graph Support for Apache Jena
7.8.1 Functions in the ARQ Function Library
SPARQL queries through the support for Apache Jena can use functions in the function library of the Jena ARQ query engine. These queries are executed in the middle tier.
The following examples use the upper-case
and namespace
functions. In these examples, the prefix fn
is <http://www.w3.org/2005/xpath-functions#>
and the prefix afn
is <http://jena.hpl.hp.com/ARQ/function#>
.
PREFIX fn: <http://www.w3.org/2005/xpath-functions#> PREFIX afn: <http://jena.hpl.hp.com/ARQ/function#> SELECT (fn:upper-case(?object) as ?object1) WHERE { ?subject dc:title ?object } PREFIX fn: <http://www.w3.org/2005/xpath-functions#> PREFIX afn: <http://jena.hpl.hp.com/ARQ/function#> SELECT ?subject (afn:namespace(?object) as ?object1) WHERE { ?subject <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?object }
7.8.2 Native Oracle Database Functions for Projected Variables
SPARQL queries through the support for Apache Jena can use native Oracle Database functions for projected variables. These queries and the functions are executed inside the database. Note that the functions described in this section should not be used together with ARQ functions (described in Functions in the ARQ Function Library).
This section lists the supported native functions and provides some examples. In the examples, the prefix oext
is <http://oracle.com/semtech/jena-adaptor/ext/function#>
.
Note:
In the preceding URL, note the spelling jena-adapt
or
, which is retained for compatibility with existing applications and which must be used in queries. The adapter spelling is used in regular text, to follow Oracle documentation style guidelines.
-
oext:upper-literal
converts literal values (except for long literals) to uppercase. For example:PREFIX oext: <http://oracle.com/semtech/jena-adaptor/ext/function#> SELECT (oext:upper-literal(?object) as ?object1) WHERE { ?subject dc:title ?object }
-
oext:lower-literal
converts literal values (except for long literals) to lowercase. For example:PREFIX oext: <http://oracle.com/semtech/jena-adaptor/ext/function#> SELECT (oext:lower-literal(?object) as ?object1) WHERE { ?subject dc:title ?object }
-
oext:build-uri-for-id
converts the value ID of a URI, bNode, or literal into a URI form. For example:PREFIX oext: <http://oracle.com/semtech/jena-adaptor/ext/function#> SELECT (oext:build-uri-for-id(?object) as ?object1) WHERE { ?subject dc:title ?object }
An example of the output might be:
<rdfvid:1716368199350136353>
One use of this function is to allow Java applications to maintain in memory a mapping of those value IDs to the lexical form of URIs, bNodes, or literals. The RDF_VALUE$ table provides such a mapping in Oracle Database.
For a given variable
?var
, if onlyoext:build-uri-for-id(?var)
is projected, the query performance is likely to be faster because fewer internal table join operations are needed to answer the query. -
oext:literal-strlen
returns the length of literal values (except for long literals). For example:PREFIX oext: <http://oracle.com/semtech/jena-adaptor/ext/function#> SELECT (oext:literal-strlen(?object) as ?objlen) WHERE { ?subject dc:title ?object }
7.8.3 User-Defined Functions
SPARQL queries through the support for Apache Jena can use user-defined functions that are stored in the database.
In the following example, assume that you want to define a string length function (my_strlen
) that handles long literals (CLOB) as well as short literals. On the SPARQL query side, this function can be referenced under the namespace of ouext
, which is http://oracle.com/semtech/jena-adaptor/ext/user-def-function#
.
PREFIX ouext: <http://oracle.com/semtech/jena-adaptor/ext/user-def-function#> SELECT ?subject ?object (ouext:my_strlen(?object) as ?obj1) WHERE { ?subject dc:title ?object }
Inside the database, functions including my_strlen
, my_strlen_cl
, my_strlen_la
, my_strlen_lt
, and my_strlen_vt
are defined to implement this capability. Conceptually, the return values of these functions are mapped as shown in Table 7-1.
Table 7-1 Functions and Return Values for my_strlen Example
Function Name | Return Value |
---|---|
my_strlen |
<VAR> |
my_strlen_cl |
<VAR>$RDFCLOB |
my_strlen_la |
<VAR>$RDFLANG |
my_strlen_lt |
<VAR>$RDFLTYP |
my_strlen_vt |
<VAR>$RDFVTYP |
A set of functions (five in all) is used to implement a user-defined function that can be referenced from SPARQL, because this aligns with the internal representation of an RDF resource (in RDF_VALUE$). There are five major columns describing an RDF resource in terms of its value, language, literal type, long value, and value type, and these five columns can be selected out using SEM_MATCH. In this context, a user-defined function simply converts one RDF resource that is represented by five columns to another RDF resource.
These functions are defined as follows:
create or replace function my_strlen(rdfvtyp in varchar2, rdfltyp in varchar2, rdflang in varchar2, rdfclob in clob, value in varchar2 ) return varchar2 as ret_val varchar2(4000); begin -- value if (rdfvtyp = 'LIT') then if (rdfclob is null) then return length(value); else return dbms_lob.getlength(rdfclob); end if; else -- Assign -1 for non-literal values so that application can -- easily differentiate return '-1'; end if; end; / create or replace function my_strlen_cl(rdfvtyp in varchar2, rdfltyp in varchar2, rdflang in varchar2, rdfclob in clob, value in varchar2 ) return clob as begin return null; end; / create or replace function my_strlen_la(rdfvtyp in varchar2, rdfltyp in varchar2, rdflang in varchar2, rdfclob in clob, value in varchar2 ) return varchar2 as begin return null; end; / create or replace function my_strlen_lt(rdfvtyp in varchar2, rdfltyp in varchar2, rdflang in varchar2, rdfclob in clob, value in varchar2 ) return varchar2 as ret_val varchar2(4000); begin -- literal type return 'http://www.w3.org/2001/XMLSchema#integer'; end; / create or replace function my_strlen_vt(rdfvtyp in varchar2, rdfltyp in varchar2, rdflang in varchar2, rdfclob in clob, value in varchar2 ) return varchar2 as ret_val varchar2(3); begin return 'LIT'; end; /
User-defined functions can also accept a parameter of VARCHAR2 type. The following five functions together define a my_shorten_str
function that accepts an integer (in VARCHAR2 form) for the substring length and returns the substring. (The substring in this example is 12 characters, and it must not be greater than 4000 bytes.)
-- SPARQL query that returns the first 12 characters of literal values. -- PREFIX ouext: <http://oracle.com/semtech/jena-adaptor/ext/user-def-function#> SELECT (ouext:my_shorten_str(?object, "12") as ?obj1) ?subject WHERE { ?subject dc:title ?object } create or replace function my_shorten_str(rdfvtyp in varchar2, rdfltyp in varchar2, rdflang in varchar2, rdfclob in clob, value in varchar2, arg in varchar2 ) return varchar2 as ret_val varchar2(4000); begin -- value if (rdfvtyp = 'LIT') then if (rdfclob is null) then return substr(value, 1, to_number(arg)); else return dbms_lob.substr(rdfclob, to_number(arg), 1); end if; else return null; end if; end; / create or replace function my_shorten_str_cl(rdfvtyp in varchar2, rdfltyp in varchar2, rdflang in varchar2, rdfclob in clob, value in varchar2, arg in varchar2 ) return clob as ret_val clob; begin -- lob return null; end; / create or replace function my_shorten_str_la(rdfvtyp in varchar2, rdfltyp in varchar2, rdflang in varchar2, rdfclob in clob, value in varchar2, arg in varchar2 ) return varchar2 as ret_val varchar2(4000); begin -- lang if (rdfvtyp = 'LIT') then return rdflang; else return null; end if; end; / create or replace function my_shorten_str_lt(rdfvtyp in varchar2, rdfltyp in varchar2, rdflang in varchar2, rdfclob in clob, value in varchar2, arg in varchar2 ) return varchar2 as ret_val varchar2(4000); begin -- literal type ret_val := rdfltyp; return ret_val; end; / create or replace function my_shorten_str_vt(rdfvtyp in varchar2, rdfltyp in varchar2, rdflang in varchar2, rdfclob in clob, value in varchar2, arg in varchar2 ) return varchar2 as ret_val varchar2(3); begin return 'LIT'; end; /