-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b927302
Added Sibling Result integration for Service Operation + test
UNEXENU 4780aea
fixed formatting
UNEXENU ec083e7
Fixed multi-value clause creation and related things
UNEXENU 7d90398
Attempt to create ServiceJoin in QueryPlanner. Doesn't work yet
UNEXENU 28946b9
A small fix in the VALUES clause...
joka921 aeb638e
Fixed duplications in values Clause, MultiColumnJoin
UNEXENU 2afa979
Fixed caching bug + unit tests
UNEXENU 94ad347
Improved tests, added checkCancellation
UNEXENU 051bef2
small changes
joka921 4673248
Merge branch 'refs/heads/master' into ServiceImprovements
joka921 fbb6e0b
merge in the master.
joka921 50525b1
Improve the test coverage etc.
joka921 21b42be
Actually fix the test
joka921 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,18 +24,24 @@ | |
// ____________________________________________________________________________ | ||
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 { | ||
std::ostringstream os; | ||
// TODO: This duplicates code in GraphPatternOperation.cpp . | ||
os << "SERVICE " << parsedServiceClause_.serviceIri_.toSparql() << " {\n" | ||
<< parsedServiceClause_.prologue_ << "\n" | ||
<< parsedServiceClause_.graphPatternAsString_ << "\n}\n"; | ||
<< parsedServiceClause_.graphPatternAsString_ << "\n"; | ||
if (siblingTree_ != nullptr) { | ||
os << siblingTree_->getRootOperation()->getCacheKey() << "\n"; | ||
} | ||
os << "}\n"; | ||
return std::move(os).str(); | ||
} | ||
|
||
|
@@ -92,6 +101,14 @@ Result Service::computeResult([[maybe_unused]] bool requestLaziness) { | |
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 +176,66 @@ Result Service::computeResult([[maybe_unused]] bool requestLaziness) { | |
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->idTable().size() > | ||
RuntimeParameters().get<"service-max-value-rows">()) { | ||
return std::nullopt; | ||
} | ||
|
||
checkCancellation(); | ||
|
||
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; | ||
} | ||
absl::StrAppend(&vars, it->first.name(), " "); | ||
commonColumnIndices.push_back(it->second.columnIndex_); | ||
} | ||
vars.back() = ')'; | ||
|
||
checkCancellation(); | ||
|
||
ad_utility::HashSet<std::string> rowSet; | ||
|
||
std::string values = " { "; | ||
for (size_t rowIndex = 0; rowIndex < siblingResult->idTable().size(); | ||
++rowIndex) { | ||
std::string row = "("; | ||
for (const auto& columnIdx : commonColumnIndices) { | ||
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. Inide the for each row loop (the outer one) should probably be one cancellation check per iterationl |
||
const auto& optionalString = ExportQueryExecutionTrees::idToStringAndType( | ||
siblingTree_->getRootOperation()->getIndex(), | ||
siblingResult->idTable()(rowIndex, columnIdx), | ||
siblingResult->localVocab()); | ||
|
||
if (optionalString.has_value()) { | ||
absl::StrAppend(&row, optionalString.value().first, " "); | ||
} | ||
} | ||
row.back() = ')'; | ||
|
||
if (rowSet.contains(row)) { | ||
continue; | ||
} | ||
|
||
rowSet.insert(row); | ||
absl::StrAppend(&values, row, " "); | ||
|
||
checkCancellation(); | ||
} | ||
|
||
return "VALUES " + vars + values + "} . "; | ||
} | ||
|
||
// ____________________________________________________________________________ | ||
template <size_t I> | ||
void Service::writeTsvResult(cppcoro::generator<std::string_view> tsvResult, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.