Skip to content

OntopSPARQLFunctions

Guohui Xiao edited this page Sep 30, 2015 · 2 revisions

Table of Contents

SPARQL Functions

Thanks to Nika Pona for the first implementation of the SPARQL functions. The implementation involved extending classes for Datalog translation and SQL generation.

Supported SPARQL functions

One can see the list of available SPARQL functions and their description here. Below we list the supported functions and provide some table summarizing the differences within various SQL dialects.

Functional Forms

  • bound
  • logical-or
  • logical-and
  • RDFterm-equal

Functions on RDF Terms

  • isIRI
  • isBlank
  • isLiteral
  • str
  • lang
  • datatype
  • STRLANG
  • UUID
  • STRUUID

Functions on Strings

  • STRLEN
  • SUBSTR
  • UCASE
  • LCASE
  • STRSTARTS
  • STRENDS
  • CONTAINS
  • STRBEFORE
  • STRAFTER
  • ENCODE_FOR_URI
  • CONCAT
  • REGEX
  • REPLACE

Functions on Numerics

  • abs
  • round
  • ceil
  • floor
  • RAND

Functions on Dates and Times:

  • now
  • year
  • month
  • day
  • hours
  • minutes
  • seconds
  • tz

Hash Functions

  • MD5
  • SHA1
  • SHA256
  • SHA512

Dialect Adapters

In columns corresponding to SQL dialects, the list of functions used in implementation that differ from the default behaviour (SQL99). For more information on regex see Regex SPARQL filter.

String functions

SPARQL SQL99/H2 MySQL SQL Server PostgreSQL Oracle Db2 Teiid HsqlDB
STRLEN() LENGTH() CHAR_LENGTH() LEN() SQL99 SQL99 SQL99 SQL99 SQL99
SUBSTR() SUBSTR() SQL99 SQL99 SUBSTRING(... FROM ... FOR) SQL99 SQL99 SQL99 SQL99
UCASE(), LCASE() UPPER(), LOWER() SQL99 SQL99 SQL99 SQL99 SQL99 UCASE(), LCASE() SQL99
STRBEFORE() LEFT(),CHARINDEX() LEFT(), INSTR() LEFT(), CHARINDEX() POSITION() SUBSTR(),INSTR() LEFT(), LOCATE() LEFT(), LOCATE() LEFT(), INSTR()
STRAFTER() SUBSTRING(), CHARINDEX(), LENGTH() SUBSTRING(), LOCATE(), LENGTH() SUBSTRING(), CHARINDEX(), LEN() SUBSTRING(), POSITION(), LENGTH() SUBSTR(), INSTR(), LENGTH() SUBSTR(), LOCATE(), LENGTH() SUBSTRING(), LOCATE(), LENGTH() SUBSTR(), LOCATE(), LENGTH()
ENCODE_FOR_URI() REPLACE() SQL99 SQL99 SQL99 SQL99 SQL99 SQL99
STRENDS() CHARINDEX(),LENGTH() CHAR_LENGTH(),INSTR() RIGHT(), LEN() LENGTH(), POSITION() SUBSTR(), LENGTH() SQL99 RIGHT(), CHAR_LENGTH() RIGHT(), CHAR_LENGTH()
STRSTARTS() RIGHT(), LENGTH() CHAR_LENGTH(),RIGHT() LEFT(), LEN() LEFT(), LENGTH() SUBSTR(), LENGTH() LEFT(), LENGTH() SUBSTRING(), CHAR_LENGTH() SUBSTRING(), CHAR_LENGTH()
CONTAINS() CHARINDEX() INSTR() SQL99 POSITION() INSTR() LOCATE() LOCATE() INSTR()

Numeric functions

Numeric functions are almost uniform across SQL dialects.

SPARQL SQL99/H2/MySQL/Db2 SQL Server PostgreSQL Oracle Teiid HsqlSB
ABS ABS(...) SQL99 SQL99 SQL99 SQL99 SQL99
ROUND ROUND(...) ROUND(... , 0) SQL99 SQL99 ROUND(... , 0) ROUND(..., 0)
CEIL CEIL(...) CEILING(...) SQL99 SQL99 CEILING(...) SQL99
FLOOR FLOOR(...) SQL99 SQL99 SQL99 SQL99 SQL99
RAND RAND() SQL99 RANDOM() dbms_random.random SQL99 SUBSTR(), LOCATE(), LENGTH()

Date and Time functions

SPARQL SQL99/MySQL/H2 PostgreSQL SQL Server Oracle Db2 Teiid HsqlDB
NOW CURRENT_TIMESTAMP() NOW() CURRENT_TIMESTAMP CURRENT_TIMESTAMP CURRENT TIMESTAMP NOW() SQL99
YEAR EXTRACT(YEAR FROM...) SQL99 YEAR(...) SQL99 SQL99 SQL99 SQL99
MONTH EXTRACT(MONTH FROM ...) SQL99 MONTH(...) SQL99 SQL99 SQL99 SQL99
DAY EXTRACT(DAY FROM ...) SQL99 DAY(...) SQL99 SQL99 SQL99 SQL99
HOURS EXTRACT(HOUR FROM ...) SQL99 DATEPART(HOUR, ...) SQL99 SQL99 SQL99 SQL99
MINUTES EXTRACT(MINUTE FROM ...) SQL99 DATEPART(MINUTE, ...) SQL99 SQL99 SQL99 SQL99
SECONDS EXTRACT(SECOND FROM ...) SQL99 DATEPART(SECOND, ...) SQL99 SQL99 SQL99 SQL99

Unsupported SPARQL functions

IF, COALESCE, EXISTS, NOT EXISTS, IN, NOT IN, sameTerm, ISNUMERIC, IRI, BNODE, STRDT, langMatches , timezone and SHA384 functions are not supported yet .

Differences based on the underlying database

Functions on strings and hash functions are implemented differently in various SQL dialects. We report here the current implementation. Most dialects support just one hash algorithm, so the default behavior is to throw an error. Supported hash, tz and uuid functions depend on the underlying database.

h2 oracle mysql sql server postgresql db2 teiid hsqldb
hash functions SHA256 MD5 and SHA1 if DBMS_CRYPTO is enabled MD5 and SHA1 MD5, SHA256, SHA1, SHA512 MD5 none none none
tz no support TIMEZONE_HOUR and TIMEZONE_MINUTE extraction no support TZ extraction modified TIMEZONE_HOUR and TIMEZONE_MINUTE extraction for version>10 TIMEZONE_HOUR and TIMEZONE_MINUTE extraction TIMEZONE_HOUR and TIMEZONE_MINUTE extraction TIMEZONE_HOUR and TIMEZONE_MINUTE extraction
uuid RANDOM_UUID() sys_guid() UUID() NEWID() use md5 to generate a random uuid no support UUID() UUID()

Implementation Details

SPARQL functions on Strings, Numerics, Date , Time and Hash functions are called in SparqlAlgebraToDatalogTranslator class. The following classes are involved:

OBDAVocabulary.java | OBDADataFactory.java| OBDADataFactoryImpl.java| SparqAlgebraToDatalogTranslator.java | SQLGenerator.java

Since functions behave differently in some dialects, the dialect adapter classes have been changed as well:

SQLDialectAdapter.java | SQL99DialectAdapter.java | H2SQlDialectAdapter.java | PostgreSQLDialectAdapter.java | MySQLDialectAdapter.java | TeiidSQLDialectAdapter.java | DB2SQLDialectAdapter.java | SQLServerDialectAdapter.java | HSQLDBDialectAdapter.java

Clone this wiki locally