Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion BigQuery/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"erusev/parsedown": "^1.6",
"google/cloud-storage": "^2.0",
"google/cloud-bigquery-reservation": "^2.0",
"nikic/php-parser": "^5.0"
"nikic/php-parser": "^5.0",
"google/cloud-bigquery-connection": "^2.1"
},
"suggest": {
"google/cloud-storage": "Makes it easier to load data from Cloud Storage into BigQuery"
Expand Down
16 changes: 16 additions & 0 deletions BigQuery/src/Dataset.php
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,22 @@ function (array $routine) {
* ]);
* ```
*
* ```
* // Create a routine with remote function options.
* $routine = $dataset->createRoutine('my_remote_udf', [
* 'routineType' => 'SCALAR_FUNCTION',
* 'language' => 'SQL',
* 'remoteFunctionOptions' => [
* 'endpoint' => 'https://us-east1-my_gcf_project.cloudfunctions.net/remote_add',
* 'connection' => 'projects/project-id/locations/us/connections/connection-id',
* 'maxBatchingRows' => '10',
* 'userDefinedContext' => [
* 'key' => 'value'
* ]
* ]
* ]);
* ```
*
* @see https://cloud.google.com/bigquery/docs/reference/rest/v2/routines/insert Insert Routines API documentation.
*
* @param string $id The routine ID.
Expand Down
14 changes: 14 additions & 0 deletions BigQuery/src/Routine.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,20 @@ public function exists(array $options = [])
* ]);
* ```
*
* ```
* // Update the routine with remote function options.
* $routine->update([
* 'remoteFunctionOptions' => [
* 'endpoint' => 'https://us-east1-my_gcf_project.cloudfunctions.net/remote_add',
* 'connection' => 'projects/project-id/locations/us/connections/connection-id',
* 'maxBatchingRows' => '10',
* 'userDefinedContext' => [
* 'key' => 'value'
* ]
* ]
* ]);
* ```
*
* @see https://cloud.google.com/bigquery/docs/reference/rest/v2/routines/update Update Routines API documentation.
* @param array $metadata The full routine resource with desired
* modifications.
Expand Down
76 changes: 76 additions & 0 deletions BigQuery/tests/System/ManageRoutinesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@

namespace Google\Cloud\BigQuery\Tests\System;

use Google\Cloud\BigQuery\Connection\V1\Client\ConnectionServiceClient;
use Google\Cloud\BigQuery\Connection\V1\CloudResourceProperties;
use Google\Cloud\BigQuery\Connection\V1\Connection;
use Google\Cloud\BigQuery\Connection\V1\CreateConnectionRequest;
use Google\Cloud\BigQuery\Connection\V1\DeleteConnectionRequest;
use Google\Cloud\BigQuery\Routine;

/**
Expand Down Expand Up @@ -133,4 +138,75 @@ public function testCreateAndDeleteRoutine()

$this->assertFalse($routine->exists());
}

public function testCreateRemoteUdf()
{
$routineId = uniqid(self::TESTING_PREFIX);
$connectionId = uniqid('udf_conn');

$connectionName = $this->createConnection($connectionId);

try {
$routine = self::$dataset->createRoutine($routineId, [
'routineType' => 'SCALAR_FUNCTION',
'language' => 'SQL',
'returnType' => [
'typeKind' => 'STRING'
],
'remoteFunctionOptions' => [
'endpoint' => 'https://us-east1-my_gcf_project.cloudfunctions.net/remote_add',
'connection' => $connectionName,
'maxBatchingRows' => '10',
'userDefinedContext' => [
'key' => 'value'
]
]
]);

$this->assertTrue($routine->exists());

$info = $routine->info();
$this->assertEquals('SCALAR_FUNCTION', $info['routineType']);
$this->assertArrayHasKey('remoteFunctionOptions', $info);
$this->assertEquals(
'https://us-east1-my_gcf_project.cloudfunctions.net/remote_add',
$info['remoteFunctionOptions']['endpoint']
);

$routine->delete();
} finally {
$this->deleteConnection($connectionName);
}
}

private function createConnection($connectionId)
{
$projectId = self::$dataset->identity()['projectId'];
$location = 'us';
$parent = "projects/$projectId/locations/$location";

$connectionClient = new ConnectionServiceClient();

$connection = new Connection();
$connection->setFriendlyName($connectionId);
$connection->setCloudResource(new CloudResourceProperties());

$request = new CreateConnectionRequest();
$request->setParent($parent);
$request->setConnectionId($connectionId);
$request->setConnection($connection);

$response = $connectionClient->createConnection($request);
return $response->getName();
}

private function deleteConnection($name)
{
$connectionClient = new ConnectionServiceClient();

$request = new DeleteConnectionRequest();
$request->setName($name);

$connectionClient->deleteConnection($request);
}
}
35 changes: 35 additions & 0 deletions BigQuery/tests/Unit/RoutineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,41 @@ public function testUpdateWithMask()
$this->assertEquals($expected, $res);
}

public function testUpdateRemoteFunctionOptions()
{
$old = [
'routineType' => 'SCALAR_FUNCTION',
'definitionBody' => '',
'language' => 'SQL'
];

$new = [
'remoteFunctionOptions' => [
'endpoint' => 'https://us-east1-my_gcf_project.cloudfunctions.net/remote_add',
'connection' => 'projects/project-id/locations/us/connections/connection-id',
'maxBatchingRows' => '10',
'userDefinedContext' => [
'key' => 'value'
]
]
];

$expected = $old + $new;

$this->connection->getRoutine($this->identity)
->willReturn($old)
->shouldBeCalledTimes(1);

$this->connection->updateRoutine($this->identity + $expected + ['retries' => 0])
->shouldBeCalledTimes(1)
->willReturn($expected);

$this->routine->___setProperty('connection', $this->connection->reveal());
$res = $this->routine->update($new);

$this->assertEquals($expected, $res);
}

public function testDelete()
{
$this->connection->deleteRoutine($this->identity + ['retries' => 0])
Expand Down
Loading