From 6dcdb5d36319dbf90b245ad4b9376bf6e807345d Mon Sep 17 00:00:00 2001 From: Daniel Sims Date: Sun, 5 May 2019 17:57:51 -0400 Subject: [PATCH 1/2] Adds an option to return raw results from BigQuery --- BigQuery/src/QueryResults.php | 10 +++++-- BigQuery/tests/Unit/QueryResultsTest.php | 35 ++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/BigQuery/src/QueryResults.php b/BigQuery/src/QueryResults.php index b41b6f50b01c..8b9d844d4355 100644 --- a/BigQuery/src/QueryResults.php +++ b/BigQuery/src/QueryResults.php @@ -146,6 +146,9 @@ public function __construct( * **Defaults to** `10000` milliseconds (10 seconds). * @type int $maxRetries The number of times to poll the Job status, * until the job is complete. By default, will poll indefinitely. + * @type bool $returnRawResults Returns the raw data types returned from + * BigQuery without converting their values into native PHP types or + * the custom type classes supported by this library. * } * @return ItemIterator * @throws JobException If the maximum number of retries while waiting for @@ -157,10 +160,11 @@ public function rows(array $options = []) $options += $this->queryResultsOptions; $this->waitUntilComplete($options); $schema = $this->info['schema']['fields']; + $returnRawResults = isset($options['returnRawResults']) ? $options['returnRawResults'] : false; return new ItemIterator( new PageIterator( - function (array $row) use ($schema) { + function (array $row) use ($schema, $returnRawResults) { $mergedRow = []; if ($row === null) { @@ -173,7 +177,9 @@ function (array $row) use ($schema) { foreach ($row['f'] as $key => $value) { $fieldSchema = $schema[$key]; - $mergedRow[$fieldSchema['name']] = $this->mapper->fromBigQuery($value, $fieldSchema); + $mergedRow[$fieldSchema['name']] = $returnRawResults + ? $value['v'] + : $this->mapper->fromBigQuery($value, $fieldSchema); } return $mergedRow; diff --git a/BigQuery/tests/Unit/QueryResultsTest.php b/BigQuery/tests/Unit/QueryResultsTest.php index 5c944170a7d6..9f0a82d607fe 100644 --- a/BigQuery/tests/Unit/QueryResultsTest.php +++ b/BigQuery/tests/Unit/QueryResultsTest.php @@ -19,6 +19,7 @@ use Google\Cloud\BigQuery\Connection\ConnectionInterface; use Google\Cloud\BigQuery\Job; +use Google\Cloud\BigQuery\Numeric; use Google\Cloud\BigQuery\QueryResults; use Google\Cloud\BigQuery\ValueMapper; use Prophecy\Argument; @@ -35,14 +36,18 @@ class QueryResultsTest extends TestCase public $queryData = [ 'jobComplete' => true, 'rows' => [ - ['f' => [['v' => 'Alton']]] + ['f' => [['v' => 'Alton'], ['v' => 1]]] ], 'schema' => [ 'fields' => [ [ 'name' => 'first_name', 'type' => 'STRING' - ] + ], + [ + 'name' => 'numeric_value', + 'type' => 'NUMERIC' + ], ] ] ]; @@ -81,6 +86,7 @@ public function testGetsRowsWithoutToken() $rows = iterator_to_array($queryResults->rows()); $this->assertEquals('Alton', $rows[0]['first_name']); + $this->assertInstanceOf(Numeric::class, $rows[0]['numeric_value']); } public function testGetsRowsWithToken() @@ -95,6 +101,31 @@ public function testGetsRowsWithToken() $rows = iterator_to_array($queryResults->rows()); $this->assertEquals('Alton', $rows[1]['first_name']); + $this->assertInstanceOf(Numeric::class, $rows[1]['numeric_value']); + } + + public function testReturnRawResults() + { + $this->connection->getQueryResults()->shouldNotBeCalled(); + $queryResults = $this->getQueryResults($this->connection, $this->queryData); + $rows = iterator_to_array($queryResults->rows([ + 'returnRawResults' => true + ])); + + $this->assertEquals('Alton', $rows[0]['first_name']); + $this->assertEquals(1, $rows[0]['numeric_value']); + } + + public function testReturnRawResultsIsFalse() + { + $this->connection->getQueryResults()->shouldNotBeCalled(); + $queryResults = $this->getQueryResults($this->connection, $this->queryData); + $rows = iterator_to_array($queryResults->rows([ + 'returnRawResults' => false + ])); + + $this->assertEquals('Alton', $rows[0]['first_name']); + $this->assertInstanceOf(Numeric::class, $rows[0]['numeric_value']); } public function testWaitsUntilComplete() From 918bc11a4b141d52074c8ca6bd7d2bd07eaf00b0 Mon Sep 17 00:00:00 2001 From: Daniel Sims Date: Wed, 22 May 2019 21:05:06 -0400 Subject: [PATCH 2/2] Adds returnRawResults option to BigQueryClient::runQuery method --- BigQuery/src/BigQueryClient.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/BigQuery/src/BigQueryClient.php b/BigQuery/src/BigQueryClient.php index 22840b1d38a5..4b0560eca212 100644 --- a/BigQuery/src/BigQueryClient.php +++ b/BigQuery/src/BigQueryClient.php @@ -316,6 +316,9 @@ public function queryConfig($query, array $options = []) * **Defaults to** `10000` milliseconds (10 seconds). * @type int $maxRetries The number of times to poll the Job status, * until the job is complete. By default, will poll indefinitely. + * @type bool $returnRawResults Returns the raw data types returned from + * BigQuery without converting their values into native PHP types or + * the custom type classes supported by this library. * } * @return QueryResults * @throws JobException If the maximum number of retries while waiting for @@ -327,7 +330,8 @@ public function runQuery(JobConfigurationInterface $query, array $options = []) 'maxResults', 'startIndex', 'timeoutMs', - 'maxRetries' + 'maxRetries', + 'returnRawResults', ], $options); $queryResultsOptions['initialTimeoutMs'] = 10000;