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

Also account for the filters when counting the subgraphs. #1705

Open
wants to merge 4 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
39 changes: 37 additions & 2 deletions src/engine/QueryPlanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1338,13 +1338,44 @@ QueryPlanner::runDynamicProgrammingOnConnectedComponent(

// _____________________________________________________________________________
size_t QueryPlanner::countSubgraphs(
std::vector<const QueryPlanner::SubtreePlan*> graph, size_t budget) {
std::vector<const QueryPlanner::SubtreePlan*> graph,
const std::vector<SparqlFilter>& filters, size_t budget) {
// Remove duplicate plans from `graph`.
auto getId = [](const SubtreePlan* v) { return v->_idsOfIncludedNodes; };
ql::ranges::sort(graph, ql::ranges::less{}, getId);
graph.erase(std::ranges::unique(graph, ql::ranges::equal_to{}, getId).begin(),
graph.end());

// We also have to consider the `filters`. To make life easy, we temporarily
// create simple `SubtreePlans` for them which just have the correct
// variables. We only create one subtree plan for each set of variables that
// is contained in the `filters`, because this will bring the estimate of this
// function closer to the actual behavior of the DP query planner (it always
// applies either all possible filters at once, or none of them).
std::vector<QueryPlanner::SubtreePlan> dummyPlansForFilter;
ad_utility::HashSet<ad_utility::HashSet<Variable>>
deduplicatedFilterVariables;
for (const auto& filter : filters) {
const auto& vars = filter.expression_.containedVariables();
ad_utility::HashSet<Variable> varSet;
// We use a `VALUES` clause as the dummy because this operation is the
// easiest to setup for a number of given variables.
parsedQuery::SparqlValues values;
for (auto* var : vars) {
values._variables.push_back(*var);
varSet.insert(*var);
}
if (deduplicatedFilterVariables.insert(std::move(varSet)).second) {
dummyPlansForFilter.push_back(
makeSubtreePlan<Values>(_qec, std::move(values)));
}
}

const size_t numPlansWithoutFilters = graph.size();
for (const auto& filterPlan : dummyPlansForFilter) {
graph.push_back(&filterPlan);
}

// Qlever currently limits the number of triples etc. per group to be <= 64
// anyway, so we can simply assert here.
AD_CORRECTNESS_CHECK(graph.size() <= 64,
Expand All @@ -1358,7 +1389,11 @@ size_t QueryPlanner::countSubgraphs(
for (size_t i = 0; i < graph.size(); ++i) {
countConnectedSubgraphs::Node v{0};
for (size_t k = 0; k < graph.size(); ++k) {
// Don't connect nodes to themselves, don't connect filters with other
// filters, otherwise connect `i` and `k` if they have at least one
// variable in common.
if ((k != i) &&
(k < numPlansWithoutFilters || i < numPlansWithoutFilters) &&
!QueryPlanner::getJoinColumns(*graph.at(k), *graph.at(i)).empty()) {
v.neighbors_ |= (1ULL << k);
}
Expand Down Expand Up @@ -1424,7 +1459,7 @@ vector<vector<QueryPlanner::SubtreePlan>> QueryPlanner::fillDpTab(
g.push_back(&plan);
}
const size_t budget = RuntimeParameters().get<"query-planning-budget">();
bool useGreedyPlanning = countSubgraphs(g, budget) > budget;
bool useGreedyPlanning = countSubgraphs(g, filters, budget) > budget;
if (useGreedyPlanning) {
LOG(INFO)
<< "Using the greedy query planner for a large connected component"
Expand Down
7 changes: 5 additions & 2 deletions src/engine/QueryPlanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,11 @@ class QueryPlanner {
// if the number of subgraphs is `> budget`. This is used to analyze the
// complexity of the query graph and to choose between the DP and the greedy
// query planner see above.
static size_t countSubgraphs(std::vector<const SubtreePlan*> graph,
size_t budget);
// Note: We also need the added filters, because they behave like additional
// graph nodes wrt the performance of the DP based query planner.
size_t countSubgraphs(std::vector<const SubtreePlan*> graph,
const std::vector<SparqlFilter>& filters,
size_t budget);

// Creates a SubtreePlan for the given text leaf node in the triple graph.
// While doing this the TextLimitMetaObjects are created and updated according
Expand Down
Loading