{"record":{"id":"fd290cbc9c60d7a0","repo":"apache/beam","slug":"pcollection-with-more-than-one-element-accessed-as-a","errorCode":null,"errorMessage":"PCollection with more than one element accessed as a singleton view. Consider using Combine.globally().asSingleton() to combine the PCollection into a single value","messagePattern":"PCollection with more than one element accessed as a singleton view\\. Consider using Combine\\.globally\\(\\)\\.asSingleton\\(\\) to combine the PCollection into a single value","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/View.java","lineNumber":544,"sourceCode":"          this.valueCoder = coder;\n          try {\n            this.defaultValue = CoderUtils.encodeToByteArray(coder, defaultValue);\n          } catch (CoderException e) {\n            throw new IllegalArgumentException(\n                String.format(\n                    \"Could not encode the default value %s with the provided coder %s\",\n                    defaultValue, coder));\n          }\n        }\n      } else {\n        this.valueCoder = null;\n        this.defaultValue = null;\n      }\n    }\n\n    @Override\n    public T apply(T left, T right) {\n      throw new IllegalArgumentException(\n          \"PCollection with more than one element \"\n              + \"accessed as a singleton view. Consider using Combine.globally().asSingleton() to \"\n              + \"combine the PCollection into a single value\");\n    }\n\n    @Override\n    public T identity() {\n      if (hasDefault) {\n        if (defaultValue == null) {\n          return null;\n        }\n        try {\n          return CoderUtils.decodeFromByteArray(valueCoder, defaultValue);\n        } catch (CoderException e) {\n          throw new IllegalArgumentException(\n              String.format(\n                  \"Could not decode the default value with the provided coder %s\", valueCoder));\n        }","sourceCodeStart":526,"sourceCodeEnd":562,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/View.java#L526-L562","documentation":"View.asSingleton() (withoutDefaults) uses SingletonCombineFn as a Combine function; its merge/apply method deliberately throws IllegalArgumentException when it is actually invoked with two elements, meaning the underlying PCollection had more than one element at combine time but was accessed as a singleton. It suggests using Combine.globally().asSingleton() to explicitly reduce, or providing a default / using other views.","triggerScenarios":"Accessing a PCollectionView built with View.asSingleton() (no default) while the side-input PCollection contains 2+ elements at evaluation time in the window; the combine's apply() is called with (left, right) and throws.","commonSituations":"Assuming an upstream filter produced exactly one element but it produced many; missing window alignment so multiple elements land in one window; datasets growing over time to include more than the assumed single record (e.g. 'latest config' row duplicated); using asSingleton instead of asList/Combine.globally on multi-element data.","solutions":["Reduce the PCollection to a single element with Combine.globally(...).asSingleton() before viewing","Use View.asList() or View.asIterable() if multiple elements are legitimate","Ensure the upstream logic (filter/top) guarantees exactly one element per window","Provide a default with asSingleton(default) if single-element semantics are not guaranteed"],"exampleFix":"// before\nPCollectionView<Integer> maxView = numbers.apply(View.asSingleton()); // throws if >1 element\n// after\nPCollectionView<Integer> maxView = numbers\n    .apply(Combine.globally(Max.ofIntegers()))\n    .apply(View.asSingleton());","handlingStrategy":"try-catch","validationCode":"// Before viewing, guarantee single element:\nPCollectionView<T> safeView = input.apply(Combine.globally(firstOrNull)).apply(View.asSingleton());","typeGuard":null,"tryCatchPattern":"try { sideValue = c.sideInput(singletonView); } catch (IllegalArgumentException e) { LOG.error(\"Singleton view had multiple elements; use Combine.globally().asSingleton() or View.asList()\", e); throw e; }","preventionTips":["Never assume a filter yields exactly one element; enforce with Combine.globally","Use View.asList()/asIterable() when multiplicity is possible","Ensure windows align so only one element per window reaches the side input","Add a default via asSingleton(default) when emptiness is possible"],"tags":["java","apache-beam","side-input","runtime","singleton-view"],"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-20T03:17:13.778Z"}