diff --git a/presto-main/src/main/java/com/facebook/presto/util/JsonUtil.java b/presto-main/src/main/java/com/facebook/presto/util/JsonUtil.java index 4b48dcbd8fff..c8948b7c2cf7 100644 --- a/presto-main/src/main/java/com/facebook/presto/util/JsonUtil.java +++ b/presto-main/src/main/java/com/facebook/presto/util/JsonUtil.java @@ -29,6 +29,7 @@ import com.facebook.presto.common.type.TypeSignature; import com.facebook.presto.common.type.TypeSignatureParameter; import com.facebook.presto.common.type.UnknownType; +import com.facebook.presto.operator.scalar.JsonFunctions; import com.facebook.presto.spi.PrestoException; import com.facebook.presto.type.BigintOperators; import com.facebook.presto.type.BooleanOperators; @@ -86,6 +87,7 @@ import static com.fasterxml.jackson.core.JsonToken.FIELD_NAME; import static com.fasterxml.jackson.core.JsonToken.START_ARRAY; import static com.fasterxml.jackson.core.JsonToken.START_OBJECT; +import static com.fasterxml.jackson.databind.SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Verify.verify; import static it.unimi.dsi.fastutil.HashCommon.arraySize; @@ -957,7 +959,7 @@ static BlockBuilderAppender createBlockBuilderAppender(Type type) case StandardTypes.JSON: return (parser, blockBuilder, sqlFunctionProperties) -> { String json = OBJECT_MAPPED_UNORDERED.writeValueAsString(parser.readValueAsTree()); - JSON.writeSlice(blockBuilder, Slices.utf8Slice(json)); + JSON.writeSlice(blockBuilder, JsonFunctions.jsonParse(Slices.utf8Slice(json))); }; case StandardTypes.ARRAY: return new ArrayBlockBuilderAppender(createBlockBuilderAppender(((ArrayType) type).getElementType())); diff --git a/presto-main/src/test/java/com/facebook/presto/type/TestMapOperators.java b/presto-main/src/test/java/com/facebook/presto/type/TestMapOperators.java index 0690252130ce..00790aa09372 100644 --- a/presto-main/src/test/java/com/facebook/presto/type/TestMapOperators.java +++ b/presto-main/src/test/java/com/facebook/presto/type/TestMapOperators.java @@ -527,15 +527,9 @@ public void testJsonToMap() .put("k8", "[null]") .build()); - // These two tests verifies that partial json cast preserves input order - // The second test should never happen in real life because valid json in presto requires natural key ordering. - // However, it is added to make sure that the order in the first test is not a coincidence. assertFunction("CAST(JSON '{\"k1\": {\"1klmnopq\":1, \"2klmnopq\":2, \"3klmnopq\":3, \"4klmnopq\":4, \"5klmnopq\":5, \"6klmnopq\":6, \"7klmnopq\":7}}' AS MAP)", mapType(VARCHAR, JSON), ImmutableMap.of("k1", "{\"1klmnopq\":1,\"2klmnopq\":2,\"3klmnopq\":3,\"4klmnopq\":4,\"5klmnopq\":5,\"6klmnopq\":6,\"7klmnopq\":7}")); - assertFunction("CAST(unchecked_to_json('{\"k1\": {\"7klmnopq\":7, \"6klmnopq\":6, \"5klmnopq\":5, \"4klmnopq\":4, \"3klmnopq\":3, \"2klmnopq\":2, \"1klmnopq\":1}}') AS MAP)", - mapType(VARCHAR, JSON), - ImmutableMap.of("k1", "{\"7klmnopq\":7,\"6klmnopq\":6,\"5klmnopq\":5,\"4klmnopq\":4,\"3klmnopq\":3,\"2klmnopq\":2,\"1klmnopq\":1}")); // nested array/map assertFunction("CAST(JSON '{\"1\": [1, 2], \"2\": [3, null], \"3\": [], \"5\": [null, null], \"8\": null}' AS MAP>)",