Abstract

The scope of this specification is to provide a way to query over quads, as defined in the RDF/JS: Data model specification. Similar to the RDF/JS: Data model specification, this is a low-level specification that provides only essential methods for working across query engine component. High-level querying interfaces can and should be using libraries that implement this interface.

The specification itself consists of a core-part that is the base for all other functions defined.

Low-level methods are using explicit parameters that cannot be omitted.

Additional high-level interfaces are outside of the scope of this specification and should be defined elsewhere.

This document provides a specification of an interface for RDF query engines in a JavaScript environment. The task force which defines this interface was formed by RDF JavaScript library developers with the wish to make existing and future libraries interoperable. This definition strives to provide the minimal necessary interface to enable interoperability of RDF querying libraries.

Currently, this specification only provides interfaces that enable filtering quads based on expressions. Additional interfaces for more general querying support will be defined in the future.

Design elements and principles

Filter expression interfaces

This section introduces interfaces that enable quad sources to be filtered based on a declarative expression.

Goals

FilterableSource interface

    interface FilterableSource {
      FilterResult matchExpression (
        optional Term? subject,
        optional Term? predicate,
        optional Term? obj,
        optional Term? graph,
        optional Expression? expression
      );
    };
    

A FilterableSource is an object that produces a FilterableSourceResult that can emit quads. The emitted quads can be directly contained in this FilterableSource object, or they can be generated on the fly.

FilterableSource is not necessarily an extension of the RDF/JS Source interface, but implementers MAY decide to implement both at the same time.

matchExpression() Returns a FilterableSourceResult that contains a quad stream that processes all quads matching the quad pattern and the expression.

When a Term parameter is defined, and is a NamedNode, Literal or BlankNode, it must match each produced quad, according to the Quad.equals semantics. When a Term parameter is a Variable, or it is undefined, it acts as a wildcard, and can match with any Term.

When matching with graph set to undefined or null it MUST match all the graphs (sometimes called the union graph). To match only the default graph set graph to a DefaultGraph

When an Expression parameter is defined, the complete quad stream is filtered according to this expression. When it is undefined, no filter is applied.

If parameters of type Variable with an equal variable name are in place, then the corresponding quad components in the resulting quad stream MUST be equal.

Expression's MAY contain Variable Term's. If their variable names are equal to Variable's in the given quad pattern, then the Expression MUST be instantiated for each variable's binding in the resulting quad stream when applying the Expression filter.

FilterResult interface

    interface FilterResult {
      Stream quads();
      Promise<QueryResultMetadata> metadata(optional QueryResultMetadataOptions? options);
      Promise<boolean> isSupported();
    };
    

A FilterResult is an object that represents the result of a filter expression of FilterableSource for a given quad pattern and expression. It MAY create results lazily after one of its methods is invoked.

quads() Returns a Stream containing all the quads that matched the given quad pattern and expression.

metadata() Asynchronously returns a QueryResultMetadata, that contains the metadata of the current result.

isSupported() Asynchronously returns a boolean indicating if the requested expression is supported by the FilterableSource. If it returns true, quads() and metadata() MAY produce a valid result. If it returns false, quads() MUST return a stream emitting an error, and metadata() MUST reject.

QueryResultMetadata interface

    interface QueryResultMetadata {
      attribute QueryResultMetadataCount? count;
    };
    

A QueryResultMetadata is an object that represents contains metadata bout a certain query result, such as invoking FilterableSource.matchExpression.

count is an optional field that contains metadata about the number of quads in the result stream.

QueryResultMetadataCount interface

    interface QueryResultMetadataCount {
      attribute string type;
      attribute number value;
    };
    

QueryResultMetadataCount is part of the QueryResultMetadata interface to represent metadata about the number of quads in the result stream.

type indicates the type of counting that was done, and MUST either be "estimate" or "exact".

value indicates an estimate of the number of quads in the stream if type = "estimate", or the exact number of quads in the stream if type = "exact".

QueryResultMetadataOptions interface

    interface QueryResultMetadataOptions {
      attribute string? count;
    };
    

A QueryResultMetadataOptions is an object that gives suggestions on what type of metadata is desired, such as when invoking FilterResult.metadata.

count is an optional field that MAY either contain "estimate" or "exact". If defined, this type MUST correspond to the type in QueryResultMetadataCount.

Expression interface

    interface Expression {
      attribute string expressionType;
    };
    

QueryResultMetadataOptions is an abstract interface that represents a generic expression over a stream of quads.

expressionType contains a value that identifies the concrete interface of the expression, since the Expression itself is not directly instantiated. Possible values include "operator" and "term".

OperatorExpression interface

    interface OperatorExpression {
      attribute string expressionType;
      attribute string operator;
      attribute FrozenArray<Expression> args;
    };
    

An OperatorExpression is represents an expression that applies a given operator on given sub-expressions.

expressionType contains the constant "operator".

operator contains a value that identifies an operator. Possible values can be found in the list of operators.

args contains an array of Expression's on to which the given operator applies. The length of this array depends on the operator.

TermExpression interface

    interface TermExpression {
      attribute string expressionType;
      attribute Term term;
    };
    

A TermExpression is an expression that contains a Term.

expressionType contains the constant "term".

term contains a Term.

ExpressionFactory interface

    interface ExpressionFactory {
      OperatorExpression operatorExpression(string operator, sequence<Expression> args);
      TermExpression termExpression(Term term);
    };
    

ExpressionFactory enables expressions to be created in an idiomatic manner.

operatorExpression creates a new OperatorExpression instance for the given operator and array of arguments.

termExpression creates a new TermExpression instance for the given term.

Expression operators

This section introduces a non-exhaustive list of operators that MAY be used in OperatorExpression.

Implementers MAY decide to support only a subset of these operators.

We omit any formal semantics behind these operators in this specification, and refer to their SPARQL 1.1. semantics.

Relational operators

This section introduces relational operators.
NameInputOutputDescription
!
  1. Literal (xsd:boolean)
Literal (xsd:boolean) The inverse of the given boolean value.
uplus
  1. Literal (numeric)
Literal (numeric) The positive value of the given numeric value.
uminus
  1. Literal (numeric)
Literal (numeric) The negative value of the given numeric value.
*
  1. Literal (numeric)
  2. Literal (numeric)
Literal (numeric) The multiplication of the given values.
/
  1. Literal (numeric)
  2. Literal (numeric)
Literal (numeric) The division of the given values.
+
  1. Literal (numeric)
  2. Literal (numeric)
Literal (numeric) The addition of the given values.
-
  1. Literal (numeric)
  2. Literal (numeric)
Literal (numeric) The subtraction of the given values.
=
  1. Term
  2. Term
Literal (xsd:boolean) If the given values are equal. If the terms are Literals, their lexical value is compared
!=
  1. Term
  2. Term
Literal (xsd:boolean) If the given values are not equal. If the terms are Literals, their lexical value is compared
<
  1. Term
  2. Term
Literal (xsd:boolean) If the first value is lesser than the second value. If the terms are Literals, their lexical value is compared
>
  1. Term
  2. Term
Literal (xsd:boolean) If the first value is greater than the second value. If the terms are Literals, their lexical value is compared
  1. Term
  2. Term
Literal (xsd:boolean) If the first value is lesser than or equal to the second value. If the terms are Literals, their lexical value is compared
  1. Term
  2. Term
Literal (xsd:boolean) If the first value is greater than or equal to the second value. If the terms are Literals, their lexical value is compared

Term functions

This section introduces functions on RDF terms.
NameInputOutputDescription
isiri
  1. Term
Literal (xsd:boolean) Returns true if term is a NamedNode. Returns false otherwise.
isblank
  1. Term
Literal (xsd:boolean) Returns true if term is a BlankNode. Returns false otherwise.
isliteral
  1. Term
Literal (xsd:boolean) Returns true if term is a Literal. Returns false otherwise.
isnumeric
  1. Term
Literal (xsd:boolean) Returns true if term is a Literal with a numeric datatype. Returns false otherwise.
str
  1. Term
Literal (xsd:string) Returns the (lexical) string representation of the given term.
lang
  1. Literal
Literal (xsd:string) Returns the language tag of the given Literal, or the empty string if the Literal has no language tag.
datatype
  1. Literal
NamedNode Returns the datatype of the given Literal.
iri
  1. Literal (xsd:string), NamedNode
NamedNode Resolve the given IRI against the base IRI of the current context.
bnode
  1. Literal (xsd:string), empty
BlankNode Create a new blank node with the given label if provided.
strdt
  1. Literal (xsd:string)
  2. NamedNode
Literal Returns a new Literal with the given datatype.
strlang
  1. Literal (xsd:string)
  2. Literal (xsd:string)
Literal Returns a new Literal with the given language tag.
uuid / NamedNode Returns a new NamedNode following the UUID URN scheme.
struuid / Literal (xsd:string) Returns a new Literal following the UUID URN scheme.

String functions

This section introduces string functions.
NameInputOutputDescription
strlen
  1. Literal (xsd:string)
Literal (xsd:integer) Returns the number of characters of the given string.
substr
  1. Literal (xsd:string)
  2. Literal (xsd:integer)
  3. Literal (xsd:integer) (optional)
Literal (xsd:string) Returns the substring of the given string starting from the given position (second parameter) with the given length (third parameter). If no third parameter is provided, then the maximum length is taken.
ucase
  1. Literal (xsd:string)
Literal (xsd:string) Transform each character in the given string to upper case.
lcase
  1. Literal (xsd:string)
Literal (xsd:string) Transform each character in the given string to lower case.
strstarts
  1. Literal (xsd:string)
  2. Literal (xsd:string)
Literal (xsd:boolean) Check if the given string starts with the given second string.
strends
  1. Literal (xsd:string)
  2. Literal (xsd:string)
Literal (xsd:boolean) Check if the given string ends with the given second string.
strcontains
  1. Literal (xsd:string)
  2. Literal (xsd:string)
Literal (xsd:boolean) Check if the given string contains the given second string.
strbefore
  1. Literal (xsd:string)
  2. Literal (xsd:string)
Literal (xsd:string) From the given first string, find the full string that occurs before the given second string.
strafter
  1. Literal (xsd:string)
  2. Literal (xsd:string)
Literal (xsd:string) From the given first string, find the full string that occurs after the given second string.
encode_for_uri
  1. Literal (xsd:string)
Literal (xsd:string) Apply URI encoding on the given string.
concat
  1. Literal (xsd:string)
  2. (variable number of arguments)
Literal (xsd:string) Concatenation of all the given strings.
langmatches
  1. Literal (xsd:string)
  2. Literal (xsd:string)
Literal (xsd:boolean) If the language tag of the first literal matches the second string.
regex
  1. Literal (xsd:string)
  2. Literal (xsd:string)
  3. Literal (xsd:string) (optional)
Literal (xsd:boolean) Check if on the given first string, the given second regular expression matches. The third parameter indicates optional regex flags.
replace
  1. Literal (xsd:string)
  2. Literal (xsd:string)
  3. Literal (xsd:string)
  4. Literal (xsd:string) (optional)
Literal (xsd:boolean) In the given first string, match the given second regular expression, and replace it with the given third string. The fourth parameter indicates optional regex flags.

Numerical functions

This section introduces numerical functions.
NameInputOutputDescription
abs
  1. Literal (numeric)
Literal (numeric) Returns the absolute value of the given numerical term.
round
  1. Literal (numeric)
Literal (numeric) Returns the number with no fractional part that is closest to the argument, with preference for rounding up.
ceil
  1. Literal (numeric)
Literal (numeric) Returns the smallest (closest to negative infinity) number with no fractional part that is not less than the value of arg.
floor
  1. Literal (numeric)
Literal (numeric) Returns the largest (closest to positive infinity) number with no fractional part that is not greater than the value of arg.
rand / Literal (xsd:double) Returns a pseudo-random, number between 0 (inclusive) and 1.0e0 (exclusive). Different numbers can be produced every time this function is invoked. Numbers should be produced with approximately equal probability.

Date and time functions

This section introduces date and time functions.
NameInputOutputDescription
now / Literal (xsd:dateTime) Returns an XSD dateTime value for the current query execution. All calls to this function in any one query execution must return the same value.
year
  1. Literal (xsd:dateTime)
Literal (xsd:integer) Returns the year of the given date.
month
  1. Literal (xsd:dateTime)
Literal (xsd:integer) Returns the month of the given date.
day
  1. Literal (xsd:dateTime)
Literal (xsd:integer) Returns the day of the given date.
hours
  1. Literal (xsd:dateTime)
Literal (xsd:integer) Returns the hours of the given date.
minutes
  1. Literal (xsd:dateTime)
Literal (xsd:integer) Returns the minutes of the given date.
seconds
  1. Literal (xsd:dateTime)
Literal (xsd:integer) Returns the seconds of the given date.
timezone
  1. Literal (xsd:dateTime)
Literal (xsd:dayTimeDuration) Returns the timezone part of the given date.
tz
  1. Literal (xsd:dateTime)
Literal (xsd:string) Returns the timezone part of the given date.

Hash functions

This section introduces hash functions.
NameInputOutputDescription
md5
  1. Literal (xsd:string)
Literal (xsd:string) Returns the MD5 checksum of the given string.
sha1
  1. Literal (xsd:string)
Literal (xsd:string) Returns the SHA1 checksum of the given string.
sha256
  1. Literal (xsd:string)
Literal (xsd:string) Returns the SHA256 checksum of the given string.
sha384
  1. Literal (xsd:string)
Literal (xsd:string) Returns the SHA384 checksum of the given string.
sha512
  1. Literal (xsd:string)
Literal (xsd:string) Returns the SHA512 checksum of the given string.

XPath constructor functions

This section introduces XPath constructor functions.
NameInputOutputDescription
http://www.w3.org/2001/XMLSchema#string
  1. Term
Literal (xsd:string) Cast the given term to xsd:string
http://www.w3.org/2001/XMLSchema#float
  1. Term
Literal (xsd:float) Cast the given term to xsd:float
http://www.w3.org/2001/XMLSchema#double
  1. Term
Literal (xsd:double) Cast the given term to xsd:double
http://www.w3.org/2001/XMLSchema#decimal
  1. Term
Literal (xsd:decimal) Cast the given term to xsd:decimal
http://www.w3.org/2001/XMLSchema#integer
  1. Term
Literal (xsd:integer) Cast the given term to xsd:integer
http://www.w3.org/2001/XMLSchema#dateTime
  1. Term
Literal (xsd:dateTime) Cast the given term to xsd:dateTime
http://www.w3.org/2001/XMLSchema#date
  1. Term
Literal (xsd:date) Cast the given term to xsd:date
http://www.w3.org/2001/XMLSchema#boolean
  1. Term
Literal (xsd:boolean) Cast the given term to xsd:boolean

Functional forms

This section introduces functional operators.
NameInputOutputDescription
bound
  1. Variable
Literal (xsd:boolean) Returns true if the given variable is bound to a value. Returns false otherwise.
if
  1. Expression
  2. Term
  3. Term
Term Evaluates the expression as effective boolean value. Returns the second argument if true, otherwise the third argument.
coalesce
  1. Expression (variable number of arguments)
Term Returns the first expression result that evaluates without an error.
||
  1. Literal (xsd:boolean)
  2. Literal (xsd:boolean)
Literal (xsd:boolean) Returns a logical OR of left and right. Note that logical-or operates on the effective boolean value of its arguments.
&&
  1. Literal (xsd:boolean)
  2. Literal (xsd:boolean)
Literal (xsd:boolean) Returns a logical AND of left and right. Note that logical-or operates on the effective boolean value of its arguments.
=
  1. Term
  2. Term
Literal (xsd:boolean) Returns true if both terms are the same follow these rules. A type error is produced if both arguments are literals but not the same.
sameterm
  1. Term
  2. Term
Literal (xsd:boolean) Returns true if both terms are the same follow these rules. False is returned instead of type errors.
in
  1. Term
  2. Expression (variable number of arguments)
Literal (xsd:boolean) Tests whether the RDF term on the left-hand side is found in the values of list of expressions on the right-hand side.
notin
  1. Term
  2. Expression (variable number of arguments)
Literal (xsd:boolean) Tests whether the RDF term on the left-hand side is not found in the values of list of expressions on the right-hand side.