{"record":{"id":"544ac4fba0a93462","repo":"apache/beam","slug":"empty-pcollection-accessed-as-a-singleton-view-consider","errorCode":null,"errorMessage":"Empty PCollection accessed as a singleton view. Consider setting withDefault to provide a default value","messagePattern":"Empty PCollection accessed as a singleton view\\. Consider setting withDefault to provide a default value","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/View.java","lineNumber":564,"sourceCode":"              + \"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        }\n      } else {\n        throw new IllegalArgumentException(\n            \"Empty PCollection accessed as a singleton view. \"\n                + \"Consider setting withDefault to provide a default value\");\n      }\n    }\n  }\n\n  /**\n   * <b><i>For internal use only; no backwards-compatibility guarantees.</i></b>\n   *\n   * <p>Public only so a {@link PipelineRunner} may override its behavior.\n   *\n   * <p>See {@link View#asMultimap()}.\n   */\n  @Internal\n  public static class AsMultimap<K, V>\n      extends PTransform<PCollection<KV<K, V>>, PCollectionView<Map<K, Iterable<V>>>> {\n    private final boolean inMemory;\n","sourceCodeStart":546,"sourceCodeEnd":582,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/View.java#L546-L582","documentation":"This error is thrown when an empty PCollection is accessed through a singleton PCollectionView that was not given a default value via withDefault(). Since there are no elements to extract a singleton from and no fallback default, Beam throws an IllegalArgumentException at access time to prevent silently producing a null or undefined value.","triggerScenarios":"Building View.asSingleton() without withDefault(...) on a PCollection that turns out to be empty, then accessing the view via sideInput() inside a DoFn/ParDo. Common with filtered inputs (e.g. filter(predicate)) that can legally produce zero elements.","commonSituations":"A filter or early-exit branch produces an empty PCollection in production but not in testing; using asSingleton() on data that is not guaranteed to have exactly one element (should be asList/asMap instead).","solutions":["Add .withDefault(defaultValue) to the View.asSingleton() builder","Use View.asList() or View.asMap() if the input may legitimately be empty or have multiple elements","Ensure the upstream PCollection is guaranteed non-empty (e.g. Validate upstream logic or add a fallback element)","Catch IllegalArgumentException around sideInput access and handle the empty case explicitly"],"exampleFix":"// before\nPCollectionView<Integer> view = pc.apply(View.asSingleton());\n// after\nPCollectionView<Integer> view = pc.apply(View.asSingleton().withDefault(0));","handlingStrategy":"fallback","validationCode":"// Guard: check emptiness before creating a singleton view, or always supply a default\nif (pc.getTypeDescriptor() != null && mayBeEmpty) {\n  view = pc.apply(View.asSingleton().withDefault(defaultValue));\n}","typeGuard":null,"tryCatchPattern":"try { T v = c.sideInput(view); } catch (IllegalArgumentException e) { T v = fallbackValue; }","preventionTips":["Never call View.asSingleton() without withDefault() on inputs that can be empty","Use View.asList()/asMap() for potentially empty collections","Assert non-emptiness upstream (e.g. via a counting side input) when the invariant matters"],"tags":["beam","side-input","empty-collection","singleton-view"],"backgroundTag":"empty-collection-accessed","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"}