{"record":{"id":"de2b5eba13390f0a","repo":"apache/beam","slug":"can-t-cast-non-numeric-types-input","errorCode":null,"errorMessage":"Can't cast non-numeric types: +input","messagePattern":"Can't cast non-numeric types: \\+input","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/transforms/Cast.java","lineNumber":336,"sourceCode":"    Row.Builder output = Row.withSchema(outputSchema);\n    for (int i = 0; i < outputSchema.getFieldCount(); i++) {\n      Schema.Field outputField = outputSchema.getField(i);\n\n      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","sourceCodeStart":318,"sourceCodeEnd":354,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/transforms/Cast.java#L318-L354","documentation":"Cast.castNumber converts numeric values between numeric TypeNames. It first asserts the INPUT type name is a numeric type; if a non-numeric input type reaches castNumber, it throws a RuntimeException, indicating the dispatch in castValue mis-routed or the type metadata is wrong.","triggerScenarios":"castValue dispatches a java Number value with an input TypeName whose isNumericType() is false — e.g. a Boolean/String-backed field value boxed as a comparable Number path, or schema metadata mismatch after custom type registration.","commonSituations":"Casting rows where a field's declared type (e.g. STRING) does not match the runtime value class; custom Schema coders or logical types that produce Number instances for non-numeric TypeNames.","solutions":["Fix the schema so the field's declared type matches its runtime value (numeric TypeTypes for Number values).","Pre-convert non-numeric values (e.g. parse strings) with a Map/MapElements before Cast.","Use a custom cast function via castRow with explicit per-field conversion logic instead of the default number path.","If caused by a Beam version bug in type dispatch, upgrade Beam."],"exampleFix":"// before (string field pushed through numeric cast)\nrows.apply(Cast.to(numericSchema));\n// after\nrows.apply(MapElements.into(TypeDescriptor.of(Row.class))\n    .via(r -> /* parse numericString to long */ rebuildRow(r))\n    ).apply(Cast.to(numericSchema));","handlingStrategy":"type-guard","validationCode":"if (row.getBaseValue(\"id\") instanceof Number\n    && schema.getField(\"id\").getType().getTypeName().isNumericType()) { /* safe */ }","typeGuard":"static boolean isDeclaredNumeric(Row row, String field) {\n  Object v = row.getValue(field);\n  return v instanceof Number && row.getSchema().getField(field).getType().getTypeName().isNumericType();\n}","tryCatchPattern":"try { rows.apply(Cast.to(outSchema)); } catch (RuntimeException e) { if (e.getMessage().startsWith(\"Can't cast non-numeric\")) { /* fix schema/value mismatch */ } else throw e; }","preventionTips":["Ensure declared field types match runtime value classes","Avoid custom coders that box non-numeric values into Number paths","Test Cast on representative Row samples before production"],"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"}