google-cloud-php icon indicating copy to clipboard operation
google-cloud-php copied to clipboard

BigQuery: Cannot run job with one project that uses a resource in another

Open alixhami opened this issue 7 years ago • 7 comments

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

  1. Run the code sample below with bigquery-public-data and with a project ID that you have permissions to create jobs in. (Also replace the bucket name with a real storage bucket name.)
  2. When the code below is run with the bigquery-public-data project, 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."
  3. 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 samples dataset.

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);

alixhami avatar Dec 04 '18 18:12 alixhami

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 avatar Aug 29 '19 17:08 shollyman

@shollyman If this is still relevant, let's go ahead and assign out. I'm not sure if it still is. Thanks!

meredithslota avatar Aug 17 '20 21:08 meredithslota

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).

shollyman avatar Aug 18 '20 16:08 shollyman

I think it's still relevant. @ava12 do you have bandwidth to investigate other languages' implementations?

jdpedrie avatar Aug 18 '20 20:08 jdpedrie

@jdpedrie, I think I can do it next week.

ava12 avatar Aug 18 '20 21:08 ava12

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 Table object to ExtractJobConfiguration#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);

ava12 avatar Aug 28 '20 18:08 ava12

I see two problems:

  1. We need a way to create references for external datasets and tables.
  2. We need a way to run jobs using external table references.

The solution is:

  1. Add optional projectId argument to BigQueryClient#dataset() method. So we'll be able to make external refs easily.
  2. Add copyTable(), extractTable(), loadTable(), and loadTableFromStorage() methods to BigQueryClient. So we'll be able to create job configurations using default project and external table refs.
  3. AddBigQueryClient#startJob() and BigQueryClient#runJob() methods, same as Table methods. So we won't need to make fake Table objects to run jobs.

ava12 avatar Aug 28 '20 20:08 ava12