Skip to content

Commit

Permalink
feat(BigQuery): Enable session in load job (#6576)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajupazhamayil committed Sep 7, 2023
1 parent ad50f74 commit c27f058
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 3 deletions.
42 changes: 42 additions & 0 deletions BigQuery/src/LoadJobConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -637,4 +637,46 @@ public function hivePartitioningOptions(array $hivePartitioningOptions)

return $this;
}

/**
* Decide whether to create a new session with a random id.
* The created session id is returned as part of the SessionInfo
* within the query statistics.
*
* Example:
* ```
* $loadJobConfig->createSession(true);
* ```
*
* @param bool $createSession
* @return LoadJobConfiguration
*/
public function createSession(bool $createSession)
{
$this->config['configuration']['load']['createSession'] = $createSession;

return $this;
}

/**
* Sets connection properties for the load job.
*
* Example:
* ```
* $loadJobConfig->connectionProperties([
* 'key' => 'session_id',
* 'value' => 'sessionId'
* ]);
* ```
*
* @param array $connectionProperties
* @return LoadJobConfiguration
*/
public function connectionProperties(array $connectionProperties)
{
$this->config['configuration']['load']['connectionProperties'] =
$connectionProperties;

return $this;
}
}
13 changes: 12 additions & 1 deletion BigQuery/tests/Snippet/LoadJobConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,18 @@ public function setterDataProvider()
'sourceUriPrefix' => 'gs://bucket/path_to_table',
'requirePartitionFilter' => false,
]
]
],
[
'createSession',
true
],
[
'connectionProperties',
[
'key' => 'session_id',
'value' => 'sessionId'
]
],
];
}
}
41 changes: 41 additions & 0 deletions BigQuery/tests/System/LoadDataAndQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,47 @@ public function testInsertValidJsonToTable($json)
$this->assertTrue($insertResponse->isSuccessful());
}

public function testLoadWithSession()
{
$dataset = self::$client->dataset('_SESSION');
$table = $dataset->table(uniqid(self::TESTING_PREFIX));
$source = __DIR__ . '/data/table-data.json';
$schema = [
'fields' => [
[
'name' => 'name',
'type' => 'STRING'
],
[
'name' => 'post_abbr',
'type' => 'STRING'
]
]
];
$loadJobConfig = $table->load(fopen($source, 'r'))
->sourceFormat('NEWLINE_DELIMITED_JSON')
->autodetect(true)
->createSession(true);

$job = self::$client->runJob($loadJobConfig);

$this->assertTrue(isset($job->info()['statistics']['sessionInfo']['sessionId']));

$sessionId = $job->info()['statistics']['sessionInfo']['sessionId'];
$loadJobConfig = $table->load(fopen($source, 'r'))
->sourceFormat('NEWLINE_DELIMITED_JSON')
->connectionProperties(
['key' => 'session_id', 'value' =>$sessionId]
);
$job = self::$client->runJob($loadJobConfig);
$this->assertFalse(isset($job->info()['status']['errorResult']));
$queryJobConfig = self::$client->query(
sprintf("CALL BQ.ABORT_SESSION('%s')", $sessionId)
);
$job = self::$client->runJob($queryJobConfig);
$this->assertFalse(isset($job->info()['status']['errorResult']));
}

public function invalidJsonProvider()
{
return[
Expand Down
13 changes: 13 additions & 0 deletions BigQuery/tests/Unit/BigQueryClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,19 @@ public function jobConfigDataProvider()
$expected + ['data' => 'abc'],
'insertJobUpload',
$uploader->reveal()
],
[
$expected + ['configuration' => [
'load' => [
'createSession' => true,
'connectionProperties' => [
'key' => 'session_id',
'value' => 'sessionId'
]
]
]],
'insertJob',
$this->jobResponse
]
];
}
Expand Down
14 changes: 12 additions & 2 deletions BigQuery/tests/Unit/LoadJobConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,12 @@ public function testFluentSetters()
]
],
'writeDisposition' => 'WRITE_TRUNCATE',
'useAvroLogicalTypes' => true
'useAvroLogicalTypes' => true,
'createSession' => true,
'connectionProperties' => [
'key' => 'session_id',
'value' => 'sessionId'
]
];
$this->expectedConfig['configuration']['load'] = $load
+ $this->expectedConfig['configuration']['load'];
Expand Down Expand Up @@ -132,7 +137,12 @@ public function testFluentSetters()
->timePartitioning($load['timePartitioning'])
->rangePartitioning($load['rangePartitioning'])
->writeDisposition($load['writeDisposition'])
->useAvroLogicalTypes($load['useAvroLogicalTypes']);
->useAvroLogicalTypes($load['useAvroLogicalTypes'])
->createSession(true)
->connectionProperties([
'key' => 'session_id',
'value' =>'sessionId'
]);

$this->assertInstanceOf(LoadJobConfiguration::class, $config);
$this->assertEquals(
Expand Down

0 comments on commit c27f058

Please sign in to comment.