{"record":{"id":"3ab2863e526394b9","repo":"apache/beam","slug":"the-input-value-cannot-be-encoded-e-getmessage","errorCode":null,"errorMessage":"The input value cannot be encoded: ${e.getMessage()}","messagePattern":"The input value cannot be encoded: (.+?)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"sdks/java/extensions/sketching/src/main/java/org/apache/beam/sdk/extensions/sketching/ApproximateDistinct.java","lineNumber":459,"sourceCode":"      checkArgument(\n          (sp > this.p && sp < 32) || (sp == 0),\n          \"Expected: p <= sp <= 32.\" + \"Actual: p = %s, sp = %s\",\n          this.p,\n          sp);\n      return new ApproximateDistinctFn<>(this.p, sp, this.inputCoder);\n    }\n\n    @Override\n    public HyperLogLogPlus createAccumulator() {\n      return new HyperLogLogPlus(p, sp);\n    }\n\n    @Override\n    public HyperLogLogPlus addInput(HyperLogLogPlus acc, InputT record) {\n      try {\n        acc.offer(CoderUtils.encodeToByteArray(inputCoder, record));\n      } catch (CoderException e) {\n        throw new IllegalStateException(\"The input value cannot be encoded: \" + e.getMessage(), e);\n      }\n      return acc;\n    }\n\n    /** Output the whole structure so it can be queried, reused or stored easily. */\n    @Override\n    public HyperLogLogPlus extractOutput(HyperLogLogPlus accumulator) {\n      return accumulator;\n    }\n\n    @Override\n    public HyperLogLogPlus mergeAccumulators(Iterable<HyperLogLogPlus> accumulators) {\n      HyperLogLogPlus mergedAccum = createAccumulator();\n      for (HyperLogLogPlus accum : accumulators) {\n        try {\n          mergedAccum.addAll(accum);\n        } catch (CardinalityMergeException e) {\n          // Should never happen because only HyperLogLogPlus accumulators are instantiated.","sourceCodeStart":441,"sourceCodeEnd":477,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/extensions/sketching/src/main/java/org/apache/beam/sdk/extensions/sketching/ApproximateDistinct.java#L441-L477","documentation":"ApproximateDistinctFn.addInput encodes each incoming record to bytes with the configured inputCoder before offering them to the HyperLogLogPlus sketch. If the record cannot be encoded (CoderException), this IllegalStateException is thrown, since the combiner cannot proceed without a byte representation.","triggerScenarios":"Streaming an element into the sketch whose runtime type does not match the Coder passed to create (e.g. coder is for String but a null or non-String element arrives); a custom coder failing on this particular value (e.g. unsupported nested type).","commonSituations":"Null elements reaching the combiner with a coder that cannot encode null; type changes upstream after a refactor so elements no longer match the declared coder; a custom coder throwing on edge-case values.","solutions":["Filter out null/incompatible elements with Filter.by(...) before the ApproximateDistinct transform.","Ensure the coder passed to create matches the PCollection's actual element type (Coder of the PCollection).","Fix the custom coder's encode method for the failing value; inspect the CoderException cause for details."],"exampleFix":"// before\nPCollection<String> items = ...; // may contain nulls\nitems.apply(ApproximateDistinct.<String>globally().create(StringUtf8Coder.of()));\n// after\nitems.apply(Filter.by(v -> v != null))\n     .apply(ApproximateDistinct.<String>globally().create(StringUtf8Coder.of()));","handlingStrategy":"validation","validationCode":"// Filter unencodable elements upstream\nPCollection<String> clean = input.apply(Filter.by(v -> v != null));","typeGuard":null,"tryCatchPattern":"try { acc.offer(CoderUtils.encodeToByteArray(inputCoder, record)); } catch (CoderException e) { LOG.error(\"unencodable element: {}\", record, e); throw e; }","preventionTips":["Ensure PCollection coder type matches the coder passed to create()","Filter nulls before combiner stages","Unit-test custom coders on edge-case values"],"tags":["java","beam","sketching","encoding","coder"],"backgroundTag":"json-marshal-failed","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"}