@@ -3410,20 +3410,166 @@ void testQueryWithArrowFormatIncompleteJob() throws Exception {
34103410 .setJobComplete (false )
34113411 .setJobReference (queryJob .toPb ());
34123412
3413+ com .google .api .services .bigquery .model .JobStatus jobStatus =
3414+ new com .google .api .services .bigquery .model .JobStatus ().setState ("DONE" );
3415+
3416+ com .google .api .services .bigquery .model .Job jobResponsePb =
3417+ new com .google .api .services .bigquery .model .Job ()
3418+ .setJobReference (queryJob .toPb ())
3419+ .setStatus (jobStatus )
3420+ .setConfiguration (
3421+ new com .google .api .services .bigquery .model .JobConfiguration ()
3422+ .setQuery (
3423+ new JobConfigurationQuery ()
3424+ .setQuery ("SELECT id FROM test" )
3425+ .setDestinationTable (TABLE_ID .toPb ())))
3426+ .setStatistics (
3427+ new com .google .api .services .bigquery .model .JobStatistics ()
3428+ .setTotalSlotMs (50L )
3429+ .setQuery (
3430+ new com .google .api .services .bigquery .model .JobStatistics2 ()
3431+ .setStatementType ("SELECT" )
3432+ .setTotalBytesBilled (100L )
3433+ .setTotalBytesProcessed (200L )));
3434+
34133435 when (bigqueryRpcMock .queryRpcSkipExceptionTranslation (eq (PROJECT ), any (QueryRequest .class )))
34143436 .thenReturn (queryResponsePb );
3437+ when (bigqueryRpcMock .getJobSkipExceptionTranslation (eq (PROJECT ), eq (JOB ), any (), any ()))
3438+ .thenReturn (jobResponsePb );
3439+ when (bigqueryRpcMock .getQueryResultsSkipExceptionTranslation (
3440+ eq (PROJECT ), eq (JOB ), any (), any ()))
3441+ .thenReturn (
3442+ new GetQueryResultsResponse ().setJobComplete (true ).setJobReference (queryJob .toPb ()));
3443+
3444+ org .apache .arrow .vector .types .pojo .Schema arrowSchema =
3445+ new org .apache .arrow .vector .types .pojo .Schema (
3446+ ImmutableList .of (
3447+ org .apache .arrow .vector .types .pojo .Field .nullable (
3448+ "id" , new ArrowType .Int (64 , true ))));
3449+
3450+ byte [] schemaBytes ;
3451+ try (ByteArrayOutputStream out = new ByteArrayOutputStream ()) {
3452+ MessageSerializer .serialize (new WriteChannel (Channels .newChannel (out )), arrowSchema );
3453+ schemaBytes = out .toByteArray ();
3454+ }
3455+
3456+ byte [] batchBytes ;
3457+ try (BufferAllocator allocator = new RootAllocator (Long .MAX_VALUE )) {
3458+ BigIntVector idVector = new BigIntVector ("id" , allocator );
3459+ idVector .allocateNew (1 );
3460+ idVector .set (0 , 42L );
3461+ idVector .setValueCount (1 );
3462+ try (VectorSchemaRoot root = new VectorSchemaRoot (ImmutableList .of (idVector ))) {
3463+ VectorUnloader unloader = new VectorUnloader (root );
3464+ try (ArrowRecordBatch recordBatch = unloader .getRecordBatch ();
3465+ ByteArrayOutputStream out = new ByteArrayOutputStream ()) {
3466+ WriteChannel channel = new WriteChannel (Channels .newChannel (out ));
3467+ MessageSerializer .serialize (channel , recordBatch );
3468+ batchBytes = out .toByteArray ();
3469+ }
3470+ } finally {
3471+ idVector .close ();
3472+ }
3473+ }
3474+
3475+ com .google .cloud .bigquery .storage .v1 .ArrowRecordBatch protoBatch =
3476+ com .google .cloud .bigquery .storage .v1 .ArrowRecordBatch .newBuilder ()
3477+ .setSerializedRecordBatch (ByteString .copyFrom (batchBytes ))
3478+ .build ();
3479+ ReadRowsResponse streamResponse =
3480+ ReadRowsResponse .newBuilder ().setArrowRecordBatch (protoBatch ).build ();
3481+
3482+ @ SuppressWarnings ("unchecked" )
3483+ ServerStreamingCallable <ReadRowsRequest , ReadRowsResponse > mockCallable =
3484+ mock (ServerStreamingCallable .class , withSettings ().withoutAnnotations ());
3485+ @ SuppressWarnings ("unchecked" )
3486+ ServerStream <ReadRowsResponse > mockServerStream =
3487+ mock (ServerStream .class , withSettings ().withoutAnnotations ());
3488+ when (mockCallable .call (any (ReadRowsRequest .class ))).thenReturn (mockServerStream );
3489+ when (mockServerStream .iterator ()).thenReturn (ImmutableList .of (streamResponse ).iterator ());
3490+
3491+ ReadSession readSession =
3492+ ReadSession .newBuilder ()
3493+ .setName ("projects/" + PROJECT + "/locations/" + LOCATION + "/sessions/session-1" )
3494+ .setArrowSchema (
3495+ com .google .cloud .bigquery .storage .v1 .ArrowSchema .newBuilder ()
3496+ .setSerializedSchema (ByteString .copyFrom (schemaBytes )))
3497+ .addStreams (ReadStream .newBuilder ().setName ("stream-1" ))
3498+ .build ();
3499+
3500+ BigQueryReadClient mockReadClient =
3501+ mock (BigQueryReadClient .class , withSettings ().withoutAnnotations ());
3502+ when (mockReadClient .createReadSession (any (CreateReadSessionRequest .class )))
3503+ .thenReturn (readSession );
3504+ when (mockReadClient .readRowsCallable ()).thenReturn (mockCallable );
34153505
34163506 bigquery = options .getService ();
3507+ ((BigQueryImpl ) bigquery ).setBigQueryReadClient (mockReadClient );
34173508
34183509 QueryJobConfiguration config =
34193510 QueryJobConfiguration .newBuilder ("SELECT id FROM test" )
34203511 .setQueryResultsFormat (QueryResultsFormat .ARROW )
34213512 .build ();
3422- UnsupportedOperationException e =
3423- assertThrows (UnsupportedOperationException .class , () -> bigquery .query (config ));
3513+
3514+ TableResult result = bigquery .query (config );
3515+
3516+ assertNotNull (result );
3517+ assertEquals (1 , Iterables .size (result .iterateAll ()));
3518+ FieldValueList row = result .iterateAll ().iterator ().next ();
3519+ assertEquals (42L , row .get ("id" ).getLongValue ());
3520+ assertEquals (50L , result .getTotalSlotMs ().longValue ());
3521+ assertEquals (100L , result .getTotalBytesBilled ().longValue ());
3522+ assertEquals (200L , result .getTotalBytesProcessed ().longValue ());
3523+ assertEquals (StatementType .SELECT , result .getStatementType ());
3524+ }
3525+
3526+ @ Test
3527+ void testQueryWithArrowFormatIncompleteJobMissingJobReference () throws Exception {
3528+ com .google .api .services .bigquery .model .QueryResponse queryResponsePb =
3529+ new com .google .api .services .bigquery .model .QueryResponse ()
3530+ .setQueryId ("q-arrow-incomplete-no-job" )
3531+ .setJobComplete (false );
3532+
3533+ when (bigqueryRpcMock .queryRpcSkipExceptionTranslation (eq (PROJECT ), any (QueryRequest .class )))
3534+ .thenReturn (queryResponsePb );
3535+
3536+ bigquery = options .getService ();
3537+
3538+ QueryJobConfiguration config =
3539+ QueryJobConfiguration .newBuilder ("SELECT id FROM test" )
3540+ .setQueryResultsFormat (QueryResultsFormat .ARROW )
3541+ .build ();
3542+
3543+ BigQueryException e = assertThrows (BigQueryException .class , () -> bigquery .query (config ));
34243544 assertTrue (
34253545 e .getMessage ()
3426- .contains ("Arrow results format for slow query path execution is not yet supported." ));
3546+ .contains (
3547+ "Query is incomplete, but no job reference was returned to await completion." ));
3548+ }
3549+
3550+ @ Test
3551+ void testQueryWithArrowFormatIncompleteJobJobNotFound () throws Exception {
3552+ JobId queryJob = JobId .of (PROJECT , JOB ).toBuilder ().setLocation (LOCATION ).build ();
3553+ com .google .api .services .bigquery .model .QueryResponse queryResponsePb =
3554+ new com .google .api .services .bigquery .model .QueryResponse ()
3555+ .setQueryId ("q-arrow-incomplete" )
3556+ .setJobComplete (false )
3557+ .setJobReference (queryJob .toPb ());
3558+
3559+ when (bigqueryRpcMock .queryRpcSkipExceptionTranslation (eq (PROJECT ), any (QueryRequest .class )))
3560+ .thenReturn (queryResponsePb );
3561+ when (bigqueryRpcMock .getJobSkipExceptionTranslation (eq (PROJECT ), eq (JOB ), any (), any ()))
3562+ .thenThrow (new BigQueryException (404 , "Not Found" ));
3563+
3564+ bigquery = options .getService ();
3565+
3566+ QueryJobConfiguration config =
3567+ QueryJobConfiguration .newBuilder ("SELECT id FROM test" )
3568+ .setQueryResultsFormat (QueryResultsFormat .ARROW )
3569+ .build ();
3570+
3571+ BigQueryException e = assertThrows (BigQueryException .class , () -> bigquery .query (config ));
3572+ assertTrue (e .getMessage ().contains ("Job no longer exists or could not be retrieved" ));
34273573 }
34283574
34293575 @ Test
0 commit comments