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


##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/VariantCastUtils.java:
##########
@@ -277,26 +281,67 @@ public static byte[] toBytes(Variant variant, int 
targetLength, boolean fixedLen
      */
     public static BinaryStringData toStringValue(
             Variant variant, TimeZone sessionZone, int targetLength, boolean 
charTarget) {
-        final String value = getVariantTypeAsString(variant, sessionZone, 
targetLength, charTarget);
+        final String targetDescription = characterTarget(targetLength, 
charTarget);
+        final String value = renderValue(variant, sessionZone, 
targetDescription);
         // numChars and substring both count code points, so a character 
outside the BMP fills one
         // position rather than the two UTF-16 units it occupies.
-        final BinaryStringData result = BinaryStringData.fromString(value);
-        final int length = result.numChars();
-        if (length > targetLength) {
-            return result.substring(0, targetLength);
+        return variantKey(value, targetLength, charTarget);
+    }
+
+    /**
+     * Renders a variant as a character string. An array becomes {@code [e1, 
e2]} and an object
+     * becomes {@code {k1=v1, k2=v2}}, matching how a regular {@code ARRAY} or 
{@code MAP} casts to
+     * a string. Elements and field values recurse through the same rendering, 
so a string stays
+     * unquoted at every depth. A scalar renders like a regular cast of its 
stored kind.
+     */
+    private static String renderValue(
+            final Variant variant, final TimeZone sessionZone, final String 
targetDescription) {
+        if (variant.isArray()) {
+            final int size = variant.getArraySize();
+            final StringBuilder sb = new StringBuilder();
+            sb.append('[');
+            for (int i = 0; i < size; i++) {
+                if (i > 0) {
+                    sb.append(", ");
+                }
+                sb.append(renderElement(variant.getElement(i), sessionZone, 
targetDescription));

Review Comment:
   Good catch. Fixed. Nested values now report an unbounded "a character 
string" target instead of the container's `CHAR(n)/VARCHAR(n)`, since an 
element is rendered in full and only the whole result is trimmed. Only a 
top-level scalar keeps its real bounded target. Added regression tests for a 
byte value nested in both an array element and an object value.



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