Skip to content

Don't project _id field in Mongo DB connector when it's not required #18081

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 4, 2023
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 @@ -490,17 +490,13 @@ public MongoCursor<Document> execute(MongoTableHandle tableHandle, List<MongoCol
Set<MongoColumnHandle> projectedColumns = tableHandle.getProjectedColumns();
checkArgument(projectedColumns.isEmpty() || projectedColumns.containsAll(columns), "projectedColumns must be empty or equal to columns");

Document output = new Document();
// Starting in MongoDB 4.4, it is illegal to project an embedded document with any of the embedded document's fields
// (https://www.mongodb.com/docs/manual/reference/limits/#mongodb-limit-Projection-Restrictions). So, Project only sufficient columns.
for (MongoColumnHandle column : projectSufficientColumns(columns)) {
output.append(column.getQualifiedName(), 1);
}
Document projection = buildProjection(columns);

MongoCollection<Document> collection = getCollection(tableHandle.getRemoteTableName());
Document filter = buildFilter(tableHandle);
FindIterable<Document> iterable = collection.find(filter).projection(output).collation(SIMPLE_COLLATION);
FindIterable<Document> iterable = collection.find(filter).projection(projection).collation(SIMPLE_COLLATION);
tableHandle.getLimit().ifPresent(iterable::limit);
log.debug("Find documents: collection: %s, filter: %s, projection: %s", tableHandle.getSchemaTableName(), filter, output);
log.debug("Find documents: collection: %s, filter: %s, projection: %s", tableHandle.getSchemaTableName(), filter, projection);

if (cursorBatchSize != 0) {
iterable.batchSize(cursorBatchSize);
Expand All @@ -509,6 +505,25 @@ public MongoCursor<Document> execute(MongoTableHandle tableHandle, List<MongoCol
return iterable.iterator();
}

@VisibleForTesting
static Document buildProjection(List<MongoColumnHandle> columns)
{
Document output = new Document();

// _id is always projected by mongodb unless its explicitly excluded.
// We exclude it explicitly at the start and later include it if its present within columns list.
// https://www.mongodb.com/docs/drivers/java/sync/current/fundamentals/builders/projections/#exclusion-of-_id
output.append("_id", 0);

// Starting in MongoDB 4.4, it is illegal to project an embedded document with any of the embedded document's fields
// (https://www.mongodb.com/docs/manual/reference/limits/#mongodb-limit-Projection-Restrictions). So, Project only sufficient columns.
for (MongoColumnHandle column : projectSufficientColumns(columns)) {
output.append(column.getQualifiedName(), 1);
}

return output;
}

/**
* Creates a set of sufficient columns for the input projected columns. For example,
* if input {@param columns} include columns "a.b" and "a.b.c", then they will be projected from a single column "a.b".
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,34 @@ public class TestMongoSession
private static final MongoColumnHandle COL5 = createColumnHandle("col5", BIGINT);
private static final MongoColumnHandle COL6 = createColumnHandle("grandparent", createUnboundedVarcharType(), "parent", "col6");

private static final MongoColumnHandle ID_COL = new MongoColumnHandle("_id", ImmutableList.of(), ObjectIdType.OBJECT_ID, false, false, Optional.empty());

@Test
public void testBuildProjectionWithoutId()
{
List<MongoColumnHandle> columns = ImmutableList.of(COL1, COL2);

Document output = MongoSession.buildProjection(columns);
Document expected = new Document()
.append(COL1.getBaseName(), 1)
.append(COL2.getBaseName(), 1)
.append(ID_COL.getBaseName(), 0);
assertEquals(output, expected);
}

@Test
public void testBuildProjectionWithId()
{
List<MongoColumnHandle> columns = ImmutableList.of(COL1, COL2, ID_COL);

Document output = MongoSession.buildProjection(columns);
Document expected = new Document()
.append(COL1.getBaseName(), 1)
.append(COL2.getBaseName(), 1)
.append(ID_COL.getBaseName(), 1);
assertEquals(output, expected);
}

@Test
public void testBuildQuery()
{
Expand Down