Skip to content

Commit

Permalink
Fix duplicate key due to non-lowercase collections in MongoDB
Browse files Browse the repository at this point in the history
  • Loading branch information
ebyhr committed Feb 13, 2025
1 parent 3d37aba commit 247b65d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,9 @@ public Set<String> getAllTables(String schema)
.collect(toSet()));
builder.addAll(getTableMetadataNames(schemaName));

return builder.build();
return builder.build().stream()
.map(name -> name.toLowerCase(ENGLISH))
.collect(toImmutableSet());
}

public MongoTable getTable(SchemaTableName tableName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import static io.trino.testing.TestingNames.randomNameSuffix;
import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Locale.ENGLISH;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assumptions.abort;
Expand Down Expand Up @@ -149,6 +150,40 @@ public void testSortItemsReflectedInExplain()
"TopNPartial\\[count = 5, orderBy = \\[nationkey DESC");
}

@Test
void testNonLowercaseCollection()
{
String suffix = randomNameSuffix();
String schema = "test_db_" + suffix;
String table = "test_collection_" + suffix;
String mixedTable = "Test_Collection_" + suffix;
try {
MongoDatabase db = client.getDatabase(schema);

db.createCollection(table);
db.getCollection(table).insertOne(new Document("lowercase", 1));

db.createCollection(mixedTable);
db.getCollection(mixedTable).insertOne(new Document("mixed", 2));

assertThatThrownBy(() -> client.getDatabase(schema.toUpperCase(ENGLISH)).createCollection(table))
.hasMessageContaining("db already exists with different case");

db.createCollection(table.toUpperCase(ENGLISH));
db.getCollection(table.toUpperCase(ENGLISH)).insertOne(new Document("uppercase", 3));

assertThat(query("SELECT * FROM information_schema.tables WHERE table_catalog = 'mongodb' AND table_schema = '" + schema + "'"))
.matches("VALUES (VARCHAR 'mongodb', VARCHAR '" + schema + "', VARCHAR '" + table + "', VARCHAR 'BASE TABLE')");
assertThat(query("SELECT table_name, column_name FROM information_schema.columns WHERE table_catalog = 'mongodb' AND table_schema = '" + schema + "'"))
.matches("VALUES (VARCHAR '" + table + "', VARCHAR 'lowercase')");
assertThat(query("SELECT * FROM " + schema + "." + table))
.matches("VALUES BIGINT '1'");
}
finally {
client.getDatabase(schema).drop();
}
}

@Override
protected Optional<DataMappingTestSetup> filterDataMappingSmokeTestData(DataMappingTestSetup dataMappingTestSetup)
{
Expand Down

0 comments on commit 247b65d

Please sign in to comment.