BigQuery: Cannot run job with one project that uses a resource in another
This is the same as an issue I encountered in the Ruby BigQuery gem. The table and dataset objects inherit the project from the client, and jobs are run from the table and dataset objects. I do not believe there is currently a way to specify a project for a dataset that is different from the client's project. This means that creating a PHP version of this python sample, which extracts a public dataset, is impossible with the PHP library. The python library allows you to specify a dataset's project. It also uses the client to run jobs, not datasets and tables. This allows the user to run a job in a project that is different from the dataset's project (as long as they have the appropriate permissions for both). This is an issue for extract and copy jobs.
Environment details
- OS: macOS High Sierra
- PHP version: 7.1.16
- Package name and version: "google/cloud-bigquery": "^1.4"
Steps to reproduce
- Run the code sample below with
bigquery-public-dataand with a project ID that you have permissions to create jobs in. (Also replace the bucket name with a real storage bucket name.) - When the code below is run with the
bigquery-public-dataproject, I get this error:"Access Denied: Project bigquery-public-data: The user <MY-SERVICE-ACCOUNT> does not have bigquery.jobs.create permission in project bigquery-public-data." - When the code below is run with a project you have create permissions in, the dataset will not be found because it will look in your project for the
samplesdataset.
Code example
use Google\Cloud\BigQuery\BigQueryClient;
$projectId = 'bigquery-public-data';
$datasetId = 'samples';
$tableId = 'shakespeare';
$bucketName = 'your-storage-bucket';
$bigQuery = new BigQueryClient([
'projectId' => $projectId,
]);
$dataset = $bigQuery->dataset($datasetId);
$table = $dataset->table($tableId);
$destinationUri = "gs://{$bucketName}/{$tableId}.csv";
$extractConfig = $table->extract($destinationUri);
$job = $table->runJob($extractConfig);
BigQuery's sharing model expects that the project in which queries are run can be distinct from the project in which tables are located. The PHP library should support this idiom; it's fine to use relative references, but the library should allow users to provide/use fully qualified references to reference data outside of the project (e.g. dataset.table implicitly indicates the project a client was instantiated with, but providing a project.dataset.table reference indicates a table in another project).
Let's prioritize this one.
@shollyman If this is still relevant, let's go ahead and assign out. I'm not sure if it still is. Thanks!
I believe it's still relevant, unless @jdpedrie is aware of changes here in PHP space. The way it's been addressed elsewhere is to provide things like additional constructors that accept the projectId arg in addition to the other resources that can exist in the project namespace (e.g. job ids, dataset ids).
I think it's still relevant. @ava12 do you have bandwidth to investigate other languages' implementations?
@jdpedrie, I think I can do it next week.
Just in case: we can extract a foreign table using current codebase (but it's a bit ugly). Two notes:
- we can change source table reference passing a
Tableobject toExtractJobConfiguration#sourceTable()method; - we can create two BigQuery clients - one for our project and one for public.
Code sample:
$myClient = new BigQueryClient();
$publicClient = new BigQueryClient([
'projectId' => 'bigquery-public-data',
]);
$publicTable = $publicClient->dataset('samples')->table('shakespeare');
// we don't actually need a real dataset and table name
$myTable = $myClient->dataset('my_dataset')->table('my_table');
$extractConfig = $myTable->extract('gs://your-storage-bucket/shakespeare.csv');
$extractConfig->sourceTable($publicTable);
$myTable->runJob($extractConfig);
I see two problems:
- We need a way to create references for external datasets and tables.
- We need a way to run jobs using external table references.
The solution is:
- Add optional
projectIdargument toBigQueryClient#dataset()method. So we'll be able to make external refs easily. - Add
copyTable(),extractTable(),loadTable(), andloadTableFromStorage()methods toBigQueryClient. So we'll be able to create job configurations using default project and external table refs. - Add
BigQueryClient#startJob()andBigQueryClient#runJob()methods, same asTablemethods. So we won't need to make fakeTableobjects to run jobs.