-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(bigtable): add view_parameters support to BoundStatement #13673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 11 commits
198cc70
79b16eb
1a170fc
c4c9e92
7f20c89
6d10dc2
391c330
8710442
328eb36
2d15072
f051e2e
786c0d8
6565a3c
ef58062
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,7 +29,7 @@ | |
|
|
||
| public class BoundStatementDeserializer { | ||
|
|
||
| static BoundStatement toBoundStatement( | ||
| public static BoundStatement toBoundStatement( | ||
| PreparedStatement preparedStatement, ExecuteQueryRequest request) { | ||
| BoundStatement.Builder boundStatementBuilder = preparedStatement.bind(); | ||
| for (Map.Entry<String, Value> paramEntry : request.getRequest().getParamsMap().entrySet()) { | ||
|
|
@@ -128,6 +128,23 @@ static BoundStatement toBoundStatement( | |
| throw new IllegalArgumentException("Unexpected query param type in param: " + value); | ||
| } | ||
| } | ||
| for (Map.Entry<String, Value> entry : request.getRequest().getViewParametersMap().entrySet()) { | ||
| String name = entry.getKey(); | ||
| Value value = entry.getValue(); | ||
| switch (value.getType().getKindCase()) { | ||
| case STRING_TYPE: | ||
| if (value.getKindCase().equals(KindCase.KIND_NOT_SET)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this right? The kindcase of null string is still string? https://github.com/googleapis/google-cloud-java/blob/main/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/sql/BoundStatement.java#L213
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was following the query parameters pattern here https://github.com/ad548/google-cloud-java/blob/f051e2e08aa1306b10c5914fa90cfe34bf2e2920/java-bigtable/test-proxy/src/main/java/com/google/cloud/bigtable/testproxy/BoundStatementDeserializer.java#L50. There is two kindcase - value.getType().getKindCase() and value.getKindCase(). For view_parameters we definitely only care about the first. So yea null string with value.getType().getKindCase() string should be ok. |
||
| boundStatementBuilder.setStringViewParameter(name, null); | ||
| } else if (value.getKindCase().equals(KindCase.STRING_VALUE)) { | ||
| boundStatementBuilder.setStringViewParameter(name, value.getStringValue()); | ||
| } else { | ||
| throw new IllegalArgumentException("Malformed string value: " + value); | ||
| } | ||
| break; | ||
| default: | ||
| throw new IllegalArgumentException("Unexpected view parameter type in param: " + value); | ||
| } | ||
| } | ||
| return boundStatementBuilder.build(); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| /* | ||
| * Copyright 2026 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 | ||
| * | ||
| * https://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.bigtable.testproxy; | ||
|
|
||
| import static com.google.common.truth.Truth.assertThat; | ||
| import static org.junit.Assert.assertThrows; | ||
| import static org.mockito.ArgumentMatchers.anyString; | ||
| import static org.mockito.ArgumentMatchers.nullable; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import com.google.bigtable.v2.ExecuteQueryRequest; | ||
| import com.google.bigtable.v2.Type; | ||
| import com.google.bigtable.v2.Value; | ||
| import com.google.cloud.bigtable.data.v2.models.sql.BoundStatement; | ||
| import com.google.cloud.bigtable.data.v2.models.sql.PreparedStatement; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.JUnit4; | ||
|
|
||
| @RunWith(JUnit4.class) | ||
| public class BoundStatementDeserializerTest { | ||
|
|
||
| @Test | ||
| public void testDeserializeStringViewParameter() { | ||
| PreparedStatement preparedStatement = mock(PreparedStatement.class); | ||
| BoundStatement.Builder builder = mock(BoundStatement.Builder.class); | ||
| when(preparedStatement.bind()).thenReturn(builder); | ||
| when(builder.setStringViewParameter(anyString(), nullable(String.class))).thenReturn(builder); | ||
|
|
||
| Value stringVal = | ||
| Value.newBuilder() | ||
| .setType(Type.newBuilder().setStringType(Type.String.getDefaultInstance())) | ||
| .setStringValue("alice") | ||
| .build(); | ||
| com.google.cloud.bigtable.testproxy.ExecuteQueryRequest request = | ||
| com.google.cloud.bigtable.testproxy.ExecuteQueryRequest.newBuilder() | ||
| .setRequest(ExecuteQueryRequest.newBuilder().putViewParameters("user_id", stringVal)) | ||
| .build(); | ||
|
|
||
| BoundStatementDeserializer.toBoundStatement(preparedStatement, request); | ||
| verify(builder).setStringViewParameter("user_id", "alice"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDeserializeNullStringViewParameter() { | ||
| PreparedStatement preparedStatement = mock(PreparedStatement.class); | ||
| BoundStatement.Builder builder = mock(BoundStatement.Builder.class); | ||
| when(preparedStatement.bind()).thenReturn(builder); | ||
| when(builder.setStringViewParameter(anyString(), nullable(String.class))).thenReturn(builder); | ||
|
|
||
| Value nullStringVal = | ||
| Value.newBuilder() | ||
| .setType(Type.newBuilder().setStringType(Type.String.getDefaultInstance())) | ||
| .build(); | ||
| com.google.cloud.bigtable.testproxy.ExecuteQueryRequest request = | ||
| com.google.cloud.bigtable.testproxy.ExecuteQueryRequest.newBuilder() | ||
| .setRequest( | ||
| ExecuteQueryRequest.newBuilder().putViewParameters("user_id", nullStringVal)) | ||
| .build(); | ||
|
|
||
| BoundStatementDeserializer.toBoundStatement(preparedStatement, request); | ||
| verify(builder).setStringViewParameter("user_id", null); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDeserializeInvalidTypeViewParameterThrows() { | ||
| PreparedStatement preparedStatement = mock(PreparedStatement.class); | ||
| BoundStatement.Builder builder = mock(BoundStatement.Builder.class); | ||
| when(preparedStatement.bind()).thenReturn(builder); | ||
|
|
||
| Value intVal = | ||
| Value.newBuilder() | ||
| .setType(Type.newBuilder().setInt64Type(Type.Int64.getDefaultInstance())) | ||
| .setIntValue(123) | ||
| .build(); | ||
| com.google.cloud.bigtable.testproxy.ExecuteQueryRequest request = | ||
| com.google.cloud.bigtable.testproxy.ExecuteQueryRequest.newBuilder() | ||
| .setRequest(ExecuteQueryRequest.newBuilder().putViewParameters("user_id", intVal)) | ||
| .build(); | ||
|
|
||
| IllegalArgumentException e = | ||
| assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> BoundStatementDeserializer.toBoundStatement(preparedStatement, request)); | ||
| assertThat(e.getMessage()).contains("Unexpected view parameter type in param"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDeserializeMalformedStringViewParameterThrows() { | ||
| PreparedStatement preparedStatement = mock(PreparedStatement.class); | ||
| BoundStatement.Builder builder = mock(BoundStatement.Builder.class); | ||
| when(preparedStatement.bind()).thenReturn(builder); | ||
|
|
||
| Value malformedVal = | ||
| Value.newBuilder() | ||
| .setType(Type.newBuilder().setStringType(Type.String.getDefaultInstance())) | ||
| .setIntValue(123) | ||
| .build(); | ||
| com.google.cloud.bigtable.testproxy.ExecuteQueryRequest request = | ||
| com.google.cloud.bigtable.testproxy.ExecuteQueryRequest.newBuilder() | ||
| .setRequest(ExecuteQueryRequest.newBuilder().putViewParameters("user_id", malformedVal)) | ||
| .build(); | ||
|
|
||
| IllegalArgumentException e = | ||
| assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> BoundStatementDeserializer.toBoundStatement(preparedStatement, request)); | ||
| assertThat(e.getMessage()).contains("Malformed string value"); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.