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 8 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
19 changes: 9 additions & 10 deletions src/engine/QueryExecutionTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@
#include <unordered_map>
#include <unordered_set>

#include "../parser/ParsedQuery.h"
#include "../parser/data/ConstructQueryExportContext.h"
#include "../parser/data/Types.h"
#include "../parser/data/VarOrTerm.h"
#include "../util/Conversions.h"
#include "../util/Generator.h"
#include "../util/HashSet.h"
#include "../util/stream_generator.h"
#include "./Operation.h"
#include "./QueryExecutionContext.h"
#include "engine/Operation.h"
#include "engine/QueryExecutionContext.h"
#include "parser/ParsedQuery.h"
#include "parser/data/ConstructQueryExportContext.h"
#include "parser/data/Types.h"
#include "util/Conversions.h"
#include "util/Generator.h"
#include "util/HashSet.h"
#include "util/stream_generator.h"

using std::shared_ptr;
using std::string;
Expand Down
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
1 change: 0 additions & 1 deletion src/parser/GraphPatternOperation.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "engine/sparqlExpressions/SparqlExpressionPimpl.h"
#include "parser/GraphPattern.h"
#include "parser/TripleComponent.h"
#include "parser/data/VarOrTerm.h"
#include "parser/data/Variable.h"
#include "util/Algorithm.h"
#include "util/VisitMixin.h"
Expand Down
10 changes: 9 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,9 @@ 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))};
}
9 changes: 7 additions & 2 deletions src/parser/ParsedQuery.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "parser/data/SolutionModifiers.h"
#include "parser/data/SparqlFilter.h"
#include "parser/data/Types.h"
#include "parser/data/VarOrTerm.h"
#include "util/Algorithm.h"
#include "util/Exception.h"
#include "util/Generator.h"
Expand Down Expand Up @@ -218,11 +217,17 @@ 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:
// Turn a blank node `_:someBlankNode` into an internal variable
// `?<prefixForInternalVariables>_someBlankNode`. This is required by the
// SPARQL parser, because blank nodes in the bodies of SPARQL queries behave
// like variables.
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
}
};
38 changes: 26 additions & 12 deletions src/parser/data/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,39 @@
#include <tuple>
#include <vector>

#include "../PropertyPath.h"
#include "./VarOrTerm.h"
#include "parser/Alias.h"
#include "parser/PropertyPath.h"
#include "parser/data/GraphTerm.h"

namespace ad_utility::sparql_types {
using Objects = std::vector<VarOrTerm>;
using Tuples = std::vector<std::array<VarOrTerm, 2>>;
// In the following, `Path` stands for `property path` (which can also be a
// single predicate). More precisely, in a type that has `Path` in its name, all
// the stored predicates can be property paths. This is in accordance with the
// terminology of the SPARQL 1.1 grammar. The types without `Path` are used in
// the templates of CONSTRUCT queries where no property paths are allowed.
using Objects = std::vector<GraphTerm>;
using PredicateObjectPairs = std::vector<std::array<GraphTerm, 2>>;
using Triples = std::vector<std::array<GraphTerm, 3>>;
using PredicateObjectPairsAndTriples = std::pair<PredicateObjectPairs, Triples>;
using ObjectsAndTriples = std::pair<Objects, Triples>;
using SubjectOrObjectAndTriples = std::pair<GraphTerm, Triples>;

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>>;
using PathObjectPair = std::pair<VarOrPath, GraphTerm>;
using PathObjectPairs = std::vector<PathObjectPair>;
struct TripleWithPropertyPath {
VarOrTerm subject_;
GraphTerm subject_;
VarOrPath predicate_;
VarOrTerm object_;
GraphTerm object_;

bool operator==(const TripleWithPropertyPath&) const = default;
};
using Node = std::pair<VarOrTerm, Triples>;
using ObjectList = std::pair<Objects, Triples>;
using PropertyList = std::pair<Tuples, Triples>;
using PathObjectPairsAndTriples =
std::pair<PathObjectPairs, std::vector<TripleWithPropertyPath>>;
using SubjectOrObjectAndPathTriples =
std::pair<GraphTerm, std::vector<TripleWithPropertyPath>>;
using ObjectsAndPathTriples =
std::pair<Objects, std::vector<TripleWithPropertyPath>>;

using VarOrAlias = std::variant<Variable, Alias>;
} // namespace ad_utility::sparql_types
38 changes: 0 additions & 38 deletions src/parser/data/VarOrTerm.h

This file was deleted.

Loading
Loading