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

Add support for json type in MongoDB #8584

Merged
merged 1 commit into from
Jul 19, 2021
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
5 changes: 5 additions & 0 deletions plugin/trino-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
</properties>

<dependencies>
<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-plugin-toolkit</artifactId>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>bootstrap</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import io.trino.spi.type.TypeSignatureParameter;
import io.trino.spi.type.VarbinaryType;
import io.trino.spi.type.VarcharType;
import org.bson.BsonInvalidOperationException;
import org.bson.Document;
import org.bson.types.Binary;
import org.bson.types.ObjectId;
Expand All @@ -58,6 +59,7 @@

import static io.trino.plugin.mongodb.ObjectIdType.OBJECT_ID;
import static io.trino.plugin.mongodb.TypeUtils.isArrayType;
import static io.trino.plugin.mongodb.TypeUtils.isJsonType;
import static io.trino.plugin.mongodb.TypeUtils.isMapType;
import static io.trino.plugin.mongodb.TypeUtils.isRowType;
import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED;
Expand Down Expand Up @@ -177,6 +179,15 @@ private Object getObjectValue(Type type, Block block, int position)
if (type instanceof DecimalType) {
return readBigDecimal((DecimalType) type, block, position);
}
if (isJsonType(type)) {
String json = type.getSlice(block, position).toStringUtf8();
try {
return Document.parse(json);
}
catch (BsonInvalidOperationException e) {
throw new TrinoException(NOT_SUPPORTED, "Can't convert json to MongoDB Document: " + json, e);
}
}
if (isArrayType(type)) {
Type elementType = type.getTypeParameters().get(0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
*/
package io.trino.plugin.mongodb;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.google.common.primitives.Shorts;
import com.google.common.primitives.SignedBytes;
import com.mongodb.client.MongoCursor;
import io.airlift.slice.Slice;
import io.airlift.slice.SliceOutput;
import io.trino.spi.Page;
import io.trino.spi.PageBuilder;
import io.trino.spi.TrinoException;
Expand All @@ -35,6 +38,8 @@
import org.bson.types.ObjectId;
import org.joda.time.chrono.ISOChronology;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
Expand All @@ -46,8 +51,10 @@
import static com.google.common.base.Verify.verify;
import static io.airlift.slice.Slices.utf8Slice;
import static io.airlift.slice.Slices.wrappedBuffer;
import static io.trino.plugin.base.util.JsonTypeUtil.jsonParse;
import static io.trino.plugin.mongodb.ObjectIdType.OBJECT_ID;
import static io.trino.plugin.mongodb.TypeUtils.isArrayType;
import static io.trino.plugin.mongodb.TypeUtils.isJsonType;
import static io.trino.plugin.mongodb.TypeUtils.isMapType;
import static io.trino.plugin.mongodb.TypeUtils.isRowType;
import static io.trino.spi.StandardErrorCode.GENERIC_INTERNAL_ERROR;
Expand Down Expand Up @@ -253,11 +260,20 @@ else if (type instanceof VarbinaryType) {
else if (type instanceof DecimalType) {
type.writeSlice(output, encodeScaledValue(((Decimal128) value).bigDecimalValue(), ((DecimalType) type).getScale()));
}
else if (isJsonType(type)) {
type.writeSlice(output, jsonParse(utf8Slice(toVarcharValue(value))));
}
else {
throw new TrinoException(GENERIC_INTERNAL_ERROR, "Unhandled type for Slice: " + type.getTypeSignature());
}
}

public static JsonGenerator createJsonGenerator(JsonFactory factory, SliceOutput output)
throws IOException
{
return factory.createGenerator((OutputStream) output);
}

private void writeBlock(BlockBuilder output, Type type, Object value)
{
if (isArrayType(type)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@
import io.trino.spi.type.RowType;
import io.trino.spi.type.Type;

import static io.trino.spi.type.StandardTypes.JSON;

public final class TypeUtils
{
private TypeUtils() {}

public static boolean isJsonType(Type type)
{
return type.getBaseName().equals(JSON);
}

public static boolean isArrayType(Type type)
{
return type instanceof ArrayType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ public void createTableWithEveryType()
", true _boolean" +
", DATE '1980-05-07' _date" +
", TIMESTAMP '1980-05-07 11:22:33.456' _timestamp" +
", ObjectId('ffffffffffffffffffffffff') _objectid";
", ObjectId('ffffffffffffffffffffffff') _objectid" +
", JSON '{\"name\":\"alice\"}' _json";

assertUpdate(query, 1);

Expand All @@ -94,6 +95,7 @@ public void createTableWithEveryType()
assertEquals(row.getField(4), true);
assertEquals(row.getField(5), LocalDate.of(1980, 5, 7));
assertEquals(row.getField(6), LocalDateTime.of(1980, 5, 7, 11, 22, 33, 456_000_000));
assertEquals(row.getField(8), "{\"name\":\"alice\"}");
assertUpdate("DROP TABLE test_types_table");

assertFalse(getQueryRunner().tableExists(getSession(), "test_types_table"));
Expand All @@ -113,6 +115,7 @@ public void testInsertWithEveryType()
", dt date" +
", ts timestamp" +
", objid objectid" +
", _json json" +
")";
getQueryRunner().execute(getSession(), createSql);

Expand All @@ -126,7 +129,8 @@ public void testInsertWithEveryType()
", true _boolean" +
", DATE '1980-05-07' _date" +
", TIMESTAMP '1980-05-07 11:22:33.456' _timestamp" +
", ObjectId('ffffffffffffffffffffffff') _objectid";
", ObjectId('ffffffffffffffffffffffff') _objectid" +
", JSON '{\"name\":\"alice\"}' _json";
getQueryRunner().execute(getSession(), insertSql);

MaterializedResult results = getQueryRunner().execute(getSession(), "SELECT * FROM test_insert_types_table").toTestTypes();
Expand All @@ -139,10 +143,36 @@ public void testInsertWithEveryType()
assertEquals(row.getField(4), true);
assertEquals(row.getField(5), LocalDate.of(1980, 5, 7));
assertEquals(row.getField(6), LocalDateTime.of(1980, 5, 7, 11, 22, 33, 456_000_000));
assertEquals(row.getField(8), "{\"name\":\"alice\"}");
assertUpdate("DROP TABLE test_insert_types_table");
assertFalse(getQueryRunner().tableExists(getSession(), "test_insert_types_table"));
}

@Test
public void testJson()
{
assertUpdate("CREATE TABLE test_json (id INT, col JSON)");

assertUpdate("INSERT INTO test_json VALUES (1, JSON '{\"name\":\"alice\"}')", 1);
assertQuery("SELECT json_extract_scalar(col, '$.name') FROM test_json WHERE id = 1", "SELECT 'alice'");

assertUpdate("INSERT INTO test_json VALUES (2, JSON '{\"numbers\":[1, 2, 3]}')", 1);
assertQuery("SELECT json_extract(col, '$.numbers[0]') FROM test_json WHERE id = 2", "SELECT 1");

assertUpdate("INSERT INTO test_json VALUES (3, NULL)", 1);
assertQuery("SELECT col FROM test_json WHERE id = 3", "SELECT NULL");

assertQueryFails(
"CREATE TABLE test_json_scalar AS SELECT JSON '1' AS col",
"Can't convert json to MongoDB Document.*");

assertQueryFails(
"CREATE TABLE test_json_array AS SELECT JSON '[\"a\", \"b\", \"c\"]' AS col",
"Can't convert json to MongoDB Document.*");

assertUpdate("DROP TABLE test_json");
}

@Test
public void testArrays()
{
Expand Down