Skip to content

Commit

Permalink
Fix collisions of internally added variables for subqueries (#1848)
Browse files Browse the repository at this point in the history
This fixes a bug where names of internal variables were wrongly reused between the body of a subquery (e.g. in the implementation of anonymous nodes (`[]`), and for internal aliases needed for `ORDER BY(expression)` or `GROUP BY(expression)`.
Fixes #1561
  • Loading branch information
RobinTF authored Mar 6, 2025
1 parent 95d7b55 commit de71056
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 13 deletions.
10 changes: 0 additions & 10 deletions src/parser/ParsedQuery.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,6 @@ class ParsedQuery {
return _rootGraphPattern._graphPatterns;
}

// TODO<joka921> This is currently necessary because of the missing scoping of
// subqueries
[[nodiscard]] int64_t getNumInternalVariables() const {
return numInternalVariables_;
}

void setNumInternalVariables(int64_t numInternalVariables) {
numInternalVariables_ = numInternalVariables;
}

private:
// Add a BIND clause to the body of the query. The second argument determines
// whether the `targetVariable` will be part of the visible variables that are
Expand Down
2 changes: 0 additions & 2 deletions src/parser/sparqlParser/SparqlQleverVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1276,9 +1276,7 @@ Visitor::SubQueryAndMaybeValues Visitor::visit(Parser::SubSelectContext* ctx) {
ParsedQuery& query = parsedQuery_;
query._clause = visit(ctx->selectClause());
visitWhereClause(ctx->whereClause(), query);
query.setNumInternalVariables(numInternalVariables_);
query.addSolutionModifiers(visit(ctx->solutionModifier()));
numInternalVariables_ = query.getNumInternalVariables();
auto values = visit(ctx->valuesClause());
// Variables that are selected in this query are visible in the parent query.
for (const auto& variable : query.selectClause().getSelectedVariables()) {
Expand Down
1 change: 0 additions & 1 deletion src/parser/sparqlParser/SparqlQleverVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ class SparqlQleverVisitor {

private:
size_t _blankNodeCounter = 0;
int64_t numInternalVariables_ = 0;
int64_t numGraphPatterns_ = 0;
// The visible variables in the order in which they are encountered in the
// query. This may contain duplicates. A variable is added via
Expand Down
10 changes: 10 additions & 0 deletions test/QueryPlannerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2982,6 +2982,16 @@ TEST(QueryPlanner, Exists) {
filter);
}

// _____________________________________________________________________________
TEST(QueryPlanner, ensureGeneratedInternalVariablesDontClash) {
h::expect("SELECT * { SELECT ?s { ?s <a> [] } ORDER BY RAND() }",
h::OrderBy({std::pair{Var{"?_QLever_internal_variable_1"},
OrderBy::AscOrDesc::Asc}},
h::Bind(h::IndexScanFromStrings(
"?s", "<a>", "?_QLever_internal_variable_0"),
"RAND()", Var{"?_QLever_internal_variable_1"})));
}

// _____________________________________________________________________________
TEST(QueryPlanner, FilterOnNeutralElement) {
h::expect("SELECT * { FILTER(false) }",
Expand Down

0 comments on commit de71056

Please sign in to comment.