Skip to content

Commit

Permalink
update support for query parameters
Browse files Browse the repository at this point in the history
deps: update dependency com.google.oauth-client:google-oauth-client-jetty to v1.33.3 (googleapis#1978)

[![WhiteSource Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [com.google.oauth-client:google-oauth-client-jetty](https://togithub.com/googleapis/google-oauth-java-client) | `1.33.2` -> `1.33.3` | [![age](https://badges.renovateapi.com/packages/maven/com.google.oauth-client:google-oauth-client-jetty/1.33.3/age-slim)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://badges.renovateapi.com/packages/maven/com.google.oauth-client:google-oauth-client-jetty/1.33.3/adoption-slim)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://badges.renovateapi.com/packages/maven/com.google.oauth-client:google-oauth-client-jetty/1.33.3/compatibility-slim/1.33.2)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://badges.renovateapi.com/packages/maven/com.google.oauth-client:google-oauth-client-jetty/1.33.3/confidence-slim/1.33.2)](https://docs.renovatebot.com/merge-confidence/) |

---

<details>
<summary>googleapis/google-oauth-java-client</summary>

[Compare Source](https://togithub.com/googleapis/google-oauth-java-client/compare/v1.33.2...v1.33.3)

</details>

---

📅 **Schedule**: At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, click this checkbox.

---

This PR has been generated by [WhiteSource Renovate](https://renovate.whitesourcesoftware.com). View repository job log [here](https://app.renovatebot.com/dashboard#github/googleapis/java-bigquery).

deps: update dependency com.google.oauth-client:google-oauth-client-java6 to v1.33.3 (googleapis#1977)

[![WhiteSource Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [com.google.oauth-client:google-oauth-client-java6](https://togithub.com/googleapis/google-oauth-java-client) | `1.33.2` -> `1.33.3` | [![age](https://badges.renovateapi.com/packages/maven/com.google.oauth-client:google-oauth-client-java6/1.33.3/age-slim)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://badges.renovateapi.com/packages/maven/com.google.oauth-client:google-oauth-client-java6/1.33.3/adoption-slim)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://badges.renovateapi.com/packages/maven/com.google.oauth-client:google-oauth-client-java6/1.33.3/compatibility-slim/1.33.2)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://badges.renovateapi.com/packages/maven/com.google.oauth-client:google-oauth-client-java6/1.33.3/confidence-slim/1.33.2)](https://docs.renovatebot.com/merge-confidence/) |

---

<details>
<summary>googleapis/google-oauth-java-client</summary>

[Compare Source](https://togithub.com/googleapis/google-oauth-java-client/compare/v1.33.2...v1.33.3)

</details>

---

📅 **Schedule**: At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, click this checkbox.

---

This PR has been generated by [WhiteSource Renovate](https://renovate.whitesourcesoftware.com). View repository job log [here](https://app.renovatebot.com/dashboard#github/googleapis/java-bigquery).
  • Loading branch information
stephaniewang526 committed Apr 19, 2022
1 parent fb77468 commit 9486217
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 18 deletions.
Expand Up @@ -17,7 +17,6 @@
package com.google.cloud.bigquery;

import com.google.api.core.BetaApi;
import com.google.api.services.bigquery.model.QueryParameter;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -92,6 +91,6 @@ public interface Connection {
*/
@BetaApi
BigQueryResultSet executeSelect(
String sql, List<QueryParameter> parameters, Map<String, String> labels)
String sql, List<Parameter> parameters, Map<String, String> labels)
throws BigQuerySQLException;
}
Expand Up @@ -42,6 +42,7 @@
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.io.IOException;
import java.util.AbstractList;
Expand Down Expand Up @@ -178,22 +179,22 @@ public BigQueryResultSet executeSelect(String sql) throws BigQuerySQLException {
@BetaApi
@Override
public BigQueryResultSet executeSelect(
String sql, List<QueryParameter> parameters, Map<String, String> labels)
String sql, List<Parameter> parameters, Map<String, String> labels)
throws BigQuerySQLException {
try {
// use jobs.query if possible
if (isFastQuerySupported()) {
final String projectId = bigQueryOptions.getProjectId();
final QueryRequest queryRequest =
createQueryRequest(connectionSettings, sql, parameters, labels);
return queryRpc(projectId, queryRequest, true);
return queryRpc(projectId, queryRequest, parameters != null);
}
// use jobs.insert otherwise
com.google.api.services.bigquery.model.Job queryJob =
createQueryJob(sql, connectionSettings, parameters, labels);
JobId jobId = JobId.fromPb(queryJob.getJobReference());
GetQueryResultsResponse firstPage = getQueryResultsFirstPage(jobId);
return getResultSet(firstPage, jobId, sql, true);
return getResultSet(firstPage, jobId, sql, parameters != null);
} catch (BigQueryException e) {
throw new BigQuerySQLException(e.getMessage(), e, e.getErrors());
}
Expand Down Expand Up @@ -967,7 +968,7 @@ private boolean containsIntervalType(Schema schema) {
QueryRequest createQueryRequest(
ConnectionSettings connectionSettings,
String sql,
List<QueryParameter> queryParameters,
List<Parameter> queryParameters,
Map<String, String> labels) {
QueryRequest content = new QueryRequest();
String requestId = UUID.randomUUID().toString();
Expand All @@ -988,7 +989,21 @@ QueryRequest createQueryRequest(
content.setMaxResults(connectionSettings.getMaxResults());
}
if (queryParameters != null) {
content.setQueryParameters(queryParameters);
// content.setQueryParameters(queryParameters);
if (queryParameters.get(0).getName() == null) {
// If query parameter name is unset, then assume mode is positional
content.setParameterMode("POSITIONAL");
// pass query parameters
List<QueryParameter> queryParametersPb =
Lists.transform(queryParameters, POSITIONAL_PARAMETER_TO_PB_FUNCTION);
content.setQueryParameters(queryParametersPb);
} else {
content.setParameterMode("NAMED");
// pass query parameters
List<QueryParameter> queryParametersPb =
Lists.transform(queryParameters, NAMED_PARAMETER_TO_PB_FUNCTION);
content.setQueryParameters(queryParametersPb);
}
}
if (connectionSettings.getCreateSession() != null) {
content.setCreateSession(connectionSettings.getCreateSession());
Expand All @@ -1008,18 +1023,26 @@ QueryRequest createQueryRequest(
com.google.api.services.bigquery.model.Job createQueryJob(
String sql,
ConnectionSettings connectionSettings,
List<QueryParameter> queryParameters,
List<Parameter> queryParameters,
Map<String, String> labels) {
com.google.api.services.bigquery.model.JobConfiguration configurationPb =
new com.google.api.services.bigquery.model.JobConfiguration();
JobConfigurationQuery queryConfigurationPb = new JobConfigurationQuery();
queryConfigurationPb.setQuery(sql);
if (queryParameters != null) {
queryConfigurationPb.setQueryParameters(queryParameters);
if (queryParameters.get(0).getName() == null) {
// If query parameter name is unset, then assume mode is positional
queryConfigurationPb.setParameterMode("POSITIONAL");
// pass query parameters
List<QueryParameter> queryParametersPb =
Lists.transform(queryParameters, POSITIONAL_PARAMETER_TO_PB_FUNCTION);
queryConfigurationPb.setQueryParameters(queryParametersPb);
} else {
queryConfigurationPb.setParameterMode("NAMED");
// pass query parameters
List<QueryParameter> queryParametersPb =
Lists.transform(queryParameters, NAMED_PARAMETER_TO_PB_FUNCTION);
queryConfigurationPb.setQueryParameters(queryParametersPb);
}
}
if (connectionSettings.getDestinationTable() != null) {
Expand Down Expand Up @@ -1155,4 +1178,23 @@ private com.google.api.services.bigquery.model.Job createDryRunJob(String sql) {
}
return dryRunJob;
}

// Convert from Parameter wrapper class to positional QueryParameter generated class
private static final Function<Parameter, QueryParameter> POSITIONAL_PARAMETER_TO_PB_FUNCTION =
value -> {
QueryParameter queryParameterPb = new QueryParameter();
queryParameterPb.setParameterValue(value.getQueryParameterValue().toValuePb());
queryParameterPb.setParameterType(value.getQueryParameterValue().toTypePb());
return queryParameterPb;
};

// Convert from Parameter wrapper class to name QueryParameter generated class
private static final Function<Parameter, QueryParameter> NAMED_PARAMETER_TO_PB_FUNCTION =
value -> {
QueryParameter queryParameterPb = new QueryParameter();
queryParameterPb.setName(value.getName());
queryParameterPb.setParameterValue(value.getQueryParameterValue().toValuePb());
queryParameterPb.setParameterType(value.getQueryParameterValue().toTypePb());
return queryParameterPb;
};
}
@@ -0,0 +1,70 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.bigquery;

import com.google.auto.value.AutoValue;
import javax.annotation.Nullable;

/* Wrapper class for query parameters */
@AutoValue
public abstract class Parameter {
Parameter() {
// Package private so users can't subclass it but AutoValue can.
}

/**
* Returns the name of the query parameter. If unset, this is a positional parameter. Otherwise,
* should be unique within a query.
*
* @return value or {@code null} for none
*/
@Nullable
public abstract String getName();

/** Returns the value for a query parameter along with its type. */
public abstract QueryParameterValue getQueryParameterValue();

/** Returns a builder pre-populated using the current values of this field. */
public abstract Builder toBuilder();

/** Returns a builder for a {@code Parameter} object. */
public static Builder newBuilder() {
return new AutoValue_Parameter.Builder();
}

@AutoValue.Builder
public abstract static class Builder {

/**
* [Optional] Sets the name of the query parameter. If unset, this is a positional parameter.
* Otherwise, should be unique within a query.
*
* @param name name or {@code null} for none
*/
public abstract Builder setName(String name);

/**
* Sets the the value for a query parameter along with its type.
*
* @param parameter parameter or {@code null} for none
*/
public abstract Builder setQueryParameterValue(QueryParameterValue parameter);

/** Creates a {@code Parameter} object. */
public abstract Parameter build();
}
}
Expand Up @@ -465,7 +465,7 @@ public void testExecuteSelectSlow() throws BigQuerySQLException {
@Test
public void testExecuteSelectSlowWithParams() throws BigQuerySQLException {
ConnectionImpl connectionSpy = Mockito.spy(connection);
List<QueryParameter> parameters = new ArrayList<>();
List<Parameter> parameters = new ArrayList<>();
Map<String, String> labels = new HashMap<>();
doReturn(false).when(connectionSpy).isFastQuerySupported();
com.google.api.services.bigquery.model.JobStatistics jobStatistics =
Expand Down
Expand Up @@ -87,6 +87,7 @@
import com.google.cloud.bigquery.Model;
import com.google.cloud.bigquery.ModelId;
import com.google.cloud.bigquery.ModelInfo;
import com.google.cloud.bigquery.Parameter;
import com.google.cloud.bigquery.ParquetOptions;
import com.google.cloud.bigquery.PolicyTags;
import com.google.cloud.bigquery.QueryJobConfiguration;
Expand Down Expand Up @@ -3285,7 +3286,6 @@ public void testQueryParameterModeWithDryRun() {
assertNotNull(statistics.getTotalBytesProcessed());
}

/* TODO(prasmish): replicate relevant parts of the test case for executeSelect */
@Test
public void testPositionalQueryParameters() throws InterruptedException {
String query =
Expand Down Expand Up @@ -3356,7 +3356,29 @@ public void testPositionalQueryParameters() throws InterruptedException {
}
}

/* TODO(prasmish): replicate relevant parts of the test case for executeSelect */
/* TODO(prasmish): expand below test case with all the fields shown in the above test case */
@Test
public void testExecuteSelectWithPositionalQueryParameters() throws BigQuerySQLException {
String query =
"SELECT TimestampField, StringField FROM "
+ TABLE_ID.getTable()
+ " WHERE StringField = ?"
+ " AND TimestampField > ?";
QueryParameterValue stringParameter = QueryParameterValue.string("stringValue");
QueryParameterValue timestampParameter =
QueryParameterValue.timestamp("2014-01-01 07:00:00.000000+00:00");
Parameter stringParam = Parameter.newBuilder().setQueryParameterValue(stringParameter).build();
Parameter timeStampParam =
Parameter.newBuilder().setQueryParameterValue(timestampParameter).build();

ConnectionSettings connectionSettings =
ConnectionSettings.newBuilder().setDefaultDataset(DatasetId.of(DATASET)).build();
Connection connection = bigquery.createConnection(connectionSettings);
List<Parameter> parameters = ImmutableList.of(stringParam, timeStampParam);
BigQueryResultSet rs = connection.executeSelect(query, parameters, null);
assertEquals(2, rs.getTotalRows());
}

@Test
public void testNamedQueryParameters() throws InterruptedException {
String query =
Expand All @@ -3379,6 +3401,35 @@ public void testNamedQueryParameters() throws InterruptedException {
assertEquals(2, Iterables.size(result.getValues()));
}

@Test
public void testExecuteSelectWithNamedQueryParameters() throws BigQuerySQLException {
String query =
"SELECT TimestampField, StringField, BooleanField FROM "
+ TABLE_ID.getTable()
+ " WHERE StringField = @stringParam"
+ " AND IntegerField IN UNNEST(@integerList)";
QueryParameterValue stringParameter = QueryParameterValue.string("stringValue");
QueryParameterValue intArrayParameter =
QueryParameterValue.array(new Integer[] {3, 4}, Integer.class);
Parameter stringParam =
Parameter.newBuilder()
.setName("stringParam")
.setQueryParameterValue(stringParameter)
.build();
Parameter intArrayParam =
Parameter.newBuilder()
.setName("integerList")
.setQueryParameterValue(intArrayParameter)
.build();

ConnectionSettings connectionSettings =
ConnectionSettings.newBuilder().setDefaultDataset(DatasetId.of(DATASET)).build();
Connection connection = bigquery.createConnection(connectionSettings);
List<Parameter> parameters = ImmutableList.of(stringParam, intArrayParam);
BigQueryResultSet rs = connection.executeSelect(query, parameters, null);
assertEquals(2, rs.getTotalRows());
}

/* TODO(prasmish): replicate relevant parts of the test case for executeSelect */
@Test
public void testStructNamedQueryParameters() throws InterruptedException {
Expand Down
4 changes: 2 additions & 2 deletions samples/install-without-bom/pom.xml
Expand Up @@ -52,12 +52,12 @@
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-java6</artifactId>
<version>1.33.2</version>
<version>1.33.3</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-jetty</artifactId>
<version>1.33.2</version>
<version>1.33.3</version>
</dependency>
<!-- Test dependencies -->
<dependency>
Expand Down
4 changes: 2 additions & 2 deletions samples/snapshot/pom.xml
Expand Up @@ -50,12 +50,12 @@
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-java6</artifactId>
<version>1.33.2</version>
<version>1.33.3</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-jetty</artifactId>
<version>1.33.2</version>
<version>1.33.3</version>
</dependency>
<!-- Test dependencies -->
<dependency>
Expand Down
4 changes: 2 additions & 2 deletions samples/snippets/pom.xml
Expand Up @@ -66,12 +66,12 @@
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-java6</artifactId>
<version>1.33.2</version>
<version>1.33.3</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-jetty</artifactId>
<version>1.33.2</version>
<version>1.33.3</version>
</dependency>
<!-- [END bigquery-enduser-installed-packages] -->

Expand Down

0 comments on commit 9486217

Please sign in to comment.