snuyanzin commented on code in PR #29341: URL: https://github.com/apache/flink/pull/29341#discussion_r4156732229
########## flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/XmlToVariantParser.java: ########## @@ -0,0 +1,588 @@ +/* + * 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.types.variant.BinaryVariantInternalBuilder; +import org.apache.flink.types.variant.BinaryVariantInternalBuilder.FieldEntry; +import org.apache.flink.types.variant.Variant; +import org.apache.flink.types.variant.VariantTypeException; + +import javax.annotation.Nullable; +import javax.xml.XMLConstants; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import java.io.StringReader; +import java.math.BigDecimal; +import java.math.BigInteger; +import java.time.DateTimeException; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoField; +import java.time.temporal.TemporalAccessor; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Consumer; + +import static org.apache.flink.types.variant.BinaryVariantInternalBuilder.toVariantDecimal; +import static org.apache.flink.types.variant.BinaryVariantUtil.SIZE_LIMIT; +import static org.apache.flink.types.variant.BinaryVariantUtil.microsSinceEpoch; +import static org.apache.flink.types.variant.BinaryVariantUtil.nanosSinceEpoch; + +/** + * Parses XML into a {@link Variant} for {@code PARSE_XML} and {@code TRY_PARSE_XML}. The mapping is + * described in the documentation of the {@code VARIANT} data type. + * + * <p>Parsing has two steps. First, the document is read into a tree of {@link XmlElement}s, which + * group the children of each element by name and apply {@code xsi:nil} and {@code xsi:type}. Then, + * the {@link VariantEncoder} writes the tree into a variant. The variant can't be written while + * reading, because the occurrences of a repeated child become one array, and they don't have to be + * next to each other in the document. + * + * <p>Instances are not thread-safe. + */ +@Internal +public final class XmlToVariantParser { + + private static final String ATTRIBUTE_PREFIX = "@"; + private static final String TEXT_KEY = "$"; + private static final String ORDER_KEY = "#"; + + private static final String XSI_NIL = "xsi:nil"; + private static final String XSI_TYPE = "xsi:type"; + private static final String XSI_NAMESPACE_DECLARATION = "xmlns:xsi"; + + private final XMLInputFactory inputFactory = createInputFactory(); + + public Variant parse(String xml, boolean forceArray) { + final XmlElement root; + try { + root = readDocument(xml); + } catch (XMLStreamException e) { + // Only the message is kept, since the location of the exception is not serializable. + throw new IllegalArgumentException(e.getMessage()); + } + return new VariantEncoder(forceArray).encodeDocument(root); + } + + private static XMLInputFactory createInputFactory() { + // The JDK implementation, regardless of other StAX implementations on the classpath. + final XMLInputFactory factory = XMLInputFactory.newDefaultFactory(); + factory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false); + factory.setProperty(XMLInputFactory.IS_COALESCING, true); + // Internal entities need DTD support. External entities are enabled only so that they + // reach the resolver and fail, instead of being dropped silently. The JDK refuses all + // protocols in case a load bypasses the resolver. + factory.setProperty(XMLInputFactory.SUPPORT_DTD, true); + factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, true); + factory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, ""); + factory.setXMLResolver( + (publicId, systemId, baseUri, namespace) -> { + throw new XMLStreamException( + String.format( + "External entities and DTDs are not supported: %s", systemId)); + }); + // Set here, so that they don't depend on the JDK version or JVM-wide settings. Apart from + // the depth and the entity sizes, these are the defaults of JDK 11 to 21. + // Each element can add an object and an array to the variant, so 500 levels give at most + // the 1000 levels that PARSE_JSON allows. + setLimit(factory, "maxElementDepth", 500); + setLimit(factory, "entityExpansionLimit", 64_000); + setLimit(factory, "totalEntitySizeLimit", SIZE_LIMIT); + setLimit(factory, "maxGeneralEntitySizeLimit", SIZE_LIMIT); + setLimit(factory, "maxParameterEntitySizeLimit", 1_000_000); + setLimit(factory, "elementAttributeLimit", 10_000); + setLimit(factory, "maxXMLNameLimit", 1_000); + return factory; + } + + private static void setLimit(XMLInputFactory factory, String limit, int value) { + factory.setProperty("http://www.oracle.com/xml/jaxp/properties/" + limit, value); + } + + private XmlElement readDocument(String xml) throws XMLStreamException { + final XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(xml)); + try { + // XML 1.1 allows characters that XML 1.0 can't represent, e.g. most control characters. + // Accepting only XML 1.0 keeps every result representable as XML 1.0. + if ("1.1".equals(reader.getVersion())) { Review Comment: can we have an enum with supported version? -- 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]
