Skip to content

Commit dcf613d

Browse files
author
David Coe
committed
add default project ID
1 parent 5ffa582 commit dcf613d

File tree

6 files changed

+77
-13
lines changed

6 files changed

+77
-13
lines changed

csharp/src/Drivers/BigQuery/BigQueryConnection.cs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class BigQueryConnection : AdbcConnection
4141
readonly IReadOnlyDictionary<string, string> properties;
4242
BigQueryClient? client;
4343
GoogleCredential? credential;
44+
bool includePublicProjectIds = false;
4445

4546
const string infoDriverName = "ADBC BigQuery Driver";
4647
const string infoDriverVersion = "1.0.0";
@@ -81,8 +82,15 @@ internal BigQueryClient Open()
8182

8283
// TODO: handle token expiration
8384

85+
// if the caller doesn't specify a projectId, use the default
8486
if (!this.properties.TryGetValue(BigQueryParameters.ProjectId, out projectId))
85-
throw new ArgumentException($"The {BigQueryParameters.ProjectId} parameter is not present");
87+
projectId = BigQueryConstants.DetectProjectId;
88+
89+
if (this.properties.TryGetValue(BigQueryParameters.IncludePublicProjectIds, out string? result))
90+
{
91+
if (!string.IsNullOrEmpty(result))
92+
includePublicProjectIds = Convert.ToBoolean(result);
93+
}
8694

8795
if (this.properties.TryGetValue(BigQueryParameters.AuthenticationType, out string? newAuthenticationType))
8896
{
@@ -303,16 +311,26 @@ private IArrowArray[] GetCatalogs(
303311
{
304312
StringArray.Builder catalogNameBuilder = new StringArray.Builder();
305313
List<IArrowArray?> catalogDbSchemasValues = new List<IArrowArray?>();
314+
306315
string catalogRegexp = PatternToRegEx(catalogPattern);
307316
PagedEnumerable<ProjectList, CloudProject>? catalogs = this.client?.ListProjects();
308317

309318
if (catalogs != null)
310319
{
311-
foreach (CloudProject catalog in catalogs)
320+
List<string> projectIds = catalogs.Select(x => x.ProjectId).ToList();
321+
322+
string publicProjectId = "bigquery-public-data";
323+
324+
if (this.includePublicProjectIds && !projectIds.Contains(publicProjectId))
325+
projectIds.Add(publicProjectId);
326+
327+
projectIds.Sort();
328+
329+
foreach (string projectId in projectIds)
312330
{
313-
if (Regex.IsMatch(catalog.ProjectId, catalogRegexp, RegexOptions.IgnoreCase))
331+
if (Regex.IsMatch(projectId, catalogRegexp, RegexOptions.IgnoreCase))
314332
{
315-
catalogNameBuilder.Append(catalog.ProjectId);
333+
catalogNameBuilder.Append(projectId);
316334

317335
if (depth == GetObjectsDepth.Catalogs)
318336
{
@@ -321,7 +339,7 @@ private IArrowArray[] GetCatalogs(
321339
else
322340
{
323341
catalogDbSchemasValues.Add(GetDbSchemas(
324-
depth, catalog.ProjectId, dbSchemaPattern,
342+
depth, projectId, dbSchemaPattern,
325343
tableNamePattern, tableTypes, columnNamePattern));
326344
}
327345
}
@@ -333,6 +351,7 @@ private IArrowArray[] GetCatalogs(
333351
catalogNameBuilder.Build(),
334352
catalogDbSchemasValues.BuildListArrayForType(new StructType(StandardSchemas.DbSchemaSchema)),
335353
};
354+
336355
StandardSchemas.GetObjectsSchema.Validate(dataArrays);
337356

338357
return dataArrays;
@@ -994,7 +1013,7 @@ public override AdbcStatement CreateStatement()
9941013
{
9951014
if (this.credential == null)
9961015
{
997-
throw new InvalidOperationException();
1016+
throw new InvalidOperationException("A credential must be set");
9981017
}
9991018

10001019
if (this.client == null)

csharp/src/Drivers/BigQuery/BigQueryParameters.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class BigQueryParameters
3636
public const string IncludeConstraintsWithGetObjects = "adbc.bigquery.include_constraints_getobjects";
3737
public const string GetQueryResultsOptionsTimeoutMinutes = "adbc.bigquery.get_query_results_options.timeout";
3838
public const string MaxFetchConcurrency = "adbc.bigquery.max_fetch_concurrency";
39+
public const string IncludePublicProjectIds = "adbc.bigquery.include_public_project_ids";
3940
}
4041

4142
/// <summary>
@@ -47,5 +48,8 @@ public class BigQueryConstants
4748
public const string ServiceAccountAuthenticationType = "service";
4849
public const string TokenEndpoint = "https://accounts.google.com/o/oauth2/token";
4950
public const string TreatLargeDecimalAsString = "true";
51+
52+
// default value per https://pkg.go.dev/cloud.google.com/go/bigquery#section-readme
53+
public const string DetectProjectId = "*detect-project-id*";
5054
}
5155
}

csharp/src/Drivers/BigQuery/readme.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ https://cloud.google.com/dotnet/docs/reference/Google.Cloud.BigQuery.V2/latest/G
6464
&nbsp;&nbsp;&nbsp;&nbsp;Optional. Sets the [DestinationTable](https://cloud.google.com/dotnet/docs/reference/Google.Cloud.BigQuery.V2/latest/Google.Cloud.BigQuery.V2.QueryOptions#Google_Cloud_BigQuery_V2_QueryOptions_DestinationTable) value of the QueryOptions if configured. Expects the format to be `{projectId}.{datasetId}.{tableId}` to set the corresponding values in the [TableReference](https://github.com/googleapis/google-api-dotnet-client/blob/6c415c73788b848711e47c6dd33c2f93c76faf97/Src/Generated/Google.Apis.Bigquery.v2/Google.Apis.Bigquery.v2.cs#L9348) class.
6565

6666
**adbc.bigquery.project_id**<br>
67-
&nbsp;&nbsp;&nbsp;&nbsp;The [Project ID](https://cloud.google.com/resource-manager/docs/creating-managing-projects) used for accessing BigQuery.
67+
&nbsp;&nbsp;&nbsp;&nbsp;The [Project ID](https://cloud.google.com/resource-manager/docs/creating-managing-projects) used for accessing BigQuery. If not specified, will default to detect the projectIds the credentials have access to.
68+
69+
**adbc.bigquery.include_public_project_ids**<br>
70+
&nbsp;&nbsp;&nbsp;&nbsp;Include the `bigquery-public-data` project ID with the list of project IDs.
6871

6972
**adbc.bigquery.refresh_token**<br>
7073
&nbsp;&nbsp;&nbsp;&nbsp;The refresh token used for when the generated OAuth token expires. Required for `user` authentication.

csharp/test/Drivers/BigQuery/BigQueryTestConfiguration.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public BigQueryTestConfiguration()
3131
}
3232

3333
[JsonPropertyName("projectId")]
34-
public string ProjectId { get; set; } = string.Empty;
34+
public string? ProjectId { get; set; }
3535

3636
[JsonPropertyName("clientId")]
3737
public string ClientId { get; set; } = string.Empty;
@@ -57,6 +57,9 @@ public BigQueryTestConfiguration()
5757
[JsonPropertyName("includeTableConstraints")]
5858
public bool IncludeTableConstraints { get; set; }
5959

60+
[JsonPropertyName("includePublicProjectIds")]
61+
public bool IncludePublicProjectIds { get; set; } = false;
62+
6063
[JsonPropertyName("timeoutMinutes")]
6164
public int? TimeoutMinutes { get; set; }
6265

csharp/test/Drivers/BigQuery/BigQueryTestingUtils.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,12 @@ BigQueryTestConfiguration testConfiguration
5151
/// <returns></returns>
5252
internal static Dictionary<string, string> GetBigQueryParameters(BigQueryTestConfiguration testConfiguration)
5353
{
54-
Dictionary<string, string> parameters = new Dictionary<string, string>
54+
Dictionary<string, string> parameters = new Dictionary<string, string>{};
55+
56+
if (!string.IsNullOrEmpty(testConfiguration.ProjectId))
5557
{
56-
{ BigQueryParameters.ProjectId, testConfiguration.ProjectId },
57-
};
58+
parameters.Add(BigQueryParameters.ProjectId, testConfiguration.ProjectId!);
59+
}
5860

5961
if (!string.IsNullOrEmpty(testConfiguration.JsonCredential))
6062
{
@@ -81,6 +83,8 @@ internal static Dictionary<string, string> GetBigQueryParameters(BigQueryTestCon
8183

8284
parameters.Add(BigQueryParameters.IncludeConstraintsWithGetObjects, testConfiguration.IncludeTableConstraints.ToString());
8385

86+
parameters.Add(BigQueryParameters.IncludePublicProjectIds, testConfiguration.IncludePublicProjectIds.ToString());
87+
8488
if (!string.IsNullOrEmpty(testConfiguration.LargeResultsDestinationTable))
8589
{
8690
parameters.Add(BigQueryParameters.LargeResultsDestinationTable, testConfiguration.LargeResultsDestinationTable);

csharp/test/Drivers/BigQuery/DriverTests.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
using Apache.Arrow.Adbc.Tests.Xunit;
2424
using Apache.Arrow.Ipc;
2525
using Xunit;
26+
using Xunit.Abstractions;
2627

2728
namespace Apache.Arrow.Adbc.Tests.Drivers.BigQuery
2829
{
@@ -37,12 +38,14 @@ namespace Apache.Arrow.Adbc.Tests.Drivers.BigQuery
3738
public class DriverTests
3839
{
3940
BigQueryTestConfiguration _testConfiguration;
41+
readonly ITestOutputHelper? outputHelper;
4042

41-
public DriverTests()
43+
public DriverTests(ITestOutputHelper? outputHelper)
4244
{
45+
this.outputHelper = outputHelper;
46+
4347
Skip.IfNot(Utils.CanExecuteTestConfig(BigQueryTestingUtils.BIGQUERY_TEST_CONFIG_VARIABLE));
4448
_testConfiguration = Utils.LoadTestConfiguration<BigQueryTestConfiguration>(BigQueryTestingUtils.BIGQUERY_TEST_CONFIG_VARIABLE);
45-
4649
}
4750

4851
/// <summary>
@@ -98,6 +101,34 @@ public void CanGetInfo()
98101
}
99102
}
100103

104+
/// <summary>
105+
/// Validates if the driver can call GetObjects.
106+
/// </summary>
107+
[SkippableFact, Order(3)]
108+
public void CanGetObjectsAllCatalogs()
109+
{
110+
// need to add the database
111+
112+
AdbcConnection adbcConnection = BigQueryTestingUtils.GetBigQueryAdbcConnection(_testConfiguration);
113+
114+
IArrowArrayStream stream = adbcConnection.GetObjects(
115+
depth: AdbcConnection.GetObjectsDepth.Catalogs,
116+
catalogPattern: null,
117+
dbSchemaPattern: null,
118+
tableNamePattern: null,
119+
tableTypes: BigQueryTableTypes.TableTypes,
120+
columnNamePattern: null);
121+
122+
RecordBatch recordBatch = stream.ReadNextRecordBatchAsync().Result;
123+
124+
List<AdbcCatalog> catalogs = GetObjectsParser.ParseCatalog(recordBatch, schemaName);
125+
126+
foreach (AdbcCatalog ct in catalogs)
127+
{
128+
this.outputHelper?.WriteLine(ct.Name);
129+
}
130+
}
131+
101132
/// <summary>
102133
/// Validates if the driver can call GetObjects.
103134
/// </summary>

0 commit comments

Comments
 (0)