@@ -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 ();
0 commit comments