-
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
Added support for HAVING. Fixes #104. #130
Merged
floriankramer
merged 2 commits into
ad-freiburg:master
from
floriankramer:feature_having
Sep 28, 2018
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
|
@@ -318,7 +318,19 @@ QueryExecutionTree QueryPlanner::createExecutionTree(ParsedQuery& pq) const { | |
final = groupByPlan; | ||
} | ||
|
||
// create filter operations for the having clauses | ||
for (const SparqlFilter& filter : pq._havingClauses) { | ||
SubtreePlan plan(_qec); | ||
auto& tree = *plan._qet.get(); | ||
tree.setVariableColumns(final._qet.get()->getVariableColumnMap()); | ||
tree.setOperation(QueryExecutionTree::FILTER, | ||
createFilterOperation(filter, final)); | ||
tree.setContextVars(final._qet.get()->getContextVars()); | ||
final = plan; | ||
} | ||
|
||
if (doGrouping) { | ||
// Either the pattern trick or another form of grouping is in use | ||
// Add the order by operation | ||
if (pq._orderBy.size() > 0) { | ||
SubtreePlan plan(_qec); | ||
|
@@ -459,10 +471,13 @@ bool QueryPlanner::checkUsePatternTrick( | |
// trick is not going to be used. | ||
if (usePatternTrick) { | ||
// Check for filters on the ql:has-predicate triple's subject or | ||
// object | ||
// object. | ||
// Filters that filter on the triple's object but have a static | ||
// rhs will be transformed to a having clause later on. | ||
for (const SparqlFilter& filter : pq->_rootGraphPattern._filters) { | ||
if (filter._lhs == t._o || filter._lhs == t._s || | ||
filter._rhs == t._o || filter._rhs == t._s) { | ||
if (!(filter._lhs == t._o && filter._rhs[0] != '?') && | ||
(filter._lhs == t._s || filter._rhs == t._o || | ||
filter._rhs == t._s)) { | ||
usePatternTrick = false; | ||
break; | ||
} | ||
|
@@ -501,6 +516,17 @@ bool QueryPlanner::checkUsePatternTrick( | |
// remove the triple from the graph | ||
pq->_rootGraphPattern._whereClauseTriples.erase( | ||
pq->_rootGraphPattern._whereClauseTriples.begin() + i); | ||
// Transform filters on the ql:has-relation triple's object that | ||
// have a static rhs to having clauses | ||
for (int i = 0; i < pq->_rootGraphPattern._filters.size(); i++) { | ||
const SparqlFilter& filter = pq->_rootGraphPattern._filters[i]; | ||
if (filter._lhs == t._o && filter._rhs[0] != '?') { | ||
pq->_havingClauses.push_back(filter); | ||
pq->_rootGraphPattern._filters.erase( | ||
pq->_rootGraphPattern._filters.begin() + i); | ||
i--; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
@@ -1446,58 +1472,8 @@ void QueryPlanner::applyFiltersIfPossible( | |
newPlan._idsOfIncludedNodes = row[n]._idsOfIncludedNodes; | ||
newPlan._isOptional = row[n]._isOptional; | ||
auto& tree = *newPlan._qet.get(); | ||
if (isVariable(filters[i]._rhs)) { | ||
std::shared_ptr<Operation> filter = std::make_shared<Filter>( | ||
_qec, row[n]._qet, filters[i]._type, | ||
row[n]._qet.get()->getVariableColumn(filters[i]._lhs), | ||
row[n]._qet.get()->getVariableColumn(filters[i]._rhs)); | ||
tree.setOperation(QueryExecutionTree::FILTER, filter); | ||
} else { | ||
string compWith = filters[i]._rhs; | ||
Id entityId = 0; | ||
if (_qec) { | ||
// TODO(schnelle): A proper SPARQL parser should have | ||
// tagged/converted numeric values already. However our parser is | ||
// currently far too crude for that | ||
if (ad_utility::isXsdValue(compWith)) { | ||
compWith = ad_utility::convertValueLiteralToIndexWord(compWith); | ||
} else if (ad_utility::isNumeric(compWith)) { | ||
compWith = ad_utility::convertNumericToIndexWord(compWith); | ||
} | ||
if (filters[i]._type == SparqlFilter::EQ || | ||
filters[i]._type == SparqlFilter::NE) { | ||
if (!_qec->getIndex().getVocab().getId(compWith, &entityId)) { | ||
entityId = std::numeric_limits<size_t>::max() - 1; | ||
} | ||
} else if (filters[i]._type == SparqlFilter::GE) { | ||
entityId = _qec->getIndex().getVocab().getValueIdForGE(compWith); | ||
} else if (filters[i]._type == SparqlFilter::GT) { | ||
entityId = _qec->getIndex().getVocab().getValueIdForGT(compWith); | ||
} else if (filters[i]._type == SparqlFilter::LT) { | ||
entityId = _qec->getIndex().getVocab().getValueIdForLT(compWith); | ||
} else if (filters[i]._type == SparqlFilter::LE) { | ||
entityId = _qec->getIndex().getVocab().getValueIdForLE(compWith); | ||
} else if (filters[i]._type == SparqlFilter::LANG_MATCHES || | ||
filters[i]._type == SparqlFilter::REGEX) { | ||
entityId = std::numeric_limits<size_t>::max() - 1; | ||
} | ||
} | ||
std::shared_ptr<Operation> filter( | ||
new Filter(_qec, row[n]._qet, filters[i]._type, | ||
row[n]._qet.get()->getVariableColumn(filters[i]._lhs), | ||
std::numeric_limits<size_t>::max(), entityId)); | ||
if (_qec && (filters[i]._type == SparqlFilter::LANG_MATCHES || | ||
filters[i]._type == SparqlFilter::REGEX)) { | ||
static_cast<Filter*>(filter.get()) | ||
->setRightHandSideString(filters[i]._rhs); | ||
if (filters[i]._type == SparqlFilter::REGEX) { | ||
static_cast<Filter*>(filter.get()) | ||
->setRegexIgnoreCase(filters[i]._regexIgnoreCase); | ||
} | ||
} | ||
tree.setOperation(QueryExecutionTree::FILTER, filter); | ||
} | ||
|
||
tree.setOperation(QueryExecutionTree::FILTER, | ||
createFilterOperation(filters[i], row[n])); | ||
tree.setVariableColumns(row[n]._qet.get()->getVariableColumnMap()); | ||
tree.setContextVars(row[n]._qet.get()->getContextVars()); | ||
if (replace) { | ||
|
@@ -1510,6 +1486,61 @@ void QueryPlanner::applyFiltersIfPossible( | |
} | ||
} | ||
|
||
// _____________________________________________________________________________ | ||
std::shared_ptr<Operation> QueryPlanner::createFilterOperation( | ||
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. this is much nicer as its own method, thanks! |
||
const SparqlFilter& filter, const SubtreePlan& parent) const { | ||
if (isVariable(filter._rhs)) { | ||
std::shared_ptr<Operation> filterOp = std::make_shared<Filter>( | ||
_qec, parent._qet, filter._type, | ||
parent._qet.get()->getVariableColumn(filter._lhs), | ||
parent._qet.get()->getVariableColumn(filter._rhs)); | ||
return filterOp; | ||
} else { | ||
string compWith = filter._rhs; | ||
Id entityId = 0; | ||
if (_qec) { | ||
// TODO(schnelle): A proper SPARQL parser should have | ||
// tagged/converted numeric values already. However our parser is | ||
// currently far too crude for that | ||
if (ad_utility::isXsdValue(compWith)) { | ||
compWith = ad_utility::convertValueLiteralToIndexWord(compWith); | ||
} else if (ad_utility::isNumeric(compWith)) { | ||
compWith = ad_utility::convertNumericToIndexWord(compWith); | ||
} | ||
if (filter._type == SparqlFilter::EQ || | ||
filter._type == SparqlFilter::NE) { | ||
if (!_qec->getIndex().getVocab().getId(compWith, &entityId)) { | ||
entityId = std::numeric_limits<size_t>::max() - 1; | ||
} | ||
} else if (filter._type == SparqlFilter::GE) { | ||
entityId = _qec->getIndex().getVocab().getValueIdForGE(compWith); | ||
} else if (filter._type == SparqlFilter::GT) { | ||
entityId = _qec->getIndex().getVocab().getValueIdForGT(compWith); | ||
} else if (filter._type == SparqlFilter::LT) { | ||
entityId = _qec->getIndex().getVocab().getValueIdForLT(compWith); | ||
} else if (filter._type == SparqlFilter::LE) { | ||
entityId = _qec->getIndex().getVocab().getValueIdForLE(compWith); | ||
} else if (filter._type == SparqlFilter::LANG_MATCHES || | ||
filter._type == SparqlFilter::REGEX) { | ||
entityId = std::numeric_limits<size_t>::max() - 1; | ||
} | ||
} | ||
std::shared_ptr<Operation> filterOp( | ||
new Filter(_qec, parent._qet, filter._type, | ||
parent._qet.get()->getVariableColumn(filter._lhs), | ||
std::numeric_limits<size_t>::max(), entityId)); | ||
if (_qec && (filter._type == SparqlFilter::LANG_MATCHES || | ||
filter._type == SparqlFilter::REGEX)) { | ||
static_cast<Filter*>(filterOp.get())->setRightHandSideString(filter._rhs); | ||
if (filter._type == SparqlFilter::REGEX) { | ||
static_cast<Filter*>(filterOp.get()) | ||
->setRegexIgnoreCase(filter._regexIgnoreCase); | ||
} | ||
} | ||
return filterOp; | ||
} | ||
} | ||
|
||
// _____________________________________________________________________________ | ||
vector<vector<QueryPlanner::SubtreePlan>> QueryPlanner::fillDpTab( | ||
const QueryPlanner::TripleGraph& tg, const vector<SparqlFilter>& filters, | ||
|
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 |
---|---|---|
|
@@ -290,7 +290,7 @@ void SparqlParser::parseWhere(const string& str, ParsedQuery& query, | |
} | ||
} | ||
for (const string& filter : filters) { | ||
addFilter(filter, currentPattern); | ||
addFilter(filter, ¤tPattern->_filters); | ||
} | ||
} | ||
|
||
|
@@ -382,7 +382,7 @@ void SparqlParser::addWhereTriple(const string& str, | |
// _____________________________________________________________________________ | ||
void SparqlParser::parseSolutionModifiers(const string& str, | ||
ParsedQuery& query) { | ||
// Split the string at any whitespace but ignoe whitespace inside brackets | ||
// Split the string at any whitespace but ignore whitespace inside brackets | ||
// to allow for alias parsing. | ||
auto tokens = ad_utility::splitWsWithEscape(str, '(', ')'); | ||
for (size_t i = 0; i < tokens.size(); ++i) { | ||
|
@@ -391,7 +391,7 @@ void SparqlParser::parseSolutionModifiers(const string& str, | |
i += 1; | ||
while (i + 1 < tokens.size() && tokens[i + 1] != "LIMIT" && | ||
tokens[i + 1] != "OFFSET" && tokens[i + 1] != "TEXTLIMIT" && | ||
tokens[i + 1] != "GROUP") { | ||
tokens[i + 1] != "GROUP" && tokens[i + 1] != "HAVING") { | ||
query._orderBy.emplace_back(OrderKey(tokens[i + 1])); | ||
++i; | ||
} | ||
|
@@ -416,12 +416,20 @@ void SparqlParser::parseSolutionModifiers(const string& str, | |
i++; | ||
} | ||
} | ||
if (tokens[i] == "HAVING" && i < tokens.size() - 1) { | ||
while (i + 1 < tokens.size() && tokens[i + 1] != "LIMIT" && | ||
tokens[i + 1] != "OFFSET" && tokens[i + 1] != "TEXTLIMIT" && | ||
tokens[i + 1] != "GROUP" && tokens[i + 1] != "ORDER") { | ||
addFilter(tokens[i + 1], &query._havingClauses); | ||
i++; | ||
} | ||
} | ||
} | ||
} | ||
|
||
// _____________________________________________________________________________ | ||
void SparqlParser::addFilter(const string& str, | ||
ParsedQuery::GraphPattern* pattern) { | ||
vector<SparqlFilter>* _filters) { | ||
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 think this is actually quite a bit clearer than the old version |
||
size_t i = str.find('('); | ||
AD_CHECK(i != string::npos); | ||
size_t j = str.rfind(')'); | ||
|
@@ -451,7 +459,7 @@ void SparqlParser::addFilter(const string& str, | |
f._type = SparqlFilter::LANG_MATCHES; | ||
f._lhs = lvar; | ||
f._rhs = rhs.substr(1, rhs.size() - 2); | ||
pattern->_filters.emplace_back(f); | ||
_filters->emplace_back(f); | ||
return; | ||
} | ||
if (pred == "regex") { | ||
|
@@ -461,7 +469,7 @@ void SparqlParser::addFilter(const string& str, | |
f._rhs = rhs.substr(1, rhs.size() - 2); | ||
f._regexIgnoreCase = | ||
(parts.size() == 3 && ad_utility::strip(parts[2], ' ') == "\"i\""); | ||
pattern->_filters.emplace_back(f); | ||
_filters->emplace_back(f); | ||
return; | ||
} | ||
if (pred == "prefix") { | ||
|
@@ -482,8 +490,8 @@ void SparqlParser::addFilter(const string& str, | |
upper += rhs[rhs.size() - 2] + 1; | ||
upper += rhs.back(); | ||
f2._rhs = upper; | ||
pattern->_filters.emplace_back(f1); | ||
pattern->_filters.emplace_back(f2); | ||
_filters->emplace_back(f1); | ||
_filters->emplace_back(f2); | ||
return; | ||
} | ||
} | ||
|
@@ -545,7 +553,7 @@ void SparqlParser::addFilter(const string& str, | |
AD_THROW(ad_semsearch::Exception::NOT_YET_IMPLEMENTED, | ||
"Filter not supported yet: " + filter); | ||
} | ||
pattern->_filters.emplace_back(f); | ||
_filters->emplace_back(f); | ||
} | ||
|
||
// _____________________________________________________________________________ | ||
|
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
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.
So these are multiple
HAVING
clauses which are treated asAND
right? I think we should document that somewhere besides the SPARQL standard. Also I think we should have a few more examples in the e2e Tests, especially also the very basic usage. I like using them as normal examples and to look up how some syntax works and thus having a few different cases is both a useful test and an example.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 added another e2e test and expanded the GROUP BY paragraph in the readme.