-
Notifications
You must be signed in to change notification settings - Fork 54
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
Add a VALUES clause to the query of a SERVICE clause to simplify the execution #1341
Changes from 6 commits
b927302
4780aea
ec083e7
7d90398
28946b9
aeb638e
2afa979
94ad347
051bef2
4673248
fbb6e0b
50525b1
21b42be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,11 +5,14 @@ | |
#include "engine/Service.h" | ||
|
||
#include <absl/strings/str_cat.h> | ||
#include <absl/strings/str_join.h> | ||
#include <absl/strings/str_split.h> | ||
|
||
#include "engine/CallFixedSize.h" | ||
#include "engine/ExportQueryExecutionTrees.h" | ||
#include "engine/Values.h" | ||
#include "engine/VariableToColumnMap.h" | ||
#include "global/RuntimeParameters.h" | ||
#include "parser/TokenizerCtre.h" | ||
#include "parser/TurtleParser.h" | ||
#include "util/Exception.h" | ||
|
@@ -21,10 +24,12 @@ | |
// ____________________________________________________________________________ | ||
Service::Service(QueryExecutionContext* qec, | ||
parsedQuery::Service parsedServiceClause, | ||
GetTsvFunction getTsvFunction) | ||
GetTsvFunction getTsvFunction, | ||
std::shared_ptr<QueryExecutionTree> siblingTree) | ||
: Operation{qec}, | ||
parsedServiceClause_{std::move(parsedServiceClause)}, | ||
getTsvFunction_{std::move(getTsvFunction)} {} | ||
getTsvFunction_{std::move(getTsvFunction)}, | ||
siblingTree_{std::move(siblingTree)} {} | ||
|
||
// ____________________________________________________________________________ | ||
std::string Service::getCacheKeyImpl() const { | ||
|
@@ -92,6 +97,14 @@ | |
serviceIriString.remove_suffix(1); | ||
ad_utility::httpUtils::Url serviceUrl{serviceIriString}; | ||
|
||
// Try to simplify the Service Query using it's sibling Operation. | ||
if (auto valuesClause = getSiblingValuesClause(); valuesClause.has_value()) { | ||
auto openBracketPos = parsedServiceClause_.graphPatternAsString_.find('{'); | ||
parsedServiceClause_.graphPatternAsString_ = | ||
"{\n" + valuesClause.value() + '\n' + | ||
parsedServiceClause_.graphPatternAsString_.substr(openBracketPos + 1); | ||
} | ||
|
||
// Construct the query to be sent to the SPARQL endpoint. | ||
std::string variablesForSelectClause = absl::StrJoin( | ||
parsedServiceClause_.visibleVariables_, " ", Variable::AbslFormatter); | ||
|
@@ -159,6 +172,59 @@ | |
return {std::move(idTable), resultSortedOn(), std::move(localVocab)}; | ||
} | ||
|
||
// ____________________________________________________________________________ | ||
std::optional<std::string> Service::getSiblingValuesClause() const { | ||
joka921 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (siblingTree_ == nullptr) { | ||
return std::nullopt; | ||
} | ||
|
||
const auto& siblingResult = siblingTree_->getResult(); | ||
if (siblingResult->size() > | ||
RuntimeParameters().get<"service-max-value-rows">()) { | ||
return std::nullopt; | ||
} | ||
|
||
std::vector<ColumnIndex> commonColumnIndices; | ||
const auto& siblingVars = siblingTree_->getVariableColumns(); | ||
std::string vars = "("; | ||
for (const auto& localVar : parsedServiceClause_.visibleVariables_) { | ||
auto it = siblingVars.find(localVar); | ||
if (it == siblingVars.end()) { | ||
continue; | ||
} | ||
vars += it->first.name() + " "; | ||
commonColumnIndices.push_back(it->second.columnIndex_); | ||
} | ||
vars.back() = ')'; | ||
|
||
ad_utility::HashSet<std::string> rowSet; | ||
|
||
std::string values = " { "; | ||
for (size_t rowIndex = 0; rowIndex < siblingResult->size(); ++rowIndex) { | ||
std::string row = "("; | ||
for (size_t i = 0; i < commonColumnIndices.size(); ++i) { | ||
const auto& optionalString = ExportQueryExecutionTrees::idToStringAndType( | ||
siblingTree_->getRootOperation()->getIndex(), | ||
siblingResult->idTable()(rowIndex, commonColumnIndices[i]), | ||
siblingResult->localVocab()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Somehow the localVocab accessed here with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll have a look at this. I however don't suspect that the problem is the local vocab. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you send me a reproducer (Dataset + Query) where something doesn't work as expected? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok sry, here is a reproducable example query, i use it with Olympics Dataset but it doesn't use the local dataset anyway:
Expected result: 3 rows with the given There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I figured it out.... |
||
|
||
if (optionalString.has_value()) { | ||
row += optionalString.value().first + ' '; | ||
} | ||
} | ||
row.back() = ')'; | ||
|
||
if (rowSet.contains(row)) { | ||
continue; | ||
} | ||
|
||
rowSet.insert(row); | ||
values += row + " "; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For all your |
||
} | ||
|
||
return "VALUES " + vars + values + "} . "; | ||
} | ||
|
||
// ____________________________________________________________________________ | ||
template <size_t I> | ||
void Service::writeTsvResult(cppcoro::generator<std::string_view> tsvResult, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should add unit tests for this planning (in the QueryPlannerTest.cpp and QueryPlannerTestHelpers.h you can add a matcher for a service clause, have a look at that code, and let me know if there is trouble)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have implemented a simple test for this in this commit, let me know if that is sufficient or provide feedback for further improvements.