Skip to content

GH-1089: Expose batch metadata from Flight SQL JDBC results - #1547

Open
yucheng-du wants to merge 2 commits into
apache:mainfrom
yucheng-du:flight-jdbc-batch-metadata
Open

yucheng-du wants to merge 2 commits into
apache:mainfrom
yucheng-du:flight-jdbc-batch-metadata

Conversation

@yucheng-du

@yucheng-du yucheng-du commented Dec 2, 2026 •

Copy link
Copy Markdown

Closes #1089.

JDBC callers can unwrap the result set as an ArrowFlightJdbcFlightStreamResultSet and read the metadata attached to the current row batch with getCurrentBatchMetadata(). It returns a copy of the bytes, or null when the batch has no metadata.

For query statistics, getLatestMetadataByEndpoint() returns each endpoint's last non-null payload in FlightInfo order. The driver keeps one value per endpoint. New progress messages replace older ones, and batches without metadata leave that value intact. Read the list after consuming the result to include all trailers, even from endpoints with no rows. An endpoint that supplied no metadata has a null entry. The existing getAppMetadata() still returns the metadata from FlightInfo.

Progress messages leave the first-row wait in place. With a server that sent progress and delayed the first row by six seconds, a two-second query timeout expired inside executeQuery after 2.02 s. Adding a second endpoint that sent only a trailer gave 2.01 s. The full Flight SQL JDBC core Maven reactor and Spotless passed on JDK 21. I also checked 1,989 rows over three endpoints with 65,537-byte payloads and a separate trailer from each endpoint.

GPT-6.2 Astra (ultra effort) assisted with the implementation and tests.

@lidavidm lidavidm added the enhancement PRs that add or improve features. label Dec 3, 2026
@apache apache deleted a comment from github-actions Bot Dec 3, 2026
// `FlightStream#next` will block until new data can be read or stream is over.
while (endpointRequest.getStream().next()) {
if (endpointRequest.getStream().getRoot().getRowCount() > 0) {
if (endpointRequest.getStream().getLatestMetadata() != null

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes when executeQuery returns, and as a result Statement#setQueryTimeout no longer covers the wait for the first rows.

The query timeout is only enforced here, through completionService.poll(timeout) in next(long, TimeUnit). Before this change, the background task kept reading until it got a batch with rows or the end of the stream, so a server that was slow to produce data hit the timeout. With getLatestMetadata() != null, the task returns at the first metadata-only message. executeQuery then returns immediately, and the first ResultSet#next() blocks in FlightStream#next() with no timeout.

Here's the reproducer: the server calls start(root), putMetadata(progress), sleeps 6s, then putNext() with one row. The client calls setQueryTimeout(2).

  • main: SQLTimeoutException: Query timed out after 2 SECONDS after ~2s
  • this PR: executeQuery returns after ~13ms, and the first next() blocks ~6s and then returns the row. No timeout.

This affects every client of a server that sends progress metadata before the first batch, including clients that never call getLatestBatchMetadata().

If the goal is to keep the trailer of a query with no rows, could the queue keep its original condition and only hand back such a stream when it ends? Something like this:

while (stream.next()) {
  if (stream.getRoot().getRowCount() > 0) {
    return endpointRequest;
  }
}
// End of stream: keep the endpoint if it ended with metadata so the trailer stays readable.
return stream.getLatestMetadata() != null ? endpointRequest : null;

We could add a regression test that combines setQueryTimeout with a producer that sends metadata first.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried the proposed EOF check with two local Flight endpoints. One sent only a trailer (byte 99), and the other delayed its first row. FlightStream.next() retained the trailer after EOF. With the existing re-enqueue in ResultSet.next(), the queue handed back the same exhausted endpoint three times. Each time next() returned false and the metadata was still 99.

Even a one-time handoff of that empty endpoint can finish the timed wait before the other endpoint supplies a row. Capturing the trailer separately would let the queue keep waiting for the first row or for all endpoints to finish. The timeout can then be tested independently of the public API for intermediate metadata.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 96c0981. The queue now saves the metadata and keeps reading until it has rows or reaches EOF. A trailer-only endpoint finishes without handing a stream back to the result set.

I reran your six-second delay with a two-second query timeout. executeQuery timed out after 2.02 s. Adding a second endpoint that immediately sent a trailer and finished gave 2.01 s. The new regression holds the first row behind a latch after sending progress. It failed on 15adc2e because executeQuery returned early.

final VectorSchemaRoot originalRoot = currentEndpointData.getStream().getRoot();
final FlightStream stream = currentEndpointData.getStream();
final ArrowBuf metadata = stream.getLatestMetadata();
latestBatchMetadata =

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This field is overwritten on every message, and next() keeps looping past empty roots within a single call. So callers never see a metadata-only message when more data follows it.

This is a new public API, so I would rather settle its shape before it ships: changing what it returns later would break callers. Some options are collecting the metadata per endpoint (for example a list of trailers), or letting callers register a listener that receives every app_metadata the driver reads.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked which messages a JDBC caller can actually observe on this head. A local server sent progress byte 1, a first row with metadata 2, two progress-only messages (3 and 4), a second row without metadata, and trailer 5. The getter returned 1 after executeQuery, 2 at the first row, null at the second row, and 5 at EOF. Bytes 3 and 4 were never visible to the caller.

That gives us two distinct behaviors to test: retaining a trailer per endpoint covers final query statistics, while exposing every progress message would require buffering those messages or delivering them as the driver reads them.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took the per-endpoint approach in 96c0981. getLatestMetadataByEndpoint() returns a copy of each endpoint's last non-null payload in FlightInfo order. It keeps one value per endpoint, including any trailer when the query is exhausted. getCurrentBatchMetadata() returns the metadata attached to the batch containing the current row.

For example, the test sends two progress messages (43 and 44) between batches. The next batch has no metadata. At its first row the batch getter returns null and the endpoint entry is 44. At EOF the entries are 99, 98 and null for the three endpoints. Callers who need every progress event would still need a listener.

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

Labels

enhancement PRs that add or improve features.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Java][FlightRPC] Flight SQL JDBC driver does not expose per-batch app_metadata from FlightStream

3 participants