manner commented on code in PR #29520:
URL: https://github.com/apache/flink/pull/29520#discussion_r4513053293


##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/VariantToXmlWriter.java:
##########
@@ -0,0 +1,784 @@
+/*
+ * 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.table.runtime.functions;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.runtime.functions.XmlVariantNames.SchemaPrefix;
+import org.apache.flink.types.variant.Variant;
+
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Document;
+
+import javax.annotation.Nullable;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+import java.io.StringWriter;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
+import java.util.Base64;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.EnumSet;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import static 
org.apache.flink.table.runtime.functions.XmlVariantNames.ATTRIBUTE_PREFIX;
+import static 
org.apache.flink.table.runtime.functions.XmlVariantNames.ORDER_KEY;
+import static 
org.apache.flink.table.runtime.functions.XmlVariantNames.TEXT_KEY;
+import static org.apache.flink.table.runtime.functions.XmlVariantNames.XSI_NIL;
+import static 
org.apache.flink.table.runtime.functions.XmlVariantNames.XSI_TYPE;
+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.util.Preconditions.checkArgument;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * Writes a {@link Variant} as an XML string for {@code XML_STRING}. It 
reverses the mapping of
+ * {@link XmlToVariantParser}, so that parsing the XML string of a variant 
from {@code PARSE_XML}
+ * gives the same variant. The mapping is described in the documentation of 
{@code XML_STRING}.
+ *
+ * <p>The content of an element is written in the order of its {@code #} 
field. Content without a
+ * position in the {@code #} field follows in the order of its keys. A variant 
{@code NULL}, or a
+ * {@code NULL} text, is written as {@code xsi:nil="true"}, and a scalar other 
than a string or a
+ * UUID gets an {@code xsi:type}, e.g. {@code xsi:type="xs:int"}. If the 
document uses the xsi or xs
+ * prefix, the root element declares it with the namespace of XML Schema, 
unless the variant
+ * declares it on the root element itself.
+ *
+ * <p>The XML writer of the JDK writes the tags and escapes the text. It 
writes tabs and line breaks
+ * in attribute values, and carriage returns in text, as they are, so a parser 
reads them back as
+ * spaces and line feeds.
+ *
+ * <p>A variant fails with an {@link IllegalArgumentException} if it has no 
XML representation:
+ *
+ * <ul>
+ *   <li>it isn't an object with a single field for the root element, or the 
root is an array,
+ *   <li>a key isn't an XML name that {@code PARSE_XML} accepts,
+ *   <li>a text or an attribute value has a character that XML 1.0 doesn't 
allow, e.g. U+0000,
+ *   <li>an array is nested in an array,
+ *   <li>an {@code @} field holds an object, an array, or a {@code NULL}, or a 
{@code $} field holds
+ *       an object,
+ *   <li>a {@code NULL} text isn't the only content of its element, or an 
element has an {@code
+ *       @xsi:nil} field that is true and content,
+ *   <li>a {@code #} field isn't an object, doesn't have one integer position 
for each item of a
+ *       key, or has a position more than once,
+ *   <li>an element would need two {@code xsi:type} or two {@code xsi:nil} 
attributes, e.g. an
+ *       {@code @xsi:type} field and a typed text, or an {@code @xsi:nil} 
field and a {@code NULL}
+ *       text.
+ * </ul>
+ */
+@Internal
+public final class VariantToXmlWriter {
+
+    private static final Set<Variant.Type> INTEGER_TYPES =
+            EnumSet.of(
+                    Variant.Type.TINYINT,
+                    Variant.Type.SMALLINT,
+                    Variant.Type.INT,
+                    Variant.Type.BIGINT);
+
+    // The XML writer of the JDK, regardless of other StAX implementations on 
the classpath. Its
+    // factory creates a new writer for each call and keeps no state of it, so 
it can be shared.
+    private static final XMLOutputFactory OUTPUT_FACTORY = 
XMLOutputFactory.newDefaultFactory();
+
+    // Any character outside of the Char production of XML 1.0, see
+    // https://www.w3.org/TR/xml/#charsets.
+    private static final Pattern NON_XML_CHAR =
+            
Pattern.compile("[^\t\n\r\u0020-\uD7FF\uE000-\uFFFD\\x{10000}-\\x{10FFFF}]");
+
+    // The DOM document that names are checked with. It isn't thread-safe, so 
each thread has its
+    // own.
+    private static final ThreadLocal<Document> NAME_DOCUMENT =
+            ThreadLocal.withInitial(VariantToXmlWriter::newDocument);
+
+    // The document that is written, and the schema prefixes that it uses.
+    private final StringWriter output = new StringWriter();
+    private final XMLStreamWriter xml;
+    private final Set<SchemaPrefix> usedSchemaPrefixes = 
EnumSet.noneOf(SchemaPrefix.class);
+
+    private VariantToXmlWriter() {
+        try {
+            xml = OUTPUT_FACTORY.createXMLStreamWriter(output);
+        } catch (XMLStreamException e) {
+            // Only the message, since the location of the exception isn't 
serializable.
+            throw new IllegalStateException(e.getMessage());
+        }
+    }
+
+    public static String write(Variant variant) {
+        final String rootName = rootName(variant);
+        final Variant root = variant.getField(rootName);
+        checkRootIsNotArray(rootName, root);
+        return new VariantToXmlWriter().writeDocument(rootName, root);

Review Comment:
   That doesn't work here, because `VariantToXmlWriter` holds some state that 
is needed for each call.  `XMLStreamWriter` is also not thread safe, so each 
`write()` needs its own instance.



-- 
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]

Reply via email to