{"record":{"id":"ab9bfcb36e0f8f1e","repo":"apache/beam","slug":"encodingexception","errorCode":null,"errorMessage":"EncodingException","messagePattern":"EncodingException","errorType":"exception","errorClass":"EncodingException","httpStatus":null,"severity":"error","filePath":"sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/changestreams/estimator/SizeEstimator.java","lineNumber":65,"sourceCode":"  public SizeEstimator(Coder<T> coder) {\n    this.coder = coder;\n    this.observer = new SizeEstimatorObserver();\n  }\n\n  /**\n   * Estimates the size in bytes of the given element with the configured {@link Coder} .\n   *\n   * @param element the element instance to be estimated\n   * @return the estimated size in bytes of the given element\n   */\n  public long sizeOf(T element) {\n    try {\n      coder.registerByteSizeObserver(element, observer);\n      observer.advance();\n\n      return observer.observedBytes;\n    } catch (Exception e) {\n      throw new EncodingException(e);\n    }\n  }\n}\n","sourceCodeStart":47,"sourceCodeEnd":69,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/changestreams/estimator/SizeEstimator.java#L47-L69","documentation":"SizeEstimator.sizeOf measures an element's encoded size via CodedSizeObserver; any exception raised during coder.registerByteSizeObserver or observer.advance() is rethrown as an org.apache.beam.sdk.coders.CoderException (EncodingException in this context). It means the element cannot be sized/encoded by its declared Coder — usually the element does not match the coder, or encoding fails mid-way.","triggerScenarios":"Estimating the size of a change stream record/partition whose runtime class does not match the registered Coder; a custom coder throwing while encoding (non-serializable field, null nested value); observer.advance() failing on malformed data.","commonSituations":"Pipelines where the element type changed but the coder wasn't updated; custom elements containing non-encodable fields used with Spanner change stream size estimation; serialization bugs in user-defined types flowing through the estimator.","solutions":["Check the wrapped cause 'e' — it names the coder and element that failed; align the element type with the coder.","Ensure the registered Coder (e.g. for ChangeStreamRecord/PartitionMetadata) matches the actual runtime element, including generics.","Fix the custom coder's encode/registerByteSizeObserver implementation if it throws on valid input (e.g. null handling).","Verify all fields of the element are encodable (transient/non-serializable fields removed or made encodable)."],"exampleFix":"// before\nCoder<ChangeStreamRecord> coder = new MyCoder(); // handles only DataChangeRecord\n// after\nCoder<ChangeStreamRecord> coder = ChangeStreamRecordsCoder.of(); // covers all record subtypes","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { long size = estimator.sizeOf(element); } catch (CoderException e) {\n  log.error(\"Cannot encode element of type {}\", element.getClass(), e);\n}","preventionTips":["Ensure the Coder registered for change stream elements matches every runtime subtype.","Update coders whenever element classes change.","Test custom coders with all record variants in unit tests."],"tags":["beam","coder","serialization","java"],"backgroundTag":"type-mismatch","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}