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

Implement [...] (blank node property lists) in SPARQL queries #1279

Merged
merged 9 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions src/global/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ static constexpr std::pair<std::string_view, std::string_view> MATH_PREFIX = {
static const std::string INTERNAL_VARIABLE_PREFIX =
"?_QLever_internal_variable_";

constexpr std::string_view INTERNAL_BLANKNODE_VARIABLE_PREFIX =
"?_QLever_internal_blanknode_variable_";

static constexpr std::string_view TEXTSCORE_VARIABLE_PREFIX = "?ql_textscore_";
static constexpr std::string_view ENTITY_VARIABLE_PREFIX = "?ql_entity_";
static constexpr std::string_view SCORE_VARIABLE_PREFIX = "?ql_score_";
Expand Down
8 changes: 8 additions & 0 deletions src/parser/ParsedQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -633,3 +633,11 @@ Variable ParsedQuery::getNewInternalVariable() {
numInternalVariables_++;
return variable;
}

Variable ParsedQuery::blankNodeToInternalVariable(std::string_view blankNode) {
AD_CONTRACT_CHECK(blankNode.starts_with("_:"));
return Variable{
absl::StrCat(INTERNAL_BLANKNODE_VARIABLE_PREFIX, blankNode.substr(2))};
// This variable is currently not visible in the query body, check whether
// this is correct.
}
4 changes: 3 additions & 1 deletion src/parser/ParsedQuery.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,13 @@ class ParsedQuery {
void addOrderByClause(OrderClause orderClause, bool isGroupBy,
std::string_view noteForImplicitGroupBy);

public:
// Return the next internal variable. Used e.g. by `addInternalBind` and
// `addInternalAlias`
Variable getNewInternalVariable();

public:
static Variable blankNodeToInternalVariable(std::string_view blankNode);

// Add the `modifiers` (like GROUP BY, HAVING, ORDER BY) to the query. Throw
// an `InvalidQueryException` if the modifiers are invalid. This might happen
// if one of the modifiers uses a variable that is either not visible in the
Expand Down
2 changes: 1 addition & 1 deletion src/parser/data/GraphTerm.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "./Iri.h"
#include "./Literal.h"

using GraphTermBase = std::variant<Literal, BlankNode, Iri>;
using GraphTermBase = std::variant<Literal, BlankNode, Iri, Variable>;
hannahbast marked this conversation as resolved.
Show resolved Hide resolved

class GraphTerm : public GraphTermBase,
public VisitMixin<GraphTerm, GraphTermBase> {
Expand Down
6 changes: 5 additions & 1 deletion src/parser/data/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ using Objects = std::vector<VarOrTerm>;
using Tuples = std::vector<std::array<VarOrTerm, 2>>;
using VarOrPath = std::variant<Variable, PropertyPath>;
using PredicateAndObject = std::pair<VarOrPath, VarOrTerm>;
using PathTuples = std::vector<PredicateAndObject>;
using Triples = std::vector<std::array<VarOrTerm, 3>>;
struct TripleWithPropertyPath {
VarOrTerm subject_;
Expand All @@ -25,8 +24,13 @@ struct TripleWithPropertyPath {

bool operator==(const TripleWithPropertyPath&) const = default;
};
using PathTuples = std::vector<PredicateAndObject>;
using PathTuplesAndTriples =
std::pair<PathTuples, std::vector<TripleWithPropertyPath>>;
using Node = std::pair<VarOrTerm, Triples>;
using NodePath = std::pair<VarOrTerm, std::vector<TripleWithPropertyPath>>;
using ObjectList = std::pair<Objects, Triples>;
using ObjectListPath = std::pair<Objects, std::vector<TripleWithPropertyPath>>;
using PropertyList = std::pair<Tuples, Triples>;
using VarOrAlias = std::variant<Variable, Alias>;
} // namespace ad_utility::sparql_types
157 changes: 109 additions & 48 deletions src/parser/sparqlParser/SparqlQleverVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ void Visitor::addVisibleVariable(Variable var) {
}

// ___________________________________________________________________________
PathTuples joinPredicateAndObject(const VarOrPath& predicate,
ObjectList objectList) {
PathTuples joinPredicateAndObject(const VarOrPath& predicate, auto objectList) {
PathTuples tuples;
tuples.reserve(objectList.first.size());
for (auto& object : objectList.first) {
Expand Down Expand Up @@ -315,13 +314,18 @@ GraphPattern Visitor::visit(Parser::GroupGraphPatternContext* ctx) {
// the graph pattern are visible outside the graph pattern.
auto visibleVariablesSoFar = std::move(visibleVariables_);
visibleVariables_.clear();
// TODO<joka921> We also have to change this for Service clauses and possibly
// other places.
auto parsedQuerySoFar = std::move(parsedQuery_);
parsedQuery_ = ParsedQuery{};
auto mergeVariables =
ad_utility::makeOnDestructionDontThrowDuringStackUnwinding(
[this, &visibleVariablesSoFar]() {
[this, &visibleVariablesSoFar, &parsedQuerySoFar]() {
std::swap(visibleVariables_, visibleVariablesSoFar);
visibleVariables_.insert(visibleVariables_.end(),
visibleVariablesSoFar.begin(),
visibleVariablesSoFar.end());
parsedQuery_ = std::move(parsedQuerySoFar);
});
pattern._id = numGraphPatterns_++;
if (ctx->subSelect()) {
Expand Down Expand Up @@ -408,10 +412,13 @@ BasicGraphPattern Visitor::visit(Parser::TriplesBlockContext* ctx) {
return TurtleStringParser<TokenizerCtre>::parseTripleObject(
literal.toSparql());
};
auto visitGraphTerm = [&visitIri, &visitBlankNode,
&visitLiteral](const GraphTerm& graphTerm) {
auto visitVariable = [](Variable var) -> TripleComponent {
return std::move(var);
};
auto visitGraphTerm = [&visitIri, &visitBlankNode, &visitLiteral,
&visitVariable](const GraphTerm& graphTerm) {
return graphTerm.visit(ad_utility::OverloadCallOperator{
visitIri, visitBlankNode, visitLiteral});
visitIri, visitBlankNode, visitLiteral, visitVariable});
hannahbast marked this conversation as resolved.
Show resolved Hide resolved
};
auto varToTripleComponent = [](const Variable& var) {
return TripleComponent{var};
Expand Down Expand Up @@ -610,6 +617,9 @@ vector<GroupKey> Visitor::visit(Parser::GroupClauseContext* ctx) {
std::optional<parsedQuery::ConstructClause> Visitor::visit(
Parser::ConstructTemplateContext* ctx) {
if (ctx->constructTriples()) {
isInsideConstructTriples_ = true;
auto cleanup =
absl::Cleanup{[this]() { isInsideConstructTriples_ = false; }};
return parsedQuery::ConstructClause{visit(ctx->constructTriples())};
} else {
return std::nullopt;
Expand Down Expand Up @@ -695,20 +705,18 @@ void Visitor::visit(Parser::PrefixDeclContext* ctx) {

// ____________________________________________________________________________________
ParsedQuery Visitor::visit(Parser::SelectQueryContext* ctx) {
ParsedQuery query;
query._clause = visit(ctx->selectClause());
parsedQuery_._clause = visit(ctx->selectClause());
visitVector(ctx->datasetClause());
auto [pattern, visibleVariables] = visit(ctx->whereClause());
query._rootGraphPattern = std::move(pattern);
query.registerVariablesVisibleInQueryBody(visibleVariables);
query.addSolutionModifiers(visit(ctx->solutionModifier()));

return query;
parsedQuery_._rootGraphPattern = std::move(pattern);
parsedQuery_.registerVariablesVisibleInQueryBody(visibleVariables);
parsedQuery_.addSolutionModifiers(visit(ctx->solutionModifier()));
return parsedQuery_;
}

// ____________________________________________________________________________________
Visitor::SubQueryAndMaybeValues Visitor::visit(Parser::SubSelectContext* ctx) {
ParsedQuery query;
ParsedQuery& query = parsedQuery_;
query._clause = visit(ctx->selectClause());
auto [pattern, visibleVariables] = visit(ctx->whereClause());
query._rootGraphPattern = std::move(pattern);
Expand Down Expand Up @@ -1070,32 +1078,50 @@ vector<TripleWithPropertyPath> Visitor::visit(
};

if (ctx->varOrTerm()) {
vector<TripleWithPropertyPath> triples;
auto subject = visit(ctx->varOrTerm());
auto tuples = visit(ctx->propertyListPathNotEmpty());
auto [tuples, triples] = visit(ctx->propertyListPathNotEmpty());
for (auto& [predicate, object] : tuples) {
setMatchingWordAndScoreVisibleIfPresent(subject, predicate, object);
triples.emplace_back(subject, std::move(predicate), std::move(object));
}
// TODO<joka921> Can't we move this setting of the matchingWordBla several
// levels up?
for (auto& [s, p, o] : triples) {
setMatchingWordAndScoreVisibleIfPresent(s, p, o);
}
return triples;
} else {
AD_CORRECTNESS_CHECK(ctx->triplesNodePath());
visit(ctx->triplesNodePath());
auto result = visit(ctx->triplesNodePath());
auto additionalTriples = visit(ctx->propertyListPath());
if (additionalTriples.has_value()) {
auto& [tuples, triples] = additionalTriples.value();
std::ranges::copy(triples, std::back_inserter(result));
auto subject = result.at(0).subject_;
for (auto& [predicate, object] : tuples) {
result.emplace_back(subject, std::move(predicate), std::move(object));
}
}
for (auto& [s, p, o] : result) {
setMatchingWordAndScoreVisibleIfPresent(s, p, o);
}
return result;
}
}

// ___________________________________________________________________________
std::optional<PathTuples> Visitor::visit(Parser::PropertyListPathContext* ctx) {
std::optional<PathTuplesAndTriples> Visitor::visit(
Parser::PropertyListPathContext* ctx) {
return visitOptional(ctx->propertyListPathNotEmpty());
}

// ___________________________________________________________________________
PathTuples Visitor::visit(Parser::PropertyListPathNotEmptyContext* ctx) {
PathTuples tuples = visit(ctx->tupleWithPath());
PathTuplesAndTriples Visitor::visit(
Parser::PropertyListPathNotEmptyContext* ctx) {
PathTuplesAndTriples tuples = visit(ctx->tupleWithPath());
vector<PathTuples> tuplesWithoutPaths = visitVector(ctx->tupleWithoutPath());
for (auto& tuplesWithoutPath : tuplesWithoutPaths) {
tuples.insert(tuples.end(), tuplesWithoutPath.begin(),
tuplesWithoutPath.end());
tuples.first.insert(tuples.first.end(), tuplesWithoutPath.begin(),
tuplesWithoutPath.end());
}
return tuples;
}
Expand All @@ -1121,10 +1147,11 @@ PathTuples Visitor::visit(Parser::TupleWithoutPathContext* ctx) {
}

// ____________________________________________________________________________________
PathTuples Visitor::visit(Parser::TupleWithPathContext* ctx) {
PathTuplesAndTriples Visitor::visit(Parser::TupleWithPathContext* ctx) {
VarOrPath predicate = visit(ctx->verbPathOrSimple());
ObjectList objectList = visit(ctx->objectListPath());
return joinPredicateAndObject(predicate, objectList);
ObjectListPath objectList = visit(ctx->objectListPath());
auto predicateObjectPairs = joinPredicateAndObject(predicate, objectList);
return {predicateObjectPairs, std::move(objectList.second)};
}

// ____________________________________________________________________________________
Expand All @@ -1134,15 +1161,25 @@ VarOrPath Visitor::visit(Parser::VerbPathOrSimpleContext* ctx) {
}

// ___________________________________________________________________________
ObjectList Visitor::visit(Parser::ObjectListPathContext* ctx) {
ObjectListPath Visitor::visit(Parser::ObjectListPathContext* ctx) {
// The second parameter is empty because collections and blank not paths,
// which might add additional triples, are currently not supported.
// When this is implemented they will be returned by visit(ObjectPathContext).
return {visitVector(ctx->objectPath()), {}};
auto objectsAndTriples = visitVector(ctx->objectPath());
std::vector<VarOrTerm> objects;
std::ranges::copy(
objectsAndTriples | std::views::transform(ad_utility::first),
std::back_inserter(objects));
std::vector<TripleWithPropertyPath> triples;
std::ranges::copy(objectsAndTriples |
std::views::transform(ad_utility::second) |
std::views::join,
std::back_inserter(triples));
return {std::move(objects), std::move(triples)};
}

// ____________________________________________________________________________________
VarOrTerm Visitor::visit(Parser::ObjectPathContext* ctx) {
NodePath Visitor::visit(Parser::ObjectPathContext* ctx) {
return visit(ctx->graphNodePath());
}

Expand Down Expand Up @@ -1244,7 +1281,13 @@ Node Visitor::visit(Parser::TriplesNodeContext* ctx) {

// ____________________________________________________________________________________
Node Visitor::visit(Parser::BlankNodePropertyListContext* ctx) {
VarOrTerm var{GraphTerm{newBlankNode()}};
VarOrTerm var = [this]() -> VarOrTerm {
if (isInsideConstructTriples_) {
return GraphTerm{newBlankNode()};
} else {
return parsedQuery_.getNewInternalVariable();
}
}();
Triples triples;
auto propertyList = visit(ctx->propertyListNotEmpty());
for (auto& tuple : propertyList.first) {
Expand All @@ -1255,15 +1298,21 @@ Node Visitor::visit(Parser::BlankNodePropertyListContext* ctx) {
}

// ____________________________________________________________________________________
void Visitor::visit(Parser::TriplesNodePathContext* ctx) {
visitAlternative<void>(ctx->blankNodePropertyListPath(),
ctx->collectionPath());
AD_FAIL();
std::vector<TripleWithPropertyPath> Visitor::visit(
Parser::TriplesNodePathContext* ctx) {
return visitAlternative<std::vector<TripleWithPropertyPath>>(
ctx->blankNodePropertyListPath(), ctx->collectionPath());
}

// ____________________________________________________________________________________
void Visitor::visit(Parser::BlankNodePropertyListPathContext* ctx) {
throwCollectionsAndBlankNodePathsNotSupported(ctx);
std::vector<TripleWithPropertyPath> Visitor::visit(
Parser::BlankNodePropertyListPathContext* ctx) {
auto subject = parsedQuery_.getNewInternalVariable();
auto [predicateObjects, triples] = visit(ctx->propertyListPathNotEmpty());
for (auto& [predicate, object] : predicateObjects) {
triples.emplace_back(subject, std::move(predicate), std::move(object));
}
return triples;
}

// ____________________________________________________________________________________
Expand Down Expand Up @@ -1294,8 +1343,9 @@ Node Visitor::visit(Parser::CollectionContext* ctx) {
}

// ____________________________________________________________________________________
void Visitor::visit(Parser::CollectionPathContext* ctx) {
throwCollectionsAndBlankNodePathsNotSupported(ctx);
std::vector<TripleWithPropertyPath> Visitor::visit(
Parser::CollectionPathContext* ctx) {
throwCollectionsNotSupported(ctx);
}

// ____________________________________________________________________________________
Expand All @@ -1309,12 +1359,14 @@ Node Visitor::visit(Parser::GraphNodeContext* ctx) {
}

// ____________________________________________________________________________________
VarOrTerm Visitor::visit(Parser::GraphNodePathContext* ctx) {
NodePath Visitor::visit(Parser::GraphNodePathContext* ctx) {
if (ctx->varOrTerm()) {
return visit(ctx->varOrTerm());
return {visit(ctx->varOrTerm()), {}};
} else {
AD_CORRECTNESS_CHECK(ctx->triplesNodePath());
visit(ctx->triplesNodePath());
auto triples = visit(ctx->triplesNodePath());
auto subject = triples.at(0).subject_;
return {subject, std::move(triples)};
}
}

Expand Down Expand Up @@ -1922,16 +1974,25 @@ bool Visitor::visit(Parser::BooleanLiteralContext* ctx) {
}

// ____________________________________________________________________________________
BlankNode Visitor::visit(Parser::BlankNodeContext* ctx) {
GraphTerm Visitor::visit(Parser::BlankNodeContext* ctx) {
if (ctx->ANON()) {
return newBlankNode();
if (isInsideConstructTriples_) {
return newBlankNode();
} else {
return parsedQuery_.getNewInternalVariable();
}
} else {
AD_CORRECTNESS_CHECK(ctx->BLANK_NODE_LABEL());
// strip _: prefix from string
constexpr size_t length = std::string_view{"_:"}.length();
const string label = ctx->BLANK_NODE_LABEL()->getText().substr(length);
// false means the query explicitly contains a blank node label
return {false, label};
if (isInsideConstructTriples_) {
// strip _: prefix from string
constexpr size_t length = std::string_view{"_:"}.length();
const string label = ctx->BLANK_NODE_LABEL()->getText().substr(length);
// false means the query explicitly contains a blank node label
return BlankNode{false, label};
} else {
return ParsedQuery::blankNodeToInternalVariable(
ctx->BLANK_NODE_LABEL()->getText());
}
}
}

Expand Down
Loading
Loading