Skip to content
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

Fix incorrect result when MongoDB query table function contains helper functions #16626

Merged
merged 1 commit into from
Mar 21, 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 @@ -84,6 +84,7 @@
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static io.trino.plugin.mongodb.ObjectIdType.OBJECT_ID;
import static io.trino.plugin.mongodb.ptf.Query.parseFilter;
import static io.trino.spi.HostAddress.fromParts;
import static io.trino.spi.type.BigintType.BIGINT;
import static io.trino.spi.type.BooleanType.BOOLEAN;
Expand Down Expand Up @@ -474,7 +475,7 @@ static Document buildFilter(MongoTableHandle table)
{
// Use $and operator because Document.putAll method overwrites existing entries where the key already exists
ImmutableList.Builder<Document> filter = ImmutableList.builder();
table.getFilter().ifPresent(filter::add);
table.getFilter().ifPresent(json -> filter.add(parseFilter(json)));
filter.add(buildQuery(table.getConstraint()));
return andPredicate(filter.build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import io.trino.spi.connector.ConnectorTableHandle;
import io.trino.spi.connector.SchemaTableName;
import io.trino.spi.predicate.TupleDomain;
import org.bson.Document;

import java.util.Objects;
import java.util.Optional;
Expand All @@ -34,10 +33,10 @@ public class MongoTableHandle
private final SchemaTableName schemaTableName;
private final RemoteTableName remoteTableName;
private final TupleDomain<ColumnHandle> constraint;
private final Optional<Document> filter;
private final Optional<String> filter;
private final OptionalInt limit;

public MongoTableHandle(SchemaTableName schemaTableName, RemoteTableName remoteTableName, Optional<Document> filter)
public MongoTableHandle(SchemaTableName schemaTableName, RemoteTableName remoteTableName, Optional<String> filter)
{
this(schemaTableName, remoteTableName, filter, TupleDomain.all(), OptionalInt.empty());
}
Expand All @@ -46,7 +45,7 @@ public MongoTableHandle(SchemaTableName schemaTableName, RemoteTableName remoteT
public MongoTableHandle(
@JsonProperty("schemaTableName") SchemaTableName schemaTableName,
@JsonProperty("remoteTableName") RemoteTableName remoteTableName,
@JsonProperty("filter") Optional<Document> filter,
@JsonProperty("filter") Optional<String> filter,
@JsonProperty("constraint") TupleDomain<ColumnHandle> constraint,
@JsonProperty("limit") OptionalInt limit)
{
Expand All @@ -70,7 +69,7 @@ public RemoteTableName getRemoteTableName()
}

@JsonProperty
public Optional<Document> getFilter()
public Optional<String> getFilter()
{
return filter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,10 @@ public TableFunctionAnalysis analyze(ConnectorSession session, ConnectorTransact
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "Only lowercase collection name is supported");
}
RemoteTableName remoteTableName = mongoSession.toRemoteSchemaTableName(new SchemaTableName(database, collection));
// Don't store Document object to MongoTableHandle for avoiding serialization issue
parseFilter(filter);

MongoTableHandle tableHandle = new MongoTableHandle(new SchemaTableName(database, collection), remoteTableName, Optional.of(parseFilter(filter)));
MongoTableHandle tableHandle = new MongoTableHandle(new SchemaTableName(database, collection), remoteTableName, Optional.of(filter));
ConnectorTableSchema tableSchema = metadata.getTableSchema(session, tableHandle);
Map<String, ColumnHandle> columnsByName = metadata.getColumnHandles(session, tableHandle);
List<ColumnHandle> columns = tableSchema.getColumns().stream()
Expand All @@ -144,7 +146,7 @@ public TableFunctionAnalysis analyze(ConnectorSession session, ConnectorTransact
}
}

private static Document parseFilter(String filter)
public static Document parseFilter(String filter)
{
try {
return Document.parse(filter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,20 @@ public void testNativeQueryNestedRow()
assertUpdate("DROP TABLE " + tableName);
}

@Test
public void testNativeQueryHelperFunction()
{
String tableName = "test_query_helper_function" + randomNameSuffix();
MongoCollection<Document> collection = client.getDatabase("tpch").getCollection(tableName);
collection.insertOne(new Document(ImmutableMap.of("id", 1, "timestamp", LocalDateTime.of(2023, 3, 20, 1, 2, 3))));
collection.insertOne(new Document(ImmutableMap.of("id", 2, "timestamp", LocalDateTime.of(2024, 3, 20, 1, 2, 3))));

assertQuery(
"SELECT id FROM TABLE(mongodb.system.query(database => 'tpch', collection => '" + tableName + "', filter => '{ timestamp: ISODate(\"2023-03-20T01:02:03.000Z\") }'))",
"VALUES 1");
assertUpdate("DROP TABLE " + tableName);
}

@Test
public void testNativeQueryFilterAndWhere()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import io.airlift.json.JsonCodec;
import io.trino.spi.connector.SchemaTableName;
import org.bson.Document;
import org.testng.annotations.Test;

import java.util.Optional;
Expand Down Expand Up @@ -57,7 +56,20 @@ public void testRoundTripWithQuery()
{
SchemaTableName schemaTableName = new SchemaTableName("schema", "table");
RemoteTableName remoteTableName = new RemoteTableName("schema", "table");
MongoTableHandle expected = new MongoTableHandle(schemaTableName, remoteTableName, Optional.of(new Document("key", "value")));
MongoTableHandle expected = new MongoTableHandle(schemaTableName, remoteTableName, Optional.of("{\"key\": \"value\"}"));

String json = codec.toJson(expected);
MongoTableHandle actual = codec.fromJson(json);

assertEquals(actual.getSchemaTableName(), expected.getSchemaTableName());
}

@Test
public void testRoundTripWithQueryHavingHelperFunction()
{
SchemaTableName schemaTableName = new SchemaTableName("schema", "table");
RemoteTableName remoteTableName = new RemoteTableName("schema", "table");
MongoTableHandle expected = new MongoTableHandle(schemaTableName, remoteTableName, Optional.of("{timestamp: ISODate(\"2023-03-20T01:02:03.000Z\")}"));

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