Implementing missing SPARQL functions in QLever - ad-freiburg/qlever GitHub Wiki

Qlever currently still misses implementations of most of SPARQL's builtin functions. The code supporting their implementation is all there, they just need to be implemented, which for each function is just a few lines of code. Here is an example for an imagined unary function SQR:

1. In NaryExpression.h, add the following code. This defines a unary function (hence "NARY" and "1") with a numerical argument (hence "NumericValueGetter"), which can then be used when parsing a query using this function; see the next step.

inline auto square = [](auto x) -> double { return x * x; };
using SquareExpression = NARY<1, FV<decltype(square), NumericValueGetter>>;
using detail::SquareExpression;

2. In SparqlQleverVisitor.h, add the following else-if to antlrcpp::Any visitBuiltInCall. This will link the SQR in the SPARQL query to an invocation of the function sparqlExpression::SquareExpression implemented in Step 1.

else if (ad_utility::getLowercase(context->children[0]->getText()) == "sqr") {
  auto child = std::move(visitExpression(context->expression()[0]).as<ExpressionPtr>());
  return createExpression<sparqlExpression::SquareExpression>(std::move(child));
}

That's it for a function that is part of the SPARQL standard!

The same mechanism can be used to add a function that is not part of the SPARQL standard. For example, to add the imagined function SQR to the set of "builtin" functions, the following additional steps are needed:

3. In the definition of the SPARQL grammar, in the rule for "builtInCall", add | SQR '(' expression ')' to the long list of functions that is already there.

4. Rebuild the automatically generated ANTLR code following these simple instructions.

5. Add the new token to SparqlLexer.cpp, as in ...BIND|MINUS|SQR)";