Skip to content

Commit f1754ea

Browse files
committed
Add review fixes for Top-N pushdown support
1 parent 8973aee commit f1754ea

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

plugin/trino-mongodb/src/main/java/io/trino/plugin/mongodb/MongoMetadata.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ public class MongoMetadata
137137

138138
private static final int MAX_QUALIFIED_IDENTIFIER_BYTE_LENGTH = 120;
139139

140+
public static final int MONGO_SORT_ASC = 1;
141+
private static final int MONGO_SORT_DESC = -1;
142+
140143
private final MongoSession mongoSession;
141144

142145
private final AtomicReference<Runnable> rollbackAction = new AtomicReference<>();
@@ -634,7 +637,12 @@ public Optional<LimitApplicationResult<ConnectorTableHandle>> applyLimit(Connect
634637
}
635638

636639
@Override
637-
public Optional<TopNApplicationResult<ConnectorTableHandle>> applyTopN(ConnectorSession session, ConnectorTableHandle table, long topNCount, List<SortItem> sortItems, Map<String, ColumnHandle> assignments)
640+
public Optional<TopNApplicationResult<ConnectorTableHandle>> applyTopN(
641+
ConnectorSession session,
642+
ConnectorTableHandle table,
643+
long topNCount,
644+
List<SortItem> sortItems,
645+
Map<String, ColumnHandle> assignments)
638646
{
639647
MongoTableHandle handle = (MongoTableHandle) table;
640648

@@ -651,15 +659,15 @@ public Optional<TopNApplicationResult<ConnectorTableHandle>> applyTopN(Connector
651659
Document sortNullFieldsDocument = null;
652660
for (SortItem sortItem : sortItems) {
653661
String columnName = sortItem.getName();
654-
int direction = (sortItem.getSortOrder() == SortOrder.ASC_NULLS_FIRST || sortItem.getSortOrder() == SortOrder.ASC_NULLS_LAST) ? 1 : -1;
662+
int direction = (sortItem.getSortOrder() == SortOrder.ASC_NULLS_FIRST || sortItem.getSortOrder() == SortOrder.ASC_NULLS_LAST) ? MONGO_SORT_ASC : MONGO_SORT_DESC;
655663

656664
// MongoDB considers null values to be less than any other value.
657665
// When we have sort items with SortOrder.ASC_NULLS_LAST or SortOrder.DESC_NULLS_FIRST,
658666
// we need to add computed fields to sort correctly.
659667
if (sortItem.getSortOrder() == SortOrder.ASC_NULLS_LAST || sortItem.getSortOrder() == SortOrder.DESC_NULLS_FIRST) {
660668
String sortColumnName = "_sortNulls_" + columnName;
661669
Document condition = new Document();
662-
condition.append("$cond", List.of(new Document("$eq", new ArrayList<>(Arrays.asList("$" + columnName, null))), 1, 0));
670+
condition.append("$cond", ImmutableList.of(new Document("$eq", Arrays.asList("$" + columnName, null)), 1, 0));
663671
if (sortNullFieldsDocument == null) {
664672
sortNullFieldsDocument = new Document();
665673
}
@@ -670,7 +678,7 @@ public Optional<TopNApplicationResult<ConnectorTableHandle>> applyTopN(Connector
670678
sortDocument.append(columnName, direction);
671679
}
672680
List<MongoTableSort> tableSortList = handle.sort().orElse(new ArrayList<>());
673-
MongoTableSort tableSort = new MongoTableSort(sortDocument, sortNullFieldsDocument == null ? Optional.empty() : Optional.of(sortNullFieldsDocument), toIntExact(topNCount));
681+
MongoTableSort tableSort = new MongoTableSort(sortDocument, Optional.ofNullable(sortNullFieldsDocument), toIntExact(topNCount));
674682
tableSortList.add(tableSort);
675683

676684
return Optional.of(new TopNApplicationResult<>(

plugin/trino-mongodb/src/main/java/io/trino/plugin/mongodb/MongoSession.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ public MongoCursor<Document> execute(MongoTableHandle tableHandle, List<MongoCol
558558
for (String sortField : tableSort.sort().keySet()) {
559559
// Sorting on the field does not work unless we add it to the projection
560560
if (!projection.containsKey(sortField)) {
561-
projection.append(sortField, 1);
561+
projection.append(sortField, MongoMetadata.MONGO_SORT_ASC);
562562
}
563563
}
564564
}

plugin/trino-mongodb/src/main/java/io/trino/plugin/mongodb/MongoTableSort.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414
package io.trino.plugin.mongodb;
1515

16+
import com.google.common.collect.ImmutableList;
1617
import org.bson.Document;
1718

1819
import java.util.List;
@@ -38,8 +39,7 @@ public String toString()
3839
.toList();
3940
builder.append(", orderBy = ").append(sortEntries);
4041
if (sortNullFields.isPresent()) {
41-
List<String> nullFields = sortNullFields.get().keySet().stream()
42-
.toList();
42+
List<String> nullFields = ImmutableList.copyOf(sortNullFields.get().keySet());
4343
builder.append(", nullFields=").append(nullFields);
4444
}
4545
return builder.toString();

0 commit comments

Comments
 (0)