apache/beam · error · IllegalArgumentException

PCollection with more than one element accessed as a…

Error message

PCollection with more than one element accessed as a singleton view.

What it means

SingletonViewFn2.apply enforces the singleton contract: it returns the single element, falls back to getDefaultValue() if the PCollection is empty, and throws IllegalArgumentException when the PCollection contains more than one element. Beam's View.asSingleton() semantics only permit zero-or-one elements per window, so multiple elements are a programming error.

Solutions

  1. Apply a global combine before the view: pc.apply(Mean.globally()).apply(View.asSingleton()).
  2. If per-window, make the view window-aware semantics explicit: with the combine fn applied per window, or use View.asMap()/asList() and iterate.
  3. Deduplicate/select one element upstream (First.of, Sample.any(1), or a distinct) so at most one element remains.
  4. If multiple values are valid, switch the API to a mapping view and access via sideInput(view).get(key).

Example fix

// before
PCollectionView<Integer> threshold = pc.apply(View.asSingleton());
// after
PCollectionView<Integer> threshold = pc.apply(Mean.globally()).apply(View.asSingleton());
Defensive patterns

Strategy: validation

Validate before calling

long n = pc.apply(Count.globally()).apply(Maybe.first()); // or reason statically
// Prefer: apply a global combine before asSingleton()
PCollectionView<T> view = pc.apply(Combine.globally(fn)).apply(View.asSingleton());

Try / catch

try {
  T v = c.sideInput(singletonView);
} catch (IllegalArgumentException e) {
  if (e.getMessage().contains("more than one element")) { /* switch to asMap/asList */ }
  throw e;
}

Prevention

When it happens

Trigger: Creating a singleton view with View.asSingleton() over a PCollection that emits two or more elements in a window and then reading it via sideInput() inside a DoFn.

Common situations: Forgetting a Combine/combiner (e.g. Mean.globally(), First.of()) before View.asSingleton(); windowed pipelines where the view is per-window and some windows receive multiple records; deduplicated joins producing multiple matches.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/1d3867a94416c7f4. Report an issue: GitHub.

Appendix: source

Thrown at sdks/java/core/src/main/java/org/apache/beam/sdk/values/PCollectionViews.java:549

        }
        return defaultValue;
      }
    }

    @Override
    public Materialization<IterableView<T>> getMaterialization() {
      return Materializations.iterable();
    }

    @Override
    public T apply(IterableView<T> primitiveViewT) {
      Iterator<T> iterator = primitiveViewT.get().iterator();
      if (!iterator.hasNext()) {
        return getDefaultValue();
      }
      T result = iterator.next();
      if (iterator.hasNext()) {
        throw new IllegalArgumentException(
            "PCollection with more than one element accessed as a singleton view.");
      }
      return result;
    }

    @Override
    public TypeDescriptor<T> getTypeDescriptor() {
      return typeDescriptorSupplier.get();
    }
  }

  @Internal
  public interface HasDefaultValue<T> {
    T getDefaultValue();
  }

  @Internal
  public interface IsSingletonView<T> {}

View on GitHub (pinned to 12126d8942)