Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private void modifyProjection(TokenQueryParameter<?> firstProjected, TokenStream
boolean hasGroupConcat = false;
for (QueryVariableMapping varMapping : queryMod.variables()) {
// PERF: GROUP_CONCAT may prevent combinatorial blowup of the number of rows
if (varMapping.canGroupConcat()) {
if (!queryAttributes.hasOrderBy() && varMapping.canGroupConcat()) {
projection.append(' ').append(varMapping.generateGroupConcat());
hasGroupConcat = true;
} else {
Expand All @@ -126,10 +126,8 @@ private void modifyProjection(TokenQueryParameter<?> firstProjected, TokenStream
tokenRewriter.insertAfter(firstProjected.getSingleToken(), projection.toString());
if (hasGroupConcat) {
tokenRewriter.insertAfter(queryAttributes.lastClosingCurlyBraceToken(), groupBy.toString());
if (!queryAttributes.hasOrderBy()) {
// Add ordering to ensure stable results with rows with the same subject in sequence
tokenRewriter.insertAfter(queryAttributes.lastClosingCurlyBraceToken(), " ORDER BY " + firstProjected.getIdentifierAsQueryString());
}
// Add ordering to ensure stable results with rows with the same subject in sequence
tokenRewriter.insertAfter(queryAttributes.lastClosingCurlyBraceToken(), " ORDER BY " + firstProjected.getIdentifierAsQueryString());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,20 @@ void modifyGroupConcatenatesPluralStringAttributeValues() {
assertThat(result, containsString("GROUP BY ?x"));
}

@Test
void modifyDoesNotAddGroupConcatWhenQueryHasOrderBy() {
final TokenStreamSparqlQueryHolder holder = parser.parseQuery("SELECT ?x WHERE { ?x a ?type ; ?hasString ?stringAttribute } ORDER BY ?stringAttribute");
final EntityGraph<OWLClassN> fetchGraph = new EntityGraphImpl<>(metamodelMocks.forOwlClassN()
.entityType(), metamodel);
fetchGraph.addAttributeNodes("pluralAnnotation");
final AttributeEnumeratingSparqlAssemblyModifier sut = createSut(metamodelMocks.forOwlClassN()
.entityType(), new EntityDescriptor(), fetchGraph);
holder.setAssemblyModifier(sut);

final String result = holder.assembleQuery();
assertThat(result, not(containsString("GROUP_CONCAT")));
}

/**
* Simplified Hamcrest matcher that checks if a SPARQL query projects a specific variable. The matcher extracts the
* projection part of the query (everything before the WHERE clause) and checks if it contains the specified
Expand Down