@@ -192,11 +192,14 @@ private FlightInfo executeQueryStatement(String peerIdentity, ConnectContext con
192192 final FlightDescriptor descriptor ) {
193193 // Schema discovery temporarily installs session state; execution must not mutate that clone.
194194 synchronized (connectContext ) {
195- return executeQueryStatementLocked (peerIdentity , connectContext , query , descriptor );
195+ return executeQueryStatementLocked (peerIdentity , connectContext , query , descriptor ). getLeft () ;
196196 }
197197 }
198198
199- private FlightInfo executeQueryStatementLocked (String peerIdentity , ConnectContext connectContext , String query ,
199+ // Processor.close() clears the context's executor before returning. Retain the final executor
200+ // with its result so prepared-schema validation and cancellation use the query that actually ran.
201+ private Pair <FlightInfo , StmtExecutor > executeQueryStatementLocked (String peerIdentity ,
202+ ConnectContext connectContext , String query ,
200203 final FlightDescriptor descriptor ) {
201204 try {
202205 Preconditions .checkState (null != connectContext );
@@ -224,9 +227,9 @@ private FlightInfo executeQueryStatementLocked(String peerIdentity, ConnectConte
224227 final ByteString handle = ByteString .copyFromUtf8 (peerIdentity + ":" + queryId );
225228 TicketStatementQuery ticketStatement = TicketStatementQuery .newBuilder ()
226229 .setStatementHandle (handle ).build ();
227- return getFlightInfoForSchema (ticketStatement , descriptor ,
230+ return Pair . of ( getFlightInfoForSchema (ticketStatement , descriptor ,
228231 connectContext .getFlightSqlChannel ().getResult (queryId ).getVectorSchemaRoot ()
229- .getSchema ());
232+ .getSchema ()), connectContext . getExecutor ()) ;
230233 } else {
231234 // A Flight Sql request can only contain one statement that returns result,
232235 // otherwise expected thrown exception during execution.
@@ -240,9 +243,10 @@ private FlightInfo executeQueryStatementLocked(String peerIdentity, ConnectConte
240243 peerIdentity + ":" + DebugUtil .printId (connectContext .queryId ()));
241244 TicketStatementQuery ticketStatement = TicketStatementQuery .newBuilder ()
242245 .setStatementHandle (handle ).build ();
243- return getFlightInfoForSchema (ticketStatement , descriptor , connectContext .getFlightSqlChannel ()
244- .getResult (DebugUtil .printId (connectContext .queryId ())).getVectorSchemaRoot ()
245- .getSchema ());
246+ return Pair .of (getFlightInfoForSchema (ticketStatement , descriptor ,
247+ connectContext .getFlightSqlChannel ()
248+ .getResult (DebugUtil .printId (connectContext .queryId ())).getVectorSchemaRoot ()
249+ .getSchema ()), connectContext .getExecutor ());
246250 }
247251 } else {
248252 // Now only query stmt will pull results from BE.
@@ -292,7 +296,8 @@ private FlightInfo executeQueryStatementLocked(String peerIdentity, ConnectConte
292296 endpoints .add (new FlightEndpoint (ticket , location ));
293297 }
294298 // TODO Set in BE callback after query end, Client will not callback.
295- return new FlightInfo (flightSQLConnectProcessor .getArrowSchema (), descriptor , endpoints , -1 , -1 );
299+ return Pair .of (new FlightInfo (flightSQLConnectProcessor .getArrowSchema (), descriptor ,
300+ endpoints , -1 , -1 ), connectContext .getExecutor ());
296301 }
297302 }
298303 } catch (Throwable e ) {
@@ -333,12 +338,14 @@ public FlightInfo getFlightInfoPreparedStatement(final CommandPreparedStatementQ
333338 ConnectContext connection = flightSessionsManager .getConnectContext (context .peerIdentity ());
334339 synchronized (connection ) {
335340 Pair <String , Schema > prepared = preparedQuery (connection , context , command );
336- FlightInfo info = executeQueryStatement (context .peerIdentity (), connection , prepared .getLeft (), descriptor );
341+ Pair <FlightInfo , StmtExecutor > result = executeQueryStatementLocked (
342+ context .peerIdentity (), connection , prepared .getLeft (), descriptor );
343+ FlightInfo info = result .getLeft ();
337344 String id = command .getPreparedStatementHandle ().toStringUtf8 ()
338345 .substring (context .peerIdentity ().length () + 1 );
339346 // Another session's DDL can change the result after reanalysis but before execution
340347 // acquires table locks. Reject the actual schema before publishing a DoGet ticket.
341- StmtExecutor executor = connection . getExecutor ();
348+ StmtExecutor executor = result . getRight ();
342349 boolean matches = !connection .isReturnResultFromLocal () && executor != null
343350 && executor .getParsedStmt () instanceof LogicalPlanAdapter
344351 ? FlightSqlQuerySchema .matchesExecutionSchema (prepared .getRight (), info .getSchema (),
@@ -347,7 +354,9 @@ public FlightInfo getFlightInfoPreparedStatement(final CommandPreparedStatementQ
347354 if (!matches ) {
348355 connection .removePreparedQuery (id );
349356 try {
350- connection .cancelQuery (Status .CANCELLED );
357+ if (executor != null ) {
358+ executor .cancel (Status .CANCELLED );
359+ }
351360 } catch (Exception e ) {
352361 LOG .warn ("Failed to cancel Flight query after prepared schema changed" , e );
353362 } finally {
0 commit comments