{"record":{"id":"0c9531bf1f1c09f0","repo":"apache/beam","slug":"override-this-function-to-provide-the-default-value","errorCode":null,"errorMessage":"Override this function to provide the default value.","messagePattern":"Override this function to provide the default value\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/CombineWithContext.java","lineNumber":127,"sourceCode":"    public AccumT compact(AccumT accumulator, Context c) {\n      return accumulator;\n    }\n\n    /**\n     * Applies this {@code CombineFnWithContext} to a collection of input values to produce a\n     * combined output value.\n     */\n    public OutputT apply(Iterable<? extends InputT> inputs, Context c) {\n      AccumT accum = createAccumulator(c);\n      for (InputT input : inputs) {\n        accum = addInput(accum, input, c);\n      }\n      return extractOutput(accum, c);\n    }\n\n    @Override\n    public OutputT defaultValue() {\n      throw new UnsupportedOperationException(\n          \"Override this function to provide the default value.\");\n    }\n  }\n}\n","sourceCodeStart":109,"sourceCodeEnd":132,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/CombineWithContext.java#L109-L132","documentation":"CombineWithContext.Contextful CombineFn's defaultValue() is intentionally unimplemented in the base class; subclasses that can produce a value without context must override it. Calling defaultValue() on a context-aware combine function that does not override it throws UnsupportedOperationException. Beam throws this because the default value cannot be computed without a context implementation supplied by the subclass.","triggerScenarios":"Calling defaultValue() on a CombineFnWithContext (or Contextful-wrapped combine fn) that did not override defaultValue — e.g. reading PCollection.getValue() on an empty (globally windowed) PCollection produced by a context-aware Combine.","commonSituations":"Calling pc.getValue() on an empty global PCollection produced by Combine.withContext-style transforms; using a custom CombineFnWithContext subclass that skipped the defaultValue override; tests inspecting empty-PCollection values.","solutions":["Override defaultValue() in your CombineWithContext.CombineFn subclass to return a valid empty-accumulation output.","Avoid calling defaultValue()/pc.getValue() on results of context-aware combines; guard with isEmpty()/size checks first.","If the combine has no meaningful default, restructure the pipeline so the default is not required (e.g. use a fallback DoFn).","Ensure the correct CombineFn base class is used — if no context is needed, extend plain CombineFn which may implement defaultValue."],"exampleFix":"// before\nnew CombineWithContext.CombineFn<Integer, Integer, Integer>() {\n  // no defaultValue override\n}\n// after\nnew CombineWithContext.CombineFn<Integer, Integer, Integer>() {\n  @Override public Integer defaultValue() { return 0; }\n}","handlingStrategy":"validation","validationCode":"if (combineFn instanceof CombineWithContext.CombineFn && !overridesDefaultValue(combineFn)) { /* supply default or avoid defaultValue() */ }","typeGuard":null,"tryCatchPattern":"try { V v = combineFn.defaultValue(); } catch (UnsupportedOperationException e) { V v = fallbackDefault; }","preventionTips":["Always override defaultValue() in CombineFnWithContext subclasses","Guard pc.getValue() on empty PCollections with an emptiness check","Prefer plain CombineFn when no context is needed"],"tags":["java","apache-beam","combine","unsupported-operation"],"backgroundTag":"method-not-implemented","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"}