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

feat: support float4 data type #1481

Merged
merged 19 commits into from
Mar 11, 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
2 changes: 1 addition & 1 deletion .github/workflows/integration-emulator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ jobs:
run: |
psql -h /pg -U postgres -c "CREATE DATABASE pgadapter"
- name: Run integration tests
run: mvn -Dpgadapter.use_virtual_threads=true -Dpgadapter.use_virtual_grpc_transport_threads=true verify -B -Dclirr.skip=true -DskipUnits=true -DskipITs=false -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn
run: mvn -Dpgadapter.use_virtual_threads=true -Dpgadapter.test_float4_type=float8 -Dpgadapter.use_virtual_grpc_transport_threads=true verify -B -Dclirr.skip=true -DskipUnits=true -DskipITs=false -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn
env:
POSTGRES_HOST: /pg
POSTGRES_PORT: 5432
Expand Down
14 changes: 11 additions & 3 deletions .github/workflows/integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ jobs:
if: github.event.schedule=='14 3 * * *'
run: |
echo "GOOGLE_CLOUD_ENDPOINT=staging-wrenchworks.sandbox.googleapis.com" >> $GITHUB_ENV
# TODO: Remove
- id: set-test-type-for-float32
run: |
if [[ $GOOGLE_CLOUD_ENDPOINT == 'staging-wrenchworks.sandbox.googleapis.com' ]]; then
echo "FLOAT32_TEST_TYPE=float4" >> $GITHUB_ENV
else
echo "FLOAT32_TEST_TYPE=float8" >> $GITHUB_ENV
fi
- id: set-excluded-integration-tests
if: github.event_name == 'pull_request'
run: |
Expand Down Expand Up @@ -105,9 +113,9 @@ jobs:
run: |
psql -h /pg -U postgres -c "CREATE DATABASE pgadapter"
- name: Run unit tests
run: mvn test -B -Ptest-all
run: mvn test -B -Ptest-all -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn
- name: Run unit tests with virtual threads
run: mvn test -Dpgadapter.use_virtual_threads=true -Dpgadapter.use_virtual_grpc_transport_threads=true -B -Ptest-all
run: mvn test -Dpgadapter.use_virtual_threads=true -Dpgadapter.use_virtual_grpc_transport_threads=true -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn -B -Ptest-all
- name: Auth
uses: google-github-actions/auth@v2
with:
Expand All @@ -117,7 +125,7 @@ jobs:
with:
project_id: ${{ secrets.GCP_PROJECT_ID }}
- name: Run integration tests
run: mvn verify -B -Dclirr.skip=true -DskipITs=false -DexcludedIntegrationTests="$EXCLUDED_INTEGRATION_TESTS" -DPG_ADAPTER_HOST="https://$GOOGLE_CLOUD_ENDPOINT" -DPG_ADAPTER_INSTANCE="$GOOGLE_CLOUD_INSTANCE" -DPG_ADAPTER_DATABASE="$GOOGLE_CLOUD_DATABASE"
run: mvn verify -B -Dclirr.skip=true -DskipITs=false -DskipUnits=true -Dpgadapter.test_float4_type="$FLOAT32_TEST_TYPE" -DexcludedIntegrationTests="$EXCLUDED_INTEGRATION_TESTS" -DPG_ADAPTER_HOST="https://$GOOGLE_CLOUD_ENDPOINT" -DPG_ADAPTER_INSTANCE="$GOOGLE_CLOUD_INSTANCE" -DPG_ADAPTER_DATABASE="$GOOGLE_CLOUD_DATABASE"
env:
POSTGRES_HOST: /pg
POSTGRES_PORT: 5432
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>26.33.0</version>
<version>26.34.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down
18 changes: 9 additions & 9 deletions samples/python/django/setting.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
INSTALLED_APPS = [
'sample_app'
'sample_app'
]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'postgres',
'PORT': '5432',
'HOST': 'localhost'
}
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'postgres',
'PORT': '5432',
'HOST': 'localhost'
}
}

USE_TZ = True
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ private List<?> toList(Value value, Code arrayElementType) {
return value.getStringArray();
case TIMESTAMP:
return value.getTimestampArray();
case FLOAT32:
return value.getFloat32Array();
case FLOAT64:
return value.getFloat64Array();
case NUMERIC: // Only PG_NUMERIC is supported
Expand Down Expand Up @@ -212,8 +214,6 @@ private static Object toValidSpannerElement(@Nonnull Object value, int elementOi
return ((Short) value).longValue();
case Oid.INT4:
return ((Integer) value).longValue();
case Oid.FLOAT4:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is being removed now, as these are data types that needed special handling because:

  1. We allow PostgreSQL clients to send us int2[], int4[] and float4[] arrays.
  2. These are converted to int8[] and float8[] before being sent to Cloud Spanner.
  3. This conversion is no longer needed for float4 when Cloud Spanner supports float4 natively.

return ((Float) value).doubleValue();
}
return value;
}
Expand Down Expand Up @@ -381,17 +381,7 @@ public void bind(Statement.Builder statementBuilder, String name) {
statementBuilder.bind(name).toPgNumericArray((List<String>) this.item);
break;
case Oid.FLOAT4:
if (this.item == null) {
statementBuilder.bind(name).toFloat64Array((double[]) null);
} else {
statementBuilder
.bind(name)
.toFloat64Array(
((List<Float>) this.item)
.stream()
.map(f -> f == null ? null : f.doubleValue())
.collect(Collectors.toList()));
}
statementBuilder.bind(name).toFloat32Array((List<Float>) this.item);
break;
case Oid.FLOAT8:
statementBuilder.bind(name).toFloat64Array((List<Double>) this.item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,22 @@

package com.google.cloud.spanner.pgadapter.parsers;

import com.google.cloud.spanner.ErrorCode;
import com.google.cloud.spanner.ResultSet;
import com.google.cloud.spanner.SpannerExceptionFactory;
import com.google.cloud.spanner.Statement;
import com.google.cloud.spanner.pgadapter.ProxyServer.DataFormat;
import com.google.cloud.spanner.pgadapter.error.PGExceptionFactory;
import java.nio.charset.StandardCharsets;
import javax.annotation.Nonnull;
import org.postgresql.util.ByteConverter;

/** Translate from wire protocol to float. */
class FloatParser extends Parser<Float> {
public class FloatParser extends Parser<Float> {

FloatParser(ResultSet item, int position) {
this.item = item.getFloat(position);
}

FloatParser(Object item) {
this.item = (Float) item;
Expand All @@ -45,6 +55,14 @@
}
}

public static float toFloat(@Nonnull byte[] data) {
if (data.length < 4) {
throw SpannerExceptionFactory.newSpannerException(
ErrorCode.INVALID_ARGUMENT, "Invalid length for float4: " + data.length);
}
return ByteConverter.float4(data, 0);
}

@Override
public String stringParse() {
return this.item == null ? null : Float.toString(this.item);
Expand All @@ -55,11 +73,28 @@
if (this.item == null) {
return null;
}
return convertToPG(this.item);
}

static byte[] convertToPG(float value) {
byte[] result = new byte[4];
ByteConverter.float4(result, 0, this.item);
ByteConverter.float4(result, 0, value);
return result;
}

public static byte[] convertToPG(ResultSet resultSet, int position, DataFormat format) {
switch (format) {
case SPANNER:
case POSTGRESQL_TEXT:
return Float.toString(resultSet.getFloat(position)).getBytes(StandardCharsets.UTF_8);
case POSTGRESQL_BINARY:
return convertToPG(resultSet.getFloat(position));
default:
throw new IllegalArgumentException("unknown data format: " + format);

Check warning on line 93 in src/main/java/com/google/cloud/spanner/pgadapter/parsers/FloatParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/google/cloud/spanner/pgadapter/parsers/FloatParser.java#L93

Added line #L93 was not covered by tests
}
}

@Override
public void bind(Statement.Builder statementBuilder, String name) {
statementBuilder.bind(name).to(this.item);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ public static Parser<?> create(
return new BinaryParser(result, columnarPosition);
case DATE:
return new DateParser(result, columnarPosition);
case FLOAT32:
return new FloatParser(result, columnarPosition);
case FLOAT64:
return new DoubleParser(result, columnarPosition);
case INT64:
Expand Down Expand Up @@ -214,6 +216,8 @@ protected static Parser<?> create(Object result, Code typeCode, SessionState ses
return new BinaryParser(result);
case DATE:
return new DateParser(result);
case FLOAT32:
return new FloatParser(result);
case FLOAT64:
return new DoubleParser(result);
case INT64:
Expand Down Expand Up @@ -248,6 +252,8 @@ public static int toOid(Type type) {
return Oid.INT8;
case PG_NUMERIC:
return Oid.NUMERIC;
case FLOAT32:
return Oid.FLOAT4;
case FLOAT64:
return Oid.FLOAT8;
case STRING:
Expand All @@ -268,6 +274,8 @@ public static int toOid(Type type) {
return Oid.INT8_ARRAY;
case PG_NUMERIC:
return Oid.NUMERIC_ARRAY;
case FLOAT32:
return Oid.FLOAT4_ARRAY;
case FLOAT64:
return Oid.FLOAT8_ARRAY;
case STRING:
Expand Down Expand Up @@ -308,6 +316,7 @@ public static Type toType(int oid) {
case Oid.DATE:
return Type.date();
case Oid.FLOAT4:
return Type.float32();
case Oid.FLOAT8:
return Type.float64();
case Oid.INT2:
Expand Down Expand Up @@ -364,6 +373,8 @@ public static int toOid(com.google.spanner.v1.Type type) {
return Oid.INT8;
case NUMERIC:
return Oid.NUMERIC;
case FLOAT32:
return Oid.FLOAT4;
case FLOAT64:
return Oid.FLOAT8;
case STRING:
Expand All @@ -384,6 +395,8 @@ public static int toOid(com.google.spanner.v1.Type type) {
return Oid.INT8_ARRAY;
case NUMERIC:
return Oid.NUMERIC_ARRAY;
case FLOAT32:
return Oid.FLOAT4_ARRAY;
case FLOAT64:
return Oid.FLOAT8_ARRAY;
case STRING:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ static Type parsePostgreSQLDataType(String columnType) {
return Type.bool();
case "bigint":
return Type.int64();
case "float4":
case "real":
return Type.float32();
case "float8":
case "double precision":
return Type.float64();
Expand Down
Loading
Loading