Skip to content

Commit 7d20875

Browse files
committed
[fix](flight-sql) Preserve invalid SQL status during preparation
1 parent 813d97e commit 7d20875

3 files changed

Lines changed: 43 additions & 14 deletions

File tree

‎fe/fe-core/src/main/java/org/apache/doris/service/arrowflight/DorisFlightSqlProducer.java‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -412,14 +412,8 @@ public SchemaResult getSchemaPreparedStatement(final CommandPreparedStatementQue
412412
String query = connection.getPreparedQuery(id);
413413
if (query != null && connection.getPreparedQueryParameterCount(id) > 0
414414
&& connection.getPreparedQueryParameters(id) == null) {
415-
try {
416-
// Metadata discovery does not execute a query and must work before values are bound.
417-
return new SchemaResult(FlightSqlQuerySchema.prepare(connection, query).first);
418-
} catch (FlightRuntimeException e) {
419-
throw e;
420-
} catch (Exception e) {
421-
throw CallStatus.INVALID_ARGUMENT.withDescription(e.getMessage()).withCause(e).toRuntimeException();
422-
}
415+
// Metadata discovery does not execute a query and must work before values are bound.
416+
return new SchemaResult(prepareQuerySchema(connection, query).first);
423417
}
424418
return new SchemaResult(preparedQuery(connection, context, command).getRight());
425419
}
@@ -469,6 +463,18 @@ private Schema analyzeQuerySchema(ConnectContext context, String query, List<Lit
469463
}
470464
}
471465

466+
private org.apache.doris.common.Pair<Schema, Schema> prepareQuerySchema(ConnectContext context, String query) {
467+
try {
468+
return FlightSqlQuerySchema.prepare(context, query);
469+
} catch (FlightRuntimeException e) {
470+
throw e;
471+
} catch (Exception e) {
472+
// Invalid SQL must report the same client error through Prepare and GetSchema.
473+
throw CallStatus.INVALID_ARGUMENT.withDescription("Cannot determine query schema: " + e.getMessage())
474+
.withCause(e).toRuntimeException();
475+
}
476+
}
477+
472478
@Override
473479
public void close() throws Exception {
474480
AutoCloseables.close(rootAllocator);
@@ -503,7 +509,7 @@ public void createPreparedStatement(final ActionCreatePreparedStatementRequest r
503509
String query = request.getQuery();
504510
// ADBC ExecuteSchema reads this dataset schema directly without calling GetSchema.
505511
// Analyze before registering a handle so failed preparation does not retain a query.
506-
org.apache.doris.common.Pair<Schema, Schema> prepared = FlightSqlQuerySchema.prepare(
512+
org.apache.doris.common.Pair<Schema, Schema> prepared = prepareQuerySchema(
507513
connectContext, query);
508514
Schema schema = prepared.first;
509515
preparedStatementId = UUID.randomUUID().toString();

‎fe/fe-core/src/test/java/org/apache/doris/service/arrowflight/DorisFlightSqlSchemaTest.java‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,20 @@ void getSchemaAndPrepareAgree() throws Exception {
182182

183183
@Test
184184
void invalidSqlIsRejectedAndConnectionRecovers() throws Exception {
185-
for (String sql : Arrays.asList("SELEC 1", "SELECT missing_column FROM schema_input")) {
186-
Assertions.assertThrows(FlightRuntimeException.class, () -> schema(sql));
185+
for (String sql : Arrays.asList("SELEC 1", "SELECT missing_column FROM schema_input",
186+
"SELECT CAST(? AS BIGINT) FROM missing_schema_table")) {
187+
FlightRuntimeException schemaFailure = Assertions.assertThrows(
188+
FlightRuntimeException.class, () -> schema(sql));
189+
Assertions.assertEquals(FlightStatusCode.INVALID_ARGUMENT, schemaFailure.status().code());
187190
Mockito.clearInvocations(connectContext);
188-
Assertions.assertThrows(java.util.concurrent.ExecutionException.class, () -> prepare(sql));
191+
java.util.concurrent.ExecutionException prepareFailure = Assertions.assertThrows(
192+
java.util.concurrent.ExecutionException.class, () -> prepare(sql));
193+
Assertions.assertTrue(prepareFailure.getCause() instanceof FlightRuntimeException);
194+
Assertions.assertEquals(FlightStatusCode.INVALID_ARGUMENT,
195+
((FlightRuntimeException) prepareFailure.getCause()).status().code());
189196
Mockito.verify(connectContext, Mockito.never()).addPreparedQuery(Mockito.anyString(), Mockito.anyString(), Mockito.any());
197+
Mockito.verify(connectContext, Mockito.never()).addPreparedQuery(
198+
Mockito.anyString(), Mockito.anyString(), Mockito.any(), Mockito.anyInt());
190199
Assertions.assertEquals("id", preparedSchema("SELECT id FROM schema_input")
191200
.getFields().get(0).getName());
192201
}

‎fe/fe-core/src/test/java/org/apache/doris/service/arrowflight/FlightSqlPreparedQueryTest.java‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,22 @@ public void unknownParameterTypesAreNotInvented() throws Exception {
208208
Assertions.assertEquals(new ArrowType.Null(),
209209
statement.getResultSetSchema().getFields().get(0).getType());
210210
}
211-
Assertions.assertThrows(FlightRuntimeException.class,
212-
() -> sqlClient.prepare("SELECT CAST(? AS BIGINT) FROM missing_parameter_table"));
211+
}
212+
213+
@Test
214+
public void invalidSqlReportsClientErrorOverFlight() throws Exception {
215+
for (String query : Arrays.asList("SELEC 1", "SELECT definitely_missing_schema_column",
216+
"SELECT CAST(? AS BIGINT) FROM missing_parameter_table")) {
217+
FlightRuntimeException prepareFailure = Assertions.assertThrows(FlightRuntimeException.class,
218+
() -> sqlClient.prepare(query));
219+
Assertions.assertEquals(FlightStatusCode.INVALID_ARGUMENT, prepareFailure.status().code());
220+
FlightRuntimeException schemaFailure = Assertions.assertThrows(FlightRuntimeException.class,
221+
() -> sqlClient.getExecuteSchema(query));
222+
Assertions.assertEquals(FlightStatusCode.INVALID_ARGUMENT, schemaFailure.status().code());
223+
try (FlightSqlClient.PreparedStatement statement = sqlClient.prepare("SELECT 1 AS id")) {
224+
Assertions.assertEquals("id", statement.getResultSetSchema().getFields().get(0).getName());
225+
}
226+
}
213227
}
214228

215229
@Test

0 commit comments

Comments
 (0)