{"record":{"id":"85247dc54a84358b","repo":"apache/iceberg","slug":"failed-to-read-variant-s-of-type-s-as-s","errorCode":null,"errorMessage":"Failed to read Variant %s of type %s as %s","messagePattern":"Failed to read Variant (.+?) of type (.+?) as (.+?)","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"flink/v2.3/flink/src/main/java/org/apache/iceberg/flink/data/VariantRowDataWrapper.java","lineNumber":255,"sourceCode":"    }\n\n    int arraySize = BinaryVariantAccessorUtils.arraySize(variant);\n    Object[] elements = new Object[arraySize];\n\n    for (int i = 0; i < arraySize; i++) {\n      Variant element = variant.getElement(i);\n      elements[i] = elementValue(element, innerElementType);\n    }\n\n    return new GenericArrayData(elements);\n  }\n\n  private static int intValue(Variant variant) {\n    return switch (variant.getType()) {\n      case TINYINT -> variant.getByte();\n      case SMALLINT -> variant.getShort();\n      case INT -> variant.getInt();\n      default -> throw new UnsupportedOperationException(errMsg(variant, \"int\"));\n    };\n  }\n\n  private static long longValue(Variant variant) {\n    return switch (variant.getType()) {\n      case TINYINT -> variant.getByte();\n      case SMALLINT -> variant.getShort();\n      case INT -> variant.getInt();\n      case BIGINT -> variant.getLong();\n      default -> throw new UnsupportedOperationException(errMsg(variant, \"long\"));\n    };\n  }\n\n  private static double doubleValue(Variant variant) {\n    return switch (variant.getType()) {\n      case FLOAT -> variant.getFloat();\n      case DOUBLE -> variant.getDouble();\n      default -> throw new UnsupportedOperationException(errMsg(variant, \"double\"));","sourceCodeStart":237,"sourceCodeEnd":273,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/flink/v2.3/flink/src/main/java/org/apache/iceberg/flink/data/VariantRowDataWrapper.java#L237-L273","documentation":"VariantRowDataWrapper.intValue narrows a Variant to an int, accepting TINYINT, SMALLINT and INT variants; anything else throws UnsupportedOperationException with errMsg(variant, \"int\"): \"Failed to read Variant <value> of type <type> as int\". Raised when the Variant's actual type cannot losslessly be read as an int.","triggerScenarios":"getInt(pos) or elementValue(...) with an INT LogicalType encountering a Variant of type BIGINT, DOUBLE, STRING, BOOLEAN, etc.","commonSituations":"Schema/type mismatch where the Flink column declares INT but the stored Variant holds a wider number or a string; mixed-typed arrays/maps with int element declarations.","solutions":["Declare the Flink field as BIGINT (and use getLong) if values exceed int range.","Fix the writer so Variant values written for int columns are of type INT.","Cast the Variant value in the query to a numeric type before int access."],"exampleFix":"// before\nDataTypes.FIELD(\"n\", DataTypes.INT()) // variant holds BIGINT\n\n// after\nDataTypes.FIELD(\"n\", DataTypes.BIGINT())","handlingStrategy":"validation","validationCode":"Variant v = ...;\nif (!(v.getType() == Variant.Type.TINYINT || v.getType() == Variant.Type.SMALLINT || v.getType() == Variant.Type.INT)) {\n  throw new IllegalArgumentException(\"Variant not int-typed: \" + v.getType());\n}","typeGuard":"boolean isIntVariant(Variant v) {\n  switch (v.getType()) {\n    case TINYINT: case SMALLINT: case INT: return true;\n    default: return false;\n  }\n}","tryCatchPattern":"try {\n  int i = rowData.getInt(pos);\n} catch (UnsupportedOperationException e) {\n  // widen the column to BIGINT or convert the variant value upstream\n}","preventionTips":["Match Flink column LogicalType to the Variant value type written.","Use BIGINT for values that may exceed int range.","Validate Variant types in a test pipeline before production reads."],"tags":["flink","variant","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"86d9c8fc543e7c56c9f624eb725f76c9baff9570","analyzedAt":"2026-09-12T00:46:39.097Z","contentChangedAt":"2026-09-12T00:46:39.097Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}