Skip to content

Commit

Permalink
Add function geof:isWktPoint
Browse files Browse the repository at this point in the history
The function returns true if and only if the argument is a WKT point.
This can be checked efficiently by checking the datatype bits of the ID.

Note that this function is not part of the GeoSPARQL standard. We add it
because we need it for https://github.com/ad-freiburg/qlever-petrimaps
to fetch alll WKT literals that are not points efficiently.
  • Loading branch information
Hannah Bast committed Oct 18, 2024
1 parent cbdd9bc commit f29ca3d
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/engine/sparqlExpressions/IsSomethingExpressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ namespace detail {
// `...Expression` (`std::move` the arguments into the constructor). The
// function should be declared in `NaryExpression.h`.

// Expressions for `isIRI`, `isBlank`, `isLiteral`, and `isNumeric`. Note that
// the value getters already return the correct `Id`, hence `std::identity`.
// Expressions for the builtin functions `isIRI`, `isBlank`, `isLiteral`,
// `isNumeric`, and the custom function `isWktPoint`. Note that the value
// getters already return the correct `Id`, hence `std::identity`.
using isIriExpression = NARY<1, FV<std::identity, IsIriValueGetter>>;
using isBlankExpression = NARY<1, FV<std::identity, IsBlankNodeValueGetter>>;
using isLiteralExpression = NARY<1, FV<std::identity, IsLiteralValueGetter>>;
using isNumericExpression = NARY<1, FV<std::identity, IsNumericValueGetter>>;
// The expression for `bound` is slightly different because
// `IsValidValueGetter` returns a `bool` and not an `Id`.
using isWktPointExpression = NARY<1, FV<std::identity, IsWktPointValueGetter>>;

// The expression for `bound` is slightly different as `IsValidValueGetter`
// returns a `bool` and not an `Id`.
inline auto boolToId = [](bool b) { return Id::makeFromBool(b); };
using boundExpression = NARY<1, FV<decltype(boolToId), IsValidValueGetter>>;

Expand All @@ -49,6 +52,9 @@ SparqlExpression::Ptr makeIsLiteralExpression(SparqlExpression::Ptr arg) {
SparqlExpression::Ptr makeIsNumericExpression(SparqlExpression::Ptr arg) {
return std::make_unique<detail::isNumericExpression>(std::move(arg));
}
SparqlExpression::Ptr makeIsWktPointExpression(SparqlExpression::Ptr arg) {
return std::make_unique<detail::isWktPointExpression>(std::move(arg));
}
SparqlExpression::Ptr makeBoundExpression(SparqlExpression::Ptr arg) {
return std::make_unique<detail::boundExpression>(std::move(arg));
}
Expand Down
1 change: 1 addition & 0 deletions src/engine/sparqlExpressions/NaryExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ SparqlExpression::Ptr makeIsIriExpression(SparqlExpression::Ptr child);
SparqlExpression::Ptr makeIsBlankExpression(SparqlExpression::Ptr child);
SparqlExpression::Ptr makeIsLiteralExpression(SparqlExpression::Ptr child);
SparqlExpression::Ptr makeIsNumericExpression(SparqlExpression::Ptr child);
SparqlExpression::Ptr makeIsWktPointExpression(SparqlExpression::Ptr child);
SparqlExpression::Ptr makeBoundExpression(SparqlExpression::Ptr child);

// For a `function` that takes `std::vector<SparqlExpression::Ptr>` (size only
Expand Down
12 changes: 12 additions & 0 deletions src/engine/sparqlExpressions/SparqlExpressionValueGetters.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,18 @@ struct IsNumericValueGetter : Mixin<IsNumericValueGetter> {
}
};

// Value getter for `isWktPoint`.
struct IsWktPointValueGetter : Mixin<IsWktPointValueGetter> {
using Mixin<IsWktPointValueGetter>::operator();
Id operator()(ValueId id, const EvaluationContext*) const {
return Id::makeFromBool(id.getDatatype() == Datatype::GeoPoint);
}

Id operator()(const LiteralOrIri&, const EvaluationContext*) const {
return Id::makeFromBool(false);
}
};

/// This class can be used as the `ValueGetter` argument of Expression
/// templates. It produces a `std::optional<DateYearOrDuration>`.
struct DateValueGetter : Mixin<DateValueGetter> {
Expand Down
3 changes: 3 additions & 0 deletions src/parser/sparqlParser/SparqlQleverVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ ExpressionPtr Visitor::processIriFunctionCall(
} else if (functionName == "latitude") {
checkNumArgs(1);
return sparqlExpression::makeLatitudeExpression(std::move(argList[0]));
} else if (functionName == "iswktpoint") {
checkNumArgs(1);
return sparqlExpression::makeIsWktPointExpression(std::move(argList[0]));
}
} else if (checkPrefix(MATH_PREFIX)) {
if (functionName == "log") {
Expand Down
2 changes: 2 additions & 0 deletions test/SparqlAntlrParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1655,6 +1655,8 @@ TEST(SparqlParser, FunctionCall) {
matchUnary(&makeLatitudeExpression));
expectFunctionCall(absl::StrCat(geof, "longitude>(?x)"),
matchUnary(&makeLongitudeExpression));
expectFunctionCall(absl::StrCat(geof, "iswktpoint>(?x)"),
matchUnary(&makeIsWktPointExpression));
expectFunctionCall(
absl::StrCat(geof, "distance>(?a, ?b)"),
matchNary(&makeDistExpression, Variable{"?a"}, Variable{"?b"}));
Expand Down
3 changes: 3 additions & 0 deletions test/SparqlExpressionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,7 @@ TEST(SparqlExpression, testToNumericExpression) {
TEST(SparqlExpression, geoSparqlExpressions) {
auto checkLat = testUnaryExpression<&makeLatitudeExpression>;
auto checkLong = testUnaryExpression<&makeLongitudeExpression>;
auto checkIsWktPoint = testUnaryExpression<&makeIsWktPointExpression>;
auto checkDist = std::bind_front(testNaryExpression, &makeDistExpression);

auto p = GeoPoint(26.8, 24.3);
Expand All @@ -1136,9 +1137,11 @@ TEST(SparqlExpression, geoSparqlExpressions) {

checkLat(v, vLat);
checkLong(v, vLng);
checkIsWktPoint(v, B(true));
checkDist(D(0.0), v, v);
checkLat(idOrLitOrStringVec({"NotAPoint", I(12)}), Ids{U, U});
checkLong(idOrLitOrStringVec({D(4.2), "NotAPoint"}), Ids{U, U});
checkIsWktPoint(IdOrLiteralOrIri{lit("NotAPoint")}, B(false));
checkDist(U, v, IdOrLiteralOrIri{I(12)});
checkDist(U, IdOrLiteralOrIri{I(12)}, v);
checkDist(U, v, IdOrLiteralOrIri{lit("NotAPoint")});
Expand Down

0 comments on commit f29ca3d

Please sign in to comment.