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 6 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
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_variable_bn_";

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
12 changes: 11 additions & 1 deletion src/parser/ParsedQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,9 @@ void ParsedQuery::registerVariablesVisibleInQueryBody(
// _____________________________________________________________________________
void ParsedQuery::registerVariableVisibleInQueryBody(const Variable& variable) {
auto addVariable = [&variable](auto& clause) {
clause.addVisibleVariable(variable);
if (!variable.name().starts_with(INTERNAL_VARIABLE_PREFIX)) {
clause.addVisibleVariable(variable);
}
};
std::visit(addVariable, _clause);
}
Expand Down Expand Up @@ -633,3 +635,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
9 changes: 6 additions & 3 deletions 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 All @@ -24,14 +24,17 @@
[[nodiscard]] std::optional<std::string> evaluate(
const ConstructQueryExportContext& context, PositionInTriple role) const {
// TODO<C++23>: Use std::visit when it is possible
return visit([&context, &role](const auto& object) {
return visit([&context, &role]<typename T>(
const T& object) -> std::optional<std::string> {
return object.evaluate(context, role);
});
}

// ___________________________________________________________________________
[[nodiscard]] std::string toSparql() const {
// TODO<C++23>: Use std::visit when it is possible
return visit([](const auto& object) { return object.toSparql(); });
return visit([]<typename T>(const T& object) -> std::string {
return object.toSparql();
});

Check warning on line 38 in src/parser/data/GraphTerm.h

View check run for this annotation

Codecov / codecov/patch

src/parser/data/GraphTerm.h#L36-L38

Added lines #L36 - L38 were not covered by tests
}
};
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
21 changes: 1 addition & 20 deletions src/parser/data/VarOrTerm.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,4 @@
using VarOrTermBase = std::variant<Variable, GraphTerm>;

// TODO: This class should have some documentation.
class VarOrTerm : public VarOrTermBase,
public VisitMixin<VarOrTerm, VarOrTermBase> {
public:
using VarOrTermBase::VarOrTermBase;

// ___________________________________________________________________________
[[nodiscard]] std::optional<std::string> evaluate(
const ConstructQueryExportContext& context, PositionInTriple role) const {
// TODO<C++23>: Use std::visit when it is possible
return visit([&context, &role](const auto& object) {
return object.evaluate(context, role);
});
}

// ___________________________________________________________________________
[[nodiscard]] std::string toSparql() const {
// TODO<C++23>: Use std::visit when it is possible
return visit([](const auto& object) { return object.toSparql(); });
}
};
using VarOrTerm = GraphTerm;
Loading
Loading