From d72b25ece9c721291d0d4d5b0f32be82400d8990 Mon Sep 17 00:00:00 2001 From: Yuya Ebihara Date: Mon, 21 Feb 2022 20:32:13 +0900 Subject: [PATCH] Support guessing BinData type as varbinary in MongoDB This supports BinData type in DBRef field. --- .../src/main/java/io/trino/plugin/mongodb/MongoSession.java | 5 +++++ .../java/io/trino/plugin/mongodb/BaseMongoConnectorTest.java | 1 + 2 files changed, 6 insertions(+) diff --git a/plugin/trino-mongodb/src/main/java/io/trino/plugin/mongodb/MongoSession.java b/plugin/trino-mongodb/src/main/java/io/trino/plugin/mongodb/MongoSession.java index f1100bfe3e90..d67b4b3063c2 100644 --- a/plugin/trino-mongodb/src/main/java/io/trino/plugin/mongodb/MongoSession.java +++ b/plugin/trino-mongodb/src/main/java/io/trino/plugin/mongodb/MongoSession.java @@ -53,6 +53,7 @@ import io.trino.spi.type.TypeSignatureParameter; import io.trino.spi.type.VarcharType; import org.bson.Document; +import org.bson.types.Binary; import org.bson.types.ObjectId; import java.util.ArrayList; @@ -80,6 +81,7 @@ import static io.trino.spi.type.SmallintType.SMALLINT; import static io.trino.spi.type.TimestampType.TIMESTAMP_MILLIS; import static io.trino.spi.type.TinyintType.TINYINT; +import static io.trino.spi.type.VarbinaryType.VARBINARY; import static io.trino.spi.type.VarcharType.VARCHAR; import static io.trino.spi.type.VarcharType.createUnboundedVarcharType; import static java.lang.Math.toIntExact; @@ -600,6 +602,9 @@ private Optional guessFieldType(Object value) if (value instanceof String) { typeSignature = createUnboundedVarcharType().getTypeSignature(); } + if (value instanceof Binary) { + typeSignature = VARBINARY.getTypeSignature(); + } else if (value instanceof Integer || value instanceof Long) { typeSignature = BIGINT.getTypeSignature(); } diff --git a/plugin/trino-mongodb/src/test/java/io/trino/plugin/mongodb/BaseMongoConnectorTest.java b/plugin/trino-mongodb/src/test/java/io/trino/plugin/mongodb/BaseMongoConnectorTest.java index 19f692a14efd..b41b0ad93344 100644 --- a/plugin/trino-mongodb/src/test/java/io/trino/plugin/mongodb/BaseMongoConnectorTest.java +++ b/plugin/trino-mongodb/src/test/java/io/trino/plugin/mongodb/BaseMongoConnectorTest.java @@ -283,6 +283,7 @@ public Object[][] dbRefProvider() { return new Object[][] { {"String type", "varchar 'String type'", "varchar"}, + {"BinData".getBytes(UTF_8), "to_utf8('BinData')", "varbinary"}, {1234567890, "bigint '1234567890'", "bigint"}, {true, "true", "boolean"}, {12.3f, "double '12.3'", "double"},