{"record":{"id":"ec97806706dffff9","repo":"apache/beam","slug":"cannot-encode-a-null-hyperloglogplus-sketch","errorCode":null,"errorMessage":"cannot encode a null HyperLogLogPlus sketch","messagePattern":"cannot encode a null HyperLogLogPlus sketch","errorType":"exception","errorClass":"CoderException","httpStatus":null,"severity":"error","filePath":"sdks/java/extensions/sketching/src/main/java/org/apache/beam/sdk/extensions/sketching/ApproximateDistinct.java","lineNumber":508,"sourceCode":"          .add(DisplayData.item(\"sp\", sp).withLabel(\"sparse representation precision\"));\n    }\n  }\n\n  /** Coder for {@link HyperLogLogPlus} class. */\n  public static class HyperLogLogPlusCoder extends CustomCoder<HyperLogLogPlus> {\n\n    private static final HyperLogLogPlusCoder INSTANCE = new HyperLogLogPlusCoder();\n\n    private static final ByteArrayCoder BYTE_ARRAY_CODER = ByteArrayCoder.of();\n\n    public static HyperLogLogPlusCoder of() {\n      return INSTANCE;\n    }\n\n    @Override\n    public void encode(HyperLogLogPlus value, OutputStream outStream) throws IOException {\n      if (value == null) {\n        throw new CoderException(\"cannot encode a null HyperLogLogPlus sketch\");\n      }\n      BYTE_ARRAY_CODER.encode(value.getBytes(), outStream);\n    }\n\n    @Override\n    public HyperLogLogPlus decode(InputStream inStream) throws IOException {\n      return HyperLogLogPlus.Builder.build(BYTE_ARRAY_CODER.decode(inStream));\n    }\n\n    @Override\n    public boolean isRegisterByteSizeObserverCheap(HyperLogLogPlus value) {\n      return true;\n    }\n\n    @Override\n    protected long getEncodedElementByteSize(HyperLogLogPlus value) throws IOException {\n      if (value == null) {\n        throw new CoderException(\"cannot encode a null HyperLogLogPlus sketch\");","sourceCodeStart":490,"sourceCodeEnd":526,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/extensions/sketching/src/main/java/org/apache/beam/sdk/extensions/sketching/ApproximateDistinct.java#L490-L526","documentation":"HyperLogLogPlusCoder.encode refuses to encode a null HyperLogLogPlus value, throwing CoderException. Coders in Beam may legally receive null in some contexts, but this coder chose to treat null as an encoding error rather than writing a null marker.","triggerScenarios":"Encoding a PCollection<HyperLogLogPlus> (or accumulator output of ApproximateDistinct) that contains a null sketch — e.g. a null accumulator produced upstream and then serialized.","commonSituations":"User code returning null from a DoFn/CombineFn that outputs sketches directly to a PCollection, then materializing/writing that PCollection.","solutions":["Ensure no null sketches are emitted: return an empty/new HyperLogLogPlus instead of null from combining or mapping code.","Filter nulls before the coded PCollection is materialized (Filter.by(Objects::nonNull)).","Wrap with a nullable-tolerant coder if nulls are semantically valid in your pipeline."],"exampleFix":"// before\nreturn acc == null ? null : acc; // null reaches coder\n// after\nreturn acc == null ? new HyperLogLogPlus(12, 0) : acc;","handlingStrategy":"validation","validationCode":"PCollection<HyperLogLogPlus> safe = sketches.apply(Filter.by(java.util.Objects::nonNull));","typeGuard":"static boolean isNonNullSketch(HyperLogLogPlus s) { return s != null; }","tryCatchPattern":"try { BYTE_ARRAY_CODER.encode(value.getBytes(), out); } catch (CoderException e) { /* null sketch leaked from upstream */ throw e; }","preventionTips":["Never return null sketches from DoFns or CombineFns","Default to an empty sketch instead of null","Filter nulls before materializing coded PCollections"],"tags":["java","beam","sketching","coder","null"],"backgroundTag":"null-argument","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"}