Skip to content

[Java] Infer JdbcIO schemas with drivers without statement metadata - #41997

Open
raman118 wants to merge 1 commit into
apache:masterfrom
raman118:fix-jdbc-metadata-unsupported
Open

raman118 wants to merge 1 commit into
apache:masterfrom
raman118:fix-jdbc-metadata-unsupported

Conversation

@raman118

@raman118 raman118 commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Fixes #26536

Some JdbcIO transforms infer a schema through PreparedStatement.getMetaData():

  • readRows(), and readWithPartitions() with row output, infer the schema of the query.
  • write() with withTable(...) and no statement infers the schema of the destination table.

Some drivers don't implement getMetaData(). The Hive JDBC driver throws SQLFeatureNotSupportedException, so these transforms failed with:

java.lang.RuntimeException: Error while determining columns from table: ...
Caused by: java.sql.SQLFeatureNotSupportedException: Method not supported
    at org.apache.hive.jdbc.HivePreparedStatement.getMetaData(...)

The JDBC spec also allows getMetaData() to return null when the driver can't describe the statement without running it.

Change

A new SchemaUtil.toBeamSchema(PreparedStatement) is used by ReadRows.inferBeamSchema and by the table schema lookup in write():

  • It still uses getMetaData() when the driver supports it, so behaviour is unchanged for those drivers.
  • Otherwise, it executes the statement with setMaxRows(1) and reads the metadata of the result set.

One limitation: the fallback can't execute a query that still has unset ? parameters, so for such queries on these drivers schema inference still fails.

Testing

  • New JdbcIOTest cases run readRows() and a schema-inferred write() against a Derby DataSource. The wrapper makes PreparedStatement.getMetaData() throw SQLFeatureNotSupportedException, like the Hive driver does. Both fail without this change and pass with it.
  • ./gradlew :sdks:java:io:jdbc:test --tests '*JdbcIOTest' --tests '*SchemaUtilTest': 60 passed.
  • spotless is clean.

Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Mention the appropriate issue in your description (for example: addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment fixes #<ISSUE NUMBER> instead.
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

🤖 Generated with Claude Code

JdbcIO infers the schema of a query (readRows, readWithPartitions with
row output) or of the destination table (write with a table and no
statement) through PreparedStatement.getMetaData(). Some drivers, such
as the Hive JDBC driver, don't implement it and throw
SQLFeatureNotSupportedException, and the JDBC spec allows it to return
null, so these transforms could not be used with them.

When the metadata isn't available from the prepared statement, execute
it with a maximum of one row and read the metadata of its result set.

Fixes apache#26536

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @Abacn added as fallback since no labels match configuration
Backup reviewer: chamikaramj (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Accessing Hive using JdbcIO throws HivePreparedStatement.getMetaData, method not supported

1 participant