You are browsing a read-only backup copy of Wikitech. The live site can be found at

User:AndreaWest/Blazegraph Features and Capabilities

From Wikitech-static
Jump to navigation Jump to search

The following is a list of Blazegraph-specific features and capabilities used by WDQS and its community. Defining alternative implementations that minimize the user impact is of critical importance.

Overview of Blazegraph-Specific Features and Capabilities

  • SPARQL functionality extensions
    • Typically, SPARQL is extended by new datatypes and functions
      • The current Blazegraph implementation has a mix of function extensions (geof:distance, geof:globe, geof:latitude, geof:longitude and wikibase:decodeUri) and SERVICE extensions (such as wikibase:label, wikibase:around or wikibase:mwapi)
      • Whereas functions return a single value, the WDQS SERVICES provide multiple outputs
      • Each of the current datatypes/functions and SERVICES are discussed below
  • Named subqueries
    • Documentation
    • Note that although support for subqueries is required for SPARQL compliance, naming is not a compliant feature
    • It is likely that subqueries will not be name-able
    • Based on the placement of the subquery in the overall SPARQL and the use of query hints, a subquery's order of execution can be controlled

SPARQL Functional Extensions

The current Blazegraph functional extensions require the creation of custom SPARQL functions (which are supported by all the Blazegraph alternative backends):

  • The functions, geof:globe, geof:latitude and geof:longitude, are simple decompositions of the geometry data of a POINT
    • In both Wikidata and GeoSPARQL, a geometric POINT utilizes a WKT (well-known text) representation, and is specified by a coordinate system,followed by a longitude/latitude
    • The coordinate system (also known as the spatial reference system) is defined either by WGS84 on Earth or identified by an item ID (within right and left carets, '<' and '>') which specifies a non-Earth/planetary body
    • The geof:globe function retrieves the coordinate system; If a coordinate system is not specified, then the default is <> (Earth)
    • geof:latitude and geof:longitude split the POINT data with longitude specified first and latitude second
    • The wikibase:geoGlobe, wikibase:geoLatitude and wikibase:geoLongitude value properties correspond to the geof:globe, geof:latitude and geof:longitude functions
      • The properties and functions can be used to construct/destruct a geospatial POINT
      • For example, a POINT location can be defined from the value properties using the following graph pattern: BIND (STRDT(CONCAT("POINT(<", wikibase:geoGlobe, "> ", wikibase:geoLongitude, " ", wikibase:geoLatitude, ")"), <>) as ?location)
  • The function, wikibase:decodeURI, will be defined using the logic at

Geospatial Support Using GeoSPARQL

The last Blazegraph property extension is geof:distance. That is supported directly by GeoSPARQL, and is also identified as geof:distance. Blazegraph's geof:distance takes as input two POINTs and returns the distance between them in kilometers. The GeoSPARQL function, geof:distance, also supports the input of two POINTs and adds a third parameter, units (which could be defaulted in the code base to kilometers). Note that GeoSPARQL 1.0 has only a few basic units of measure defined. But, the proposed GeoSPARQL 1.1 specification indicates the use of the Quantities, Units, Dimensions and Types ontology (QUDT) which is much broader.

Beyond the distance function, there are other valuable GeoSPARQL features and functions which could be used in Wikidata queries. These include:

  • Specification of geometries/locations beyond POINTs, such as POLYGONs (which are specified as a group of POINTs that define the geometry's boundary)
  • geof:buffer function, which conceptualizes the space around a geometry (such as a POINT), where the space is defined by a radius given by some units
  • geof:envelope function, which returns the minimal bounding box for an input geometry
    • Given a complex POLYGON, the function would return another POLYGON defining the 4 corners of the minimal bounding box
  • Specification of topology relation functions which compare two geometries and return a boolean indicating if they meet the criteria of the function:
    • geof:sfEquals, returns true if the 2 geometries are equal
    • geof:sfDisjoint, returns true if the 2 geometries are disjoint/separate (inverse of geof:sfEquals)
    • geof:sfIntersects, returns true if any part of the first geometry overlaps with any part of the second
    • geof:sfTouches, returns true if a boundary of the first geometry comes into contact with the boundary of the second (but the interiors of the geometries do NOT intersect)
    • geof:sfCrosses, returns true if the interior of the first geometry comes into contact with the interior or boundary of the second
    • geof:sfWithin, returns true if the second geometry completely encloses the first
    • geof:sfContains, returns true if the first geometry completely encloses the second

Note that some of the above will be used to address the Blazegraph geospatial SERVICEs (wikibase:around and wikibase:box), as explained below.

SERVICE Extensions

This section describes how the Blazegraph-specific SERVICEs (wikibase:label, wikibase:mwapi, wikibase:around, wikibase:box, gas:service, bd:sample and bd:slice) will be supported moving forward.

The geospatial SERVICES, wikibase:around and :box, can (and should) be provided by the use of GeoSPARQL. Details and examples are discussed below.

Unfortunately, there is no straightforward, functional approach for supporting wikibase:mwapi, the GAS service, bd:sample and bd:slice. The problem is that these SERVICEs return multiple (possibly many) results, and some execute based on complex parameters that are defined using unique triple patterns. That combination of requirements does not translate into the standard SPARQL function extensions, which take a set of predefined parameters and return a single result. In order to support these SERVICEs, modifications to the backend code bases will be required - to distinguish local SERVICE IRIs from HTTP federated requests, and then invoke appropriate "handlers".

Note that this discussion did not reference the wikibase:label SERVICE. Label details can be provided using a SPARQL function extension, although that function (described below) will be less convenient than the existing SERVICE approach. If it is decided to extend the various backend alternatives to support custom, local SERVICES, then that same approach should also be applied to wikibase:label.

wikibase:around and wikibase:box

It is reasonable to replace the wikibase:around and :box SERVICEs with graph patterns that utilize the GeoSPARQL topology relation functions discussed above. This approach might be most easily explained by using examples.

Let us first examine a query using the wikibase:around SERVICE, which finds airports within 100km of Berlin:

SELECT ?place ?location ?dist WHERE {
  wd:Q64 wdt:P625 ?berlinLoc .       # Berlin coordinates
  SERVICE wikibase:around { 
      ?place wdt:P625 ?location . 
      bd:serviceParam wikibase:center ?berlinLoc . 
      bd:serviceParam wikibase:radius "100" . 
      bd:serviceParam wikibase:distance ?dist.
  FILTER EXISTS { ?place wdt:P31/wdt:P279* wd:Q1248784 }    # Is an airport
} ORDER BY ASC(?dist)

This could be written as:

prefix uom: <>
SELECT ?place ?location ?dist WHERE {
  wd:Q64 wdt:P625 ?berlinLoc .             # Berlin location
  ?place wdt:P31/wdt:P279* wd:Q1248784 ;   # Get airports
         wdt:P625 ?location .              # And their coordinates
  BIND (geof:distance(?berlinLoc, ?location, uom:meter) as ?dist) .
  FILTER (?dist <= 100000)
} ORDER BY ASC(?dist)

Alternately, the check could be accomplished by the following query:

prefix uom: <>
SELECT ?place ?location ?dist WHERE {
     SELECT ?berlinLoc ?aroundBerlinLoc WHERE {
        wd:Q64 wdt:P625 ?berlinLoc .       # Berlin location
        BIND (geof:buffer(?berlinLoc, 100000, uom:meter) as ?aroundBerlinLoc) }   # Geometry surrounding Berlin
  ?place wdt:P31/wdt:P279* wd:Q1248784 ;   # Get airports
         wdt:P625 ?location .              # And their coordinates
  # Filter if the airport location is within the specified geometry
  FILTER (geof:sfWithin(?location, ?aroundBerlinLoc)) .
  BIND (geof:distance(?berlinLoc, ?location, uom:meter) as ?dist) .    # Get the actual distance after filtering
} ORDER BY ASC(?dist)

In order to support the wikibase:box functionality, a similar approach is taken - although geof:buffer is replaced by a custom geof:box function. For example, this query using wikibase:box finds all schools between San Jose and San Francisco CA:

SELECT ?place ?location WHERE {
  wd:Q62 wdt:P625 ?point1 .     # San Francisco location
  wd:Q16553 wdt:P625 ?point2 .  # San Jose location
  SERVICE wikibase:box {
    ?place wdt:P625 ?location .
    bd:serviceParam wikibase:cornerWest ?point1 .
    bd:serviceParam wikibase:cornerEast ?point2 .
  FILTER EXISTS { ?place wdt:P31/wdt:P279* wd:Q3914 }   # Get schools

It becomes:

prefix uom: <>
SELECT ?place ?location WHERE {
     SELECT ?boundingBox WHERE {
        wd:Q62 wdt:P625 ?westPoint .        # San Francisco location
        wd:Q16553 wdt:P625 ?eastPoint .     # San Jose location
        BIND (geof:box(?westPoint, ?eastPoint) as ?boundingBox) }  
  ?place wdt:P31/wdt:P279* wd:Q3914 ;   # Get schools
         wdt:P625 ?location .           # And their coordinates
  # Filter if the school location is within the specified bounding box
  FILTER (geof:sfWithin(?location, ?boundingBox))   

Note that the above proposes a new function (geof:box) that constructs a bounding polygon based on two POINT locations - where the first parameter is the western-most point and the second parameter is the eastern-most point. The latter is simply a decomposition of the two POINTs into their latitudes and longitudes, and then the creation of a POLYGON using the SPARQL STRDT function. The function could be provided for convenience. If not provided, the functionality is implemented using the following graph patterns:

BIND (geof:latitude(?westPoint) as ?westLat) .
BIND (geof:longitude(?westPoint) as ?westLong).
BIND (geof:latitude(?eastPoint) as ?eastLat) .
BIND (geof:longitude(?eastPoint) as ?eastLong) .
# Note that a POLYGON must be closed (e.g., begin and end at the same POINT)
BIND (CONCAT("POLYGON(", STR(?westLong), " ", STR(?westLat), ", ", STR(?eastLong), " ", STR(?westLat), ", ",
             STR(?eastLong), " ", STR(?eastLat), ", ", STR(?westLong), " ", STR(?eastLat), ", ",
             STR(?westLong), " ", STR(?westLat), ")") as ?polygonString ) .   
BIND (STRDT(?polygonString, geo:wktLiteral) as ?boundingBox) .


The wikibase:label SERVICE provides an easy means to retrieve rdfs:label, skos:altLabel and schema:description values for an entity. Its main uses are to simplify the SPARQL query and to provide language preferences for the text that is returned. The latter is the more significant aspect of the SERVICE and is the main focus of the functions defined here.

The label SERVICE could be implemented in 3 new SPARQL functions, each returning a string literal:

string literal wikibase:label (variable var, "string_of_language_codes")
string literal wikibase:altLabel (variable var, "string_of_language_codes")
string literal wikibase:description (variable var, "string_of_language_codes")

These functions would be used in BIND statements to associate specific variable names to the returned texts, which would then be referenced in the query's SELECT clause or used later in the query, for example in a FILTER statement.

As an example of the use of the current label SERVICE, the following query lists the US presidents and their spouses:

SELECT ?p ?pLabel ?w ?wLabel WHERE {
   wd:Q30 p:P6/ps:P6 ?p .
   ?p wdt:P26 ?w .
   SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en" .

This would be (re)written as:

SELECT ?p ?pLabel ?w ?wLabel WHERE {
   wd:Q30 p:P6/ps:P6 ?p .
   ?p wdt:P26 ?w .
   BIND (wikibase:label(?p, "en") as ?pLabel)
   BIND (wikibase:label(?w, "en") as ?wLabel)

As another example, consider this query which uses the manual mode of the label SERVICE:

     SERVICE wikibase:label {
       bd:serviceParam wikibase:language "fr,de,en" .
       wd:Q123 rdfs:label ?q123Label .
       wd:Q123 skos:altLabel ?q123Alt .
       wd:Q123 schema:description ?q123Desc .
       wd:Q321 rdfs:label ?q321Label .

This would be written as:

     BIND (wikibase:label(wd:Q123, "fr,de,en") as ?q123Label) .
     BIND (wikibase:altLabel(wd:Q123, "fr,de,en") as ?q123Alt) .
     BIND (wikibase:description(wd:Q123, "fr,de,en") as ?q123Desc) .
     BIND (wikibase:label(wd:Q321, "fr,de,en") as ?q321Label) .

The downside of this approach is the need to repeat the language preferences in each function call.

wikibase:mwapi and Blazegraph GAS, bd:sample and bd:slice

The remainder of the Blazegraph SERVICEs are described on the following pages:

In order to provide similar functionality, the backend code base would have to be modified to distinguish a SERVICE invocation addressed to a local IRI (with the prefix, "urn:", "wikibase:" or similar) and an HTTP endpoint. That checking could occur when the SPARQL is being parsed (its algebra/semantics are being defined) or while iterating through/executing the component clauses. It would seem that the later is a better option - since the backend infrastructure would already account for variable bindings.

For a local IRI, it is most logical to check a registry of possible "handlers" and then invoke the appropriate code or return an error. The graph patterns of the SERVICE clause and current variable bindings would be passed to the "handler" code, as is done for all SERVICEs. Results would have to be returned consistent with the SPARQL 1.1 Federated Query specification, meaning that they would be an array of variable-RDF term bindings.

It is likely that the current SERVICE implementations would need to be adapted to the design points of the specific backends, but the majority of the processing logic should be able to be reused.