Skip to content

Commit

Permalink
feat(csharp/src/Drivers/BigQuery): add override for excluding table c…
Browse files Browse the repository at this point in the history
…onstraints (#1512)

- Adds a new parameter value to exclude the additional calls to obtain
table constraints
- Fixes some common nits about spacing

---------

Co-authored-by: David Coe <[email protected]>
  • Loading branch information
davidhcoe and David Coe authored Feb 5, 2024
1 parent b1947bf commit 3828e0b
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 18 deletions.
2 changes: 1 addition & 1 deletion csharp/src/Client/AdbcColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public AdbcColumn(string name, Type dataType, IArrowType arrowType, bool nullabl
this.DataType = dataType;
this.ArrowType = arrowType;

if(precision.HasValue && scale.HasValue)
if (precision.HasValue && scale.HasValue)
{
this.NumericScale = scale.Value;
this.NumericPrecision = precision.Value;
Expand Down
6 changes: 3 additions & 3 deletions csharp/src/Client/AdbcCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public sealed class AdbcCommand : DbCommand
/// <exception cref="ArgumentNullException"></exception>
public AdbcCommand(AdbcStatement adbcStatement, AdbcConnection adbcConnection) : base()
{
if(adbcStatement == null)
if (adbcStatement == null)
throw new ArgumentNullException(nameof(adbcStatement));

if(adbcConnection == null)
if (adbcConnection == null)
throw new ArgumentNullException(nameof(adbcConnection));

this.adbcStatement = adbcStatement;
Expand Down Expand Up @@ -183,7 +183,7 @@ protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)

protected override void Dispose(bool disposing)
{
if(disposing)
if (disposing)
{
// TODO: ensure not in the middle of pulling
this.adbcStatement?.Dispose();
Expand Down
6 changes: 3 additions & 3 deletions csharp/src/Client/AdbcDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public override object GetValue(int ordinal)
if (value == null)
return null;

if(value is SqlDecimal dValue)
if (value is SqlDecimal dValue)
{
if (this.DecimalBehavior == DecimalBehavior.UseSqlDecimal)
{
Expand Down Expand Up @@ -317,7 +317,7 @@ public ReadOnlyCollection<AdbcColumn> GetAdbcColumnSchema()
{
Type t = SchemaConverter.ConvertArrowType(f, this.DecimalBehavior);

if(f.HasMetadata &&
if (f.HasMetadata &&
f.Metadata.ContainsKey("precision") &&
f.Metadata.ContainsKey("scale"))
{
Expand Down Expand Up @@ -361,7 +361,7 @@ private ValueTask<RecordBatch> ReadNextRecordBatchAsync(CancellationToken cancel

RecordBatch recordBatch = this.adbcQueryResult.Stream.ReadNextRecordBatchAsync(cancellationToken).Result;

if( recordBatch != null )
if (recordBatch != null)
{
this.TotalBatches += 1;
}
Expand Down
2 changes: 1 addition & 1 deletion csharp/src/Client/SchemaConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal class SchemaConverter
/// <exception cref="ArgumentNullException"></exception>
public static DataTable ConvertArrowSchema(Schema schema, AdbcStatement adbcStatement, DecimalBehavior decimalBehavior)
{
if(schema == null)
if (schema == null)
throw new ArgumentNullException(nameof(schema));

if (adbcStatement == null)
Expand Down
18 changes: 16 additions & 2 deletions csharp/src/Drivers/BigQuery/BigQueryConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -427,15 +427,29 @@ private StructArray GetTableSchemas(

if (result != null)
{
bool includeConstraints = true;

if (this.properties.TryGetValue(BigQueryParameters.IncludeConstraintsWithGetObjects, out string includeConstraintsValue))

Check warning on line 432 in csharp/src/Drivers/BigQuery/BigQueryConnection.cs

View workflow job for this annotation

GitHub Actions / C# ubuntu-latest

Converting null literal or possible null value to non-nullable type.

Check warning on line 432 in csharp/src/Drivers/BigQuery/BigQueryConnection.cs

View workflow job for this annotation

GitHub Actions / C# macos-latest

Converting null literal or possible null value to non-nullable type.

Check warning on line 432 in csharp/src/Drivers/BigQuery/BigQueryConnection.cs

View workflow job for this annotation

GitHub Actions / C# windows-2019

Converting null literal or possible null value to non-nullable type.
{
bool.TryParse(includeConstraintsValue, out includeConstraints);
}

foreach (BigQueryRow row in result)
{
tableNameBuilder.Append(GetValue(row["table_name"]));
tableTypeBuilder.Append(GetValue(row["table_type"]));
nullBitmapBuffer.Append(true);
length++;

tableConstraintsValues.Add(GetConstraintSchema(
depth, catalog, dbSchema, GetValue(row["table_name"]), columnNamePattern));
if (includeConstraints)
{
tableConstraintsValues.Add(GetConstraintSchema(
depth, catalog, dbSchema, GetValue(row["table_name"]), columnNamePattern));
}
else
{
tableConstraintsValues.Add(null);
}

if (depth == GetObjectsDepth.Tables)
{
Expand Down
1 change: 1 addition & 0 deletions csharp/src/Drivers/BigQuery/BigQueryParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class BigQueryParameters
public const string UseLegacySQL = "adbc.bigquery.use_legacy_sql";
public const string LargeDecimalsAsString = "adbc.bigquery.large_decimals_as_string";
public const string Scopes = "adbc.bigquery.scopes";
public const string IncludeConstraintsWithGetObjects = "adbc.bigquery.include_constraints_getobjects";
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions csharp/src/Drivers/BigQuery/BigQueryStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,14 @@ static IArrowReader ReadChunk(BigQueryReadClient readClient, string streamName)

string[] segments = destinationTable.Split('.');

if(segments.Length != 3)
if (segments.Length != 3)
throw new InvalidOperationException($"{BigQueryParameters.LargeResultsDestinationTable} cannot be parsed");

projectId = segments[0];
datasetId = segments[1];
tableId = segments[2];

if(string.IsNullOrEmpty(projectId.Trim()) || string.IsNullOrEmpty(datasetId.Trim()) || string.IsNullOrEmpty(tableId.Trim()))
if (string.IsNullOrEmpty(projectId.Trim()) || string.IsNullOrEmpty(datasetId.Trim()) || string.IsNullOrEmpty(tableId.Trim()))
throw new InvalidOperationException($"{BigQueryParameters.LargeResultsDestinationTable} contains invalid values");

options.DestinationTable = new TableReference()
Expand Down
8 changes: 6 additions & 2 deletions csharp/src/Drivers/BigQuery/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ The ADBC driver passes the configured credentials to BigQuery, but you may need

## Parameters

The following parameters can be used to configure the driver behavior. The parameters are case sensitive.

**adbc.bigquery.allow_large_results**<br>
&nbsp;&nbsp;&nbsp;&nbsp;Sets the [AllowLargeResults](https://cloud.google.com/dotnet/docs/reference/Google.Cloud.BigQuery.V2/latest/Google.Cloud.BigQuery.V2.QueryOptions#Google_Cloud_BigQuery_V2_QueryOptions_AllowLargeResults) value of the QueryOptions to `true` if configured; otherwise, the default is `false`.

Expand All @@ -49,9 +51,11 @@ https://cloud.google.com/dotnet/docs/reference/Google.Cloud.BigQuery.V2/latest/G
**adbc.bigquery.auth_json_credential**<br>
&nbsp;&nbsp;&nbsp;&nbsp;Required if using `service` authentication. This value is passed to the [GoogleCredential.FromJson](https://cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.GoogleCredential#Google_Apis_Auth_OAuth2_GoogleCredential_FromJson_System_String) method.

**adbc.bigquery.large_results_destination_table**<br>
&nbsp;&nbsp;&nbsp;&nbsp;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.
**adbc.bigquery.include_constraints_getobjects**<br>
&nbsp;&nbsp;&nbsp;&nbsp;Optional. Some callers do not need the constraint details when they get the table information and can improve the speed of obtaining the results. Setting this value to `"false"` will not include the constraint details. The default value is `"true"`.

**adbc.bigquery.large_results_destination_table**<br>
&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.

**adbc.bigquery.project_id**<br>
&nbsp;&nbsp;&nbsp;&nbsp;The [Project ID](https://cloud.google.com/resource-manager/docs/creating-managing-projects) used for accessing BigQuery.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static AdbcDriver LoadDriver()
{
string file = "libadbc_driver_snowflake.dll";

if(File.Exists(file))
if (File.Exists(file))
{
// get the full path because some .NET versions need it
file = Path.GetFullPath(file);
Expand Down
4 changes: 4 additions & 0 deletions csharp/test/Drivers/BigQuery/BigQueryTestConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ internal class BigQueryTestConfiguration : TestConfiguration
public BigQueryTestConfiguration()
{
AllowLargeResults = false;
IncludeTableConstraints = true;
}

[JsonPropertyName("projectId")]
Expand All @@ -52,5 +53,8 @@ public BigQueryTestConfiguration()

[JsonPropertyName("largeResultsDestinationTable")]
public string LargeResultsDestinationTable { get; set; }

[JsonPropertyName("includeTableConstraints")]
public bool IncludeTableConstraints { get; set; }
}
}
6 changes: 4 additions & 2 deletions csharp/test/Drivers/BigQuery/BigQueryTestingUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,14 @@ internal static Dictionary<string,string> GetBigQueryParameters(BigQueryTestConf
parameters.Add(BigQueryParameters.Scopes, testConfiguration.Scopes);
}

if(testConfiguration.AllowLargeResults)
if (testConfiguration.AllowLargeResults)
{
parameters.Add(BigQueryParameters.AllowLargeResults, testConfiguration.AllowLargeResults.ToString());
}

if(!string.IsNullOrEmpty(testConfiguration.LargeResultsDestinationTable))
parameters.Add(BigQueryParameters.IncludeConstraintsWithGetObjects, testConfiguration.IncludeTableConstraints.ToString());

if (!string.IsNullOrEmpty(testConfiguration.LargeResultsDestinationTable))
{
parameters.Add(BigQueryParameters.LargeResultsDestinationTable, testConfiguration.LargeResultsDestinationTable);
}
Expand Down
20 changes: 19 additions & 1 deletion csharp/test/Drivers/BigQuery/ClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,22 @@ public void VerifyTypesAndValues()
}
}

[SkippableFact]
public void VerifySchemaTablesWithNoConstraints()
{
using (Adbc.Client.AdbcConnection adbcConnection = GetAdbcConnection(includeTableConstraints: false))
{
adbcConnection.Open();

string schema = "Tables";

var tables = adbcConnection.GetSchema(schema);

Assert.True(tables.Rows.Count > 0, $"No tables were found in the schema '{schema}'");
}
}


[SkippableFact]
public void VerifySchemaTables()
{
Expand Down Expand Up @@ -166,8 +182,10 @@ public void VerifySchemaTables()
}
}

private Adbc.Client.AdbcConnection GetAdbcConnection()
private Adbc.Client.AdbcConnection GetAdbcConnection(bool includeTableConstraints = true)
{
_testConfiguration.IncludeTableConstraints = includeTableConstraints;

return new Adbc.Client.AdbcConnection(
new BigQueryDriver(),
BigQueryTestingUtils.GetBigQueryParameters(_testConfiguration),
Expand Down

0 comments on commit 3828e0b

Please sign in to comment.