Skip to content

Commit

Permalink
feat(csharp/src/Drivers/BigQuery): add support for configurable query…
Browse files Browse the repository at this point in the history
… timeouts (#2043)

Adds a new option to support specifying the timeout value for a query

---------

Co-authored-by: David Coe <coedavid@umich.edu>
  • Loading branch information
davidhcoe and David Coe authored Jul 31, 2024
1 parent 8fb7c2f commit bf31b1f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 16 deletions.
26 changes: 11 additions & 15 deletions csharp/src/Drivers/BigQuery/BigQueryConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -997,23 +997,19 @@ private IReadOnlyDictionary<string, string> ParseOptions()
{
Dictionary<string, string> options = new Dictionary<string, string>();

foreach (KeyValuePair<string, string> keyValuePair in this.properties)
string[] statementOptions = new string[] {
BigQueryParameters.AllowLargeResults,
BigQueryParameters.UseLegacySQL,
BigQueryParameters.LargeDecimalsAsString,
BigQueryParameters.LargeResultsDestinationTable,
BigQueryParameters.GetQueryResultsOptionsTimeoutMinutes
};

foreach (string key in statementOptions)
{
if (keyValuePair.Key == BigQueryParameters.AllowLargeResults)
{
options[keyValuePair.Key] = keyValuePair.Value;
}
if (keyValuePair.Key == BigQueryParameters.UseLegacySQL)
{
options[keyValuePair.Key] = keyValuePair.Value;
}
if (keyValuePair.Key == BigQueryParameters.LargeDecimalsAsString)
{
options[keyValuePair.Key] = keyValuePair.Value;
}
if (keyValuePair.Key == BigQueryParameters.LargeResultsDestinationTable)
if (properties.TryGetValue(key, out string? value))
{
options[keyValuePair.Key] = keyValuePair.Value;
options[key] = value;
}
}

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 @@ -34,6 +34,7 @@ public class BigQueryParameters
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";
public const string GetQueryResultsOptionsTimeoutMinutes = "adbc.bigquery.get_query_results_options.timeout";
}

/// <summary>
Expand Down
16 changes: 15 additions & 1 deletion csharp/src/Drivers/BigQuery/BigQueryStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,21 @@ public override QueryResult ExecuteQuery()
{
QueryOptions? queryOptions = ValidateOptions();
BigQueryJob job = this.client.CreateQueryJob(SqlQuery, null, queryOptions);
BigQueryResults results = job.GetQueryResults();

GetQueryResultsOptions getQueryResultsOptions = new GetQueryResultsOptions();

if (this.Options?.TryGetValue(BigQueryParameters.GetQueryResultsOptionsTimeoutMinutes, out string? timeoutMinutes) == true)
{
if (int.TryParse(timeoutMinutes, out int minutes))
{
if (minutes >= 0)
{
getQueryResultsOptions.Timeout = TimeSpan.FromMinutes(minutes);
}
}
}

BigQueryResults results = job.GetQueryResults(getQueryResultsOptions);

BigQueryReadClientBuilder readClientBuilder = new BigQueryReadClientBuilder();
readClientBuilder.Credential = this.credential;
Expand Down
3 changes: 3 additions & 0 deletions csharp/test/Drivers/BigQuery/BigQueryTestConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,8 @@ public BigQueryTestConfiguration()

[JsonPropertyName("includeTableConstraints")]
public bool IncludeTableConstraints { get; set; }

[JsonPropertyName("timeoutMinutes")]
public int? TimeoutMinutes { get; set; }
}
}
5 changes: 5 additions & 0 deletions csharp/test/Drivers/BigQuery/BigQueryTestingUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ internal static Dictionary<string, string> GetBigQueryParameters(BigQueryTestCon
parameters.Add(BigQueryParameters.LargeResultsDestinationTable, testConfiguration.LargeResultsDestinationTable);
}

if (testConfiguration.TimeoutMinutes.HasValue)
{
parameters.Add(BigQueryParameters.GetQueryResultsOptionsTimeoutMinutes, testConfiguration.TimeoutMinutes.Value.ToString());
}

return parameters;
}

Expand Down

0 comments on commit bf31b1f

Please sign in to comment.