Skip to content

Commit 55dea7d

Browse files
authored
Move the $project stage to the end of the paginate partitioner pipeline (mongodb#142)
* Move the $project stage to the end of the paginate partitioner pipeline SPARK-452
1 parent f93ee03 commit 55dea7d

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/main/java/com/mongodb/spark/sql/connector/read/partitioner/PaginatePartitioner.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ private List<BsonDocument> createUpperBounds(
8989
: Projections.fields(Projections.include(partitionField), Projections.excludeId());
9090

9191
List<Bson> aggregationPipeline = new ArrayList<>(readConfig.getAggregationPipeline());
92-
aggregationPipeline.add(Aggregates.project(projection));
9392
aggregationPipeline.add(Aggregates.sort(Sorts.ascending(partitionField)));
9493

9594
BsonDocument boundary = readConfig.withCollection(coll -> {
@@ -108,6 +107,9 @@ private List<BsonDocument> createUpperBounds(
108107
boundaryPipeline.addAll(aggregationPipeline);
109108
boundaryPipeline.add(Aggregates.skip(numDocumentsPerPartition));
110109
boundaryPipeline.add(Aggregates.limit(1));
110+
// $project at the end of the pipeline as per SERVER-49306.
111+
// Ensures indexes can be utilized for the $skip stage
112+
boundaryPipeline.add(Aggregates.project(projection));
111113
return coll.aggregate(boundaryPipeline)
112114
.allowDiskUse(readConfig.getAggregationAllowDiskUse())
113115
.comment(readConfig.getComment())

0 commit comments

Comments
 (0)