Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function geof:isWktPoint #1565

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 == "isPointWKT") {
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, "isPointWKT>(?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
Loading