@@ -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 <>(
0 commit comments