{"record":{"id":"6509014719b0bcfa","repo":"apache/beam","slug":"cast-isn-t-compatible-using-validator-reason","errorCode":null,"errorMessage":"Cast isn't compatible using +validator()+:\n\t+reason","messagePattern":"Cast isn't compatible using \\+validator\\(\\)\\+:\n\t\\+reason","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/transforms/Cast.java","lineNumber":281,"sourceCode":"        || type == TypeName.INT32\n        || type == TypeName.INT64;\n  }\n\n  /** Checks if type is decimal. */\n  public static boolean isDecimal(TypeName type) {\n    return type == TypeName.FLOAT || type == TypeName.DOUBLE || type == TypeName.DECIMAL;\n  }\n\n  public void verifyCompatibility(Schema inputSchema) {\n    List<CompatibilityError> errors = validator().apply(inputSchema, outputSchema());\n\n    if (!errors.isEmpty()) {\n      String reason =\n          errors.stream()\n              .map(x -> Joiner.on('.').join(x.path()) + \": \" + x.message())\n              .collect(Collectors.joining(\"\\n\\t\"));\n\n      throw new IllegalArgumentException(\n          \"Cast isn't compatible using \" + validator() + \":\\n\\t\" + reason);\n    }\n  }\n\n  @Override\n  public PCollection<Row> expand(PCollection<T> input) {\n    Schema inputSchema = input.getSchema();\n\n    verifyCompatibility(inputSchema);\n\n    return input\n        .apply(\n            ParDo.of(\n                new DoFn<T, Row>() {\n                  // TODO: This should be the same as resolved so that Beam knows which fields\n                  // are being accessed. Currently Beam only supports wildcard descriptors.\n                  // Once https://github.com/apache/beam/issues/18903 is fixed, fix this.\n                  @FieldAccess(\"filterFields\")","sourceCodeStart":263,"sourceCodeEnd":299,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/transforms/Cast.java#L263-L299","documentation":"Cast.verifyCompatibility validates, before running, that every field of the input schema can be cast to the output schema under the configured validator (upcast/downcast/lossless-unsafe). It accumulates errors as path+message pairs and throws one IllegalArgumentException listing all incompatibilities.","triggerScenarios":"Applying Cast.to(outputSchema) (or castRow with a validator) via PCollection.apply where field types are incompatible, e.g. casting an ARRAY to an INT, or a downcast like INT64->INT32 with Cast.validator(LosslessValidator).","commonSituations":"Aligning two pipeline stages' schemas after one side changed types; forcing a wide-to-narrow numeric cast that the chosen validator rejects; renaming/reordering fields so required outputs are missing in the input.","solutions":["Read the reason list in the exception: each line is 'field.path: message' identifying the exact incompatibility.","Align the output schema with the input types, or widen input types before the cast.","Choose a permissive validator, e.g. Cast.validator(Cast.DefaultValidator.UNSAFE) for lossy casts, or write a custom CastValidator.","Use withUnsafe or drop/rename the offending fields before casting."],"exampleFix":"// before (INT64 -> INT32 rejected by lossless validator)\nrows.apply(Cast.to(outSchema).validator(Cast.DefaultValidator.LOSSLESS));\n// after: allow unsafe/lossy numeric casts\nrows.apply(Cast.to(outSchema).validator(Cast.DefaultValidator.UNSAFE));","handlingStrategy":"try-catch","validationCode":"for (Schema.Field in : inSchema.getFields()) {\n  Schema.Field out = outSchema.getField(in.getName());\n  if (out != null && !Cast.canCast(in.getType(), out.getType(), validator)) {\n    throw new IllegalStateException(\"incompatible field: \" + in.getName());\n  }\n}","typeGuard":null,"tryCatchPattern":"try { rows.apply(Cast.to(outSchema).validator(v)); }\ncatch (IllegalArgumentException e) {\n  if (e.getMessage().startsWith(\"Cast isn't compatible\")) { logInvalidPaths(e.getMessage()); }\n  else throw e;\n}","preventionTips":["Call verify-compatible logic early in pipeline construction","Match validator strictness (UNSAFE/LOSSLESS) to actual type deltas","Keep input/output schemas in sync via a shared schema definition module"],"tags":["java","apache-beam","cast","schema-validation"],"backgroundTag":"schema-validation-failed","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"}