Skip to content

Commit

Permalink
feat: support float32 type
Browse files Browse the repository at this point in the history
  • Loading branch information
arawind committed Jul 9, 2024
1 parent cf3539e commit 7f0f3e8
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ sequence in the database.

The `increment_size` for this pooled generator can not exceed 200.

This is the **recommended configuration** for bit-reversed sequences:
This is the **recommended configuration** for bit-reversed sequences:

[source, java]
----
Expand Down Expand Up @@ -318,6 +318,7 @@ This project offers the following Hibernate type mappings for specific Spanner c
| `ARRAY<BOOL>` | `com.google.cloud.spanner.hibernate.types.SpannerBoolArray`
| `ARRAY<BYTES>` | `com.google.cloud.spanner.hibernate.types.SpannerBytesArray`
| `ARRAY<DATE>` | `com.google.cloud.spanner.hibernate.types.SpannerDateArray`
| `ARRAY<FLOAT32>` | `com.google.cloud.spanner.hibernate.types.SpannerFloat32Array`
| `ARRAY<FLOAT64>` | `com.google.cloud.spanner.hibernate.types.SpannerFloat64Array`
| `ARRAY<INT64>` | `com.google.cloud.spanner.hibernate.types.SpannerInt64Array`
| `ARRAY<JSON>` | `com.google.cloud.spanner.hibernate.types.SpannerJsonArray`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2019-2024 Google LLC
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/

package com.google.cloud.spanner.hibernate.types;

import com.google.cloud.spanner.Type.Code;
import java.util.List;

/**
* Hibernate type definition for
*
* <pre>{@code ARRAY<FLOAT32>}</pre>
*
* .
*/
public class SpannerFloat32Array extends AbstractSpannerArrayType<Float, Float> {

@Override
public Code getSpannerTypeCode() {
return Code.FLOAT32;
}

@Override
public Float[] toArray(List<Float> value) {
return value == null ? null : value.toArray(new Float[0]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ private static Code getSpannerTypeCode(Class<?> javaType) {
return Code.INT64;
} else if (Double.class.isAssignableFrom(javaType)) {
return Code.FLOAT64;
} else if (Float.class.isAssignableFrom(javaType)) {
return Code.FLOAT32;
} else if (String.class.isAssignableFrom(javaType)) {
return Code.STRING;
} else if (UUID.class.isAssignableFrom(javaType)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ public void testAllTypes() {
saved.setColBytes("test".getBytes(StandardCharsets.UTF_8));
saved.setColDate(LocalDate.of(2024, 2, 2));
saved.setColFloat64(3.14d);
saved.setColFloat32(2.71f);
saved.setColInt64(100L);
saved.setColJson("{\"key\":\"value\"}");
saved.setColNumeric(new BigDecimal("6.626"));
Expand All @@ -490,6 +491,7 @@ public void testAllTypes() {
saved.setColDateArray(
Arrays.asList(LocalDate.of(2000, 1, 1), null, LocalDate.of(1970, 1, 1)));
saved.setColFloat64Array(Arrays.asList(3.14d, null, 6.626d));
saved.setColFloat32Array(Arrays.asList(2.71f, null, 6.022f));
saved.setColInt64Array(Arrays.asList(1L, null, 2L));
saved.setColJsonArray(Arrays.asList("{\"key\":\"value1\"}", null, "{\"key\":\"value2\"}"));
saved.setColNumericArray(Arrays.asList(BigDecimal.ZERO, null, BigDecimal.TEN));
Expand All @@ -509,6 +511,7 @@ public void testAllTypes() {
assertArrayEquals(saved.getColBytes(), fetched.getColBytes());
assertEquals(saved.getColDate(), fetched.getColDate());
assertEquals(saved.getColFloat64(), fetched.getColFloat64());
assertEquals(saved.getColFloat32(), fetched.getColFloat32());
assertEquals(saved.getColInt64(), fetched.getColInt64());
assertEquals(saved.getColJson(), fetched.getColJson());
assertEquals(saved.getColNumeric(), fetched.getColNumeric());
Expand All @@ -521,6 +524,7 @@ public void testAllTypes() {
}
assertEquals(saved.getColDateArray(), fetched.getColDateArray());
assertEquals(saved.getColFloat64Array(), fetched.getColFloat64Array());
assertEquals(saved.getColFloat32Array(), fetched.getColFloat32Array());
assertEquals(saved.getColInt64Array(), fetched.getColInt64Array());
assertEquals(saved.getColJsonArray(), fetched.getColJsonArray());
assertEquals(saved.getColNumericArray(), fetched.getColNumericArray());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.cloud.spanner.hibernate.types.SpannerBoolArray;
import com.google.cloud.spanner.hibernate.types.SpannerBytesArray;
import com.google.cloud.spanner.hibernate.types.SpannerDateArray;
import com.google.cloud.spanner.hibernate.types.SpannerFloat32Array;
import com.google.cloud.spanner.hibernate.types.SpannerFloat64Array;
import com.google.cloud.spanner.hibernate.types.SpannerInt64Array;
import com.google.cloud.spanner.hibernate.types.SpannerJsonArray;
Expand Down Expand Up @@ -49,6 +50,8 @@ public class AllTypes extends AbstractBaseEntity {

private Double colFloat64;

private Float colFloat32;

private Long colInt64;

private String colJson;
Expand All @@ -71,6 +74,9 @@ public class AllTypes extends AbstractBaseEntity {
@Type(SpannerFloat64Array.class)
private List<Double> colFloat64Array;

@Type(SpannerFloat32Array.class)
private List<Float> colFloat32Array;

@Type(SpannerInt64Array.class)
private List<Long> colInt64Array;

Expand Down Expand Up @@ -127,6 +133,14 @@ public void setColFloat64(Double colFloat64) {
this.colFloat64 = colFloat64;
}

public Float getColFloat32() {
return colFloat32;
}

public void setColFloat32(Float colFloat32) {
this.colFloat32 = colFloat32;
}

public Long getColInt64() {
return colInt64;
}
Expand Down Expand Up @@ -199,6 +213,14 @@ public void setColFloat64Array(List<Double> colFloat64Array) {
this.colFloat64Array = colFloat64Array;
}

public List<Float> getColFloat32Array() {
return colFloat32Array;
}

public void setColFloat32Array(List<Float> colFloat32Array) {
this.colFloat32Array = colFloat32Array;
}

public List<Long> getColInt64Array() {
return colInt64Array;
}
Expand Down

0 comments on commit 7f0f3e8

Please sign in to comment.