Skip to content

Commit 09aadf7

Browse files
committed
feat: Add support for snapshot clone BigQuery
1 parent e8672b7 commit 09aadf7

4 files changed

Lines changed: 147 additions & 2 deletions

File tree

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License").
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using Google.Apis.Bigquery.v2.Data;
16+
using Xunit;
17+
18+
namespace Google.Cloud.BigQuery.V2.IntegrationTests;
19+
20+
[Collection(nameof(BigQueryFixture))]
21+
public class SnapshotCloneTest
22+
{
23+
private readonly BigQueryFixture _fixture;
24+
public SnapshotCloneTest(BigQueryFixture fixture)
25+
{
26+
_fixture = fixture;
27+
}
28+
29+
private TableReference GetTableReference(string datasetId, string tableId) =>
30+
new TableReference { ProjectId = _fixture.ProjectId, DatasetId = datasetId, TableId = tableId };
31+
32+
[Fact]
33+
public void CreateTableSnapshot()
34+
{
35+
var client = BigQueryClient.Create(_fixture.ProjectId);
36+
var datasetId = _fixture.DatasetId;
37+
var tableId = _fixture.PeopleTableId;
38+
39+
var sourceTableReference = GetTableReference(datasetId, tableId);
40+
var destinationTableReference = GetTableReference(datasetId, tableId + "SnapshotTable");
41+
var jobOptions = new CreateCopyJobOptions
42+
{
43+
OperationType = CopyOperationType.Snapshot
44+
};
45+
var job = client.CreateCopyJob(sourceTableReference, destinationTableReference, jobOptions).PollUntilCompleted();
46+
Assert.Null(job.Status.ErrorResult);
47+
48+
var destTable = client.GetTable(_fixture.DatasetId, destinationTableReference.TableId);
49+
Assert.NotNull(destTable.Resource.SnapshotDefinition);
50+
51+
var baseTableReference = destTable.Resource.SnapshotDefinition.BaseTableReference;
52+
Assert.Equal(sourceTableReference.TableId, baseTableReference.TableId);
53+
Assert.Equal(sourceTableReference.ProjectId, baseTableReference.ProjectId);
54+
Assert.Equal(sourceTableReference.DatasetId, baseTableReference.DatasetId);
55+
}
56+
57+
[Fact]
58+
public void CreateTableClone()
59+
{
60+
var client = BigQueryClient.Create(_fixture.ProjectId);
61+
var datasetId = _fixture.DatasetId;
62+
var tableId = _fixture.PeopleTableId;
63+
64+
var sourceTableReference = GetTableReference(datasetId, tableId);
65+
var destinationTableReference = GetTableReference(datasetId, tableId + "CloneTable");
66+
var jobOptions = new CreateCopyJobOptions
67+
{
68+
OperationType = CopyOperationType.Clone
69+
};
70+
var job = client.CreateCopyJob(sourceTableReference, destinationTableReference, jobOptions).PollUntilCompleted();
71+
Assert.Null(job.Status.ErrorResult);
72+
73+
var destTable = client.GetTable(_fixture.DatasetId, destinationTableReference.TableId);
74+
Assert.NotNull(destTable.Resource.CloneDefinition);
75+
76+
var baseTableReference = destTable.Resource.CloneDefinition.BaseTableReference;
77+
Assert.Equal(sourceTableReference.TableId, baseTableReference.TableId);
78+
Assert.Equal(sourceTableReference.ProjectId, baseTableReference.ProjectId);
79+
Assert.Equal(sourceTableReference.DatasetId, baseTableReference.DatasetId);
80+
}
81+
}

apis/Google.Cloud.BigQuery.V2/Google.Cloud.BigQuery.V2.Tests/CreateCopyJobOptionsTest.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2017 Google Inc. All Rights Reserved.
1+
// Copyright 2017 Google Inc. All Rights Reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -27,11 +27,13 @@ public void ModifyRequest()
2727
CreateDisposition = CreateDisposition.CreateIfNeeded,
2828
WriteDisposition = WriteDisposition.WriteIfEmpty,
2929
DestinationEncryptionConfiguration = new EncryptionConfiguration { KmsKeyName = "projects/1/locations/us/keyRings/1/cryptoKeys/1" },
30+
OperationType = CopyOperationType.Clone
3031
};
3132
JobConfigurationTableCopy request = new JobConfigurationTableCopy();
3233
options.ModifyRequest(request);
3334
Assert.Equal("CREATE_IF_NEEDED", request.CreateDisposition);
3435
Assert.Equal("WRITE_EMPTY", request.WriteDisposition);
36+
Assert.Equal("CLONE", request.OperationType);
3537
Assert.Equal("projects/1/locations/us/keyRings/1/cryptoKeys/1", request.DestinationEncryptionConfiguration.KmsKeyName);
3638
}
3739
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2023 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
namespace Google.Cloud.BigQuery.V2
16+
{
17+
/// <summary>
18+
/// Indicates different operation types supported in table copy job.
19+
/// </summary>
20+
public enum CopyOperationType
21+
{
22+
/// <summary>
23+
/// Operation not specified.
24+
/// </summary>
25+
[ApiValue("OPERATION_TYPE_UNSPECIFIED")]
26+
Unspecified,
27+
28+
/// <summary>
29+
/// The source and destination table have the same table type.
30+
/// </summary>
31+
[ApiValue("COPY")]
32+
Copy,
33+
34+
/// <summary>
35+
/// The source table type is TABLE and the destination table type is SNAPSHOT.
36+
/// </summary>
37+
[ApiValue("SNAPSHOT")]
38+
Snapshot,
39+
40+
/// <summary>
41+
/// The source table type is SNAPSHOT and the destination table type is TABLE.
42+
/// </summary>
43+
[ApiValue("RESTORE")]
44+
Restore,
45+
46+
/// <summary>
47+
/// The source and destination table have the same table type,
48+
/// but we charge only for the delta bytes.
49+
/// </summary>
50+
[ApiValue("CLONE")]
51+
Clone,
52+
}
53+
}

apis/Google.Cloud.BigQuery.V2/Google.Cloud.BigQuery.V2/CreateCopyJobOptions.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2017 Google Inc. All Rights Reserved.
1+
// Copyright 2017 Google Inc. All Rights Reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -38,6 +38,11 @@ public sealed class CreateCopyJobOptions : JobCreationOptions
3838
/// </summary>
3939
public EncryptionConfiguration DestinationEncryptionConfiguration { get; set; }
4040

41+
/// <summary>
42+
/// Specifies the operation type for the job, if any.
43+
/// </summary>
44+
public CopyOperationType? OperationType { get; set; }
45+
4146
internal void ModifyRequest(JobConfigurationTableCopy copy)
4247
{
4348
if (CreateDisposition != null)
@@ -52,6 +57,10 @@ internal void ModifyRequest(JobConfigurationTableCopy copy)
5257
{
5358
copy.DestinationEncryptionConfiguration = DestinationEncryptionConfiguration;
5459
}
60+
if (OperationType != null)
61+
{
62+
copy.OperationType = EnumMap.ToApiValue(OperationType.Value);
63+
}
5564
}
5665
}
5766
}

0 commit comments

Comments
 (0)