{"record":{"id":"6349853da6b39ed6","repo":"apache/beam","slug":"failed-to-infer-beam-schema","errorCode":null,"errorMessage":"Failed to infer Beam schema","messagePattern":"Failed to infer Beam schema","errorType":"exception","errorClass":"BeamSchemaInferenceException","httpStatus":null,"severity":"error","filePath":"sdks/java/io/jdbc/src/main/java/org/apache/beam/sdk/io/jdbc/JdbcIO.java","lineNumber":981,"sourceCode":"                  .withDisableAutoCommit(getDisableAutoCommit())\n                  .withOutputParallelization(getOutputParallelization())\n                  .withStatementPreparator(checkStateNotNull(getStatementPreparator())));\n      rows.setRowSchema(schema);\n      return rows;\n    }\n\n    // Spotbugs seems to not understand the multi-statement try-with-resources\n    @SuppressFBWarnings(\"OBL_UNSATISFIED_OBLIGATION\")\n    public static Schema inferBeamSchema(DataSource ds, String query) {\n      try (Connection conn = ds.getConnection();\n          PreparedStatement statement =\n              conn.prepareStatement(\n                  query, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) {\n        ResultSetMetaData metadata =\n            checkStateNotNull(statement.getMetaData(), \"could not get statement metadata\");\n        return SchemaUtil.toBeamSchema(metadata);\n      } catch (SQLException e) {\n        throw new BeamSchemaInferenceException(\"Failed to infer Beam schema\", e);\n      }\n    }\n\n    @Override\n    public void populateDisplayData(DisplayData.Builder builder) {\n      super.populateDisplayData(builder);\n      builder.add(DisplayData.item(\"query\", getQuery()));\n      if (getDataSourceProviderFn() instanceof HasDisplayData) {\n        ((HasDisplayData) getDataSourceProviderFn()).populateDisplayData(builder);\n      }\n    }\n  }\n\n  /** Implementation of {@link #read}. */\n  @AutoValue\n  public abstract static class Read<T> extends PTransform<PBegin, PCollection<T>> {\n\n    @Pure","sourceCodeStart":963,"sourceCodeEnd":999,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/io/jdbc/src/main/java/org/apache/beam/sdk/io/jdbc/JdbcIO.java#L963-L999","documentation":"JdbcIO.ReadRows infers a Beam Schema by executing the query and converting ResultSetMetaData via SchemaUtil.toBeamSchema. A SQLException during statement preparation, execution, or metadata retrieval is surfaced as BeamSchemaInferenceException with this message.","triggerScenarios":"Running JdbcIO.readRows() where the prepared statement's metadata cannot be fetched — invalid SQL, nonexistent table/columns, insufficient privileges, unreachable database, or a driver that does not return metadata for the prepared statement.","commonSituations":"Typo in query or table name; missing driver dependency on the classpath; database credentials/network problems; DB user lacking metadata read privileges; statement.getMetaData() returning null for exotic drivers.","solutions":["Run the query directly against the database (psql/sqlplus/jdbc client) to confirm it is valid and authorized","Verify connection configuration (URL, user, password, driver jar in the classpath / --jars)","Catch BeamSchemaInferenceException and fall back to an explicitly provided schema via withRowMapper/withSchema if inference is unsupported for your driver"],"exampleFix":"// before\nJdbcIO.<Void>readRows().withQuery(\"SELCT * FROM users\").withDataSourceConfiguration(config);\n// after\nJdbcIO.<Void>readRows().withQuery(\"SELECT * FROM users\").withDataSourceConfiguration(config);","handlingStrategy":"try-catch","validationCode":"// preflight: run the query on a plain connection before building the pipeline\ntry (Connection c = config.buildOrGetConnectionFactory().getConnection();\n     PreparedStatement ps = c.prepareStatement(query, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) {\n  checkState(ps.getMetaData() != null, \"driver returns no metadata for: \" + query);\n}","typeGuard":null,"tryCatchPattern":"try {\n  pipeline.apply(JdbcIO.<Void>readRows().withQuery(query).withDataSourceConfiguration(cfg));\n} catch (BeamSchemaInferenceException e) {\n  // fall back to explicit schema or fix query/credentials\n}","preventionTips":["Test the SQL directly against the target database before wiring the pipeline","Ship the correct JDBC driver on the classpath","Verify DB credentials and metadata privileges at deploy time"],"tags":["jdbc","schema-inference","sql"],"backgroundTag":"sql-query-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"}