apache/beam · error · IllegalArgumentException

Tried to merge a non-existent window:

Error message

Tried to merge a non-existent window:

What it means

WindowFnTestUtils.runWindowFunction/merge helper throws IllegalArgumentException when merging windows references a window that was never assigned any elements in the test's window-to-elements map. The utility requires every merged window to have a prior entry.

Solutions

  1. Ensure every window passed to the merge call appears in the assignWindows output (add elements for it)
  2. Fix the expected window set in the test to match what the WindowFn produces
  3. Verify windows are constructed identically (same IntervalWindow bounds) as those returned by assignWindows

Example fix

// before
runWindowFunction(new IntervalWindow(0, 10), Arrays.asList(new IntervalWindow(20, 30))) // (20,30) has no elements
// after
assign elements to (20,30) via assignWindows first, or pass only windows present in the map
Defensive patterns

Strategy: validation

Validate before calling

List<W> assigned = windowFn.assignWindows(...);
if (!assigned.containsAll(expectedMergeWindows)) {
  throw new AssertionError("expected windows not assigned: " + expectedMergeWindows);
}

Try / catch

try { WindowFnTestUtils.runWindowFunction(...); }
catch (IllegalArgumentException e) {
  if (e.getMessage().startsWith("Tried to merge a non-existent window")) { /* fix expected window set */ }
  throw e;
}

Prevention

When it happens

Trigger: Calling WindowFnTestUtils validate/merge helpers with a set of otherWindows where at least one window W is not a key in the elements map built from assignWindows results.

Common situations: Writing unit tests for a custom WindowFn where the expected merge set includes a window the WindowFn never actually assigned, or elements were not registered for all windows.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at sdks/java/core/src/main/java/org/apache/beam/sdk/testing/WindowFnTestUtils.java:182

    private Map<W, Set<V>> elements = new HashMap<>();

    public void put(W window, V value) {
      Set<V> all = elements.computeIfAbsent(window, k -> new HashSet<>());
      all.add(value);
    }

    public void merge(Collection<W> otherWindows, W window) {
      if (otherWindows.isEmpty()) {
        return;
      }
      Set<V> merged = new HashSet<>();
      if (elements.containsKey(window) && !otherWindows.contains(window)) {
        merged.addAll(elements.get(window));
      }
      for (W w : otherWindows) {
        if (!elements.containsKey(w)) {
          throw new IllegalArgumentException("Tried to merge a non-existent window:" + w);
        }
        merged.addAll(elements.get(w));
        elements.remove(w);
      }
      elements.put(window, merged);
    }

    public Collection<W> windows() {
      return elements.keySet();
    }

    // For testing.

    public Set<V> get(W window) {
      return elements.get(window);
    }
  }

View on GitHub (pinned to 12126d8942)