{"record":{"id":"90d13dc6c1ae0f40","repo":"apache/beam","slug":"cannot-call-gettorowfunction-when-there-is-no-schema","errorCode":null,"errorMessage":"Cannot call getToRowFunction when there is no schema","messagePattern":"Cannot call getToRowFunction when there is no schema","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/values/PCollection.java","lineNumber":333,"sourceCode":"  }\n\n  /** Returns whether this {@link PCollection} has an attached schema. */\n  public boolean hasSchema() {\n    return coderOrFailure.coder != null && coderOrFailure.coder instanceof SchemaCoder;\n  }\n\n  /** Returns the attached schema. */\n  public Schema getSchema() {\n    if (!hasSchema()) {\n      throw new IllegalStateException(\"Cannot call getSchema when there is no schema\");\n    }\n    return ((SchemaCoder) getCoder()).getSchema();\n  }\n\n  /** Returns the attached schema's toRowFunction. */\n  public SerializableFunction<T, Row> getToRowFunction() {\n    if (!hasSchema()) {\n      throw new IllegalStateException(\"Cannot call getToRowFunction when there is no schema\");\n    }\n    return ((SchemaCoder<T>) getCoder()).getToRowFunction();\n  }\n\n  /** Returns the attached schema's fromRowFunction. */\n  public SerializableFunction<Row, T> getFromRowFunction() {\n    if (!hasSchema()) {\n      throw new IllegalStateException(\"Cannot call getFromRowFunction when there is no schema\");\n    }\n    return ((SchemaCoder<T>) getCoder()).getFromRowFunction();\n  }\n\n  /**\n   * of the {@link PTransform}.\n   *\n   * @return the output of the applied {@link PTransform}\n   */\n  public <OutputT extends POutput> OutputT apply(PTransform<? super PCollection<T>, OutputT> t) {","sourceCodeStart":315,"sourceCodeEnd":351,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/values/PCollection.java#L315-L351","documentation":"PCollection.getToRowFunction() throws this IllegalStateException when the PCollection has no schema. The toRow function (T -> Row) only exists on a SchemaCoder, so the method guards with hasSchema() and fails fast rather than returning null.","triggerScenarios":"Calling getToRowFunction() directly, or via toRow(), toRowFunction(), rows(), schemaEntryMapper(), or schema-based expansion helpers, on a PCollection whose coder is not a SchemaCoder.","commonSituations":"Converting elements to Row for BigQuery writes without first setting a schema; applying Rows.toRows-style helpers on a PCollection from Create.of or a raw DoFn output.","solutions":["Check pc.hasSchema() before calling","Attach a schema with pc.setSchema(...) or pc.setRowSchema(... so a toRowFunction exists","Use a schema-registered element type so Beam derives the toRow function automatically"],"exampleFix":"// before\nSerializableFunction<T, Row> fn = pc.getToRowFunction(); // throws\n// after\nif (!pc.hasSchema()) {\n  pc = pc.setRowSchema(mySchema);\n}\nSerializableFunction<T, Row> fn = pc.getToRowFunction();","handlingStrategy":"validation","validationCode":"if (!pc.hasSchema()) { pc = pc.setRowSchema(mySchema); }","typeGuard":"boolean canConvertToRow(PCollection<?> pc) { return pc.hasSchema(); }","tryCatchPattern":"try { var fn = pc.getToRowFunction(); } catch (IllegalStateException e) { pc = pc.setRowSchema(mySchema); }","preventionTips":["Attach schema immediately after creating the PCollection","Use setRowSchema before Row-based IO connectors","Avoid raw DoFn outputs when Row conversion is planned"],"tags":["java","apache-beam","schema","illegal-state"],"backgroundTag":"missing-required-config","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"}