snuyanzin commented on code in PR #29310: URL: https://github.com/apache/flink/pull/29310#discussion_r4122240698
########## flink-core/src/main/java/org/apache/flink/types/variant/JsonVariantFormatter.java: ########## @@ -0,0 +1,259 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.types.variant; + +import org.apache.flink.annotation.Internal; + +import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory; +import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator; + +import java.io.CharArrayWriter; +import java.io.IOException; +import java.time.LocalDate; +import java.time.LocalTime; +import java.time.ZoneOffset; +import java.util.Base64; + +import static org.apache.flink.types.variant.BinaryVariantUtil.TIMESTAMP_FORMATTER; +import static org.apache.flink.types.variant.BinaryVariantUtil.TIMESTAMP_LTZ_FORMATTER; +import static org.apache.flink.types.variant.BinaryVariantUtil.TIME_FORMATTER; +import static org.apache.flink.types.variant.BinaryVariantUtil.getMetadataKey; +import static org.apache.flink.types.variant.BinaryVariantUtil.handleArray; +import static org.apache.flink.types.variant.BinaryVariantUtil.handleObject; +import static org.apache.flink.types.variant.BinaryVariantUtil.microsToInstant; +import static org.apache.flink.types.variant.BinaryVariantUtil.nanosToInstant; +import static org.apache.flink.types.variant.BinaryVariantUtil.readUnsigned; +import static org.apache.flink.types.variant.BinaryVariantUtil.unexpectedType; + +/** + * Renders a {@link BinaryVariant} as JSON. + * + * <p>{@link #STRICT} backs {@link Variant#toJson()} and fails on NaN, infinity, and undecodable + * nodes. {@link #LENIENT} backs {@link BinaryVariant#toString()} and never fails: it writes NaN as + * {@code "NaN"}, a type this version does not know as {@code "<UNKNOWN>"}, and other undecodable + * data as {@code "<INVALID>"}. + */ +@Internal +public final class JsonVariantFormatter implements VariantFormatter { + + public static final JsonVariantFormatter STRICT = new JsonVariantFormatter(false); + + public static final JsonVariantFormatter LENIENT = new JsonVariantFormatter(true); + + private static final String UNKNOWN_NODE = "<UNKNOWN>"; + + private static final String INVALID_NODE = "<INVALID>"; + + private final boolean lenient; + + private JsonVariantFormatter(final boolean lenient) { + this.lenient = lenient; + } + + @Override + public String format(final Variant variant) { + final BinaryVariant binary = (BinaryVariant) variant; + final StringBuilder sb = new StringBuilder(); + append(binary.rawValue(), binary.getMetadata(), binary.getPos(), sb); + return sb.toString(); + } + + private void append( + final byte[] value, final byte[] metadata, final int pos, final StringBuilder sb) { + final int start = sb.length(); + try { + appendNode(value, metadata, pos, sb); + } catch (VariantTypeException e) { + if (!lenient) { + throw e; + } + // Drop the node's partial output, such as a dangling key. + sb.setLength(start); + appendQuoted( + sb, BinaryVariantUtil.isUnknownType(value, pos) ? UNKNOWN_NODE : INVALID_NODE); + } + } + + private void appendNode(byte[] value, byte[] metadata, int pos, StringBuilder sb) { + switch (BinaryVariantUtil.getType(value, pos)) { + case OBJECT: + handleObject( + value, + pos, + (size, idSize, offsetSize, idStart, offsetStart, dataStart) -> { + sb.append('{'); + for (int i = 0; i < size; ++i) { + int id = readUnsigned(value, idStart + idSize * i, idSize); + int offset = + readUnsigned( + value, offsetStart + offsetSize * i, offsetSize); + int elementPos = dataStart + offset; + if (i != 0) { + sb.append(','); + } + sb.append(escapeJson(getMetadataKey(metadata, id))); + sb.append(':'); + append(value, metadata, elementPos, sb); + } + sb.append('}'); + return null; + }); + break; + case ARRAY: + handleArray( + value, + pos, + (size, offsetSize, offsetStart, dataStart) -> { + sb.append('['); + for (int i = 0; i < size; ++i) { + int offset = + readUnsigned( + value, offsetStart + offsetSize * i, offsetSize); + int elementPos = dataStart + offset; + if (i != 0) { + sb.append(','); + } + append(value, metadata, elementPos, sb); + } + sb.append(']'); + return null; + }); + break; + case NULL: + sb.append("null"); + break; + case BOOLEAN: + sb.append(BinaryVariantUtil.getBoolean(value, pos)); + break; + case TINYINT: + case SMALLINT: + case INT: + case BIGINT: + sb.append(BinaryVariantUtil.getLong(value, pos)); + break; + case STRING: + sb.append(escapeJson(BinaryVariantUtil.getString(value, pos))); + break; + case DOUBLE: + { + final double d = BinaryVariantUtil.getDouble(value, pos); + if (Double.isFinite(d)) { + sb.append(d); + } else { + appendNonFinite(sb, Double.toString(d), d); + } + break; + } + case DECIMAL: + sb.append(BinaryVariantUtil.getDecimal(value, pos).toPlainString()); + break; + case DATE: + appendQuoted( + sb, + LocalDate.ofEpochDay((int) BinaryVariantUtil.getLong(value, pos)) + .toString()); + break; + case TIMESTAMP_LTZ: + appendQuoted( + sb, + TIMESTAMP_LTZ_FORMATTER.format( + microsToInstant(BinaryVariantUtil.getLong(value, pos)) + .atZone(ZoneOffset.UTC))); + break; + case TIMESTAMP: + appendQuoted( + sb, + TIMESTAMP_FORMATTER.format( + microsToInstant(BinaryVariantUtil.getLong(value, pos)) + .atZone(ZoneOffset.UTC))); + break; + case TIME: + appendQuoted( + sb, + TIME_FORMATTER.format( + LocalTime.ofNanoOfDay( + BinaryVariantUtil.getLong(value, pos) * 1000))); + break; + case TIMESTAMP_LTZ_NS: + appendQuoted( + sb, + TIMESTAMP_LTZ_FORMATTER.format( + nanosToInstant(BinaryVariantUtil.getLong(value, pos)) + .atZone(ZoneOffset.UTC))); + break; + case TIMESTAMP_NS: + appendQuoted( + sb, + TIMESTAMP_FORMATTER.format( + nanosToInstant(BinaryVariantUtil.getLong(value, pos)) + .atZone(ZoneOffset.UTC))); + break; + case FLOAT: + { + final float f = BinaryVariantUtil.getFloat(value, pos); + if (Float.isFinite(f)) { + sb.append(f); + } else { + appendNonFinite(sb, Float.toString(f), f); + } + break; + } + case BYTES: + appendQuoted( + sb, + Base64.getEncoder() + .encodeToString(BinaryVariantUtil.getBinary(value, pos))); + break; + case UUID: + appendQuoted(sb, BinaryVariantUtil.getUuid(value, pos).toString()); + break; + default: + throw unexpectedType(BinaryVariantUtil.getType(value, pos)); + } + } + + /** JSON has no literal for NaN or infinity. */ + private void appendNonFinite(final StringBuilder sb, final String text, final double number) { + if (!lenient) { + throw new VariantTypeException( + String.format("Non-finite value %s cannot be serialized to JSON.", number)); + } + appendQuoted(sb, text); + } + + // Escape a string so that it can be pasted into JSON structure. + // For example, if `str` only contains a new-line character, then the result content is "\n" + // (4 characters). + private static String escapeJson(String str) { + try (CharArrayWriter writer = new CharArrayWriter(); + JsonGenerator gen = new JsonFactory().createGenerator(writer)) { Review Comment: Should we create create factory on each call? Jackson says it is reusable -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
