{"record":{"id":"883f3f5b2f1e78ff","repo":"apache/beam","slug":"attempting-to-call-and-on-a-cogbkresult-apparently-not","errorCode":null,"errorMessage":"Attempting to call and() on a CoGbkResult apparently not created by of().","messagePattern":"Attempting to call and\\(\\) on a CoGbkResult apparently not created by of\\(\\)\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/join/CoGbkResult.java","lineNumber":362,"sourceCode":"  }\n\n  //////////////////////////////////////////////////////////////////////////////\n  // Methods for directly constructing a CoGbkResult\n  //\n  // (for example, creating test data for a transform that consumes a\n  // CoGbkResult)\n\n  /** Returns a new CoGbkResult that contains just the given tag and given data. */\n  public static <V> CoGbkResult of(TupleTag<V> tag, List<V> data) {\n    return CoGbkResult.empty().and(tag, data);\n  }\n\n  /**\n   * Returns a new {@link CoGbkResult} based on this, with the given tag and given data added to it.\n   */\n  public <V> CoGbkResult and(TupleTag<V> tag, List<V> data) {\n    if (nextTestUnionId != schema.size()) {\n      throw new IllegalArgumentException(\n          \"Attempting to call and() on a CoGbkResult apparently not created by\" + \" of().\");\n    }\n    List<Iterable<?>> valueMap = new ArrayList<>(this.valueMap);\n    valueMap.add(data);\n    return new CoGbkResult(\n        new CoGbkResultSchema(schema.getTupleTagList().and(tag)), valueMap, nextTestUnionId + 1);\n  }\n\n  /** Returns an empty {@link CoGbkResult}. */\n  public static CoGbkResult empty() {\n    return new CoGbkResult(\n        new CoGbkResultSchema(TupleTagList.empty()), new ArrayList<Iterable<?>>());\n  }\n\n  //////////////////////////////////////////////////////////////////////////////\n\n  private int nextTestUnionId = 0;\n","sourceCodeStart":344,"sourceCodeEnd":380,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/join/CoGbkResult.java#L344-L380","documentation":"CoGbkResult.and() is only valid on results created via CoGbkResult.of(), which track nextTestUnionId as they grow. Calling and() on a result constructed directly with the constructor (or after repeated and() calls beyond the schema) breaks that invariant and is rejected.","triggerScenarios":"Calling and() on a CoGbkResult created with new CoGbkResult(schema, valueMap, nextUnionId) where nextTestUnionId != schema.size(), or chaining and() more times than the base schema allows.","commonSituations":"Custom pipeline code constructing CoGbkResult instances manually for testing or deserialization and then trying to append tags; framework-internal results (e.g. from raw union decoding) being mutated.","solutions":["Build results with CoGbkResult.of(tupleTagList, rawResults) and use and() from there","If you must append, construct the CoGbkResult yourself with the extended TupleTagList and full valueMap instead of chaining and()","Verify nextTestUnionId equals schema.size() before calling and()"],"exampleFix":"// before\nCoGbkResult r = new CoGbkResult(schema, valueMap, 0);\nr = r.and(tag, data); // throws\n// after\nCoGbkResult r = CoGbkResult.of(tagList.and(tag), rawValueMap);","handlingStrategy":"type-guard","validationCode":"if (nextTestUnionId != schema.size()) { throw new IllegalStateException(\"Use CoGbkResult.of() before and()\"); }","typeGuard":null,"tryCatchPattern":"try { return result.and(tag, data); } catch (IllegalArgumentException e) { throw new IllegalStateException(\"CoGbkResult not created via of()\", e); }","preventionTips":["Always use CoGbkResult.of() factory methods","Avoid direct constructor use in user code","Track and() call counts against schema size in tests"],"tags":["beam","cogbkresult","illegal-argument","misuse"],"backgroundTag":"invalid-state-transition","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"}