Skip to content

Commit 60bf987

Browse files
tswastTakashi Matsuo
authored andcommitted
Add option to use standard SQL syntax to BigQuery sample. (GoogleCloudPlatform#152)
1 parent 032524d commit 60bf987

File tree

4 files changed

+89
-9
lines changed

4 files changed

+89
-9
lines changed

bigquery/api/src/QueryCommand.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ protected function configure()
6565
InputOption::VALUE_NONE,
6666
'run the query syncronously'
6767
)
68+
->addOption(
69+
'standardSql',
70+
null,
71+
InputOption::VALUE_NONE,
72+
'run the query using standard SQL instead of legacy SQL syntax'
73+
)
6874
;
6975
}
7076

@@ -87,9 +93,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
8793

8894
try {
8995
if ($input->getOption('sync')) {
90-
run_query($projectId, $query);
96+
run_query(
97+
$projectId,
98+
$query,
99+
!$input->getOption('standardSql'));
91100
} else {
92-
run_query_as_job($projectId, $query);
101+
run_query_as_job(
102+
$projectId,
103+
$query,
104+
!$input->getOption('standardSql'));
93105
}
94106
} catch (BadRequestException $e) {
95107
$response = $e->getServiceException()->getResponse();

bigquery/api/src/functions/run_query.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@
3232
* ```
3333
* $query = 'SELECT TOP(corpus, 10) as title, COUNT(*) as unique_words ' .
3434
* 'FROM [publicdata:samples.shakespeare]';
35-
* run_query($projectId, $query);
35+
* run_query($projectId, $query, true);
3636
* ```.
3737
*
3838
* @param string $projectId The Google project ID.
3939
* @param string $query A SQL query to run.
40+
* @param bool $useLegacySql Specifies whether to use BigQuery's legacy SQL
41+
* syntax or standard SQL syntax for this query.
4042
*/
41-
function run_query($projectId, $query)
43+
function run_query($projectId, $query, $useLegacySql)
4244
{
4345
# [START build_service]
4446
$builder = new ServiceBuilder([
@@ -47,7 +49,9 @@ function run_query($projectId, $query)
4749
$bigQuery = $builder->bigQuery();
4850
# [END build_service]
4951
# [START run_query]
50-
$queryResults = $bigQuery->runQuery($query);
52+
$queryResults = $bigQuery->runQuery(
53+
$query,
54+
['useLegacySql' => $useLegacySql]);
5155
# [END run_query]
5256

5357
# [START print_results]

bigquery/api/src/functions/run_query_as_job.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,23 @@
3434
* ```
3535
* $query = 'SELECT TOP(corpus, 10) as title, COUNT(*) as unique_words ' .
3636
* 'FROM [publicdata:samples.shakespeare]';
37-
* run_query_as_job($projectId, $query);
37+
* run_query_as_job($projectId, $query, true);
3838
* ```.
3939
*
4040
* @param string $projectId The Google project ID.
41-
* @param string $query A SQL query to run.
41+
* @param string $query A SQL query to run. *
42+
* @param bool $useLegacySql Specifies whether to use BigQuery's legacy SQL
43+
* syntax or standard SQL syntax for this query.
4244
*/
43-
function run_query_as_job($projectId, $query)
45+
function run_query_as_job($projectId, $query, $useLegacySql)
4446
{
4547
$builder = new ServiceBuilder([
4648
'projectId' => $projectId,
4749
]);
4850
$bigQuery = $builder->bigQuery();
49-
$job = $bigQuery->runQueryAsJob($query);
51+
$job = $bigQuery->runQueryAsJob(
52+
$query,
53+
['jobConfig' => ['useLegacySql' => $useLegacySql]]);
5054
$backoff = new ExponentialBackoff(10);
5155
$backoff->execute(function () use ($job) {
5256
print('Waiting for job to complete' . PHP_EOL);

bigquery/api/test/QueryCommandTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,38 @@ public function testQuery()
105105
$this->expectOutputRegex('/Found 1 row\(s\)/');
106106
}
107107

108+
public function testQueryStandardSql()
109+
{
110+
if (!self::$hasCredentials) {
111+
$this->markTestSkipped('No application credentials were found.');
112+
}
113+
if (!$projectId = getenv('GOOGLE_PROJECT_ID')) {
114+
$this->markTestSkipped('No project ID');
115+
}
116+
if (!$datasetId = getenv('GOOGLE_BIGQUERY_DATASET')) {
117+
$this->markTestSkipped('No bigquery dataset name');
118+
}
119+
if (!$tableId = getenv('GOOGLE_BIGQUERY_TABLE')) {
120+
$this->markTestSkipped('No bigquery table name');
121+
}
122+
123+
$query = sprintf('SELECT * FROM `%s.%s` LIMIT 1', $datasetId, $tableId);
124+
125+
$application = new Application();
126+
$application->add(new QueryCommand());
127+
$commandTester = new CommandTester($application->get('query'));
128+
$commandTester->execute(
129+
[
130+
'query' => $query,
131+
'--project' => $projectId,
132+
'--sync',
133+
'--standardSql'],
134+
['interactive' => false]
135+
);
136+
137+
$this->expectOutputRegex('/Found 1 row\(s\)/');
138+
}
139+
108140
public function testQueryAsJob()
109141
{
110142
if (!self::$hasCredentials) {
@@ -132,4 +164,32 @@ public function testQueryAsJob()
132164

133165
$this->expectOutputRegex('/Found 1 row\(s\)/');
134166
}
167+
168+
public function testQueryAsJobStandardSql()
169+
{
170+
if (!self::$hasCredentials) {
171+
$this->markTestSkipped('No application credentials were found.');
172+
}
173+
if (!$projectId = getenv('GOOGLE_PROJECT_ID')) {
174+
$this->markTestSkipped('No project ID');
175+
}
176+
if (!$datasetId = getenv('GOOGLE_BIGQUERY_DATASET')) {
177+
$this->markTestSkipped('No bigquery dataset name');
178+
}
179+
if (!$tableId = getenv('GOOGLE_BIGQUERY_TABLE')) {
180+
$this->markTestSkipped('No bigquery table name');
181+
}
182+
183+
$query = sprintf('SELECT * FROM `%s.%s` LIMIT 1', $datasetId, $tableId);
184+
185+
$application = new Application();
186+
$application->add(new QueryCommand());
187+
$commandTester = new CommandTester($application->get('query'));
188+
$commandTester->execute(
189+
['query' => $query, '--project' => $projectId, '--standardSql'],
190+
['interactive' => false]
191+
);
192+
193+
$this->expectOutputRegex('/Found 1 row\(s\)/');
194+
}
135195
}

0 commit comments

Comments
 (0)