-
Notifications
You must be signed in to change notification settings - Fork 88
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
test: Add test proxy implementation for ExecuteQuery api #2360
Merged
jackdingilian
merged 3 commits into
googleapis:main
from
jackdingilian:test-proxy-rebase
Oct 15, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
233 changes: 233 additions & 0 deletions
233
test-proxy/src/main/java/com/google/cloud/bigtable/testproxy/ResultSetSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,233 @@ | ||
/* | ||
* Copyright 2024 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 com.google.bigtable.v2.ArrayValue; | ||
import com.google.bigtable.v2.Type; | ||
import com.google.bigtable.v2.Type.Array; | ||
import com.google.bigtable.v2.Type.Bool; | ||
import com.google.bigtable.v2.Type.Bytes; | ||
import com.google.bigtable.v2.Type.Float32; | ||
import com.google.bigtable.v2.Type.Float64; | ||
import com.google.bigtable.v2.Type.Int64; | ||
import com.google.bigtable.v2.Type.Map; | ||
import com.google.bigtable.v2.Type.Struct; | ||
import com.google.bigtable.v2.Type.Timestamp; | ||
import com.google.bigtable.v2.Value; | ||
import com.google.cloud.Date; | ||
import com.google.cloud.bigtable.data.v2.models.sql.ColumnMetadata; | ||
import com.google.cloud.bigtable.data.v2.models.sql.ResultSet; | ||
import com.google.cloud.bigtable.data.v2.models.sql.SqlType; | ||
import com.google.cloud.bigtable.data.v2.models.sql.StructReader; | ||
import com.google.protobuf.ByteString; | ||
import java.util.List; | ||
import java.util.concurrent.ExecutionException; | ||
import org.threeten.bp.Instant; | ||
|
||
public class ResultSetSerializer { | ||
public static ExecuteQueryResult toExecuteQueryResult(ResultSet resultSet) | ||
throws ExecutionException, InterruptedException { | ||
ExecuteQueryResult.Builder resultBuilder = ExecuteQueryResult.newBuilder(); | ||
for (ColumnMetadata columnMetadata : resultSet.getMetadata().getColumns()) { | ||
resultBuilder | ||
.getMetadataBuilder() | ||
.addColumnsBuilder() | ||
.setName(columnMetadata.name()) | ||
.setType(toProtoType(columnMetadata.type())); | ||
} | ||
|
||
while (resultSet.next()) { | ||
SqlRow.Builder rowBuilder = resultBuilder.addRowsBuilder(); | ||
|
||
for (int i = 0; i < resultSet.getMetadata().getColumns().size(); i++) { | ||
SqlType<?> colType = resultSet.getMetadata().getColumnType(i); | ||
rowBuilder.addValues(toProtoValue(getColumn(resultSet, i, colType), colType)); | ||
} | ||
} | ||
|
||
return resultBuilder.build(); | ||
} | ||
|
||
private static Value toProtoValue(Object value, SqlType<?> type) { | ||
if (value == null) { | ||
return Value.getDefaultInstance(); | ||
} | ||
|
||
Value.Builder valueBuilder = Value.newBuilder(); | ||
switch (type.getCode()) { | ||
case BYTES: | ||
valueBuilder.setBytesValue((ByteString) value); | ||
break; | ||
case STRING: | ||
valueBuilder.setStringValue((String) value); | ||
break; | ||
case INT64: | ||
valueBuilder.setIntValue((Long) value); | ||
break; | ||
case FLOAT32: | ||
valueBuilder.setFloatValue((Float) value); | ||
break; | ||
case FLOAT64: | ||
valueBuilder.setFloatValue((Double) value); | ||
break; | ||
case BOOL: | ||
valueBuilder.setBoolValue((Boolean) value); | ||
break; | ||
case TIMESTAMP: | ||
Instant ts = (Instant) value; | ||
valueBuilder.setTimestampValue( | ||
com.google.protobuf.Timestamp.newBuilder() | ||
.setSeconds(ts.getEpochSecond()) | ||
.setNanos(ts.getNano()) | ||
.build()); | ||
break; | ||
case DATE: | ||
Date date = (Date) value; | ||
valueBuilder.setDateValue( | ||
com.google.type.Date.newBuilder() | ||
.setYear(date.getYear()) | ||
.setMonth(date.getMonth()) | ||
.setDay(date.getDayOfMonth()) | ||
.build()); | ||
break; | ||
case ARRAY: | ||
SqlType<?> elementType = ((SqlType.Array<?>) type).getElementType(); | ||
ArrayValue.Builder arrayValue = ArrayValue.newBuilder(); | ||
for (Object item : (List<?>) value) { | ||
arrayValue.addValues(toProtoValue(item, elementType)); | ||
} | ||
valueBuilder.setArrayValue(arrayValue.build()); | ||
break; | ||
case MAP: | ||
SqlType.Map<?, ?> mapType = (SqlType.Map<?, ?>) type; | ||
SqlType<?> mapKeyType = mapType.getKeyType(); | ||
SqlType<?> mapValueType = mapType.getValueType(); | ||
|
||
ArrayValue.Builder mapArrayValue = ArrayValue.newBuilder(); | ||
((java.util.Map<?, ?>) value) | ||
.forEach( | ||
(k, v) -> | ||
mapArrayValue.addValues( | ||
Value.newBuilder() | ||
.setArrayValue( | ||
ArrayValue.newBuilder() | ||
.addValues(toProtoValue(k, mapKeyType)) | ||
.addValues(toProtoValue(v, mapValueType)) | ||
.build()))); | ||
valueBuilder.setArrayValue(mapArrayValue.build()); | ||
break; | ||
case STRUCT: | ||
StructReader structValue = (StructReader) value; | ||
SqlType.Struct structType = (SqlType.Struct) type; | ||
ArrayValue.Builder structArrayValue = ArrayValue.newBuilder(); | ||
for (int i = 0; i < structType.getFields().size(); ++i) { | ||
SqlType<?> fieldType = structType.getType(i); | ||
structArrayValue.addValues(toProtoValue(getColumn(structValue, i, fieldType), fieldType)); | ||
} | ||
valueBuilder.setArrayValue(structArrayValue); | ||
break; | ||
default: | ||
throw new IllegalStateException("Unexpected Type: " + type); | ||
} | ||
|
||
return valueBuilder.build(); | ||
} | ||
|
||
private static Object getColumn(StructReader struct, int fieldIndex, SqlType<?> fieldType) { | ||
if (struct.isNull(fieldIndex)) { | ||
return null; | ||
} | ||
|
||
switch (fieldType.getCode()) { | ||
case ARRAY: | ||
return struct.getList(fieldIndex, (SqlType.Array<?>) fieldType); | ||
case BOOL: | ||
return struct.getBoolean(fieldIndex); | ||
case BYTES: | ||
return struct.getBytes(fieldIndex); | ||
case DATE: | ||
return struct.getDate(fieldIndex); | ||
case FLOAT32: | ||
return struct.getFloat(fieldIndex); | ||
case FLOAT64: | ||
return struct.getDouble(fieldIndex); | ||
case INT64: | ||
return struct.getLong(fieldIndex); | ||
case MAP: | ||
return struct.getMap(fieldIndex, (SqlType.Map<?, ?>) fieldType); | ||
case STRING: | ||
return struct.getString(fieldIndex); | ||
case STRUCT: | ||
return struct.getStruct(fieldIndex); | ||
case TIMESTAMP: | ||
return struct.getTimestamp(fieldIndex); | ||
default: | ||
throw new IllegalStateException("Unexpected Type: " + fieldType); | ||
} | ||
} | ||
|
||
private static Type toProtoType(SqlType<?> type) { | ||
switch (type.getCode()) { | ||
case BYTES: | ||
return Type.newBuilder().setBytesType(Bytes.getDefaultInstance()).build(); | ||
case STRING: | ||
return Type.newBuilder() | ||
.setStringType(com.google.bigtable.v2.Type.String.getDefaultInstance()) | ||
.build(); | ||
case INT64: | ||
return Type.newBuilder().setInt64Type(Int64.getDefaultInstance()).build(); | ||
case FLOAT32: | ||
return Type.newBuilder().setFloat32Type(Float32.getDefaultInstance()).build(); | ||
case FLOAT64: | ||
return Type.newBuilder().setFloat64Type(Float64.getDefaultInstance()).build(); | ||
case BOOL: | ||
return Type.newBuilder().setBoolType(Bool.getDefaultInstance()).build(); | ||
case TIMESTAMP: | ||
return Type.newBuilder().setTimestampType(Timestamp.getDefaultInstance()).build(); | ||
case DATE: | ||
return Type.newBuilder() | ||
.setDateType(com.google.bigtable.v2.Type.Date.getDefaultInstance()) | ||
.build(); | ||
case ARRAY: | ||
SqlType.Array<?> arrayType = (SqlType.Array<?>) type; | ||
return Type.newBuilder() | ||
.setArrayType( | ||
Array.newBuilder().setElementType(toProtoType(arrayType.getElementType()))) | ||
.build(); | ||
case MAP: | ||
SqlType.Map<?, ?> mapType = (SqlType.Map<?, ?>) type; | ||
return Type.newBuilder() | ||
.setMapType( | ||
Map.newBuilder() | ||
.setKeyType(toProtoType(mapType.getKeyType())) | ||
.setValueType(toProtoType(mapType.getValueType()))) | ||
.build(); | ||
case STRUCT: | ||
SqlType.Struct structType = (SqlType.Struct) type; | ||
Struct.Builder structBuilder = Struct.newBuilder(); | ||
for (SqlType.Struct.Field field : structType.getFields()) { | ||
structBuilder | ||
.addFieldsBuilder() | ||
.setFieldName(field.name()) | ||
.setType(toProtoType(field.type())); | ||
} | ||
return Type.newBuilder().setStructType(structBuilder.build()).build(); | ||
|
||
default: | ||
throw new IllegalStateException("Unexpected Type: " + type); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there existing client code for serialization and deserialization? This is a lot of code to maintain and test proxy should just be a thin layer that calls into the client
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is client code (within ResultSet) that handles deserializing protobuf Value messages to the corresponding java types. I agree this feels too 'heavy' for the testproxy but I think with the introduction of types it is somewhat unavoidable.
We want to validate that each client is converting to the relevant types correctly, to do so we need to do some form of roundtripping back to a shared format we can validate against. We reuse Value from the api here. I think no matter how we do it we need to do some conversion from java type back to some protobuf format.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For existing java type -> Value serialization theres some existing code that does a subset of what we're doing here but it's not sufficient to cover what we need and Im not sure either is a great fit for replacing this.
https://github.com/googleapis/java-bigtable/blob/main/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/Value.java#L112 - This serializes some Values to proto but is only used for the types supported by aggregates right now. We would have no use for the additional SQL types there and we would need to have the aggregates apis reject those value types (https://github.com/googleapis/java-bigtable/blob/main/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/RowMutation.java#L231)
https://github.com/googleapis/java-bigtable/blob/main/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/sql/Statement.java#L194
Statement does this for the types of query params we support but it doesn't support all the types we need (no map or struct) and sets the Value.type field which we don't expect here (though I guess it would be ok)