Skip to content

Commit

Permalink
Duplicate column check when distinct
Browse files Browse the repository at this point in the history
  • Loading branch information
Jugen committed Feb 7, 2024
1 parent 0cb5627 commit d5bbd52
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,18 @@ private void processOrdering(QualifierTranslator qualifierTranslator, Translator

// If query is DISTINCT then we need to add all ORDER BY clauses as result columns
if(!context.isDistinctSuppression()) {
// TODO: need to check duplicates?
// need UPPER() function here too, as some DB expect exactly the same expression in select and in ordering
ResultNodeDescriptor descriptor = context.addResultNode(nodeBuilder.build().deepCopy());
if(exp instanceof ASTAggregateFunctionCall) {
descriptor.setAggregate(true);
// Duplicate column check
String nodeStr = translatedNode.toString();
boolean isNotPresent = context.getResultNodeList().stream()
.map( result -> result.getNode().toString() )
.noneMatch( node -> node.equals( nodeStr ) );

if ( isNotPresent ) {
// deepCopy as some DB expect exactly the same expression in select and in ordering
ResultNodeDescriptor descriptor = context.addResultNode(nodeBuilder.build().deepCopy());
if(exp instanceof ASTAggregateFunctionCall) {
descriptor.setAggregate(true);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public void prepareContext() {
.withDbEntity(dbEntity)
.withObjEntity(objEntity)
.build())
.withDistinct( true )
.build();
context = new MockTranslatorContext(wrapper);
}
Expand Down Expand Up @@ -108,4 +109,16 @@ public void perform() {
assertEquals("path", columnNode.getColumn());
assertEquals("Node { DESC}", child.getChild(0).getChild(1).toString());
}

@Test
public void testNoDuplicateColumnsWhenDistinct() {
ColumnExtractorStage columnStage = new ColumnExtractorStage();
columnStage.perform(context);

OrderingStage orderingStage = new OrderingStage();
orderingStage.perform(context);

assertTrue(context.getQuery().isDistinct());
assertEquals(1, context.getResultNodeList().size());
}
}

0 comments on commit d5bbd52

Please sign in to comment.