You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "xborder (via GitHub)" <gi...@apache.org> on 2026/03/24 22:43:38 UTC
[PR] GH-62: Optimize Flight SQL JDBC Statement execution paths [arrow-java]
xborder opened a new pull request, #1090:
URL: https://github.com/apache/arrow-java/pull/1090
## What's Changed
- This change simplifies the Flight SQL JDBC request flow for `Statement.executeQuery/executeUpdate` avoiding the `PreparedStatement` request flow
- Preserved the current requirement that `Statement.execute(String)` still flows through PreparedStatement
- Right now, Arrow Flight does not offer a good solution for this use case. There is no way for the client to disambiguate between a `CommandStatementUpdate` or `CommandStatemenQuery` without doing a server call
- Moved prepared-statement-specific creation and execution concerns into `ArrowFlightPreparedStatement`
- Similarly, created `ArrowFlightStatement` with specific logic to handle `Statements`
- Removed statement/prepared-statement construction details and logic from `ArrowFlightMetaImpl`. `ArrowFlightMetaImpl` was left it focused on orchestration
Closes #62.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-62: Optimize Flight SQL JDBC Statement execution paths [arrow-java]
Posted by "xborder (via GitHub) via github" <gi...@arrow.apache.org>.
xborder commented on code in PR #1090:
URL: https://github.com/apache/arrow-java/pull/1090#discussion_r4524390957
##########
flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightPreparedStatement.java:
##########
@@ -16,66 +16,263 @@
*/
package org.apache.arrow.driver.jdbc;
-import java.sql.PreparedStatement;
import java.sql.SQLException;
+import java.util.Collections;
+import java.util.List;
import org.apache.arrow.driver.jdbc.client.ArrowFlightSqlClientHandler;
+import org.apache.arrow.driver.jdbc.utils.AvaticaParameterBinder;
import org.apache.arrow.flight.FlightInfo;
+import org.apache.arrow.flight.FlightRuntimeException;
import org.apache.arrow.util.Preconditions;
+import org.apache.arrow.vector.types.pojo.Schema;
import org.apache.calcite.avatica.AvaticaPreparedStatement;
+import org.apache.calcite.avatica.AvaticaStatement;
+import org.apache.calcite.avatica.Meta.ExecuteBatchResult;
+import org.apache.calcite.avatica.Meta.ExecuteResult;
+import org.apache.calcite.avatica.Meta.MetaResultSet;
+import org.apache.calcite.avatica.Meta.PrepareCallback;
import org.apache.calcite.avatica.Meta.Signature;
import org.apache.calcite.avatica.Meta.StatementHandle;
+import org.apache.calcite.avatica.Meta.StatementType;
+import org.apache.calcite.avatica.remote.TypedValue;
-/** Arrow Flight JBCS's implementation {@link PreparedStatement}. */
+/** Arrow Flight JDBC's implementation {@link java.sql.PreparedStatement}. */
public class ArrowFlightPreparedStatement extends AvaticaPreparedStatement
implements ArrowFlightInfoStatement {
- private final ArrowFlightSqlClientHandler.PreparedStatement preparedStatement;
+ private ArrowFlightSqlClientHandler.PreparedStatement preparedStatement;
private ArrowFlightPreparedStatement(
final ArrowFlightConnection connection,
- final ArrowFlightSqlClientHandler.PreparedStatement preparedStatement,
final StatementHandle handle,
final Signature signature,
+ final ArrowFlightSqlClientHandler.PreparedStatement preparedStatement,
final int resultSetType,
final int resultSetConcurrency,
final int resultSetHoldability)
throws SQLException {
super(connection, handle, signature, resultSetType, resultSetConcurrency, resultSetHoldability);
this.preparedStatement = Preconditions.checkNotNull(preparedStatement);
+ this.handle.signature = signature;
+ setSignature(signature);
}
- static ArrowFlightPreparedStatement newPreparedStatement(
- final ArrowFlightConnection connection,
- final ArrowFlightSqlClientHandler.PreparedStatement preparedStmt,
- final StatementHandle statementHandle,
- final Signature signature,
- final int resultSetType,
- final int resultSetConcurrency,
- final int resultSetHoldability)
- throws SQLException {
- return new ArrowFlightPreparedStatement(
- connection,
- preparedStmt,
- statementHandle,
- signature,
- resultSetType,
- resultSetConcurrency,
- resultSetHoldability);
+ static Builder builder(final ArrowFlightConnection connection) {
+ return new Builder(connection);
}
@Override
public ArrowFlightConnection getConnection() throws SQLException {
return (ArrowFlightConnection) super.getConnection();
}
+ ExecuteResult prepareAndExecuteInternal(final PrepareCallback callback) throws SQLException {
+ ensurePrepared();
+ final StatementType statementType = preparedStatement.getType();
+ final long updateCount =
+ statementType.equals(StatementType.UPDATE) ? preparedStatement.executeUpdate() : -1;
+ synchronized (callback.getMonitor()) {
+ callback.clear();
+ callback.assign(handle.signature, null, updateCount);
+ }
+ callback.execute();
+ final MetaResultSet metaResultSet =
+ MetaResultSet.create(handle.connectionId, handle.id, false, handle.signature, null);
+ return new ExecuteResult(Collections.singletonList(metaResultSet));
+ }
+
+ ExecuteResult prepareAndExecuteInternal(
+ final String query,
+ final long maxRowCount,
+ final int maxRowsInFirstFrame,
+ final PrepareCallback callback)
+ throws SQLException {
+
+ return ArrowFlightPreparedStatement.builder(getConnection())
+ .withQuery(query)
+ .withExistingStatement(this)
+ .build()
+ .prepareAndExecuteInternal(callback);
+ }
+
+ Schema getDataSetSchema() {
+ ensurePrepared();
+ return preparedStatement.getDataSetSchema();
+ }
+
@Override
- public synchronized void close() throws SQLException {
- this.preparedStatement.close();
- super.close();
+ protected void close_() {
+ RuntimeException closeFailure = null;
+ try {
+ super.close_();
+ } catch (final RuntimeException e) {
+ closeFailure = e;
+ }
+ try {
+ closePreparedResources();
+ } catch (final RuntimeException e) {
+ if (closeFailure == null) {
+ closeFailure = e;
+ } else {
+ closeFailure.addSuppressed(e);
+ }
+ }
+ ((ArrowFlightConnection) connection).unregisterStatementOwner(this);
+ if (closeFailure != null) {
+ throw closeFailure;
+ }
+ }
+
+ void closePreparedResources() {
+ final ArrowFlightSqlClientHandler.PreparedStatement statement = preparedStatement;
+ preparedStatement = null;
+ if (statement != null) {
+ statement.close();
+ }
+ }
+
+ ExecuteResult executeWithTypedValues(
+ final StatementHandle statementHandle,
+ final List<TypedValue> typedValues,
+ final long maxRowCount) {
+ ensurePrepared();
+ Preconditions.checkArgument(
+ connection.id.equals(statementHandle.connectionId), "Connection IDs are not consistent");
+ new AvaticaParameterBinder(
+ preparedStatement, ((ArrowFlightConnection) connection).getBufferAllocator())
+ .bind(typedValues);
+
+ if (statementHandle.signature == null
+ || statementHandle.signature.statementType == StatementType.IS_DML) {
+ long updatedCount = preparedStatement.executeUpdate();
+ return new ExecuteResult(
+ Collections.singletonList(
+ MetaResultSet.count(statementHandle.connectionId, statementHandle.id, updatedCount)));
+ }
+
+ // TODO Why is maxRowCount ignored?
+ return new ExecuteResult(
+ Collections.singletonList(
+ MetaResultSet.create(
+ statementHandle.connectionId,
+ statementHandle.id,
+ true,
+ statementHandle.signature,
+ null)));
+ }
+
+ ExecuteBatchResult executeBatchWithTypedValues(
+ final StatementHandle statementHandle, final List<List<TypedValue>> parameterValuesList) {
+ ensurePrepared();
+ Preconditions.checkArgument(
+ connection.id.equals(statementHandle.connectionId), "Connection IDs are not consistent");
+ final AvaticaParameterBinder binder =
+ new AvaticaParameterBinder(
+ preparedStatement, ((ArrowFlightConnection) connection).getBufferAllocator());
+ for (int i = 0; i < parameterValuesList.size(); i++) {
+ binder.bind(parameterValuesList.get(i), i);
+ }
+
+ long[] updatedCounts = {preparedStatement.executeUpdate()};
+ return new ExecuteBatchResult(updatedCounts);
}
@Override
public FlightInfo executeFlightInfoQuery() throws SQLException {
+ ensurePrepared();
return preparedStatement.executeQuery();
}
+
+ private void ensurePrepared() {
+ if (preparedStatement == null) {
+ throw new IllegalStateException("PreparedStatement is already closed.");
+ }
+ }
+
+ static final class Builder {
+ private final ArrowFlightConnection connection;
+ private StatementHandle handle;
+ private String query;
+ private Integer resultSetType;
+ private Integer resultSetConcurrency;
+ private Integer resultSetHoldability;
+ private boolean generateHandle;
+
+ private Builder(final ArrowFlightConnection connection) {
+ this.connection = Preconditions.checkNotNull(connection);
+ }
+
+ Builder withQuery(final String query) {
+ this.query = Preconditions.checkNotNull(query);
+ return this;
+ }
+
+ Builder withGeneratedHandle() {
+ this.generateHandle = true;
+ this.handle = null;
+ return this;
+ }
+
+ Builder withExistingStatement(final AvaticaStatement statement) throws SQLException {
+ Preconditions.checkNotNull(statement);
+ this.generateHandle = false;
+ this.handle = Preconditions.checkNotNull(statement.handle);
+ this.resultSetType = statement.getResultSetType();
+ this.resultSetConcurrency = statement.getResultSetConcurrency();
+ this.resultSetHoldability = statement.getResultSetHoldability();
+ return this;
+ }
+
+ Builder withResultSetType(final int resultSetType) {
+ this.resultSetType = resultSetType;
+ return this;
+ }
+
+ Builder withResultSetConcurrency(final int resultSetConcurrency) {
+ this.resultSetConcurrency = resultSetConcurrency;
+ return this;
+ }
+
+ Builder withResultSetHoldability(final int resultSetHoldability) {
+ this.resultSetHoldability = resultSetHoldability;
+ return this;
+ }
+
+ ArrowFlightPreparedStatement build() throws SQLException {
+ Preconditions.checkNotNull(query);
+ Preconditions.checkNotNull(resultSetType);
+ Preconditions.checkNotNull(resultSetConcurrency);
+ Preconditions.checkNotNull(resultSetHoldability);
+ if (!generateHandle && handle == null) {
+ throw new IllegalStateException("PreparedStatement builder requires a handle.");
+ }
+
+ final ArrowFlightSqlClientHandler.PreparedStatement preparedStatement;
+ try {
+ preparedStatement = connection.getClientHandler().prepare(query);
+ } catch (final FlightRuntimeException e) {
+ throw new SQLException(e.getMessage(), e);
+ }
+ final Signature signature =
Review Comment:
https://github.com/apache/arrow-java/pull/1090#discussion_r4524384459
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-62: Optimize Flight SQL JDBC Statement execution paths [arrow-java]
Posted by "xborder (via GitHub) via github" <gi...@arrow.apache.org>.
xborder commented on code in PR #1090:
URL: https://github.com/apache/arrow-java/pull/1090#discussion_r4527109953
##########
flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ResultSetTest.java:
##########
@@ -235,6 +235,44 @@ public void testShouldCloseStatementWhenIsCloseOnCompletion() throws Exception {
}
}
+ @Test
+ public void testCloseOnCompletionUnregistersDirectStatementWhenResultSetClosedEarly()
+ throws Exception {
+ try (Statement statement = connection.createStatement()) {
+ final ResultSet resultSet =
+ statement.executeQuery(CoreMockedSqlProducers.LEGACY_REGULAR_SQL_CMD);
+ statement.closeOnCompletion();
+
+ resultSet.close();
+
+ assertThat(statement.isClosed(), is(true));
+ assertThat(isStatementOwner(statement), is(false));
+ }
+ }
+
+ @Test
+ public void testCloseOnCompletionUnregistersPreparedStatementWhenResultSetClosedEarly()
+ throws Exception {
+ try (PreparedStatement statement =
+ connection.prepareStatement(CoreMockedSqlProducers.LEGACY_REGULAR_SQL_CMD)) {
+ final ResultSet resultSet = statement.executeQuery();
+ statement.closeOnCompletion();
+
+ resultSet.close();
+
+ assertThat(statement.isClosed(), is(true));
+ assertThat(isStatementOwner(statement), is(false));
+ }
+ }
+
+ private static boolean isStatementOwner(final Statement statement)
+ throws ReflectiveOperationException {
+ final java.lang.reflect.Field statementOwnersField =
+ ArrowFlightConnection.class.getDeclaredField("statementOwners");
+ statementOwnersField.setAccessible(true);
+ return ((Set<?>) statementOwnersField.get(connection)).contains(statement);
Review Comment:
done. replaced reflection with a package-private @VisibleForTesting accessor instead
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-62: Optimize Flight SQL JDBC Statement execution paths [arrow-java]
Posted by "lidavidm (via GitHub) via github" <gi...@arrow.apache.org>.
lidavidm commented on code in PR #1090:
URL: https://github.com/apache/arrow-java/pull/1090#discussion_r4463124919
##########
flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightConnection.java:
##########
@@ -45,7 +52,10 @@ public final class ArrowFlightConnection extends AvaticaConnection {
private final ArrowFlightConnectionConfigImpl config;
private ExecutorService executorService;
private int metadataResultSetCount;
+ // Track result sets and statements owned by this connection so close() can release them.
Review Comment:
Comment in wrong place
##########
flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightConnection.java:
##########
@@ -45,7 +52,10 @@ public final class ArrowFlightConnection extends AvaticaConnection {
private final ArrowFlightConnectionConfigImpl config;
private ExecutorService executorService;
private int metadataResultSetCount;
+ // Track result sets and statements owned by this connection so close() can release them.
private Map<Integer, ArrowFlightJdbcFlightStreamResultSet> metadataResultSetMap = new HashMap<>();
+ private final Set<AvaticaStatement> statementOwners =
Review Comment:
I wonder, could we separate this change out into its own PR so it shows up more clearly in the changelog/history and is easier to review?
##########
flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightStatementApiTest.java:
##########
Review Comment:
Um, is this really necessary? Smells like overzealous AI?
##########
flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightStatementExecuteTest.java:
##########
@@ -142,6 +152,13 @@ public static void tearDownAfterClass() throws Exception {
AutoCloseables.close(PRODUCER);
}
+ @Test
+ public void testStatementKeepsFlightInfoStatementApi() {
+ final ArrowFlightInfoStatement flightInfoStatement = (ArrowFlightInfoStatement) statement;
+
+ assertSame(statement, flightInfoStatement);
+ }
+
Review Comment:
What is the point of this test...?
##########
flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightPreparedStatementTest.java:
##########
@@ -79,6 +88,17 @@ public void before() {
PRODUCER.clearActionTypeCounter();
}
+ @Test
+ public void testPreparedStatementKeepsFlightInfoStatementApi() throws SQLException {
+ try (final PreparedStatement preparedStatement =
+ connection.prepareStatement(CoreMockedSqlProducers.LEGACY_REGULAR_SQL_CMD)) {
+ final ArrowFlightInfoStatement flightInfoStatement =
+ (ArrowFlightInfoStatement) preparedStatement;
+
+ assertSame(preparedStatement, flightInfoStatement);
+ }
+ }
+
Review Comment:
Same here...
##########
flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightConnection.java:
##########
@@ -257,4 +290,39 @@ BufferAllocator getBufferAllocator() {
public ArrowFlightMetaImpl getMeta() {
return (ArrowFlightMetaImpl) this.meta;
}
+
+ @Override
+ public PreparedStatement prepareStatement(final String sql) throws SQLException {
+ checkOpen();
Review Comment:
(nit, but isn't prepareStatement itself about to checkOpen?)
##########
flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ResultSetTest.java:
##########
@@ -235,6 +235,44 @@ public void testShouldCloseStatementWhenIsCloseOnCompletion() throws Exception {
}
}
+ @Test
+ public void testCloseOnCompletionUnregistersDirectStatementWhenResultSetClosedEarly()
+ throws Exception {
+ try (Statement statement = connection.createStatement()) {
+ final ResultSet resultSet =
+ statement.executeQuery(CoreMockedSqlProducers.LEGACY_REGULAR_SQL_CMD);
+ statement.closeOnCompletion();
+
+ resultSet.close();
+
+ assertThat(statement.isClosed(), is(true));
+ assertThat(isStatementOwner(statement), is(false));
+ }
+ }
+
+ @Test
+ public void testCloseOnCompletionUnregistersPreparedStatementWhenResultSetClosedEarly()
+ throws Exception {
+ try (PreparedStatement statement =
+ connection.prepareStatement(CoreMockedSqlProducers.LEGACY_REGULAR_SQL_CMD)) {
+ final ResultSet resultSet = statement.executeQuery();
+ statement.closeOnCompletion();
+
+ resultSet.close();
+
+ assertThat(statement.isClosed(), is(true));
+ assertThat(isStatementOwner(statement), is(false));
+ }
+ }
+
+ private static boolean isStatementOwner(final Statement statement)
+ throws ReflectiveOperationException {
+ final java.lang.reflect.Field statementOwnersField =
+ ArrowFlightConnection.class.getDeclaredField("statementOwners");
+ statementOwnersField.setAccessible(true);
+ return ((Set<?>) statementOwnersField.get(connection)).contains(statement);
Review Comment:
If we _really_ need to test this, maybe making the property package-private and marking it with VisibleForTesting is better?
##########
flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightConnection.java:
##########
@@ -45,7 +52,10 @@ public final class ArrowFlightConnection extends AvaticaConnection {
private final ArrowFlightConnectionConfigImpl config;
private ExecutorService executorService;
private int metadataResultSetCount;
+ // Track result sets and statements owned by this connection so close() can release them.
private Map<Integer, ArrowFlightJdbcFlightStreamResultSet> metadataResultSetMap = new HashMap<>();
+ private final Set<AvaticaStatement> statementOwners =
Review Comment:
Does this perhaps need to be concurrency-safe?
##########
flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightStatementExecuteTest.java:
##########
@@ -165,6 +182,109 @@ public void testExecuteShouldRunSelectQuery() throws SQLException {
is(allOf(equalTo(statement.getLargeUpdateCount()), equalTo(-1L))));
}
+ @Test
+ public void testExecuteReplacesStatementMapEntryWithPreparedStatement() throws SQLException {
+ final ArrowFlightStatement arrowStatement = (ArrowFlightStatement) statement;
+ final ArrowFlightConnection arrowConnection = (ArrowFlightConnection) connection;
+
+ assertThat(statement.execute(SAMPLE_QUERY_CMD), is(true));
+
+ final Object preparedStatement = arrowConnection.statementMap.get(arrowStatement.handle.id);
+
+ assertNotNull(preparedStatement);
+ assertSame(preparedStatement, arrowConnection.statementMap.get(arrowStatement.handle.id));
+ assertThat(preparedStatement, instanceOf(ArrowFlightPreparedStatement.class));
+ }
+
+ @Test
+ public void testExecutePrepareFailurePreservesServerError() {
+ final SQLException exception =
+ assertThrows(
+ SQLException.class, () -> statement.execute("SELECT * FROM unregistered_table"));
+
+ assertThat(exception.getMessage(), containsString("Query not found"));
+ }
+
+ @Test
+ public void testConnectionCloseClosesStatementAndResultSetAfterExecute() throws SQLException {
+ PRODUCER.clearActionTypeCounter();
+ final ArrowFlightStatement arrowStatement = (ArrowFlightStatement) statement;
+ final ArrowFlightConnection arrowConnection = (ArrowFlightConnection) connection;
+
+ assertThat(statement.execute(SAMPLE_QUERY_CMD), is(true));
+ final ResultSet resultSet = statement.getResultSet();
+ assertNotNull(resultSet);
+
+ connection.close();
+
+ assertThat(statement.isClosed(), is(true));
+ assertThat(resultSet.isClosed(), is(true));
+ assertThat(arrowConnection.statementMap.containsKey(arrowStatement.handle.id), is(false));
+ assertThat(
+ PRODUCER
+ .getActionTypeCounter()
+ .getOrDefault(FlightSqlUtils.FLIGHT_SQL_CLOSE_PREPARED_STATEMENT.getType(), 0),
+ is(1));
+ }
+
+ @Test
+ public void testExecuteQueryRestoresStatementMapEntryWithStatement() throws SQLException {
+ final ArrowFlightStatement arrowStatement = (ArrowFlightStatement) statement;
+ final ArrowFlightConnection arrowConnection = (ArrowFlightConnection) connection;
+
+ assertThat(statement.execute(SAMPLE_QUERY_CMD), is(true));
+
+ try (ResultSet resultSet = statement.executeQuery(SAMPLE_QUERY_CMD)) {
+ assertThat(resultSet.next(), is(true));
+ }
+
+ assertSame(arrowStatement, arrowConnection.statementMap.get(arrowStatement.handle.id));
+ }
+
+ @Test
+ public void testExecuteQueryMapsPreparedStatementCloseFailureToSQLException()
+ throws SQLException {
+ final ArrowFlightStatement arrowStatement = (ArrowFlightStatement) statement;
+ final ArrowFlightConnection arrowConnection = (ArrowFlightConnection) connection;
+ final ResultSet previousResultSet = statement.executeQuery(SAMPLE_QUERY_CMD);
+ final ArrowFlightPreparedStatement preparedStatement = mock(ArrowFlightPreparedStatement.class);
+ final FlightRuntimeException closeFailure =
+ CallStatus.INTERNAL.withDescription("Prepared statement close failed").toRuntimeException();
+ doThrow(closeFailure).when(preparedStatement).closePreparedResources();
+ arrowConnection.statementMap.put(arrowStatement.handle.id, preparedStatement);
+
+ try {
+ final SQLException exception =
+ assertThrows(SQLException.class, () -> statement.executeQuery(SAMPLE_QUERY_CMD));
+
+ assertSame(closeFailure, exception.getCause());
+ assertThat(previousResultSet.isClosed(), is(true));
+ } finally {
+ arrowConnection.statementMap.put(arrowStatement.handle.id, arrowStatement);
Review Comment:
For the stated purpose of these tests, why do we need to manipulate the internal state of the connection?
##########
flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightConnection.java:
##########
@@ -45,7 +52,10 @@ public final class ArrowFlightConnection extends AvaticaConnection {
private final ArrowFlightConnectionConfigImpl config;
private ExecutorService executorService;
private int metadataResultSetCount;
+ // Track result sets and statements owned by this connection so close() can release them.
private Map<Integer, ArrowFlightJdbcFlightStreamResultSet> metadataResultSetMap = new HashMap<>();
+ private final Set<AvaticaStatement> statementOwners =
Review Comment:
Albeit if it can't be separated then that's fine
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-62: Optimize Flight SQL JDBC Statement execution paths [arrow-java]
Posted by "xborder (via GitHub)" <gi...@apache.org>.
xborder commented on code in PR #1090:
URL: https://github.com/apache/arrow-java/pull/1090#discussion_r3147451015
##########
flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightStatement.java:
##########
@@ -42,20 +49,140 @@ public ArrowFlightConnection getConnection() throws SQLException {
return (ArrowFlightConnection) super.getConnection();
}
+ @Override
+ public ExecuteResult prepareAndExecute(
+ final String query,
+ final long maxRowCount,
+ final int maxRowsInFirstFrame,
+ final PrepareCallback callback)
+ throws SQLException {
+ // Keep Avatica Statement.execute(String) behavior: Avatica calls Meta.prepareAndExecute,
+ // which resolves to this statement hook.
+ this.closeStatement();
+
+ return ArrowFlightPreparedStatement.builder(getConnection())
+ .withQuery(query)
+ .withExistingStatement(this)
+ .build()
+ .prepareAndExecute(callback);
Review Comment:
Good catch. This issue existed before the refactor. I've created an issue #1129 to be addressed separately
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-62: Optimize Flight SQL JDBC Statement execution paths [arrow-java]
Posted by "xborder (via GitHub) via github" <gi...@arrow.apache.org>.
xborder commented on code in PR #1090:
URL: https://github.com/apache/arrow-java/pull/1090#discussion_r4524446142
##########
flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightStatementExecuteTest.java:
##########
@@ -142,6 +152,13 @@ public static void tearDownAfterClass() throws Exception {
AutoCloseables.close(PRODUCER);
}
+ @Test
+ public void testStatementKeepsFlightInfoStatementApi() {
+ final ArrowFlightInfoStatement flightInfoStatement = (ArrowFlightInfoStatement) statement;
+
+ assertSame(statement, flightInfoStatement);
+ }
+
Review Comment:
thanks for identifying this. My process has changed since this was last changed, I'll review it more thoroughly
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-62: Optimize Flight SQL JDBC Statement execution paths [arrow-java]
Posted by "xborder (via GitHub)" <gi...@apache.org>.
xborder commented on code in PR #1090:
URL: https://github.com/apache/arrow-java/pull/1090#discussion_r3065191331
##########
flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/example/ArrowFlightJdbcSampleApp.java:
##########
Review Comment:
removed, usually I create sample apps. Forgot to remove it
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-62: Optimize Flight SQL JDBC Statement execution paths [arrow-java]
Posted by "xborder (via GitHub) via github" <gi...@arrow.apache.org>.
xborder commented on code in PR #1090:
URL: https://github.com/apache/arrow-java/pull/1090#discussion_r4524446155
##########
flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightStatementApiTest.java:
##########
Review Comment:
removed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-62: Optimize Flight SQL JDBC Statement execution paths [arrow-java]
Posted by "xborder (via GitHub) via github" <gi...@arrow.apache.org>.
xborder commented on code in PR #1090:
URL: https://github.com/apache/arrow-java/pull/1090#discussion_r4526896347
##########
flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcFlightStreamResultSet.java:
##########
@@ -192,23 +198,40 @@ public boolean next() throws SQLException {
return true;
}
- if (currentEndpointData != null) {
- currentEndpointData.getStream().getRoot().clear();
- if (currentEndpointData.getStream().next()) {
- populateDataForCurrentFlightStream();
- continue;
+ try {
+ if (currentEndpointData != null) {
+ currentEndpointData.getStream().getRoot().clear();
+ if (currentEndpointData.getStream().next()) {
+ populateDataForCurrentFlightStream();
+ continue;
+ }
+
+ enqueueEndpointData(currentEndpointData);
}
- flightEndpointDataQueue.enqueue(currentEndpointData);
+ currentEndpointData = getNextEndpointStream(false);
+ } catch (final FlightRuntimeException e) {
+ // A concurrent statement.cancel() (or close) cancels in-flight FlightStreams,
+ // which surface here as CANCELLED. Normalize to Avatica's "Statement canceled".
+ if (flightEndpointDataQueue.isClosed()
Review Comment:
Done. Added the cancellation flag and a regression test for cancelling a blocked next().
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-62: Optimize Flight SQL JDBC Statement execution paths [arrow-java]
Posted by "jbonofre (via GitHub) via github" <gi...@arrow.apache.org>.
jbonofre commented on code in PR #1090:
URL: https://github.com/apache/arrow-java/pull/1090#discussion_r4523606700
##########
flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightPreparedStatement.java:
##########
@@ -16,66 +16,263 @@
*/
package org.apache.arrow.driver.jdbc;
-import java.sql.PreparedStatement;
import java.sql.SQLException;
+import java.util.Collections;
+import java.util.List;
import org.apache.arrow.driver.jdbc.client.ArrowFlightSqlClientHandler;
+import org.apache.arrow.driver.jdbc.utils.AvaticaParameterBinder;
import org.apache.arrow.flight.FlightInfo;
+import org.apache.arrow.flight.FlightRuntimeException;
import org.apache.arrow.util.Preconditions;
+import org.apache.arrow.vector.types.pojo.Schema;
import org.apache.calcite.avatica.AvaticaPreparedStatement;
+import org.apache.calcite.avatica.AvaticaStatement;
+import org.apache.calcite.avatica.Meta.ExecuteBatchResult;
+import org.apache.calcite.avatica.Meta.ExecuteResult;
+import org.apache.calcite.avatica.Meta.MetaResultSet;
+import org.apache.calcite.avatica.Meta.PrepareCallback;
import org.apache.calcite.avatica.Meta.Signature;
import org.apache.calcite.avatica.Meta.StatementHandle;
+import org.apache.calcite.avatica.Meta.StatementType;
+import org.apache.calcite.avatica.remote.TypedValue;
-/** Arrow Flight JBCS's implementation {@link PreparedStatement}. */
+/** Arrow Flight JDBC's implementation {@link java.sql.PreparedStatement}. */
public class ArrowFlightPreparedStatement extends AvaticaPreparedStatement
implements ArrowFlightInfoStatement {
- private final ArrowFlightSqlClientHandler.PreparedStatement preparedStatement;
+ private ArrowFlightSqlClientHandler.PreparedStatement preparedStatement;
private ArrowFlightPreparedStatement(
final ArrowFlightConnection connection,
- final ArrowFlightSqlClientHandler.PreparedStatement preparedStatement,
final StatementHandle handle,
final Signature signature,
+ final ArrowFlightSqlClientHandler.PreparedStatement preparedStatement,
final int resultSetType,
final int resultSetConcurrency,
final int resultSetHoldability)
throws SQLException {
super(connection, handle, signature, resultSetType, resultSetConcurrency, resultSetHoldability);
this.preparedStatement = Preconditions.checkNotNull(preparedStatement);
+ this.handle.signature = signature;
+ setSignature(signature);
}
- static ArrowFlightPreparedStatement newPreparedStatement(
- final ArrowFlightConnection connection,
- final ArrowFlightSqlClientHandler.PreparedStatement preparedStmt,
- final StatementHandle statementHandle,
- final Signature signature,
- final int resultSetType,
- final int resultSetConcurrency,
- final int resultSetHoldability)
- throws SQLException {
- return new ArrowFlightPreparedStatement(
- connection,
- preparedStmt,
- statementHandle,
- signature,
- resultSetType,
- resultSetConcurrency,
- resultSetHoldability);
+ static Builder builder(final ArrowFlightConnection connection) {
+ return new Builder(connection);
}
@Override
public ArrowFlightConnection getConnection() throws SQLException {
return (ArrowFlightConnection) super.getConnection();
}
+ ExecuteResult prepareAndExecuteInternal(final PrepareCallback callback) throws SQLException {
+ ensurePrepared();
+ final StatementType statementType = preparedStatement.getType();
+ final long updateCount =
+ statementType.equals(StatementType.UPDATE) ? preparedStatement.executeUpdate() : -1;
+ synchronized (callback.getMonitor()) {
+ callback.clear();
+ callback.assign(handle.signature, null, updateCount);
+ }
+ callback.execute();
+ final MetaResultSet metaResultSet =
+ MetaResultSet.create(handle.connectionId, handle.id, false, handle.signature, null);
+ return new ExecuteResult(Collections.singletonList(metaResultSet));
+ }
+
+ ExecuteResult prepareAndExecuteInternal(
+ final String query,
+ final long maxRowCount,
+ final int maxRowsInFirstFrame,
+ final PrepareCallback callback)
+ throws SQLException {
+
+ return ArrowFlightPreparedStatement.builder(getConnection())
Review Comment:
Calling `Statement.execute(String)` again on the same statement leaks the previous server-side prepared statement.
On the second call, `statementMap` holds the internal `ArrowFlightPreparedStatement` created by the first call, so `ArrowFlightMetaImpl.prepareAndExecute` routes here. We build a new prepared statement under the same handle, and its constructor replaces the `statementMap` entry, but `this.preparedStatement` is never closed. No `ClosePreparedStatement` is sent for it. A loop of N `execute()` calls leaves N-1 prepared statements open on the server, and `stmt.close()` only closes the last one.
The old `statementHandlePreparedStatementMap.put(...)` dropped the previous entry the same way, so this is not a regression. Since this method is new, could we close the previous one once the replacement is built?
```java
final ArrowFlightPreparedStatement replacement =
ArrowFlightPreparedStatement.builder(getConnection())
.withQuery(query)
.withExistingStatement(this)
.build();
closePreparedResources();
return replacement.prepareAndExecuteInternal(callback);
```
A test that calls `execute()` a few times on one statement and checks how many prepared statements the mock producer closed would cover this.
##########
flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcFlightStreamResultSet.java:
##########
@@ -192,23 +198,40 @@ public boolean next() throws SQLException {
return true;
}
- if (currentEndpointData != null) {
- currentEndpointData.getStream().getRoot().clear();
- if (currentEndpointData.getStream().next()) {
- populateDataForCurrentFlightStream();
- continue;
+ try {
+ if (currentEndpointData != null) {
+ currentEndpointData.getStream().getRoot().clear();
+ if (currentEndpointData.getStream().next()) {
+ populateDataForCurrentFlightStream();
+ continue;
+ }
+
+ enqueueEndpointData(currentEndpointData);
}
- flightEndpointDataQueue.enqueue(currentEndpointData);
+ currentEndpointData = getNextEndpointStream(false);
+ } catch (final FlightRuntimeException e) {
+ // A concurrent statement.cancel() (or close) cancels in-flight FlightStreams,
+ // which surface here as CANCELLED. Normalize to Avatica's "Statement canceled".
+ if (flightEndpointDataQueue.isClosed()
Review Comment:
This check can miss a cancel. `cancel()` cancels the current stream first and only then calls `flightEndpointDataQueue.close()`. `FlightEndpointDataQueue.close()` sets `closed` in its `finally` block, after it has cancelled the other streams, waited on the futures and closed the endpoints.
A reader blocked in `currentEndpointData.getStream().next()` is woken by the stream cancel and can reach this line while `isClosed()` is still `false`. It then rethrows the raw `FlightRuntimeException` instead of `Statement canceled`. The `isClosed()` check at line 231 has the same race.
`AvaticaResultSet.cancel()` sets the statement cancel flag first, but that field is protected on `AvaticaStatement`, so it is not reachable from here. A simple option is a `volatile boolean cancelled` field that `cancel()` sets before it cancels the stream, checked here and at line 231.
It would also be good to add a test where one thread is blocked in `next()` while another calls `statement.cancel()`. The current test only covers the enqueue path.
##########
flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightPreparedStatement.java:
##########
@@ -16,66 +16,263 @@
*/
package org.apache.arrow.driver.jdbc;
-import java.sql.PreparedStatement;
import java.sql.SQLException;
+import java.util.Collections;
+import java.util.List;
import org.apache.arrow.driver.jdbc.client.ArrowFlightSqlClientHandler;
+import org.apache.arrow.driver.jdbc.utils.AvaticaParameterBinder;
import org.apache.arrow.flight.FlightInfo;
+import org.apache.arrow.flight.FlightRuntimeException;
import org.apache.arrow.util.Preconditions;
+import org.apache.arrow.vector.types.pojo.Schema;
import org.apache.calcite.avatica.AvaticaPreparedStatement;
+import org.apache.calcite.avatica.AvaticaStatement;
+import org.apache.calcite.avatica.Meta.ExecuteBatchResult;
+import org.apache.calcite.avatica.Meta.ExecuteResult;
+import org.apache.calcite.avatica.Meta.MetaResultSet;
+import org.apache.calcite.avatica.Meta.PrepareCallback;
import org.apache.calcite.avatica.Meta.Signature;
import org.apache.calcite.avatica.Meta.StatementHandle;
+import org.apache.calcite.avatica.Meta.StatementType;
+import org.apache.calcite.avatica.remote.TypedValue;
-/** Arrow Flight JBCS's implementation {@link PreparedStatement}. */
+/** Arrow Flight JDBC's implementation {@link java.sql.PreparedStatement}. */
public class ArrowFlightPreparedStatement extends AvaticaPreparedStatement
implements ArrowFlightInfoStatement {
- private final ArrowFlightSqlClientHandler.PreparedStatement preparedStatement;
+ private ArrowFlightSqlClientHandler.PreparedStatement preparedStatement;
private ArrowFlightPreparedStatement(
final ArrowFlightConnection connection,
- final ArrowFlightSqlClientHandler.PreparedStatement preparedStatement,
final StatementHandle handle,
final Signature signature,
+ final ArrowFlightSqlClientHandler.PreparedStatement preparedStatement,
final int resultSetType,
final int resultSetConcurrency,
final int resultSetHoldability)
throws SQLException {
super(connection, handle, signature, resultSetType, resultSetConcurrency, resultSetHoldability);
this.preparedStatement = Preconditions.checkNotNull(preparedStatement);
+ this.handle.signature = signature;
+ setSignature(signature);
}
- static ArrowFlightPreparedStatement newPreparedStatement(
- final ArrowFlightConnection connection,
- final ArrowFlightSqlClientHandler.PreparedStatement preparedStmt,
- final StatementHandle statementHandle,
- final Signature signature,
- final int resultSetType,
- final int resultSetConcurrency,
- final int resultSetHoldability)
- throws SQLException {
- return new ArrowFlightPreparedStatement(
- connection,
- preparedStmt,
- statementHandle,
- signature,
- resultSetType,
- resultSetConcurrency,
- resultSetHoldability);
+ static Builder builder(final ArrowFlightConnection connection) {
+ return new Builder(connection);
}
@Override
public ArrowFlightConnection getConnection() throws SQLException {
return (ArrowFlightConnection) super.getConnection();
}
+ ExecuteResult prepareAndExecuteInternal(final PrepareCallback callback) throws SQLException {
+ ensurePrepared();
+ final StatementType statementType = preparedStatement.getType();
+ final long updateCount =
+ statementType.equals(StatementType.UPDATE) ? preparedStatement.executeUpdate() : -1;
+ synchronized (callback.getMonitor()) {
+ callback.clear();
+ callback.assign(handle.signature, null, updateCount);
+ }
+ callback.execute();
+ final MetaResultSet metaResultSet =
+ MetaResultSet.create(handle.connectionId, handle.id, false, handle.signature, null);
+ return new ExecuteResult(Collections.singletonList(metaResultSet));
+ }
+
+ ExecuteResult prepareAndExecuteInternal(
+ final String query,
+ final long maxRowCount,
+ final int maxRowsInFirstFrame,
+ final PrepareCallback callback)
+ throws SQLException {
+
+ return ArrowFlightPreparedStatement.builder(getConnection())
+ .withQuery(query)
+ .withExistingStatement(this)
+ .build()
+ .prepareAndExecuteInternal(callback);
+ }
+
+ Schema getDataSetSchema() {
+ ensurePrepared();
+ return preparedStatement.getDataSetSchema();
+ }
+
@Override
- public synchronized void close() throws SQLException {
- this.preparedStatement.close();
- super.close();
+ protected void close_() {
+ RuntimeException closeFailure = null;
+ try {
+ super.close_();
+ } catch (final RuntimeException e) {
+ closeFailure = e;
+ }
+ try {
+ closePreparedResources();
+ } catch (final RuntimeException e) {
+ if (closeFailure == null) {
+ closeFailure = e;
+ } else {
+ closeFailure.addSuppressed(e);
+ }
+ }
+ ((ArrowFlightConnection) connection).unregisterStatementOwner(this);
+ if (closeFailure != null) {
+ throw closeFailure;
+ }
+ }
+
+ void closePreparedResources() {
+ final ArrowFlightSqlClientHandler.PreparedStatement statement = preparedStatement;
+ preparedStatement = null;
+ if (statement != null) {
+ statement.close();
+ }
+ }
+
+ ExecuteResult executeWithTypedValues(
+ final StatementHandle statementHandle,
+ final List<TypedValue> typedValues,
+ final long maxRowCount) {
+ ensurePrepared();
+ Preconditions.checkArgument(
+ connection.id.equals(statementHandle.connectionId), "Connection IDs are not consistent");
+ new AvaticaParameterBinder(
+ preparedStatement, ((ArrowFlightConnection) connection).getBufferAllocator())
+ .bind(typedValues);
+
+ if (statementHandle.signature == null
+ || statementHandle.signature.statementType == StatementType.IS_DML) {
+ long updatedCount = preparedStatement.executeUpdate();
+ return new ExecuteResult(
+ Collections.singletonList(
+ MetaResultSet.count(statementHandle.connectionId, statementHandle.id, updatedCount)));
+ }
+
+ // TODO Why is maxRowCount ignored?
+ return new ExecuteResult(
+ Collections.singletonList(
+ MetaResultSet.create(
+ statementHandle.connectionId,
+ statementHandle.id,
+ true,
+ statementHandle.signature,
+ null)));
+ }
+
+ ExecuteBatchResult executeBatchWithTypedValues(
+ final StatementHandle statementHandle, final List<List<TypedValue>> parameterValuesList) {
+ ensurePrepared();
+ Preconditions.checkArgument(
+ connection.id.equals(statementHandle.connectionId), "Connection IDs are not consistent");
+ final AvaticaParameterBinder binder =
+ new AvaticaParameterBinder(
+ preparedStatement, ((ArrowFlightConnection) connection).getBufferAllocator());
+ for (int i = 0; i < parameterValuesList.size(); i++) {
+ binder.bind(parameterValuesList.get(i), i);
+ }
+
+ long[] updatedCounts = {preparedStatement.executeUpdate()};
+ return new ExecuteBatchResult(updatedCounts);
}
@Override
public FlightInfo executeFlightInfoQuery() throws SQLException {
+ ensurePrepared();
return preparedStatement.executeQuery();
}
+
+ private void ensurePrepared() {
+ if (preparedStatement == null) {
+ throw new IllegalStateException("PreparedStatement is already closed.");
+ }
+ }
+
+ static final class Builder {
+ private final ArrowFlightConnection connection;
+ private StatementHandle handle;
+ private String query;
+ private Integer resultSetType;
+ private Integer resultSetConcurrency;
+ private Integer resultSetHoldability;
+ private boolean generateHandle;
+
+ private Builder(final ArrowFlightConnection connection) {
+ this.connection = Preconditions.checkNotNull(connection);
+ }
+
+ Builder withQuery(final String query) {
+ this.query = Preconditions.checkNotNull(query);
+ return this;
+ }
+
+ Builder withGeneratedHandle() {
+ this.generateHandle = true;
+ this.handle = null;
+ return this;
+ }
+
+ Builder withExistingStatement(final AvaticaStatement statement) throws SQLException {
+ Preconditions.checkNotNull(statement);
+ this.generateHandle = false;
+ this.handle = Preconditions.checkNotNull(statement.handle);
+ this.resultSetType = statement.getResultSetType();
+ this.resultSetConcurrency = statement.getResultSetConcurrency();
+ this.resultSetHoldability = statement.getResultSetHoldability();
+ return this;
+ }
+
+ Builder withResultSetType(final int resultSetType) {
+ this.resultSetType = resultSetType;
+ return this;
+ }
+
+ Builder withResultSetConcurrency(final int resultSetConcurrency) {
+ this.resultSetConcurrency = resultSetConcurrency;
+ return this;
+ }
+
+ Builder withResultSetHoldability(final int resultSetHoldability) {
+ this.resultSetHoldability = resultSetHoldability;
+ return this;
+ }
+
+ ArrowFlightPreparedStatement build() throws SQLException {
+ Preconditions.checkNotNull(query);
+ Preconditions.checkNotNull(resultSetType);
+ Preconditions.checkNotNull(resultSetConcurrency);
+ Preconditions.checkNotNull(resultSetHoldability);
+ if (!generateHandle && handle == null) {
+ throw new IllegalStateException("PreparedStatement builder requires a handle.");
+ }
+
+ final ArrowFlightSqlClientHandler.PreparedStatement preparedStatement;
+ try {
+ preparedStatement = connection.getClientHandler().prepare(query);
+ } catch (final FlightRuntimeException e) {
+ throw new SQLException(e.getMessage(), e);
+ }
+ final Signature signature =
Review Comment:
If anything throws between `prepare()` and the end of `build()`, the server side prepared statement is never cloud. One concrete case: a parameter schema with a `ListView`, `LargeListView` or `RunEndEncoded` field makes `ConvertUtils.convertArrowFieldsToAvaticaParameters` throw `UnsupportedOperationException` from `buildSignature`.
The old `prepareForHandle` had the same gap. Now that `build()` owns the whole lifecycle, could we close it on failure?
```java
final ArrowFlightPreparedStatement statement;
try {
final Signature signature =
ArrowFlightMetaImpl.buildSignature(
query,
preparedStatement.getDataSetSchema(),
preparedStatement.getParameterSchema(),
preparedStatement.isUpdate());
statement =
new ArrowFlightPreparedStatement(
connection,
generateHandle ? null : handle,
signature,
preparedStatement,
resultSetType,
resultSetConcurrency,
resultSetHoldability);
} catch (final Exception e) {
try {
preparedStatement.close();
} catch (final Exception closeException) {
e.addSuppressed(closeException);
}
throw e;
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-62: Optimize Flight SQL JDBC Statement execution paths [arrow-java]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #1090:
URL: https://github.com/apache/arrow-java/pull/1090#issuecomment-4224817286
<!-- label_helper_comment -->
Thank you for opening a pull request!
Please label the PR with one or more of:
- bug-fix
- chore
- dependencies
- documentation
- enhancement
Also, add the 'breaking-change' label if appropriate.
See [CONTRIBUTING.md](https://github.com/apache/arrow-java/blob/main/CONTRIBUTING.md) for details.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-62: Optimize Flight SQL JDBC Statement execution paths [arrow-java]
Posted by "xborder (via GitHub) via github" <gi...@arrow.apache.org>.
xborder commented on code in PR #1090:
URL: https://github.com/apache/arrow-java/pull/1090#discussion_r4524384459
##########
flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightPreparedStatement.java:
##########
@@ -16,66 +16,263 @@
*/
package org.apache.arrow.driver.jdbc;
-import java.sql.PreparedStatement;
import java.sql.SQLException;
+import java.util.Collections;
+import java.util.List;
import org.apache.arrow.driver.jdbc.client.ArrowFlightSqlClientHandler;
+import org.apache.arrow.driver.jdbc.utils.AvaticaParameterBinder;
import org.apache.arrow.flight.FlightInfo;
+import org.apache.arrow.flight.FlightRuntimeException;
import org.apache.arrow.util.Preconditions;
+import org.apache.arrow.vector.types.pojo.Schema;
import org.apache.calcite.avatica.AvaticaPreparedStatement;
+import org.apache.calcite.avatica.AvaticaStatement;
+import org.apache.calcite.avatica.Meta.ExecuteBatchResult;
+import org.apache.calcite.avatica.Meta.ExecuteResult;
+import org.apache.calcite.avatica.Meta.MetaResultSet;
+import org.apache.calcite.avatica.Meta.PrepareCallback;
import org.apache.calcite.avatica.Meta.Signature;
import org.apache.calcite.avatica.Meta.StatementHandle;
+import org.apache.calcite.avatica.Meta.StatementType;
+import org.apache.calcite.avatica.remote.TypedValue;
-/** Arrow Flight JBCS's implementation {@link PreparedStatement}. */
+/** Arrow Flight JDBC's implementation {@link java.sql.PreparedStatement}. */
public class ArrowFlightPreparedStatement extends AvaticaPreparedStatement
implements ArrowFlightInfoStatement {
- private final ArrowFlightSqlClientHandler.PreparedStatement preparedStatement;
+ private ArrowFlightSqlClientHandler.PreparedStatement preparedStatement;
private ArrowFlightPreparedStatement(
final ArrowFlightConnection connection,
- final ArrowFlightSqlClientHandler.PreparedStatement preparedStatement,
final StatementHandle handle,
final Signature signature,
+ final ArrowFlightSqlClientHandler.PreparedStatement preparedStatement,
final int resultSetType,
final int resultSetConcurrency,
final int resultSetHoldability)
throws SQLException {
super(connection, handle, signature, resultSetType, resultSetConcurrency, resultSetHoldability);
this.preparedStatement = Preconditions.checkNotNull(preparedStatement);
+ this.handle.signature = signature;
+ setSignature(signature);
}
- static ArrowFlightPreparedStatement newPreparedStatement(
- final ArrowFlightConnection connection,
- final ArrowFlightSqlClientHandler.PreparedStatement preparedStmt,
- final StatementHandle statementHandle,
- final Signature signature,
- final int resultSetType,
- final int resultSetConcurrency,
- final int resultSetHoldability)
- throws SQLException {
- return new ArrowFlightPreparedStatement(
- connection,
- preparedStmt,
- statementHandle,
- signature,
- resultSetType,
- resultSetConcurrency,
- resultSetHoldability);
+ static Builder builder(final ArrowFlightConnection connection) {
+ return new Builder(connection);
}
@Override
public ArrowFlightConnection getConnection() throws SQLException {
return (ArrowFlightConnection) super.getConnection();
}
+ ExecuteResult prepareAndExecuteInternal(final PrepareCallback callback) throws SQLException {
+ ensurePrepared();
+ final StatementType statementType = preparedStatement.getType();
+ final long updateCount =
+ statementType.equals(StatementType.UPDATE) ? preparedStatement.executeUpdate() : -1;
+ synchronized (callback.getMonitor()) {
+ callback.clear();
+ callback.assign(handle.signature, null, updateCount);
+ }
+ callback.execute();
+ final MetaResultSet metaResultSet =
+ MetaResultSet.create(handle.connectionId, handle.id, false, handle.signature, null);
+ return new ExecuteResult(Collections.singletonList(metaResultSet));
+ }
+
+ ExecuteResult prepareAndExecuteInternal(
+ final String query,
+ final long maxRowCount,
+ final int maxRowsInFirstFrame,
+ final PrepareCallback callback)
+ throws SQLException {
+
+ return ArrowFlightPreparedStatement.builder(getConnection())
Review Comment:
I had already identified this issue. It already exists on main. I suggest addressing it in a follow-up PR after this one merges.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-62: Optimize Flight SQL JDBC Statement execution paths [arrow-java]
Posted by "xborder (via GitHub) via github" <gi...@arrow.apache.org>.
xborder commented on code in PR #1090:
URL: https://github.com/apache/arrow-java/pull/1090#discussion_r4524446159
##########
flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightConnection.java:
##########
@@ -45,7 +52,10 @@ public final class ArrowFlightConnection extends AvaticaConnection {
private final ArrowFlightConnectionConfigImpl config;
private ExecutorService executorService;
private int metadataResultSetCount;
+ // Track result sets and statements owned by this connection so close() can release them.
Review Comment:
done
##########
flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightConnection.java:
##########
@@ -257,4 +290,39 @@ BufferAllocator getBufferAllocator() {
public ArrowFlightMetaImpl getMeta() {
return (ArrowFlightMetaImpl) this.meta;
}
+
+ @Override
+ public PreparedStatement prepareStatement(final String sql) throws SQLException {
+ checkOpen();
Review Comment:
done
##########
flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightStatementApiTest.java:
##########
Review Comment:
done
##########
flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightStatementExecuteTest.java:
##########
@@ -142,6 +152,13 @@ public static void tearDownAfterClass() throws Exception {
AutoCloseables.close(PRODUCER);
}
+ @Test
+ public void testStatementKeepsFlightInfoStatementApi() {
+ final ArrowFlightInfoStatement flightInfoStatement = (ArrowFlightInfoStatement) statement;
+
+ assertSame(statement, flightInfoStatement);
+ }
+
Review Comment:
done
##########
flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightPreparedStatementTest.java:
##########
@@ -79,6 +88,17 @@ public void before() {
PRODUCER.clearActionTypeCounter();
}
+ @Test
+ public void testPreparedStatementKeepsFlightInfoStatementApi() throws SQLException {
+ try (final PreparedStatement preparedStatement =
+ connection.prepareStatement(CoreMockedSqlProducers.LEGACY_REGULAR_SQL_CMD)) {
+ final ArrowFlightInfoStatement flightInfoStatement =
+ (ArrowFlightInfoStatement) preparedStatement;
+
+ assertSame(preparedStatement, flightInfoStatement);
+ }
+ }
+
Review Comment:
done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-62: Optimize Flight SQL JDBC Statement execution paths [arrow-java]
Posted by "xborder (via GitHub) via github" <gi...@arrow.apache.org>.
xborder commented on code in PR #1090:
URL: https://github.com/apache/arrow-java/pull/1090#discussion_r4526241498
##########
flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightConnection.java:
##########
@@ -45,7 +52,10 @@ public final class ArrowFlightConnection extends AvaticaConnection {
private final ArrowFlightConnectionConfigImpl config;
private ExecutorService executorService;
private int metadataResultSetCount;
+ // Track result sets and statements owned by this connection so close() can release them.
private Map<Integer, ArrowFlightJdbcFlightStreamResultSet> metadataResultSetMap = new HashMap<>();
+ private final Set<AvaticaStatement> statementOwners =
Review Comment:
This is to ensure that we close the result sets for the original statements when the connection is closed or reset, even if their entries in Avatica’s statement map have been replaced.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-62: Optimize Flight SQL JDBC Statement execution paths [arrow-java]
Posted by "xborder (via GitHub) via github" <gi...@arrow.apache.org>.
xborder commented on code in PR #1090:
URL: https://github.com/apache/arrow-java/pull/1090#discussion_r4527096251
##########
flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightStatementExecuteTest.java:
##########
@@ -165,6 +182,109 @@ public void testExecuteShouldRunSelectQuery() throws SQLException {
is(allOf(equalTo(statement.getLargeUpdateCount()), equalTo(-1L))));
}
+ @Test
+ public void testExecuteReplacesStatementMapEntryWithPreparedStatement() throws SQLException {
+ final ArrowFlightStatement arrowStatement = (ArrowFlightStatement) statement;
+ final ArrowFlightConnection arrowConnection = (ArrowFlightConnection) connection;
+
+ assertThat(statement.execute(SAMPLE_QUERY_CMD), is(true));
+
+ final Object preparedStatement = arrowConnection.statementMap.get(arrowStatement.handle.id);
+
+ assertNotNull(preparedStatement);
+ assertSame(preparedStatement, arrowConnection.statementMap.get(arrowStatement.handle.id));
+ assertThat(preparedStatement, instanceOf(ArrowFlightPreparedStatement.class));
+ }
+
+ @Test
+ public void testExecutePrepareFailurePreservesServerError() {
+ final SQLException exception =
+ assertThrows(
+ SQLException.class, () -> statement.execute("SELECT * FROM unregistered_table"));
+
+ assertThat(exception.getMessage(), containsString("Query not found"));
+ }
+
+ @Test
+ public void testConnectionCloseClosesStatementAndResultSetAfterExecute() throws SQLException {
+ PRODUCER.clearActionTypeCounter();
+ final ArrowFlightStatement arrowStatement = (ArrowFlightStatement) statement;
+ final ArrowFlightConnection arrowConnection = (ArrowFlightConnection) connection;
+
+ assertThat(statement.execute(SAMPLE_QUERY_CMD), is(true));
+ final ResultSet resultSet = statement.getResultSet();
+ assertNotNull(resultSet);
+
+ connection.close();
+
+ assertThat(statement.isClosed(), is(true));
+ assertThat(resultSet.isClosed(), is(true));
+ assertThat(arrowConnection.statementMap.containsKey(arrowStatement.handle.id), is(false));
+ assertThat(
+ PRODUCER
+ .getActionTypeCounter()
+ .getOrDefault(FlightSqlUtils.FLIGHT_SQL_CLOSE_PREPARED_STATEMENT.getType(), 0),
+ is(1));
+ }
+
+ @Test
+ public void testExecuteQueryRestoresStatementMapEntryWithStatement() throws SQLException {
+ final ArrowFlightStatement arrowStatement = (ArrowFlightStatement) statement;
+ final ArrowFlightConnection arrowConnection = (ArrowFlightConnection) connection;
+
+ assertThat(statement.execute(SAMPLE_QUERY_CMD), is(true));
+
+ try (ResultSet resultSet = statement.executeQuery(SAMPLE_QUERY_CMD)) {
+ assertThat(resultSet.next(), is(true));
+ }
+
+ assertSame(arrowStatement, arrowConnection.statementMap.get(arrowStatement.handle.id));
+ }
+
+ @Test
+ public void testExecuteQueryMapsPreparedStatementCloseFailureToSQLException()
+ throws SQLException {
+ final ArrowFlightStatement arrowStatement = (ArrowFlightStatement) statement;
+ final ArrowFlightConnection arrowConnection = (ArrowFlightConnection) connection;
+ final ResultSet previousResultSet = statement.executeQuery(SAMPLE_QUERY_CMD);
+ final ArrowFlightPreparedStatement preparedStatement = mock(ArrowFlightPreparedStatement.class);
+ final FlightRuntimeException closeFailure =
+ CallStatus.INTERNAL.withDescription("Prepared statement close failed").toRuntimeException();
+ doThrow(closeFailure).when(preparedStatement).closePreparedResources();
+ arrowConnection.statementMap.put(arrowStatement.handle.id, preparedStatement);
+
+ try {
+ final SQLException exception =
+ assertThrows(SQLException.class, () -> statement.executeQuery(SAMPLE_QUERY_CMD));
+
+ assertSame(closeFailure, exception.getCause());
+ assertThat(previousResultSet.isClosed(), is(true));
+ } finally {
+ arrowConnection.statementMap.put(arrowStatement.handle.id, arrowStatement);
Review Comment:
It was used to force a prepared-statement cleanup failure and verify the resulting SQLException and result-set cleanup. I’ve changed the tests to inject the failure through the mock server instead.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-62: Optimize Flight SQL JDBC Statement execution paths [arrow-java]
Posted by "jbonofre (via GitHub) via github" <gi...@arrow.apache.org>.
jbonofre commented on code in PR #1090:
URL: https://github.com/apache/arrow-java/pull/1090#discussion_r4525038506
##########
flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightPreparedStatement.java:
##########
@@ -16,66 +16,263 @@
*/
package org.apache.arrow.driver.jdbc;
-import java.sql.PreparedStatement;
import java.sql.SQLException;
+import java.util.Collections;
+import java.util.List;
import org.apache.arrow.driver.jdbc.client.ArrowFlightSqlClientHandler;
+import org.apache.arrow.driver.jdbc.utils.AvaticaParameterBinder;
import org.apache.arrow.flight.FlightInfo;
+import org.apache.arrow.flight.FlightRuntimeException;
import org.apache.arrow.util.Preconditions;
+import org.apache.arrow.vector.types.pojo.Schema;
import org.apache.calcite.avatica.AvaticaPreparedStatement;
+import org.apache.calcite.avatica.AvaticaStatement;
+import org.apache.calcite.avatica.Meta.ExecuteBatchResult;
+import org.apache.calcite.avatica.Meta.ExecuteResult;
+import org.apache.calcite.avatica.Meta.MetaResultSet;
+import org.apache.calcite.avatica.Meta.PrepareCallback;
import org.apache.calcite.avatica.Meta.Signature;
import org.apache.calcite.avatica.Meta.StatementHandle;
+import org.apache.calcite.avatica.Meta.StatementType;
+import org.apache.calcite.avatica.remote.TypedValue;
-/** Arrow Flight JBCS's implementation {@link PreparedStatement}. */
+/** Arrow Flight JDBC's implementation {@link java.sql.PreparedStatement}. */
public class ArrowFlightPreparedStatement extends AvaticaPreparedStatement
implements ArrowFlightInfoStatement {
- private final ArrowFlightSqlClientHandler.PreparedStatement preparedStatement;
+ private ArrowFlightSqlClientHandler.PreparedStatement preparedStatement;
private ArrowFlightPreparedStatement(
final ArrowFlightConnection connection,
- final ArrowFlightSqlClientHandler.PreparedStatement preparedStatement,
final StatementHandle handle,
final Signature signature,
+ final ArrowFlightSqlClientHandler.PreparedStatement preparedStatement,
final int resultSetType,
final int resultSetConcurrency,
final int resultSetHoldability)
throws SQLException {
super(connection, handle, signature, resultSetType, resultSetConcurrency, resultSetHoldability);
this.preparedStatement = Preconditions.checkNotNull(preparedStatement);
+ this.handle.signature = signature;
+ setSignature(signature);
}
- static ArrowFlightPreparedStatement newPreparedStatement(
- final ArrowFlightConnection connection,
- final ArrowFlightSqlClientHandler.PreparedStatement preparedStmt,
- final StatementHandle statementHandle,
- final Signature signature,
- final int resultSetType,
- final int resultSetConcurrency,
- final int resultSetHoldability)
- throws SQLException {
- return new ArrowFlightPreparedStatement(
- connection,
- preparedStmt,
- statementHandle,
- signature,
- resultSetType,
- resultSetConcurrency,
- resultSetHoldability);
+ static Builder builder(final ArrowFlightConnection connection) {
+ return new Builder(connection);
}
@Override
public ArrowFlightConnection getConnection() throws SQLException {
return (ArrowFlightConnection) super.getConnection();
}
+ ExecuteResult prepareAndExecuteInternal(final PrepareCallback callback) throws SQLException {
+ ensurePrepared();
+ final StatementType statementType = preparedStatement.getType();
+ final long updateCount =
+ statementType.equals(StatementType.UPDATE) ? preparedStatement.executeUpdate() : -1;
+ synchronized (callback.getMonitor()) {
+ callback.clear();
+ callback.assign(handle.signature, null, updateCount);
+ }
+ callback.execute();
+ final MetaResultSet metaResultSet =
+ MetaResultSet.create(handle.connectionId, handle.id, false, handle.signature, null);
+ return new ExecuteResult(Collections.singletonList(metaResultSet));
+ }
+
+ ExecuteResult prepareAndExecuteInternal(
+ final String query,
+ final long maxRowCount,
+ final int maxRowsInFirstFrame,
+ final PrepareCallback callback)
+ throws SQLException {
+
+ return ArrowFlightPreparedStatement.builder(getConnection())
Review Comment:
That works for me. Thanks!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-62: Optimize Flight SQL JDBC Statement execution paths [arrow-java]
Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre commented on PR #1090:
URL: https://github.com/apache/arrow-java/pull/1090#issuecomment-5872078668
@xborder thanks for the update! I will do the review.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-62: Optimize Flight SQL JDBC Statement execution paths [arrow-java]
Posted by "ennuite (via GitHub)" <gi...@apache.org>.
ennuite commented on code in PR #1090:
URL: https://github.com/apache/arrow-java/pull/1090#discussion_r3017184988
##########
flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/example/ArrowFlightJdbcSampleApp.java:
##########
Review Comment:
Do we need/want this sample app? It seems unrelated to the purpose of the PR.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-62: Optimize Flight SQL JDBC Statement execution paths [arrow-java]
Posted by "xborder (via GitHub)" <gi...@apache.org>.
xborder commented on code in PR #1090:
URL: https://github.com/apache/arrow-java/pull/1090#discussion_r3147451015
##########
flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightStatement.java:
##########
@@ -42,20 +49,140 @@ public ArrowFlightConnection getConnection() throws SQLException {
return (ArrowFlightConnection) super.getConnection();
}
+ @Override
+ public ExecuteResult prepareAndExecute(
+ final String query,
+ final long maxRowCount,
+ final int maxRowsInFirstFrame,
+ final PrepareCallback callback)
+ throws SQLException {
+ // Keep Avatica Statement.execute(String) behavior: Avatica calls Meta.prepareAndExecute,
+ // which resolves to this statement hook.
+ this.closeStatement();
+
+ return ArrowFlightPreparedStatement.builder(getConnection())
+ .withQuery(query)
+ .withExistingStatement(this)
+ .build()
+ .prepareAndExecute(callback);
Review Comment:
Good catch. This issue existed before the refactor. I've created #1129 to be addressed separately
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-62: Optimize Flight SQL JDBC Statement execution paths [arrow-java]
Posted by "xborder (via GitHub) via github" <gi...@arrow.apache.org>.
xborder commented on code in PR #1090:
URL: https://github.com/apache/arrow-java/pull/1090#discussion_r4526932008
##########
flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightConnection.java:
##########
@@ -45,7 +52,10 @@ public final class ArrowFlightConnection extends AvaticaConnection {
private final ArrowFlightConnectionConfigImpl config;
private ExecutorService executorService;
private int metadataResultSetCount;
+ // Track result sets and statements owned by this connection so close() can release them.
private Map<Integer, ArrowFlightJdbcFlightStreamResultSet> metadataResultSetMap = new HashMap<>();
+ private final Set<AvaticaStatement> statementOwners =
Review Comment:
All accesses already synchronize on the connection. Just removed a redundant clear that was the only usage not synchronizing.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-62: Optimize Flight SQL JDBC Statement execution paths [arrow-java]
Posted by "jbonofre (via GitHub) via github" <gi...@arrow.apache.org>.
jbonofre merged PR #1090:
URL: https://github.com/apache/arrow-java/pull/1090
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org