Skip to content
Open
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 @@ -50,9 +50,12 @@
import io.trino.spi.connector.SaveMode;
import io.trino.spi.connector.SchemaTableName;
import io.trino.spi.connector.SchemaTablePrefix;
import io.trino.spi.connector.SortItem;
import io.trino.spi.connector.SortOrder;
import io.trino.spi.connector.SortingProperty;
import io.trino.spi.connector.TableFunctionApplicationResult;
import io.trino.spi.connector.TableNotFoundException;
import io.trino.spi.connector.TopNApplicationResult;
import io.trino.spi.expression.ConnectorExpression;
import io.trino.spi.expression.FieldDereference;
import io.trino.spi.expression.Variable;
Expand All @@ -72,6 +75,8 @@
import org.bson.Document;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -132,6 +137,9 @@ public class MongoMetadata

private static final int MAX_QUALIFIED_IDENTIFIER_BYTE_LENGTH = 120;

public static final int MONGO_SORT_ASC = 1;
private static final int MONGO_SORT_DESC = -1;

private final MongoSession mongoSession;

private final AtomicReference<Runnable> rollbackAction = new AtomicReference<>();
Expand Down Expand Up @@ -622,11 +630,75 @@ public Optional<LimitApplicationResult<ConnectorTableHandle>> applyLimit(Connect
handle.filter(),
handle.constraint(),
handle.projectedColumns(),
handle.sort(),
OptionalInt.of(toIntExact(limit))),
true,
false));
}

@Override
public Optional<TopNApplicationResult<ConnectorTableHandle>> applyTopN(
ConnectorSession session,
ConnectorTableHandle table,
long topNCount,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the topNCount is more than range of integer ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added check similar to applyLimit method

        // MongoDB doesn't support topN number greater than integer max
        if (topNCount > Integer.MAX_VALUE) {
            return Optional.empty();
        }```

List<SortItem> sortItems,
Map<String, ColumnHandle> assignments)
{
MongoTableHandle handle = (MongoTableHandle) table;

// MongoDB doesn't support topN number greater than integer max
if (topNCount > Integer.MAX_VALUE) {
return Optional.empty();
}

for (Map.Entry<String, ColumnHandle> columnHandleEntry : assignments.entrySet()) {
MongoColumnHandle columnHandle = (MongoColumnHandle) columnHandleEntry.getValue();
if (!columnHandleEntry.getKey().equals(columnHandle.baseName())) {
// We don't support complex nested queries
return Optional.empty();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about for dbRef column type ?

Copy link
Member Author

@codluca codluca Sep 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand it, applyTopN is called only for one table, no JOIN. The dbRef from MongoDB would be a reference to another collection, what we achieve with a JOIN. If it's something else, please clarify.
I wrote a simple test, and the execution doesn't enter the applyTopN method.

     * Test topN dbRef.
     */
    @Test
    public void testJoinTopNDbRef() {
        Session session = Session.builder(getSession())
                .setSystemProperty(IGNORE_STATS_CALCULATOR_FAILURES, "false")
                .build();

        assertQuery(
                session,
                "SELECT n.name, c.name " +
                        "FROM nation n " +
                        "JOIN customer c ON c.nationkey = n.nationkey " +
                        "ORDER BY n.nationkey, c.name LIMIT 2");
    }```

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is the case, we could simply add a defense check to Make sure the columnHandle#dbRefField is false, otherwise return Optional.empty() to indicate we don't support it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AbstractTestQueries.testPredicate() fails.

    @Test
    public void testPredicate()
    {
        assertQuery("" +
                "SELECT *\n" +
                "FROM (\n" +
                "  SELECT orderkey+1 AS a FROM orders WHERE orderstatus = 'F' UNION ALL \n" +
                "  SELECT orderkey FROM orders WHERE orderkey % 2 = 0 UNION ALL \n" +
                "  (SELECT orderkey+custkey FROM orders ORDER BY orderkey LIMIT 10)\n" +
                ") \n" +
                "WHERE a < 20 OR a > 100 \n" +
                "ORDER BY a");
    }

columnHandle.dbRefField is false.
However,
columnHandle.baseName() is "orderkey"
columnHandleEntry.getKey() is "orderkey_12"
So I used this condition to avoid the pushdown for this test case also.

}
}

// Convert Trino sort items to a BSON sort document for MongoDB.
Document sortDocument = new Document();
Document sortNullFieldsDocument = null;
for (SortItem sortItem : sortItems) {
String columnName = sortItem.getName();
int direction = (sortItem.getSortOrder() == SortOrder.ASC_NULLS_FIRST || sortItem.getSortOrder() == SortOrder.ASC_NULLS_LAST) ? MONGO_SORT_ASC : MONGO_SORT_DESC;

// MongoDB considers null values to be less than any other value.
// When we have sort items with SortOrder.ASC_NULLS_LAST or SortOrder.DESC_NULLS_FIRST,
// we need to add computed fields to sort correctly.
if (sortItem.getSortOrder() == SortOrder.ASC_NULLS_LAST || sortItem.getSortOrder() == SortOrder.DESC_NULLS_FIRST) {
String sortColumnName = "_sortNulls_" + columnName;
Document condition = new Document();
condition.append("$cond", ImmutableList.of(new Document("$eq", Arrays.asList("$" + columnName, null)), 1, 0));
if (sortNullFieldsDocument == null) {
sortNullFieldsDocument = new Document();
}
sortNullFieldsDocument.append(sortColumnName, condition);
sortDocument.append(sortColumnName, direction);
}

sortDocument.append(columnName, direction);
}
List<MongoTableSort> tableSortList = handle.sort().orElse(new ArrayList<>());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consider using ImmutableList.Builder

MongoTableSort tableSort = new MongoTableSort(sortDocument, Optional.ofNullable(sortNullFieldsDocument), toIntExact(topNCount));
tableSortList.add(tableSort);

return Optional.of(new TopNApplicationResult<>(
new MongoTableHandle(
handle.schemaTableName(),
handle.remoteTableName(),
handle.filter(),
handle.constraint(),
handle.projectedColumns(),
Optional.of(tableSortList),
OptionalInt.empty()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's safer to put handle.limit()

true,
false));
}

@Override
public Optional<ConstraintApplicationResult<ConnectorTableHandle>> applyFilter(ConnectorSession session, ConnectorTableHandle table, Constraint constraint)
{
Expand Down Expand Up @@ -670,6 +742,7 @@ public Optional<ConstraintApplicationResult<ConnectorTableHandle>> applyFilter(C
handle.filter(),
newDomain,
handle.projectedColumns(),
handle.sort(),
handle.limit());

return Optional.of(new ConstraintApplicationResult<>(handle, remainingFilter, constraint.getExpression(), false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.mongodb.DBRef;
import com.mongodb.MongoCommandException;
import com.mongodb.MongoNamespace;
import com.mongodb.client.AggregateIterable;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoCollection;
Expand Down Expand Up @@ -533,15 +534,57 @@ public MongoCursor<Document> execute(MongoTableHandle tableHandle, List<MongoCol

MongoCollection<Document> collection = getCollection(tableHandle.remoteTableName());
Document filter = buildFilter(tableHandle);
FindIterable<Document> iterable = collection.find(filter).projection(projection).collation(SIMPLE_COLLATION);
tableHandle.limit().ifPresent(iterable::limit);

log.debug("Find documents: collection: %s, filter: %s, projection: %s", tableHandle.schemaTableName(), filter, projection);

if (cursorBatchSize != 0) {
iterable.batchSize(cursorBatchSize);
boolean useFind = tableHandle.sort().isEmpty() ||
(tableHandle.sort().get().size() == 1 && tableHandle.sort().get().getFirst().sortNullFields().isEmpty());

if (useFind) {
FindIterable<Document> iterable = collection.find(filter).projection(projection).collation(SIMPLE_COLLATION);
tableHandle.sort().ifPresent(sortList -> {
iterable.sort(sortList.getFirst().sort());
iterable.limit(toIntExact(sortList.getFirst().limit()));
});
tableHandle.limit().ifPresent(iterable::limit);

if (cursorBatchSize != 0) {
iterable.batchSize(cursorBatchSize);
}

return iterable.iterator();
}
else {
// MongoDB considers null values to be less than any other value.
// We can handle sort items with SortOrder.ASC_NULLS_LAST or SortOrder.DESC_NULLS_FIRST
// with an aggregation pipeline.
List<MongoTableSort> tableSortList = tableHandle.sort().get();
for (MongoTableSort tableSort : tableSortList) {
for (String sortField : tableSort.sort().keySet()) {
// Sorting on the field does not work unless we add it to the projection
if (!projection.containsKey(sortField)) {
projection.append(sortField, MongoMetadata.MONGO_SORT_ASC);
}
}
}

List<Document> aggregateList = new ArrayList<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use ImmuableList and rename the aggregateList to aggregatePipeline

aggregateList.add(new Document("$match", filter));
for (MongoTableSort tableSort : tableSortList) {
tableSort.sortNullFields().ifPresent(sortNullFields -> new Document("$addFields", sortNullFields));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line seems not doing anything?

aggregateList.add(new Document("$sort", tableSort.sort()));
aggregateList.add(new Document("$limit", tableSort.limit()));
}
tableHandle.limit().ifPresent(limit -> aggregateList.add(new Document("$limit", limit)));
aggregateList.add(new Document("$project", projection));
AggregateIterable<Document> aggregateIterable = collection.aggregate(aggregateList).collation(SIMPLE_COLLATION);

return iterable.iterator();
if (cursorBatchSize != 0) {
aggregateIterable.batchSize(cursorBatchSize);
}

return aggregateIterable.iterator();
}
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.trino.spi.connector.SchemaTableName;
import io.trino.spi.predicate.TupleDomain;

import java.util.List;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.Set;
Expand All @@ -32,12 +33,13 @@ public record MongoTableHandle(
Optional<String> filter,
TupleDomain<ColumnHandle> constraint,
Set<MongoColumnHandle> projectedColumns,
Optional<List<MongoTableSort>> sort,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why it requires List instead of Optional

Copy link
Member Author

@codluca codluca Nov 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a single MongoTableSort is used,
BaseConnectorTest.testTopNPushdown() test fails.

// TopN over TopN
assertThat(query("SELECT orderkey, totalprice FROM (SELECT orderkey, totalprice FROM orders ORDER BY 1, 2 LIMIT 10) ORDER BY 2, 1 LIMIT 5"))
        .ordered()
        .isFullyPushedDown();
Expected :[[2, 38426.09], [6, 45523.1], [4, 56000.91], [34, 73315.48], [5, 105367.67]]
Actual   :[[35271, 874.89], [28647, 924.33], [58145, 929.03], [8354, 974.04], [37415, 986.63]]

Also, AbstractTestQueries.testTopN() fails for similar issue.

  // TopN over TopN
  assertQueryOrdered("SELECT orderkey, totalprice FROM (SELECT orderkey, totalprice FROM orders ORDER BY 1, 2 LIMIT 10) ORDER BY 2, 1 LIMIT 5");

The nested SELECT will produce a MongoTableSort.
The root SELECT will need to aggregate the MongoTableSort entities.

Copy link
Contributor

@chenjian2664 chenjian2664 Nov 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, got it, so it is not "entire table sort", it is actual one sort item, we could rename it similar to "MongoSortItem"
It seems not, I needs to take a look at this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is for the case handle push topN into a table already contains topN info

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.
Should I do the rename to MongoSortItem?

OptionalInt limit)
implements ConnectorTableHandle
{
public MongoTableHandle(SchemaTableName schemaTableName, RemoteTableName remoteTableName, Optional<String> filter)
{
this(schemaTableName, remoteTableName, filter, TupleDomain.all(), ImmutableSet.of(), OptionalInt.empty());
this(schemaTableName, remoteTableName, filter, TupleDomain.all(), ImmutableSet.of(), Optional.empty(), OptionalInt.empty());
}

public MongoTableHandle
Expand All @@ -47,6 +49,7 @@ public MongoTableHandle(SchemaTableName schemaTableName, RemoteTableName remoteT
requireNonNull(filter, "filter is null");
requireNonNull(constraint, "constraint is null");
projectedColumns = ImmutableSet.copyOf(requireNonNull(projectedColumns, "projectedColumns is null"));
requireNonNull(sort, "sort is null");
requireNonNull(limit, "limit is null");
}

Expand All @@ -68,6 +71,7 @@ else if (!constraint.isAll()) {
if (!projectedColumns.isEmpty()) {
builder.append(" columns=").append(projectedColumns);
}
sort.ifPresent(value -> builder.append(" TopNPartial").append(value));
limit.ifPresent(value -> builder.append(" limit=").append(value));
return builder.toString();
}
Expand All @@ -80,6 +84,7 @@ public MongoTableHandle withProjectedColumns(Set<MongoColumnHandle> projectedCol
filter,
constraint,
projectedColumns,
sort,
limit);
}

Expand All @@ -91,6 +96,7 @@ public MongoTableHandle withConstraint(TupleDomain<ColumnHandle> constraint)
filter,
constraint,
projectedColumns,
sort,
limit);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.trino.plugin.mongodb;

import com.google.common.collect.ImmutableList;
import org.bson.Document;

import java.util.List;
import java.util.Optional;

import static java.util.Objects.requireNonNull;

public record MongoTableSort(Document sort, Optional<Document> sortNullFields, int limit)
{
public MongoTableSort
{
requireNonNull(sort, "sort is null");
requireNonNull(sortNullFields, "sortNullFields is null");
}

@Override
public String toString()
{
StringBuilder builder = new StringBuilder();
builder.append("count = ").append(limit);
List<String> sortEntries = sort.entrySet().stream()
.map(entry -> entry.getKey() + " " + ("1".equals(entry.getValue()) ? "ASC" : "DESC"))
.toList();
builder.append(", orderBy = ").append(sortEntries);
if (sortNullFields.isPresent()) {
List<String> nullFields = ImmutableList.copyOf(sortNullFields.get().keySet());
builder.append(", nullFields=").append(nullFields);
}
return builder.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
SUPPORTS_RENAME_FIELD,
SUPPORTS_RENAME_SCHEMA,
SUPPORTS_SET_FIELD_TYPE,
SUPPORTS_TOPN_PUSHDOWN,
SUPPORTS_TRUNCATE,
SUPPORTS_UPDATE -> false;
default -> super.hasBehavior(connectorBehavior);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public void testRoundTripWithProjectedColumns()
Optional.empty(),
TupleDomain.all(),
projectedColumns,
Optional.empty(),
OptionalInt.empty());

String json = codec.toJson(expected);
Expand Down