{"record":{"id":"f70cc5b3b39169b2","repo":"apache/flink","slug":"expecting-a-primitive-variant-but-got-s","errorCode":null,"errorMessage":"Expecting a primitive variant but got %s","messagePattern":"Expecting a primitive variant but got (.+?)","errorType":"exception","errorClass":"VariantTypeException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/types/variant/BinaryVariant.java","lineNumber":232,"sourceCode":"                return getLong();\n            case FLOAT:\n                return getFloat();\n            case DOUBLE:\n                return getDouble();\n            case DECIMAL:\n                return getDecimal();\n            case STRING:\n                return getString();\n            case DATE:\n                return getDate();\n            case TIMESTAMP:\n                return getDateTime();\n            case TIMESTAMP_LTZ:\n                return getInstant();\n            case BYTES:\n                return getBytes();\n            default:\n                throw new VariantTypeException(\n                        String.format(\"Expecting a primitive variant but got %s\", getType()));\n        }\n    }\n\n    @Override\n    public <T> T getAs() throws VariantTypeException {\n        return (T) get();\n    }\n\n    @Override\n    public Variant getElement(int index) throws VariantTypeException {\n        return getElementAtIndex(index);\n    }\n\n    @Override\n    public int getArraySize() throws VariantTypeException {\n        return handleArray(value, pos, (size, offsetSize, offsetStart, dataStart) -> size);\n    }","sourceCodeStart":214,"sourceCodeEnd":250,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/types/variant/BinaryVariant.java#L214-L250","documentation":"BinaryVariant.get() switches on the variant's type tag and materializes the primitive value; the default branch throws VariantTypeException because the stored type is not one of the handled primitive kinds (it is likely an OBJECT or ARRAY, i.e. a compound value).","triggerScenarios":"Calling get()/getAs() on a Variant whose getType() is OBJECT or ARRAY (or an unknown future type tag). get() is intended for primitives only; getObject/getArray must be used for compound types.","commonSituations":"Schema-on-read pipelines where a column is Variant and some rows hold objects/arrays while code assumes scalars; upstream producer changed a field from a scalar to a nested object; generic code doing variant.getAs() without first checking type.","solutions":["Branch on getType() first: use getObject(...)/getArray(...) for OBJECT/ARRAY and get() for primitives","If the value should be a scalar, fix the producer that wrote a nested object/array into this variant","Use the typed accessors (getString, getLong, ...) which give clearer errors, after a type check"],"exampleFix":"// before\nObject v = variant.get(); // throws if variant holds an object\n\n// after\nObject v;\nswitch (variant.getType()) {\n    case OBJECT: v = variant.getObject(); break;\n    case ARRAY:  v = variant.getArray();  break;\n    default:     v = variant.get();       break;\n}","handlingStrategy":"type-guard","validationCode":"if (variant.getType() == VariantType.OBJECT || variant.getType() == VariantType.ARRAY) {\n    /* use getObject()/getArray() instead of get() */\n}","typeGuard":"static boolean isPrimitiveVariant(Variant v) {\n    VariantType t = v.getType();\n    return t != VariantType.OBJECT && t != VariantType.ARRAY;\n}","tryCatchPattern":"catch (VariantTypeException e) { /* fall back to per-type handling based on getType() */ }","preventionTips":["Always switch on getType() before accessing variant values","Keep per-field variant types stable at the producer","Add contract tests asserting scalar types for columns consumed via get()"],"tags":["variant","type-mismatch","flink-core","semi-structured"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}