Skip to content

Commit

Permalink
feat(spanner): add support for float32 (#2020)
Browse files Browse the repository at this point in the history
This PR contains support for FLOAT32 type on Cloud Spanner.
  • Loading branch information
alkatrivedi committed Mar 26, 2024
1 parent b1424e5 commit 99e2c1d
Show file tree
Hide file tree
Showing 6 changed files with 593 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -12,3 +12,4 @@ system-test/*key.json
.DS_Store
package-lock.json
__pycache__
.vscode
26 changes: 26 additions & 0 deletions src/codec.ts
Expand Up @@ -118,6 +118,21 @@ abstract class WrappedNumber {
abstract valueOf(): number;
}

/**
* @typedef Float32
* @see Spanner.float32
*/
export class Float32 extends WrappedNumber {
value: number;
constructor(value: number) {
super();
this.value = value;
}
valueOf(): number {
return Number(this.value);
}
}

/**
* @typedef Float
* @see Spanner.float
Expand Down Expand Up @@ -377,6 +392,10 @@ function decode(value: Value, type: spannerClient.spanner.v1.Type): Value {
case 'BYTES':
decoded = Buffer.from(decoded, 'base64');
break;
case spannerClient.spanner.v1.TypeCode.FLOAT32:
case 'FLOAT32':
decoded = new Float32(decoded);
break;
case spannerClient.spanner.v1.TypeCode.FLOAT64:
case 'FLOAT64':
decoded = new Float(decoded);
Expand Down Expand Up @@ -531,6 +550,7 @@ const TypeCode: {
bool: 'BOOL',
int64: 'INT64',
pgOid: 'INT64',
float32: 'FLOAT32',
float64: 'FLOAT64',
numeric: 'NUMERIC',
pgNumeric: 'NUMERIC',
Expand Down Expand Up @@ -567,6 +587,7 @@ interface FieldType extends Type {
/**
* @typedef {object} ParamType
* @property {string} type The param type. Must be one of the following:
* - float32
* - float64
* - int64
* - numeric
Expand Down Expand Up @@ -601,6 +622,10 @@ function getType(value: Value): Type {
const isSpecialNumber =
is.infinite(value) || (is.number(value) && isNaN(value));

if (value instanceof Float32) {
return {type: 'float32'};
}

if (is.decimal(value) || isSpecialNumber || value instanceof Float) {
return {type: 'float64'};
}
Expand Down Expand Up @@ -780,6 +805,7 @@ export const codec = {
convertProtoTimestampToDate,
createTypeObject,
SpannerDate,
Float32,
Float,
Int,
Numeric,
Expand Down
20 changes: 19 additions & 1 deletion src/index.ts
Expand Up @@ -26,6 +26,7 @@ import * as streamEvents from 'stream-events';
import * as through from 'through2';
import {
codec,
Float32,
Float,
Int,
Numeric,
Expand Down Expand Up @@ -1671,6 +1672,22 @@ class Spanner extends GrpcService {
return new PreciseDate(value as number);
}

/**
* Helper function to get a Cloud Spanner Float32 object.
*
* @param {string|number} value The float as a number or string.
* @returns {Float32}
*
* @example
* ```
* const {Spanner} = require('@google-cloud/spanner');
* const float = Spanner.float32(10);
* ```
*/
static float32(value): Float32 {
return new codec.Float32(value);
}

/**
* Helper function to get a Cloud Spanner Float64 object.
*
Expand Down Expand Up @@ -1786,6 +1803,7 @@ class Spanner extends GrpcService {
promisifyAll(Spanner, {
exclude: [
'date',
'float32',
'float',
'instance',
'instanceConfig',
Expand Down Expand Up @@ -1946,4 +1964,4 @@ import * as protos from '../protos/protos';
import IInstanceConfig = instanceAdmin.spanner.admin.instance.v1.IInstanceConfig;
export {v1, protos};
export default {Spanner};
export {Float, Int, Struct, Numeric, PGNumeric, SpannerDate};
export {Float32, Float, Int, Struct, Numeric, PGNumeric, SpannerDate};

0 comments on commit 99e2c1d

Please sign in to comment.