manner commented on code in PR #29341: URL: https://github.com/apache/flink/pull/29341#discussion_r4231407815
########## flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/XmlToVariantParser.java: ########## @@ -0,0 +1,656 @@ +/* + * 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.annotation.VisibleForTesting; +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.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.Arrays; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Consumer; +import java.util.regex.Pattern; + +import static java.util.Map.entry; +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, {@link #read} reads the document 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, {@link #encode} 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(); + + @VisibleForTesting + Variant parse(String xml, boolean forceArray) { + return encode(read(xml), forceArray); + } + + /** + * Reads the document into the tree of its root element. Calls on the same document can share + * the tree, since {@link #encode} doesn't change it. + */ + XmlElement read(String xml) { + try { + return readDocument(xml); + } catch (XMLStreamException e) { + // Only the message is kept, since the location of the exception is not serializable. + throw new IllegalArgumentException(e.getMessage()); Review Comment: `Location` in `XMLStreamException` isn’t serializable, so if we add it as the cause, the client only gets `SerializedThrowable` instead of the `TableRuntimeException`. -- 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]
