Skip to content
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

fix(4.x): upgrade libraries-bom to 26.34.0 and add spanner support float32 type #2667

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ private static Map<Class<?>, BiConsumer<ValueBinder<?>, Iterable>> createIterabl
map.put(Boolean.class, ValueBinder::toBoolArray);
map.put(Long.class, ValueBinder::toInt64Array);
map.put(Double.class, ValueBinder::toFloat64Array);
map.put(Float.class, ValueBinder::toFloat32Array);
map.put(BigDecimal.class, ValueBinder::toNumericArray);
map.put(Timestamp.class, ValueBinder::toTimestampArray);
map.put(Date.class, ValueBinder::toDateArray);
Expand All @@ -97,12 +98,15 @@ private static Map<Class<?>, BiConsumer<ValueBinder<?>, Iterable>> createIterabl
map.put(long.class, (BiFunction<ValueBinder, Long, ?>) ValueBinder::to);
map.put(Double.class, (BiFunction<ValueBinder, Double, ?>) ValueBinder::to);
map.put(double.class, (BiFunction<ValueBinder, Double, ?>) ValueBinder::to);
map.put(Float.class, (BiFunction<ValueBinder, Float, ?>) ValueBinder::to);
map.put(float.class, (BiFunction<ValueBinder, Float, ?>) ValueBinder::to);
map.put(BigDecimal.class, (BiFunction<ValueBinder, BigDecimal, ?>) ValueBinder::to);
map.put(Timestamp.class, (BiFunction<ValueBinder, Timestamp, ?>) ValueBinder::to);
map.put(Date.class, (BiFunction<ValueBinder, Date, ?>) ValueBinder::to);
map.put(ByteArray.class, (BiFunction<ValueBinder, ByteArray, ?>) ValueBinder::to);
map.put(String.class, (BiFunction<ValueBinder, String, ?>) ValueBinder::to);
map.put(double[].class, (BiFunction<ValueBinder, double[], ?>) ValueBinder::toFloat64Array);
map.put(float[].class, (BiFunction<ValueBinder, float[], ?>) ValueBinder::toFloat32Array);
map.put(boolean[].class, (BiFunction<ValueBinder, boolean[], ?>) ValueBinder::toBoolArray);
map.put(long[].class, (BiFunction<ValueBinder, long[], ?>) ValueBinder::toInt64Array);
map.put(Struct.class, (BiFunction<ValueBinder, Struct, ?>) ValueBinder::to);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ private SpannerTypeMapper() {}
.put(Type.Code.BYTES, ByteArray.class)
.put(Type.Code.DATE, com.google.cloud.Date.class)
.put(Type.Code.FLOAT64, Double.class)
.put(Type.Code.FLOAT32, Float.class)
.put(Type.Code.NUMERIC, BigDecimal.class)
.put(Type.Code.INT64, Long.class)
.put(Type.Code.STRING, String.class)
Expand All @@ -59,6 +60,7 @@ private SpannerTypeMapper() {}
.put(Type.Code.BYTES, ByteArray[].class)
.put(Type.Code.DATE, com.google.cloud.Date[].class)
.put(Type.Code.FLOAT64, double[].class)
.put(Type.Code.FLOAT32, float[].class)
.put(Type.Code.NUMERIC, BigDecimal[].class)
.put(Type.Code.INT64, long[].class)
.put(Type.Code.STRING, String[].class)
Expand All @@ -74,6 +76,7 @@ private SpannerTypeMapper() {}
type ->
builderMap.put(SPANNER_SIMPLE_COLUMN_CODES_TO_JAVA_TYPE_MAPPING.get(type), type));
builderMap.put(double.class, Code.FLOAT64);
builderMap.put(float.class, Code.FLOAT32);
builderMap.put(long.class, Code.INT64);
JAVA_TYPE_TO_SPANNER_SIMPLE_COLUMN_TYPE_MAPPING = Collections.unmodifiableMap(builderMap);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class StructAccessor {
.put(Long.class, AbstractStructReader::getLongList)
.put(String.class, AbstractStructReader::getStringList)
.put(Double.class, AbstractStructReader::getDoubleList)
.put(Float.class, AbstractStructReader::getFloatList)
.put(Timestamp.class, AbstractStructReader::getTimestampList)
.put(Date.class, AbstractStructReader::getDateList)
.put(ByteArray.class, AbstractStructReader::getBytesList)
Expand All @@ -64,11 +65,14 @@ public class StructAccessor {
.put(String.class, AbstractStructReader::getString)
.put(Double.class, AbstractStructReader::getDouble)
.put(double.class, AbstractStructReader::getDouble)
.put(Float.class, AbstractStructReader::getFloat)
.put(float.class, AbstractStructReader::getFloat)
.put(Timestamp.class, AbstractStructReader::getTimestamp)
.put(Date.class, AbstractStructReader::getDate)
.put(BigDecimal.class, AbstractStructReader::getBigDecimal)
.put(ByteArray.class, AbstractStructReader::getBytes)
.put(double[].class, AbstractStructReader::getDoubleArray)
.put(float[].class, AbstractStructReader::getFloatArray)
.put(long[].class, AbstractStructReader::getLongArray)
.put(boolean[].class, AbstractStructReader::getBooleanArray)
// Note that Struct.class appears in this map. While we support
Expand All @@ -85,11 +89,14 @@ public class StructAccessor {
.put(String.class, AbstractStructReader::getString)
.put(Double.class, AbstractStructReader::getDouble)
.put(double.class, AbstractStructReader::getDouble)
.put(Float.class, AbstractStructReader::getFloat)
.put(float.class, AbstractStructReader::getFloat)
.put(Timestamp.class, AbstractStructReader::getTimestamp)
.put(Date.class, AbstractStructReader::getDate)
.put(BigDecimal.class, AbstractStructReader::getBigDecimal)
.put(ByteArray.class, AbstractStructReader::getBytes)
.put(double[].class, AbstractStructReader::getDoubleArray)
.put(float[].class, AbstractStructReader::getFloatArray)
.put(long[].class, AbstractStructReader::getLongArray)
.put(boolean[].class, AbstractStructReader::getBooleanArray)
// Note that Struct.class appears in this map. While we support
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,15 @@ void getDropDdlTest() {
@Test
void getCreateDdlTest() {
String ddlResult =
"CREATE TABLE custom_test_table ( id STRING(MAX) , id3 INT64 , id_2 STRING(MAX) , bytes2"
+ " BYTES(MAX) , custom_col FLOAT64 NOT NULL , other STRING(333) , primitiveDoubleField"
+ " FLOAT64 , bigDoubleField FLOAT64 , bigLongField INT64 , primitiveIntField INT64 ,"
+ " bigIntField INT64 , bytes BYTES(MAX) , bytesList ARRAY<BYTES(111)> , integerList"
+ " ARRAY<INT64> , doubles ARRAY<FLOAT64> , commitTimestamp TIMESTAMP OPTIONS"
+ " (allow_commit_timestamp=true) , bigDecimalField NUMERIC , bigDecimals"
+ " ARRAY<NUMERIC> , jsonCol JSON ) PRIMARY KEY ( id , id_2 , id3 )";
"CREATE TABLE custom_test_table ( id STRING(MAX) , id3 INT64 , id_2 STRING(MAX) , "
+ "bytes2 BYTES(MAX) , custom_col FLOAT64 NOT NULL , other STRING(333) , "
+ "primitiveDoubleField FLOAT64 , bigDoubleField FLOAT64 , primitiveFloatField FLOAT32 , "
+ "bigFloatField FLOAT32 , bigLongField INT64 , primitiveIntField INT64 , "
+ "bigIntField INT64 , bytes BYTES(MAX) , bytesList ARRAY<BYTES(111)> , "
+ "integerList ARRAY<INT64> , doubles ARRAY<FLOAT64> , floats ARRAY<FLOAT32> , "
+ "commitTimestamp TIMESTAMP OPTIONS (allow_commit_timestamp=true) , "
+ "bigDecimalField NUMERIC , bigDecimals ARRAY<NUMERIC> , jsonCol JSON ) "
+ "PRIMARY KEY ( id , id_2 , id3 )";

assertThat(this.spannerSchemaUtils.getCreateTableDdlString(TestEntity.class))
.isEqualTo(ddlResult);
Expand Down Expand Up @@ -157,6 +159,17 @@ void ddlForListOfListOfDoubles() {
"doubleList ARRAY<FLOAT64>");
}

@Test
void ddlForListOfListOfFloats() {
assertColumnDdl(
List.class,
Float.class,
"floatList",
null,
OptionalLong.empty(),
"floatList ARRAY<FLOAT32>");
}

@Test
void createDdlForJson() {
assertColumnDdl(
Expand Down Expand Up @@ -260,6 +273,10 @@ private static class TestEntity {

Double bigDoubleField;

float primitiveFloatField;

Float bigFloatField;

Long bigLongField;

int primitiveIntField;
Expand All @@ -275,6 +292,8 @@ private static class TestEntity {

double[] doubles;

float[] floats;

// this is intentionally a double to test that it is forced to be TIMESTAMP on Spanner
// anyway
@Column(spannerCommitTimestamp = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
class ConverterAwareMappingSpannerEntityProcessorTests {

private static final Offset<Double> DELTA = Offset.offset(0.00001);
private static final Offset<Float> DELTA_FLOAT = Offset.offset(0.00001F);

private static final Converter<SpannerType, JavaType> SPANNER_TO_JAVA =
new Converter<SpannerType, JavaType>() {
Expand Down Expand Up @@ -183,6 +184,7 @@ private void verifyCanConvert(
@Test
void mapToListTest() {
List<Double> doubleList = Collections.singletonList(3.33);
List<Float> floatList = Collections.singletonList(3.33F);
List<String> stringList = Collections.singletonList("string");
List<Instant> instants =
Arrays.asList(
Expand Down Expand Up @@ -219,6 +221,12 @@ void mapToListTest() {
.to(Value.float64Array(new double[] {3.33, 3.33, 3.33}))
.set("doubleList")
.to(Value.float64Array(doubleList))
.set("floatField")
.to(Value.float32(3.33F))
.set("floatArray")
.to(Value.float32Array(new float[] {3.33F, 3.33F, 3.33F}))
.set("floatList")
.to(Value.float32Array(floatList))
.set("stringList")
.to(Value.stringArray(stringList))
.set("booleanList")
Expand Down Expand Up @@ -273,6 +281,12 @@ void mapToListTest() {
.to(Value.float64Array(new double[] {5.55, 5.55}))
.set("doubleList")
.to(Value.float64Array(doubleList))
.set("floatField")
.to(Value.float32(5.55F))
.set("floatArray")
.to(Value.float32Array(new float[] {5.55F, 5.55F}))
.set("floatList")
.to(Value.float32Array(floatList))
.set("stringList")
.to(Value.stringArray(stringList))
.set("booleanList")
Expand Down Expand Up @@ -337,6 +351,8 @@ void mapToListTest() {
assertThat(t1.dateField.getYear()).isEqualTo(2018);
assertThat(t1.doubleField).isEqualTo(3.33, DELTA);
assertThat(t1.doubleArray).hasSize(3);
assertThat(t1.floatField).isEqualTo(3.33F, DELTA_FLOAT);
assertThat(t1.floatArray).hasSize(3);
assertThat(t1.bigDecimals).containsExactly(BigDecimal.ONE, BigDecimal.ZERO);

assertThat(t2)
Expand All @@ -358,6 +374,10 @@ void mapToListTest() {
assertThat(t2.doubleArray).hasSize(2);
assertThat(t2.doubleList).hasSize(1);
assertThat(t2.doubleList.get(0)).isEqualTo(3.33, DELTA);
assertThat(t2.floatField).isEqualTo(5.55F, DELTA_FLOAT);
assertThat(t2.floatArray).hasSize(2);
assertThat(t2.floatList).hasSize(1);
assertThat(t2.floatList.get(0)).isEqualTo(3.33F, DELTA_FLOAT);
assertThat(t2.stringList).containsExactly("string");
assertThat(t2.bigDecimalField).isEqualTo(new BigDecimal("0.0001"));
assertThat(t2.bigDecimals).containsExactly(new BigDecimal("-0.999"), new BigDecimal("10.9001"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyDouble;
import static org.mockito.ArgumentMatchers.anyFloat;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.isNull;
Expand Down Expand Up @@ -97,6 +98,10 @@ void writeTest() {
t.doubleArray = new double[] {3.33, 3.33, 3.33};
t.doubleList = new ArrayList<>();
t.doubleList.add(3.33);
t.floatField = 3.33F;
t.floatArray = new float[] {3.33F, 3.33F, 3.33F};
t.floatList = new ArrayList<>();
t.floatList.add(3.33F);
t.stringList = new ArrayList<>();
t.stringList.add("stringstringstring");
t.dateField = Date.fromYearMonthDay(2018, 11, 22);
Expand Down Expand Up @@ -192,6 +197,18 @@ void writeTest() {
when(doubleListFieldBinder.toFloat64Array((Iterable<Double>) any())).thenReturn(null);
when(writeBuilder.set("doubleList")).thenReturn(doubleListFieldBinder);

ValueBinder<WriteBuilder> floatFieldBinder = mock(ValueBinder.class);
when(floatFieldBinder.to(anyFloat())).thenReturn(null);
when(writeBuilder.set("floatField")).thenReturn(floatFieldBinder);

ValueBinder<WriteBuilder> floatArrayFieldBinder = mock(ValueBinder.class);
when(floatArrayFieldBinder.toStringArray(any())).thenReturn(null);
when(writeBuilder.set("floatArray")).thenReturn(floatArrayFieldBinder);

ValueBinder<WriteBuilder> floatListFieldBinder = mock(ValueBinder.class);
when(floatListFieldBinder.toFloat32Array((Iterable<Float>) any())).thenReturn(null);
when(writeBuilder.set("floatList")).thenReturn(floatListFieldBinder);

ValueBinder<WriteBuilder> stringListFieldBinder = mock(ValueBinder.class);
when(stringListFieldBinder.toStringArray(any())).thenReturn(null);
when(writeBuilder.set("stringList")).thenReturn(stringListFieldBinder);
Expand Down Expand Up @@ -257,6 +274,9 @@ void writeTest() {
verify(doubleFieldBinder, times(1)).to(Double.valueOf(t.doubleField));
verify(doubleArrayFieldBinder, times(1)).to("3.33,3.33,3.33");
verify(doubleListFieldBinder, times(1)).toFloat64Array(t.doubleList);
verify(floatFieldBinder, times(1)).to(Float.valueOf(t.floatField));
verify(floatArrayFieldBinder, times(1)).to("3.33,3.33,3.33");
verify(floatListFieldBinder, times(1)).toFloat32Array(t.floatList);
verify(stringListFieldBinder, times(1)).toStringArray(t.stringList);
verify(booleanListFieldBinder, times(1)).toBoolArray(t.booleanList);
verify(longListFieldBinder, times(1)).toStringArray(any());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,21 @@ static class TestEntity {
long longField;

double doubleField;
float floatField;

// This double array is forced to be stored as a String for testing
@Column(spannerType = TypeCode.STRING)
double[] doubleArray;

// This double array is forced to be stored as a String for testing
@Column(spannerType = TypeCode.STRING)
float[] floatArray;

// int is not a native Cloud Spanner type, so this will utilize custom conversions.
int intField;

List<Double> doubleList;
List<Float> floatList;

List<String> stringList;

Expand Down
2 changes: 1 addition & 1 deletion spring-cloud-gcp-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
</distributionManagement>

<properties>
<gcp-libraries-bom.version>26.33.0</gcp-libraries-bom.version>
<gcp-libraries-bom.version>26.34.0</gcp-libraries-bom.version>
<cloud-sql-socket-factory.version>1.14.1</cloud-sql-socket-factory.version>
<r2dbc-postgres-driver.version>1.0.4.RELEASE</r2dbc-postgres-driver.version>
<cloud-spanner-r2dbc.version>1.3.0</cloud-spanner-r2dbc.version>
Expand Down
Loading