|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package com.redis.trino; |
| 15 | + |
| 16 | +import com.fasterxml.jackson.core.JsonFactory; |
| 17 | +import com.fasterxml.jackson.core.JsonFactoryBuilder; |
| 18 | +import com.fasterxml.jackson.core.JsonParser; |
| 19 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | +import io.airlift.json.ObjectMapperProvider; |
| 21 | +import io.airlift.slice.DynamicSliceOutput; |
| 22 | +import io.airlift.slice.Slice; |
| 23 | +import io.airlift.slice.SliceOutput; |
| 24 | +import io.airlift.slice.Slices; |
| 25 | +import io.trino.spi.TrinoException; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | +import java.io.InputStreamReader; |
| 29 | +import java.io.OutputStream; |
| 30 | + |
| 31 | +import static com.fasterxml.jackson.core.JsonFactory.Feature.CANONICALIZE_FIELD_NAMES; |
| 32 | +import static com.fasterxml.jackson.databind.SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS; |
| 33 | +import static com.google.common.base.Preconditions.checkState; |
| 34 | +import static io.trino.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT; |
| 35 | +import static java.lang.String.format; |
| 36 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 37 | + |
| 38 | +public final class JsonTypeUtil |
| 39 | +{ |
| 40 | + private static final JsonFactory JSON_FACTORY = new JsonFactoryBuilder().disable(CANONICALIZE_FIELD_NAMES).build(); |
| 41 | + private static final ObjectMapper SORTED_MAPPER = new ObjectMapperProvider().get().configure(ORDER_MAP_ENTRIES_BY_KEYS, true); |
| 42 | + |
| 43 | + private JsonTypeUtil() {} |
| 44 | + |
| 45 | + // TODO: this should be available from the engine |
| 46 | + public static Slice jsonParse(Slice slice) |
| 47 | + { |
| 48 | + // cast(json_parse(x) AS t)` will be optimized into `$internal$json_string_to_array/map/row_cast` in ExpressionOptimizer |
| 49 | + // If you make changes to this function (e.g. use parse JSON string into some internal representation), |
| 50 | + // make sure `$internal$json_string_to_array/map/row_cast` is changed accordingly. |
| 51 | + try (JsonParser parser = createJsonParser(JSON_FACTORY, slice)) { |
| 52 | + SliceOutput output = new DynamicSliceOutput(slice.length()); |
| 53 | + SORTED_MAPPER.writeValue((OutputStream) output, SORTED_MAPPER.readValue(parser, Object.class)); |
| 54 | + // At this point, the end of input should be reached. nextToken() has three possible results: |
| 55 | + // - null, if the end of the input was reached |
| 56 | + // - token, if a correct JSON token is found (e.g. '{', 'null', '1') |
| 57 | + // - exception, if there are characters which do not form a valid JSON token (e.g. 'abc') |
| 58 | + checkState(parser.nextToken() == null, "Found characters after the expected end of input"); |
| 59 | + return output.slice(); |
| 60 | + } |
| 61 | + catch (IOException | RuntimeException e) { |
| 62 | + throw new TrinoException(INVALID_FUNCTION_ARGUMENT, format("Cannot convert value to JSON: '%s'", slice.toStringUtf8()), e); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public static Slice toJsonValue(Object value) |
| 67 | + throws IOException |
| 68 | + { |
| 69 | + return Slices.wrappedBuffer(SORTED_MAPPER.writeValueAsBytes(value)); |
| 70 | + } |
| 71 | + |
| 72 | + private static JsonParser createJsonParser(JsonFactory factory, Slice json) |
| 73 | + throws IOException |
| 74 | + { |
| 75 | + // Jackson tries to detect the character encoding automatically when |
| 76 | + // using InputStream, so we pass an InputStreamReader instead. |
| 77 | + return factory.createParser(new InputStreamReader(json.getInput(), UTF_8)); |
| 78 | + } |
| 79 | +} |
0 commit comments