-
Notifications
You must be signed in to change notification settings - Fork 37
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
Starting 'Select *' implementation #546
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3ada215
Starting 'Select *' implementation
joaomarques90 fe6577e
Update SparqlLexer.cpp
joaomarques90 7b8c3d8
redraw implementation (mainly after exec-results)
joaomarques90 c45ad7a
update sparqlParserTest
joaomarques90 c176e1f
Sugestions from @joka921 mostly implemented
joaomarques90 8cb8570
Continuing the re-factoring of the code
joaomarques90 3bb4aaa
Re-factor from list to set
joaomarques90 085f23d
Ordering the variables selected
joaomarques90 4054695
Log warning implemented
joaomarques90 43d7f9c
Implementing the suggestions made by @joka921
joaomarques90 ed8ea69
Merge branch 'master' into latest
joka921 c331fc4
syncing with the merge & implementing latest suggestions
joaomarques90 afdbb93
run clang-format before merging.
joka921 c40cb3f
Merge branch 'master' into latest
joka921 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
#include <sstream> | ||
#include <string> | ||
#include <utility> | ||
#include <variant> | ||
|
||
#include "../parser/RdfEscaping.h" | ||
#include "./Distinct.h" | ||
|
@@ -91,22 +92,46 @@ void QueryExecutionTree::setVariableColumns( | |
// _____________________________________________________________________________ | ||
template <QueryExecutionTree::ExportSubFormat format> | ||
ad_utility::stream_generator::stream_generator | ||
QueryExecutionTree::generateResults(const vector<string>& selectVars, | ||
size_t limit, size_t offset) const { | ||
QueryExecutionTree::generateResults( | ||
const SelectedVarsOrAsterisk& selectedVarsOrAsterisk, size_t limit, | ||
size_t offset) const { | ||
// They may trigger computation (but does not have to). | ||
shared_ptr<const ResultTable> resultTable = getResult(); | ||
LOG(DEBUG) << "Resolving strings for finished binary result...\n"; | ||
vector<std::optional<pair<size_t, ResultTable::ResultType>>> validIndices; | ||
for (auto var : selectVars) { | ||
if (var.starts_with("TEXT(")) { | ||
var = var.substr(5, var.rfind(')') - 5); | ||
if (selectedVarsOrAsterisk.isAsterisk()) { | ||
auto orderedVariablesFromQuery = | ||
selectedVarsOrAsterisk.orderedVariablesFromQueryBody(); | ||
auto variablesFromExecutionTree = getVariableColumns(); | ||
for (const auto& variableFromQuery : orderedVariablesFromQuery) { | ||
auto it = variablesFromExecutionTree.find(variableFromQuery); | ||
if (it != variablesFromExecutionTree.end()) { | ||
validIndices.emplace_back(pair<size_t, ResultTable::ResultType>( | ||
it->second, resultTable->getResultType(it->second))); | ||
variablesFromExecutionTree.erase(it); | ||
} else { | ||
validIndices.emplace_back(std::nullopt); | ||
} | ||
} | ||
auto it = getVariableColumns().find(var); | ||
if (it != getVariableColumns().end()) { | ||
validIndices.push_back(pair<size_t, ResultTable::ResultType>( | ||
it->second, resultTable->getResultType(it->second))); | ||
} else { | ||
validIndices.push_back(std::nullopt); | ||
for (const auto& variableFromQuery : variablesFromExecutionTree) { | ||
LOG(WARN) << "The variable \"" << variableFromQuery.first | ||
<< "\" was found in the execution tree, but not in the " | ||
"original query. " | ||
"This is likely a bug\n"; | ||
} | ||
} else { | ||
for (auto variableFromQuery : selectedVarsOrAsterisk.getSelectVariables()) { | ||
if (variableFromQuery.starts_with("TEXT(")) { | ||
variableFromQuery = | ||
variableFromQuery.substr(5, variableFromQuery.rfind(')') - 5); | ||
} | ||
auto it = getVariableColumns().find(variableFromQuery); | ||
if (it != getVariableColumns().end()) { | ||
validIndices.emplace_back(pair<size_t, ResultTable::ResultType>( | ||
it->second, resultTable->getResultType(it->second))); | ||
} else { | ||
validIndices.emplace_back(std::nullopt); | ||
} | ||
} | ||
} | ||
if (validIndices.empty()) { | ||
|
@@ -123,48 +148,81 @@ QueryExecutionTree::generateResults(const vector<string>& selectVars, | |
|
||
template ad_utility::stream_generator::stream_generator | ||
QueryExecutionTree::generateResults<QueryExecutionTree::ExportSubFormat::CSV>( | ||
const vector<string>& selectVars, size_t limit, size_t offset) const; | ||
const SelectedVarsOrAsterisk& selectedVarsOrAsterisk, size_t limit, | ||
size_t offset) const; | ||
|
||
template ad_utility::stream_generator::stream_generator | ||
QueryExecutionTree::generateResults<QueryExecutionTree::ExportSubFormat::TSV>( | ||
const vector<string>& selectVars, size_t limit, size_t offset) const; | ||
const SelectedVarsOrAsterisk& selectedVarsOrAsterisk, size_t limit, | ||
size_t offset) const; | ||
|
||
template ad_utility::stream_generator::stream_generator QueryExecutionTree:: | ||
generateResults<QueryExecutionTree::ExportSubFormat::BINARY>( | ||
const vector<string>& selectVars, size_t limit, size_t offset) const; | ||
const SelectedVarsOrAsterisk& selectedVarsOrAsterisk, size_t limit, | ||
size_t offset) const; | ||
|
||
// ___________________________________________________________________________ | ||
QueryExecutionTree::ColumnIndicesAndTypes | ||
QueryExecutionTree::selectedVariablesToColumnIndices( | ||
const std::vector<string>& selectVariables, | ||
const SelectedVarsOrAsterisk& selectedVarsOrAsterisk, | ||
const ResultTable& resultTable) const { | ||
ColumnIndicesAndTypes exportColumns; | ||
for (auto var : selectVariables) { | ||
if (var.starts_with("TEXT(")) { | ||
var = var.substr(5, var.rfind(')') - 5); | ||
if (selectedVarsOrAsterisk.isAsterisk()) { | ||
auto variablesFromExecutionTree = getVariableColumns(); | ||
for (auto variableFromQuery : | ||
selectedVarsOrAsterisk.orderedVariablesFromQueryBody()) { | ||
if (getVariableColumns().contains(variableFromQuery)) { | ||
auto columnIndex = getVariableColumns().at(variableFromQuery); | ||
exportColumns.push_back( | ||
VariableAndColumnIndex{variableFromQuery, columnIndex, | ||
resultTable.getResultType(columnIndex)}); | ||
variablesFromExecutionTree.erase(variableFromQuery); | ||
} else { | ||
exportColumns.emplace_back(std::nullopt); | ||
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. Also add the complementary warning here. |
||
LOG(WARN) << "The variable \"" << variableFromQuery | ||
<< "\" was found in the original query, but not in the " | ||
"execution tree. " | ||
"This is likely a bug\n"; | ||
} | ||
} | ||
if (getVariableColumns().contains(var)) { | ||
auto columnIndex = getVariableColumns().at(var); | ||
exportColumns.push_back(VariableAndColumnIndex{ | ||
var, columnIndex, resultTable.getResultType(columnIndex)}); | ||
} else { | ||
exportColumns.emplace_back(std::nullopt); | ||
for (const auto& variableFromQuery : variablesFromExecutionTree) { | ||
LOG(WARN) << "The variable \"" << variableFromQuery.first | ||
<< "\" was found in the execution tree, but not in the " | ||
"original query. " | ||
"This is likely a bug\n"; | ||
} | ||
} else { | ||
for (auto var : selectedVarsOrAsterisk.getSelectVariables()) { | ||
if (var.starts_with("TEXT(")) { | ||
var = var.substr(5, var.rfind(')') - 5); | ||
} | ||
if (getVariableColumns().contains(var)) { | ||
auto columnIndex = getVariableColumns().at(var); | ||
exportColumns.push_back(VariableAndColumnIndex{ | ||
var, columnIndex, resultTable.getResultType(columnIndex)}); | ||
} else { | ||
exportColumns.emplace_back(std::nullopt); | ||
LOG(WARN) << "The variable \"" << var | ||
<< "\" was found in the original query, but not in the " | ||
"execution tree. " | ||
"This is likely a bug\n"; | ||
} | ||
} | ||
} | ||
return exportColumns; | ||
} | ||
|
||
// _____________________________________________________________________________ | ||
nlohmann::json QueryExecutionTree::writeResultAsQLeverJson( | ||
const vector<string>& selectVars, size_t limit, size_t offset, | ||
shared_ptr<const ResultTable> resultTable) const { | ||
const SelectedVarsOrAsterisk& selectedVarsOrAsterisk, size_t limit, | ||
size_t offset, shared_ptr<const ResultTable> resultTable) const { | ||
// They may trigger computation (but does not have to). | ||
if (!resultTable) { | ||
resultTable = getResult(); | ||
} | ||
LOG(DEBUG) << "Resolving strings for finished binary result...\n"; | ||
ColumnIndicesAndTypes validIndices = | ||
selectedVariablesToColumnIndices(selectVars, *resultTable); | ||
selectedVariablesToColumnIndices(selectedVarsOrAsterisk, *resultTable); | ||
if (validIndices.empty()) { | ||
return {std::vector<std::string>()}; | ||
} | ||
|
@@ -175,8 +233,8 @@ nlohmann::json QueryExecutionTree::writeResultAsQLeverJson( | |
|
||
// _____________________________________________________________________________ | ||
nlohmann::json QueryExecutionTree::writeResultAsSparqlJson( | ||
const vector<string>& selectVars, size_t limit, size_t offset, | ||
shared_ptr<const ResultTable> resultTable) const { | ||
const SelectedVarsOrAsterisk& selectedVarsOrAsterisk, size_t limit, | ||
size_t offset, shared_ptr<const ResultTable> resultTable) const { | ||
using nlohmann::json; | ||
|
||
// This might trigger the actual query processing. | ||
|
@@ -186,7 +244,7 @@ nlohmann::json QueryExecutionTree::writeResultAsSparqlJson( | |
LOG(DEBUG) << "Finished computing the query result in the ID space. " | ||
"Resolving strings in result...\n"; | ||
ColumnIndicesAndTypes columns = | ||
selectedVariablesToColumnIndices(selectVars, *resultTable); | ||
selectedVariablesToColumnIndices(selectedVarsOrAsterisk, *resultTable); | ||
|
||
std::erase(columns, std::nullopt); | ||
|
||
|
@@ -197,7 +255,17 @@ nlohmann::json QueryExecutionTree::writeResultAsSparqlJson( | |
const IdTable& idTable = resultTable->_idTable; | ||
|
||
json result; | ||
result["head"]["vars"] = selectVars; | ||
|
||
if (selectedVarsOrAsterisk.isAsterisk()) { | ||
vector<string> vars_names; | ||
for (auto const& variable : | ||
selectedVarsOrAsterisk.orderedVariablesFromQueryBody()) { | ||
vars_names.push_back(variable); | ||
} | ||
result["head"]["vars"] = vars_names; | ||
} else { | ||
result["head"]["vars"] = selectedVarsOrAsterisk.getSelectVariables(); | ||
} | ||
|
||
json bindings = json::array(); | ||
|
||
|
@@ -242,6 +310,8 @@ nlohmann::json QueryExecutionTree::writeResultAsSparqlJson( | |
}; | ||
|
||
for (size_t rowIndex = offset; rowIndex < upperBound; ++rowIndex) { | ||
// TODO: ordered_json` entries are ordered alphabetically, but insertion | ||
// order would be preferable. | ||
nlohmann::ordered_json binding; | ||
for (const auto& column : columns) { | ||
const auto& currentId = idTable(rowIndex, column->_columnIndex); | ||
|
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.
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.
This else branch should also warn:
OG(WARN) << "The variable "" << variableFromQuery <<
"" was found in the original, query, but not in the execution tree. "
"This is likely a bug\n";