-
Notifications
You must be signed in to change notification settings - Fork 86
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
feat: Support float32, float64, and array type query params #2297
Merged
jackdingilian
merged 2 commits into
googleapis:main
from
jackdingilian:add-array-params
Jul 31, 2024
Merged
Changes from all commits
Commits
Show all changes
2 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
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
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 |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
|
||
import com.google.api.core.BetaApi; | ||
import com.google.api.core.InternalApi; | ||
import com.google.bigtable.v2.ArrayValue; | ||
import com.google.bigtable.v2.ExecuteQueryRequest; | ||
import com.google.bigtable.v2.Type; | ||
import com.google.bigtable.v2.Value; | ||
|
@@ -27,6 +28,7 @@ | |
import com.google.protobuf.ByteString; | ||
import com.google.protobuf.Timestamp; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import javax.annotation.Nullable; | ||
import org.threeten.bp.Instant; | ||
|
@@ -65,6 +67,10 @@ public class Statement { | |
Type.newBuilder().setBytesType(Type.Bytes.getDefaultInstance()).build(); | ||
private static final Type INT64_TYPE = | ||
Type.newBuilder().setInt64Type(Type.Int64.getDefaultInstance()).build(); | ||
private static final Type FLOAT32_TYPE = | ||
Type.newBuilder().setFloat32Type(Type.Float32.getDefaultInstance()).build(); | ||
private static final Type FLOAT64_TYPE = | ||
Type.newBuilder().setFloat64Type(Type.Float64.getDefaultInstance()).build(); | ||
private static final Type BOOL_TYPE = | ||
Type.newBuilder().setBoolType(Type.Bool.getDefaultInstance()).build(); | ||
private static final Type TIMESTAMP_TYPE = | ||
|
@@ -131,6 +137,24 @@ public Builder setLongParam(String paramName, @Nullable Long value) { | |
return this; | ||
} | ||
|
||
/** | ||
* Sets a query parameter with the name {@code paramName} and the FLOAT32 typed value {@code | ||
* value} | ||
*/ | ||
public Builder setFloatParam(String paramName, @Nullable Float value) { | ||
params.put(paramName, float32ParamOf(value)); | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets a query parameter with the name {@code paramName} and the FLOAT64 typed value {@code | ||
* value} | ||
*/ | ||
public Builder setDoubleParam(String paramName, @Nullable Double value) { | ||
params.put(paramName, float64ParamOf(value)); | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets a query parameter with the name {@code paramName} and the BOOL typed value {@code value} | ||
*/ | ||
|
@@ -156,6 +180,17 @@ public Builder setDateParam(String paramName, @Nullable Date value) { | |
return this; | ||
} | ||
|
||
/** | ||
* Sets a query parameter with the name {@code paramName} and the ARRAY typed value {@code | ||
* value}. The array element type is specified by {@code arrayType} and the List elements must | ||
* be of the corresponding Java type. Null array elements are valid. | ||
*/ | ||
public <T> Builder setListParam( | ||
String paramName, @Nullable List<T> value, SqlType.Array<T> arrayType) { | ||
params.put(paramName, arrayParamOf(value, arrayType)); | ||
return this; | ||
} | ||
|
||
private static Value stringParamOf(@Nullable String value) { | ||
Value.Builder builder = nullValueWithType(STRING_TYPE); | ||
if (value != null) { | ||
|
@@ -180,6 +215,22 @@ private static Value int64ParamOf(@Nullable Long value) { | |
return builder.build(); | ||
} | ||
|
||
private static Value float32ParamOf(@Nullable Float value) { | ||
Value.Builder builder = nullValueWithType(FLOAT32_TYPE); | ||
if (value != null) { | ||
builder.setFloatValue(value); | ||
} | ||
return builder.build(); | ||
} | ||
|
||
private static Value float64ParamOf(@Nullable Double value) { | ||
Value.Builder builder = nullValueWithType(FLOAT64_TYPE); | ||
if (value != null) { | ||
builder.setFloatValue(value); | ||
} | ||
return builder.build(); | ||
} | ||
|
||
private static Value booleanParamOf(@Nullable Boolean value) { | ||
Value.Builder builder = nullValueWithType(BOOL_TYPE); | ||
if (value != null) { | ||
|
@@ -191,28 +242,120 @@ private static Value booleanParamOf(@Nullable Boolean value) { | |
private static Value timestampParamOf(@Nullable Instant value) { | ||
Value.Builder builder = nullValueWithType(TIMESTAMP_TYPE); | ||
if (value != null) { | ||
builder.setTimestampValue( | ||
Timestamp.newBuilder() | ||
.setSeconds(value.getEpochSecond()) | ||
.setNanos(value.getNano()) | ||
.build()); | ||
builder.setTimestampValue(toTimestamp(value)); | ||
} | ||
return builder.build(); | ||
} | ||
|
||
private static Value dateParamOf(@Nullable Date value) { | ||
Value.Builder builder = nullValueWithType(DATE_TYPE); | ||
if (value != null) { | ||
builder.setDateValue( | ||
com.google.type.Date.newBuilder() | ||
.setYear(value.getYear()) | ||
.setMonth(value.getMonth()) | ||
.setDay(value.getDayOfMonth()) | ||
.build()); | ||
builder.setDateValue(toProtoDate(value)); | ||
} | ||
return builder.build(); | ||
} | ||
|
||
private static <T> Value arrayParamOf(@Nullable List<T> value, SqlType.Array<T> arrayType) { | ||
Type type = | ||
Type.newBuilder() | ||
.setArrayType( | ||
Type.Array.newBuilder().setElementType(getElementType(arrayType)).build()) | ||
.build(); | ||
Value.Builder builder = nullValueWithType(type); | ||
if (value != null) { | ||
builder.setArrayValue(arrayValueOf(value, arrayType)); | ||
} | ||
return builder.build(); | ||
} | ||
|
||
private static Type getElementType(SqlType.Array<?> arrayType) { | ||
switch (arrayType.getElementType().getCode()) { | ||
case BYTES: | ||
return BYTES_TYPE; | ||
case STRING: | ||
return STRING_TYPE; | ||
case INT64: | ||
return INT64_TYPE; | ||
case FLOAT32: | ||
return FLOAT32_TYPE; | ||
case FLOAT64: | ||
return FLOAT64_TYPE; | ||
case BOOL: | ||
return BOOL_TYPE; | ||
case TIMESTAMP: | ||
return TIMESTAMP_TYPE; | ||
case DATE: | ||
return DATE_TYPE; | ||
default: | ||
throw new IllegalArgumentException( | ||
"Unsupported query parameter Array element type: " + arrayType.getElementType()); | ||
} | ||
} | ||
|
||
private static ArrayValue arrayValueOf(List<?> value, SqlType.Array<?> arrayType) { | ||
ArrayValue.Builder valueBuilder = ArrayValue.newBuilder(); | ||
for (Object element : value) { | ||
if (element == null) { | ||
valueBuilder.addValues(Value.getDefaultInstance()); | ||
continue; | ||
} | ||
switch (arrayType.getElementType().getCode()) { | ||
case BYTES: | ||
ByteString bytesElem = (ByteString) element; | ||
valueBuilder.addValues(Value.newBuilder().setBytesValue(bytesElem).build()); | ||
break; | ||
case STRING: | ||
String stringElem = (String) element; | ||
valueBuilder.addValues(Value.newBuilder().setStringValue(stringElem).build()); | ||
break; | ||
case INT64: | ||
Long longElem = (Long) element; | ||
valueBuilder.addValues(Value.newBuilder().setIntValue(longElem).build()); | ||
break; | ||
case FLOAT32: | ||
Float floatElem = (Float) element; | ||
valueBuilder.addValues(Value.newBuilder().setFloatValue(floatElem).build()); | ||
break; | ||
case FLOAT64: | ||
Double doubleElem = (Double) element; | ||
valueBuilder.addValues(Value.newBuilder().setFloatValue(doubleElem).build()); | ||
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. Could we 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. No, because we don't want to set type on the nested elements |
||
break; | ||
case BOOL: | ||
Boolean boolElem = (Boolean) element; | ||
valueBuilder.addValues(Value.newBuilder().setBoolValue(boolElem).build()); | ||
break; | ||
case TIMESTAMP: | ||
Instant timestampElem = (Instant) element; | ||
valueBuilder.addValues( | ||
Value.newBuilder().setTimestampValue(toTimestamp(timestampElem)).build()); | ||
break; | ||
case DATE: | ||
Date dateElem = (Date) element; | ||
valueBuilder.addValues(Value.newBuilder().setDateValue(toProtoDate(dateElem)).build()); | ||
break; | ||
default: | ||
throw new IllegalArgumentException( | ||
"Unsupported query parameter Array element type: " + arrayType.getElementType()); | ||
} | ||
} | ||
return valueBuilder.build(); | ||
} | ||
|
||
private static Timestamp toTimestamp(Instant instant) { | ||
return Timestamp.newBuilder() | ||
.setSeconds(instant.getEpochSecond()) | ||
.setNanos(instant.getNano()) | ||
.build(); | ||
} | ||
|
||
private static com.google.type.Date toProtoDate(Date date) { | ||
return com.google.type.Date.newBuilder() | ||
.setYear(date.getYear()) | ||
.setMonth(date.getMonth()) | ||
.setDay(date.getDayOfMonth()) | ||
.build(); | ||
} | ||
|
||
private static Value.Builder nullValueWithType(Type type) { | ||
return Value.newBuilder().setType(type); | ||
} | ||
|
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
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.
Should this be of a
nullValueWithType
?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.
No, the API only wants the type set on the outermost value, not nested elements