{"record":{"id":"2861a3ba797ae282","repo":"apache/beam","slug":"ordering-of-elements-in-a-set-may-be-non-deterministic","errorCode":null,"errorMessage":"Ordering of elements in a set may be non-deterministic.","messagePattern":"Ordering of elements in a set may be non-deterministic\\.","errorType":"exception","errorClass":"NonDeterministicException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/coders/SetCoder.java","lineNumber":47,"sourceCode":" *\n * @param <T> the type of the elements of the set\n */\npublic class SetCoder<T> extends IterableLikeCoder<T, Set<T>> {\n\n  /** Produces a {@link SetCoder} with the given {@code elementCoder}. */\n  public static <T> SetCoder<T> of(Coder<T> elementCoder) {\n    return new SetCoder<>(elementCoder);\n  }\n\n  /**\n   * {@inheritDoc}\n   *\n   * @throws NonDeterministicException always. Sets are not ordered, but they are encoded in the\n   *     order of an arbitrary iteration.\n   */\n  @Override\n  public void verifyDeterministic() throws NonDeterministicException {\n    throw new NonDeterministicException(\n        this, \"Ordering of elements in a set may be non-deterministic.\");\n  }\n\n  @Override\n  public TypeDescriptor<Set<T>> getEncodedTypeDescriptor() {\n    return new TypeDescriptor<Set<T>>() {}.where(\n        new TypeParameter<T>() {}, getElemCoder().getEncodedTypeDescriptor());\n  }\n\n  /////////////////////////////////////////////////////////////////////////////\n  // Internal operations below here.\n\n  /**\n   * {@inheritDoc}\n   *\n   * @return A new {@link Set} built from the elements in the {@link List} decoded by {@link\n   *     IterableLikeCoder}.\n   */","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/SetCoder.java#L29-L65","documentation":"SetCoder.verifyDeterministic() always throws NonDeterministicException because Sets have no defined iteration order, yet SetCoder encodes elements in arbitrary iteration order. Equal sets can therefore encode to different byte sequences, which breaks determinism requirements in Beam grouping operations.","triggerScenarios":"Using a Set (via SetCoder.of(elementCoder)) as a PCollection element/key in GroupByKey, Combine, or any operation that calls verifyDeterministic().","commonSituations":"Grouping records keyed by a HashSet; validation at pipeline submit time; tests exercising Coder determinism checks.","solutions":["Use SortedSet/TreeSet with ListCoder or an ordered collection coder so iteration order is fixed","Encode as a deterministic structure: convert the set to a sorted List and use ListCoder","Use a primitive/array key representation instead of a Set for keyed operations","Call SetCoder.verifyDeterministic() only if you switch to an ordered Set implementation with deterministic element coder"],"exampleFix":"// before\nPCollection<Set<String>> keys; // SetCoder -> NonDeterministicException on GroupByKey\n// after\nPCollection<List<String>> keys = sets.apply(MapElements.into(new TypeDescriptor<List<String>>() {}).via(s -> new ArrayList<>(new TreeSet<>(s))));","handlingStrategy":"try-catch","validationCode":"try { setCoder.verifyDeterministic(); } catch (NonDeterministicException e) { /* use sorted/list representation */ }","typeGuard":"null","tryCatchPattern":"try { coder.verifyDeterministic(); } catch (NonDeterministicException e) { convert to sorted List / TreeSet before encoding }","preventionTips":["Use TreeSet + deterministic element coder if sets must be keys","Unit-test key coders for determinism before submit","Prefer ListCoder over SetCoder when order matters"],"tags":["java","beam","coders","determinism"],"backgroundTag":"unsupported-operation","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"}