raminqaf commented on code in PR #29369:
URL: https://github.com/apache/flink/pull/29369#discussion_r4184318671


##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/ToVariantConverter.java:
##########
@@ -0,0 +1,260 @@
+/*
+ * 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.api.TableRuntimeException;
+import org.apache.flink.table.data.ArrayData;
+import org.apache.flink.table.data.DecimalData;
+import org.apache.flink.table.data.MapData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.TimestampData;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.DistinctType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.LogicalTypeRoot;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.utils.LogicalTypeChecks;
+import org.apache.flink.table.types.logical.utils.UuidUtils;
+import org.apache.flink.types.variant.BinaryVariant;
+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 java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Converts internal data of a SQL type into a {@link Variant}, like {@code 
CAST(x AS VARIANT)}.
+ *
+ * <p>An {@code ARRAY} becomes a variant array. A {@code ROW} or {@code 
STRUCTURED} value becomes a
+ * variant object keyed by its field names, and a {@code MAP} one keyed by its 
character string
+ * keys. A {@code NULL} element, field or map value becomes a variant null. A 
leaf is stored like
+ * the cast stores it on its own, see {@link VariantCastUtils}, and a nested 
{@code VARIANT} is
+ * embedded as is.
+ *
+ * <p>The converter is created once per type and writes a whole value into one 
builder, so a nested
+ * value is encoded in a single pass. It keeps no state between calls.
+ */
+@Internal
+public final class ToVariantConverter implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    private final String sourceType;
+    private final ValueWriter writer;
+
+    private ToVariantConverter(String sourceType, ValueWriter writer) {
+        this.sourceType = sourceType;
+        this.writer = writer;
+    }
+
+    /** Creates a converter for a type that {@code LogicalTypeCasts} allows to 
cast to VARIANT. */
+    public static ToVariantConverter create(LogicalType type) {
+        return new ToVariantConverter(type.asSummaryString(), 
createWriter(type));
+    }
+
+    /**
+     * Converts a value that is not {@code NULL}.
+     *
+     * @throws TableRuntimeException if the value has a {@code NULL} map key, 
a timestamp outside
+     *     the range of its variant kind, or does not fit into the 16 MiB of a 
{@code VARIANT}
+     */
+    public Variant convert(Object value) {
+        // A MAP can hold the same key twice at runtime. The builder keeps the 
last one, like the
+        // MAP constructor does.
+        final BinaryVariantInternalBuilder builder = new 
BinaryVariantInternalBuilder(true);
+        try {
+            writer.write(builder, value);
+            return builder.build();
+        } catch (VariantTypeException e) {
+            if (e != 
BinaryVariantInternalBuilder.VARIANT_SIZE_LIMIT_EXCEPTION) {
+                throw e;
+            }
+            throw new TableRuntimeException(
+                    String.format(
+                            "Cannot cast a value of type %s to VARIANT. A 
VARIANT is limited to "
+                                    + "16 MiB.",
+                            sourceType));
+        }
+    }
+
+    @FunctionalInterface
+    private interface ValueWriter extends Serializable {
+        void write(BinaryVariantInternalBuilder builder, Object value);
+    }
+
+    private static ValueWriter createNullableWriter(LogicalType type) {
+        final ValueWriter writer = createWriter(type);
+        return (builder, value) -> {
+            if (value == null) {
+                builder.appendNull();
+            } else {
+                writer.write(builder, value);
+            }
+        };
+    }
+
+    private static ValueWriter createWriter(LogicalType type) {

Review Comment:
   Done, with your second option. `ToVariantCastRule` replaces 
`PrimitiveToVariantCastRule`, and the primitive cast now calls 
`ToVariantConverter` too. `createWriter` is now the only mapping from a SQL 
type to a variant kind, and the `VariantCastUtils.from*` methods are gone. The 
string and decimal fast paths from #29370 will move into the converter's 
writers.



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