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


##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/VariantCastUtils.java:
##########
@@ -0,0 +1,98 @@
+/*
+ * 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.data.DecimalData;
+import org.apache.flink.types.variant.Variant;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+
+/**
+ * Runtime helpers for casting a {@code VARIANT} value to a SQL type.
+ *
+ * <p>Numeric targets are range-checked and raise {@link ArithmeticException} 
on overflow instead of
+ * wrapping. Casting to a string extracts the scalar value (a string stays 
unquoted), while objects
+ * and arrays use their JSON representation.
+ */
+@Internal
+public final class VariantCastUtils {
+
+    private VariantCastUtils() {}
+
+    public static byte toByteExact(Number value) {
+        return (byte) toIntegralExact(value, Byte.MIN_VALUE, Byte.MAX_VALUE, 
"TINYINT");
+    }
+
+    public static short toShortExact(Number value) {
+        return (short) toIntegralExact(value, Short.MIN_VALUE, 
Short.MAX_VALUE, "SMALLINT");
+    }
+
+    public static int toIntExact(Number value) {
+        return (int) toIntegralExact(value, Integer.MIN_VALUE, 
Integer.MAX_VALUE, "INTEGER");
+    }
+
+    public static long toLongExact(Number value) {
+        return toIntegralExact(value, Long.MIN_VALUE, Long.MAX_VALUE, 
"BIGINT");
+    }
+
+    public static DecimalData toDecimalExact(Number value, int precision, int 
scale) {
+        final DecimalData decimal =
+                DecimalData.fromBigDecimal(toBigDecimal(value), precision, 
scale);
+        if (decimal == null) {
+            throw new ArithmeticException(
+                    String.format(
+                            "Casting the VARIANT value %s to DECIMAL(%d, %d) 
overflowed.",
+                            value, precision, scale));
+        }
+        return decimal;
+    }
+
+    /**
+     * Casts a {@code VARIANT} to its SQL string value: scalars use their raw 
value (a string stays
+     * unquoted, unlike {@code JSON_STRING}), objects and arrays use JSON.
+     */
+    public static String toStringValue(Variant variant) {
+        switch (variant.getType()) {
+            case OBJECT:
+            case ARRAY:
+            case BYTES:
+                return variant.toJson();
+            default:
+                return String.valueOf(variant.get());
+        }
+    }
+
+    private static long toIntegralExact(Number value, long min, long max, 
String targetType) {
+        // toBigInteger truncates any fractional part toward zero, matching 
regular numeric casts.
+        final BigInteger integral = toBigDecimal(value).toBigInteger();
+        if (integral.compareTo(BigInteger.valueOf(min)) < 0
+                || integral.compareTo(BigInteger.valueOf(max)) > 0) {
+            throw new ArithmeticException(
+                    String.format(
+                            "Casting the VARIANT value %s to %s overflowed.", 
value, targetType));
+        }
+        return integral.longValue();
+    }
+
+    private static BigDecimal toBigDecimal(Number value) {
+        return value instanceof BigDecimal ? (BigDecimal) value : new 
BigDecimal(value.toString());

Review Comment:
   addressed



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