Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.arrow.flight.FlightInfo;
import org.apache.arrow.flight.FlightRuntimeException;
import org.apache.arrow.flight.FlightStream;
import org.apache.arrow.memory.ArrowBuf;
import org.apache.arrow.util.AutoCloseables;
import org.apache.arrow.vector.VectorSchemaRoot;
import org.apache.arrow.vector.types.pojo.Schema;
Expand All @@ -59,6 +60,7 @@ public final class ArrowFlightJdbcFlightStreamResultSet
private VectorSchemaRootTransformer transformer;
private VectorSchemaRoot currentVectorSchemaRoot;

private byte[] latestBatchMetadata;
private Schema schema;
private Integer id = null; // used for metadata result sets only

Expand Down Expand Up @@ -160,7 +162,14 @@ private void populateData() throws SQLException {
}

private void populateDataForCurrentFlightStream() throws SQLException {
final VectorSchemaRoot originalRoot = currentEndpointData.getStream().getRoot();
final FlightStream stream = currentEndpointData.getStream();
final ArrowBuf metadata = stream.getLatestMetadata();
latestBatchMetadata =
metadata == null
? null
: metadata.getBytesAsArray(
metadata.readerIndex(), Math.toIntExact(metadata.readableBytes()));
final VectorSchemaRoot originalRoot = stream.getRoot();

if (transformer != null) {
try {
Expand All @@ -180,6 +189,15 @@ public byte[] getAppMetadata() {
return flightInfo.getAppMetadata();
}

/**
* Returns a copy of the application metadata from the most recent Flight data or metadata-only
* message consumed by this result set. Returns null if that message had no metadata or no message
* has been consumed. Trailing metadata remains available after next() returns false.
*/
public byte[] getLatestBatchMetadata() {
return latestBatchMetadata == null ? null : latestBatchMetadata.clone();
}

@Override
public boolean next() throws SQLException {
if (currentVectorSchemaRoot == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private CloseableEndpointStreamPair next(final EndpointStreamSupplier endpointSt
futures.remove(future);
try {
final CloseableEndpointStreamPair endpoint = future.get();
// Get the next FlightStream that has a root with content.
// Get the next FlightStream with rows or application metadata.
if (endpoint != null) {
return endpoint;
}
Expand Down Expand Up @@ -183,7 +183,8 @@ public synchronized void enqueue(final CloseableEndpointStreamPair endpointReque
() -> {
// `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
|| endpointRequest.getStream().getRoot().getRowCount() > 0) {
return endpointRequest;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.arrow.driver.jdbc.utils.CoreMockedSqlProducers;
import org.apache.arrow.driver.jdbc.utils.FallbackFlightSqlProducer;
import org.apache.arrow.driver.jdbc.utils.MockFlightSqlProducer;
import org.apache.arrow.driver.jdbc.utils.PartitionedFlightSqlProducer;
import org.apache.arrow.flight.Action;
import org.apache.arrow.flight.FlightConstants;
Expand All @@ -62,6 +63,7 @@
import org.apache.arrow.flight.Location;
import org.apache.arrow.flight.Result;
import org.apache.arrow.flight.Ticket;
import org.apache.arrow.memory.ArrowBuf;
import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.vector.IntVector;
Expand All @@ -75,14 +77,17 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

public class ResultSetTest {
private static final Random RANDOM = new Random(10);

private static final MockFlightSqlProducer PRODUCER = CoreMockedSqlProducers.getLegacyProducer();

@RegisterExtension
public static final FlightServerTestExtension FLIGHT_SERVER_TEST_EXTENSION =
FlightServerTestExtension.createStandardTestExtension(
CoreMockedSqlProducers.getLegacyProducer());
FlightServerTestExtension.createStandardTestExtension(PRODUCER);

private static Connection connection;

Expand Down Expand Up @@ -888,6 +893,59 @@ public void testShouldRunSelectQueryWithEmptyVectorsEmbedded() throws Exception
}
}

@ParameterizedTest
@ValueSource(booleans = {true, false})
public void testBatchMetadata(boolean withRows) throws Exception {
final Schema schema =
new Schema(Collections.singletonList(Field.nullable("id", Types.MinorType.INT.getType())));
final String query = withRows ? "select batch metadata" : "select empty batch metadata";
PRODUCER.addSelectQuery(
query,
schema,
List.of(
listener -> {
try (BufferAllocator allocator = new RootAllocator();
VectorSchemaRoot data = VectorSchemaRoot.create(schema, allocator)) {
listener.start(data);
if (withRows) {
IntVector ids = (IntVector) data.getVector(0);
ids.setSafe(0, 10);
ids.setSafe(1, 20);
data.setRowCount(2);
ArrowBuf metadata = allocator.buffer(1);
metadata.writeByte(42);
listener.putNext(metadata);
data.setRowCount(1);
listener.putNext();
}
ArrowBuf trailer = allocator.buffer(1);
trailer.writeByte(99);
listener.putMetadata(trailer);
} finally {
listener.completed();
}
}));
try (Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(query)) {
ArrowFlightJdbcFlightStreamResultSet flight =
result.unwrap(ArrowFlightJdbcFlightStreamResultSet.class);
if (withRows) {
assertTrue(result.next());
assertEquals(10, result.getInt(1));
byte[] first = flight.getLatestBatchMetadata();
assertArrayEquals(new byte[] {42}, first);
first[0] = 0;
assertTrue(result.next());
assertEquals(20, result.getInt(1));
assertArrayEquals(new byte[] {42}, flight.getLatestBatchMetadata());
assertTrue(result.next());
assertNull(flight.getLatestBatchMetadata());
}
assertFalse(result.next());
assertArrayEquals(new byte[] {99}, flight.getLatestBatchMetadata());
}
}

@Test
public void testResultSetAppMetadata() throws Exception {
try (Statement statement = connection.createStatement();
Expand Down
Loading