From 1ef756dca31d67a9110f3403b89b6d1b028bd07f Mon Sep 17 00:00:00 2001 From: jdpedrie Date: Thu, 17 Aug 2017 14:06:02 -0400 Subject: [PATCH 1/4] Switch to standard SQL by default --- src/BigQuery/BigQueryClient.php | 17 ++++++++++++----- tests/unit/BigQuery/BigQueryClientTest.php | 3 ++- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/BigQuery/BigQueryClient.php b/src/BigQuery/BigQueryClient.php index 598f0f1f66fd..e444b61a9f45 100644 --- a/src/BigQuery/BigQueryClient.php +++ b/src/BigQuery/BigQueryClient.php @@ -114,7 +114,7 @@ public function __construct(array $config = []) * * Queries constructed using * [standard SQL](https://cloud.google.com/bigquery/docs/reference/standard-sql/) - * can take advantage of parametriziation. + * can take advantage of parameterization. * * Refer to the table below for a guide on how parameter types are mapped to * their BigQuery equivalents. @@ -194,8 +194,8 @@ public function __construct(array $config = []) * @type bool $useQueryCache Whether to look for the result in the query * cache. * @type bool $useLegacySql Specifies whether to use BigQuery's legacy - * SQL dialect for this query. **Defaults to** `true`. If set to - * false, the query will use + * SQL dialect for this query. **Defaults to** `false`. Unless set to + * true, the query will use * [BigQuery's standard SQL](https://cloud.google.com/bigquery/sql-reference). * @type array $parameters Only available for standard SQL queries. * When providing a non-associative array positional parameters @@ -209,7 +209,8 @@ public function __construct(array $config = []) public function runQuery($query, array $options = []) { $options += [ - 'maxRetries' => 100 + 'maxRetries' => 100, + 'useLegacySql' => false ]; if (isset($options['parameters'])) { @@ -257,7 +258,7 @@ public function runQuery($query, array $options = []) * * Queries constructed using * [standard SQL](https://cloud.google.com/bigquery/docs/reference/standard-sql/) - * can take advantage of parametriziation. For more details and examples + * can take advantage of parameterization. For more details and examples * please see {@see Google\Cloud\BigQuery\BigQueryClient::runQuery()}. * * Example: @@ -296,6 +297,12 @@ public function runQuery($query, array $options = []) */ public function runQueryAsJob($query, array $options = []) { + $options = array_merge_recursive($options, [ + 'jobConfig' => [ + 'useLegacySql' => false + ] + ]); + if (isset($options['parameters'])) { if (!isset($options['jobConfig'])) { $options['jobConfig'] = []; diff --git a/tests/unit/BigQuery/BigQueryClientTest.php b/tests/unit/BigQuery/BigQueryClientTest.php index 6fddc4bb132f..2a16851e7e9a 100644 --- a/tests/unit/BigQuery/BigQueryClientTest.php +++ b/tests/unit/BigQuery/BigQueryClientTest.php @@ -130,7 +130,8 @@ public function queryDataProvider() [], [ 'projectId' => $this->projectId, - 'query' => $query + 'query' => $query, + 'useLegacySql' => false ] ], [ From 0883c24c00062f84a29321676e505ff7e961996e Mon Sep 17 00:00:00 2001 From: jdpedrie Date: Fri, 18 Aug 2017 13:03:32 -0400 Subject: [PATCH 2/4] Fix setting of useLegacySql option --- src/BigQuery/BigQueryClient.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/BigQuery/BigQueryClient.php b/src/BigQuery/BigQueryClient.php index e444b61a9f45..b699bfa7f9d9 100644 --- a/src/BigQuery/BigQueryClient.php +++ b/src/BigQuery/BigQueryClient.php @@ -291,24 +291,23 @@ public function runQuery($query, array $options = []) * named parameters will be used (`@name`). * @type array $jobConfig Configuration settings for a query job are * outlined in the [API Docs for `configuration.query`](https://goo.gl/PuRa3I). - * If not provided default settings will be used. + * If not provided default settings will be used, with the exception + * of `configuration.query.useLegacySql`, which defaults to `false` + * in this client. * } * @return Job */ public function runQueryAsJob($query, array $options = []) { - $options = array_merge_recursive($options, [ - 'jobConfig' => [ - 'useLegacySql' => false - ] - ]); - if (isset($options['parameters'])) { if (!isset($options['jobConfig'])) { $options['jobConfig'] = []; } - $options['jobConfig'] += $this->formatQueryParameters($options['parameters']); + $options['jobConfig'] += $this->formatQueryParameters($options['parameters']) + [ + 'useLegacySql' => false + ]; + unset($options['parameters']); } From 1436f7a961ea6a0d447b812e52df216b7d381897 Mon Sep 17 00:00:00 2001 From: jdpedrie Date: Fri, 18 Aug 2017 13:28:16 -0400 Subject: [PATCH 3/4] Fix job config logic --- src/BigQuery/BigQueryClient.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/BigQuery/BigQueryClient.php b/src/BigQuery/BigQueryClient.php index b699bfa7f9d9..56fa58fdad0f 100644 --- a/src/BigQuery/BigQueryClient.php +++ b/src/BigQuery/BigQueryClient.php @@ -299,18 +299,20 @@ public function runQuery($query, array $options = []) */ public function runQueryAsJob($query, array $options = []) { - if (isset($options['parameters'])) { - if (!isset($options['jobConfig'])) { - $options['jobConfig'] = []; - } + $options += [ + 'jobConfig' => [] + ]; - $options['jobConfig'] += $this->formatQueryParameters($options['parameters']) + [ - 'useLegacySql' => false - ]; + if (isset($options['parameters'])) { + $options['jobConfig'] += $this->formatQueryParameters($options['parameters']); unset($options['parameters']); } + $options['jobConfig'] += [ + 'useLegacySql' => false + ]; + $config = $this->buildJobConfig( 'query', $this->projectId, From 2a4f57e19c462e551fc1d66347ab42e746c0cb0c Mon Sep 17 00:00:00 2001 From: jdpedrie Date: Fri, 18 Aug 2017 13:48:54 -0400 Subject: [PATCH 4/4] Address code review --- src/BigQuery/BigQueryClient.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/BigQuery/BigQueryClient.php b/src/BigQuery/BigQueryClient.php index 56fa58fdad0f..5a9e3339ec0e 100644 --- a/src/BigQuery/BigQueryClient.php +++ b/src/BigQuery/BigQueryClient.php @@ -138,7 +138,7 @@ public function __construct(array $config = []) * * Example: * ``` - * $queryResults = $bigQuery->runQuery('SELECT commit FROM [bigquery-public-data:github_repos.commits] LIMIT 100'); + * $queryResults = $bigQuery->runQuery('SELECT commit FROM `bigquery-public-data.github_repos.commits` LIMIT 100'); * * foreach ($queryResults->rows() as $row) { * echo $row['commit']; @@ -193,10 +193,10 @@ public function __construct(array $config = []) * query has completed. **Defaults to** `100`. * @type bool $useQueryCache Whether to look for the result in the query * cache. - * @type bool $useLegacySql Specifies whether to use BigQuery's legacy - * SQL dialect for this query. **Defaults to** `false`. Unless set to - * true, the query will use - * [BigQuery's standard SQL](https://cloud.google.com/bigquery/sql-reference). + * @type bool $useLegacySql If set to true the query will use + * [BigQuery's legacy SQL](https://cloud.google.com/bigquery/docs/reference/legacy-sql), + * otherwise [BigQuery's standard SQL](https://cloud.google.com/bigquery/sql-reference). + * **Defaults to** `false`. * @type array $parameters Only available for standard SQL queries. * When providing a non-associative array positional parameters * (`?`) will be used. When providing an associative array @@ -263,7 +263,7 @@ public function runQuery($query, array $options = []) * * Example: * ``` - * $job = $bigQuery->runQueryAsJob('SELECT commit FROM [bigquery-public-data:github_repos.commits] LIMIT 100'); + * $job = $bigQuery->runQueryAsJob('SELECT commit FROM `bigquery-public-data.github_repos.commits` LIMIT 100'); * * $isComplete = false; * $queryResults = $job->queryResults();