Skip to content

Commit

Permalink
Extend MongoDB connector support binData
Browse files Browse the repository at this point in the history
  • Loading branch information
exxiang authored and tdcmeehan committed Aug 27, 2024
1 parent 428cee2 commit 581eb87
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.facebook.presto.common.type.TypeManager;
import com.facebook.presto.common.type.TypeSignature;
import com.facebook.presto.common.type.TypeSignatureParameter;
import com.facebook.presto.common.type.VarbinaryType;
import com.facebook.presto.spi.ColumnHandle;
import com.facebook.presto.spi.PrestoException;
import com.facebook.presto.spi.SchemaNotFoundException;
Expand All @@ -46,6 +47,7 @@
import com.mongodb.client.result.DeleteResult;
import io.airlift.slice.Slice;
import org.bson.Document;
import org.bson.types.Binary;
import org.bson.types.ObjectId;

import java.util.ArrayList;
Expand Down Expand Up @@ -519,6 +521,9 @@ else if (value instanceof Date) {
else if (value instanceof ObjectId) {
typeSignature = OBJECT_ID.getTypeSignature();
}
else if (value instanceof Binary) {
typeSignature = VarbinaryType.VARBINARY.getTypeSignature();
}
else if (value instanceof List) {
List<Optional<TypeSignature>> subTypes = ((List<?>) value).stream()
.map(this::guessFieldType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,19 @@ public void testObjectIds()
assertOneNotNullResult("SELECT id FROM tmp_objectid WHERE id = ObjectId('ffffffffffffffffffffffff')");
}

@Test
public void testBinarys()
{
assertUpdate("CREATE TABLE tmp_binary AS SELECT cast('value' as varbinary) AS _varbinary", 1);
assertOneNotNullResult("SELECT _varbinary FROM tmp_binary");

MaterializedResult results = getQueryRunner().execute(getSession(), "SELECT _varbinary FROM tmp_binary").toTestTypes();
assertEquals(results.getRowCount(), 1);

MaterializedRow row = results.getMaterializedRows().get(0);
assertEquals(row.getField(0), "value".getBytes(UTF_8));
}

private void assertOneNotNullResult(String query)
{
MaterializedResult results = getQueryRunner().execute(getSession(), query).toTestTypes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,19 @@
import static com.facebook.presto.common.predicate.Range.lessThan;
import static com.facebook.presto.common.predicate.Range.range;
import static com.facebook.presto.common.type.BigintType.BIGINT;
import static com.facebook.presto.common.type.VarbinaryType.VARBINARY;
import static com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType;
import static io.airlift.slice.Slices.utf8Slice;
import static io.airlift.slice.Slices.wrappedBuffer;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Arrays.asList;
import static org.testng.Assert.assertEquals;

public class TestMongoSession
{
private static final MongoColumnHandle COL1 = new MongoColumnHandle("col1", BIGINT, false);
private static final MongoColumnHandle COL2 = new MongoColumnHandle("col2", createUnboundedVarcharType(), false);
private static final MongoColumnHandle COL3 = new MongoColumnHandle("col3", VARBINARY, false);

@Test
public void testBuildQuery()
Expand All @@ -52,6 +56,18 @@ public void testBuildQuery()
assertEquals(query, expected);
}

@Test
public void testBuildQueryBinaryType()
{
TupleDomain<ColumnHandle> tupleDomain = TupleDomain.withColumnDomains(ImmutableMap.of(
COL3, Domain.singleValue(VARBINARY, wrappedBuffer("VarBinary Value".getBytes(UTF_8)))));

Document query = MongoSession.buildQuery(tupleDomain);
Document expected = new Document()
.append(COL3.getName(), new Document().append("$eq", "VarBinary Value"));
assertEquals(query, expected);
}

@Test
public void testBuildQueryStringType()
{
Expand Down

0 comments on commit 581eb87

Please sign in to comment.