{"record":{"id":"9692a2ee7314cd74","repo":"apache/beam","slug":"can-t-cast-numbers-to-non-numeric-type-output","errorCode":null,"errorMessage":"Can't cast numbers to non-numeric type: +output","messagePattern":"Can't cast numbers to non-numeric type: \\+output","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/transforms/Cast.java","lineNumber":340,"sourceCode":"      int fromFieldIdx = inputSchema.indexOf(outputField.getName());\n      Schema.Field inputField = inputSchema.getField(fromFieldIdx);\n\n      Object inputValue = input.getValue(fromFieldIdx);\n      Object outputValue = castValue(inputValue, inputField.getType(), outputField.getType());\n\n      output.addValue(outputValue);\n    }\n\n    return output.build();\n  }\n\n  public static Number castNumber(Number value, TypeName input, TypeName output) {\n    if (!input.isNumericType()) {\n      throw new RuntimeException(\"Can't cast non-numeric types: \" + input);\n    }\n\n    if (!output.isNumericType()) {\n      throw new RuntimeException(\"Can't cast numbers to non-numeric type: \" + output);\n    }\n\n    if (value == null) {\n      return null;\n    }\n\n    if (input == output) {\n      return value;\n    }\n\n    switch (output) {\n      case BYTE:\n        return value.byteValue();\n\n      case INT16:\n        return value.shortValue();\n\n      case INT32:","sourceCodeStart":322,"sourceCodeEnd":358,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/transforms/Cast.java#L322-L358","documentation":"Cast.castNumber also validates that the OUTPUT TypeName is numeric; casting a number to e.g. a STRING or BOOLEAN output type is rejected with a RuntimeException rather than silently converting via toString.","triggerScenarios":"Calling Cast.to(outputSchema) where an input field is numeric (e.g. INT64) but the corresponding output field is a non-numeric type (STRING, BOOLEAN, etc.).","commonSituations":"Changing a field's type from numeric to string in the target schema and expecting Cast to stringify automatically; copying schemas between systems where one side stores numbers as strings.","solutions":["Change the output schema field to a numeric type matching the input.","Convert the numeric field to string explicitly beforehand with a Map transform instead of Cast.","Use a custom cast function (castRow with per-field Function) that handles number->string conversion.","Validate input and output schemas for type compatibility before applying Cast."],"exampleFix":"// before\nSchema out = Schema.of(Schema.Field.of(\"id\", FieldType.STRING)); // id was INT64\nrows.apply(Cast.to(out));\n// after\nrows.apply(MapElements.into(TypeDescriptor.of(Row.class))\n    .via(r -> Row.withSchema(out).addValue(String.valueOf(r.getInt64(\"id\"))).build()))","handlingStrategy":"validation","validationCode":"Schema.Field in = inSchema.getField(\"id\");\nSchema.Field out = outSchema.getField(\"id\");\nif (in.getType().getTypeName().isNumericType() && !out.getType().getTypeName().isNumericType()) {\n  throw new IllegalStateException(\"id requires number->non-numeric conversion, not Cast\");\n}","typeGuard":null,"tryCatchPattern":"try { rows.apply(Cast.to(outSchema)); } catch (RuntimeException e) { if (e.getMessage().startsWith(\"Can't cast numbers to non-numeric\")) { /* add explicit conversion step */ } else throw e; }","preventionTips":["Diff input and output schemas for numeric->non-numeric field changes","Use explicit MapElements conversions for number-to-string needs","Centralize schema definitions so numeric fields stay numeric across stages"],"tags":["java","apache-beam","cast","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}