Skip to content

Commit d0260d3

Browse files
committed
Fix the pagination bug for stateless queries
1 parent 493b705 commit d0260d3

2 files changed

Lines changed: 53 additions & 2 deletions

File tree

BigQuery/src/BigQueryClient.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,13 +438,14 @@ public function runQuery(JobConfigurationInterface $query, array $options = [])
438438
$response = $this->connection->query($statelessArgs);
439439

440440
if ($response['jobComplete'] ?? false) {
441+
$jobId = $response['jobReference']['jobId'] ?? '';
441442
return new QueryResults(
442443
$this->connection,
443-
'',
444+
$jobId,
444445
$this->projectId,
445446
$response,
446447
$this->mapper,
447-
$this->createJob([], ''), // create an empty job
448+
$this->createJob($response, $jobId),
448449
$queryResultsOptions
449450
);
450451
}

BigQuery/tests/Unit/BigQueryClientTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,56 @@ public function testRunQueryStateless()
182182
$this->assertTrue($queryResults->isComplete());
183183
}
184184

185+
public function testRunQueryStatelessWithPagination()
186+
{
187+
$client = $this->getClient();
188+
$query = $client->query(self::QUERY_STRING);
189+
190+
$this->connection->query(Argument::allOf(
191+
Argument::withEntry('projectId', self::PROJECT_ID),
192+
Argument::withEntry('query', self::QUERY_STRING),
193+
Argument::withEntry('jobCreationMode', 'JOB_CREATION_OPTIONAL')
194+
))
195+
->willReturn([
196+
'jobComplete' => true,
197+
'jobReference' => [
198+
'jobId' => self::JOB_ID,
199+
'projectId' => self::PROJECT_ID
200+
],
201+
'schema' => [
202+
'fields' => [
203+
['name' => 'col1', 'type' => 'STRING']
204+
]
205+
],
206+
'rows' => [
207+
['f' => [['v' => 'val1']]]
208+
],
209+
'pageToken' => 'next-page-token'
210+
])
211+
->shouldBeCalledTimes(1);
212+
213+
$this->connection->getQueryResults(Argument::allOf(
214+
Argument::withEntry('projectId', self::PROJECT_ID),
215+
Argument::withEntry('jobId', self::JOB_ID),
216+
Argument::withEntry('pageToken', 'next-page-token')
217+
))
218+
->willReturn([
219+
'jobComplete' => true,
220+
'rows' => [
221+
['f' => [['v' => 'val2']]]
222+
]
223+
])
224+
->shouldBeCalledTimes(1);
225+
226+
$client->___setProperty('connection', $this->connection->reveal());
227+
$results = $client->runQuery($query);
228+
$rows = iterator_to_array($results);
229+
230+
$this->assertCount(2, $rows);
231+
$this->assertEquals('val1', $rows[0]['col1']);
232+
$this->assertEquals('val2', $rows[1]['col1']);
233+
}
234+
185235
public function testRunQueryJobQueryEndpointReturnsAJob()
186236
{
187237
$client = $this->getClient();

0 commit comments

Comments
 (0)