Skip to content

Commit 99e2c1d

Browse files
authored
feat(spanner): add support for float32 (#2020)
This PR contains support for FLOAT32 type on Cloud Spanner.
1 parent b1424e5 commit 99e2c1d

File tree

6 files changed

+593
-2
lines changed

6 files changed

+593
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ system-test/*key.json
1212
.DS_Store
1313
package-lock.json
1414
__pycache__
15+
.vscode

src/codec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,21 @@ abstract class WrappedNumber {
118118
abstract valueOf(): number;
119119
}
120120

121+
/**
122+
* @typedef Float32
123+
* @see Spanner.float32
124+
*/
125+
export class Float32 extends WrappedNumber {
126+
value: number;
127+
constructor(value: number) {
128+
super();
129+
this.value = value;
130+
}
131+
valueOf(): number {
132+
return Number(this.value);
133+
}
134+
}
135+
121136
/**
122137
* @typedef Float
123138
* @see Spanner.float
@@ -377,6 +392,10 @@ function decode(value: Value, type: spannerClient.spanner.v1.Type): Value {
377392
case 'BYTES':
378393
decoded = Buffer.from(decoded, 'base64');
379394
break;
395+
case spannerClient.spanner.v1.TypeCode.FLOAT32:
396+
case 'FLOAT32':
397+
decoded = new Float32(decoded);
398+
break;
380399
case spannerClient.spanner.v1.TypeCode.FLOAT64:
381400
case 'FLOAT64':
382401
decoded = new Float(decoded);
@@ -531,6 +550,7 @@ const TypeCode: {
531550
bool: 'BOOL',
532551
int64: 'INT64',
533552
pgOid: 'INT64',
553+
float32: 'FLOAT32',
534554
float64: 'FLOAT64',
535555
numeric: 'NUMERIC',
536556
pgNumeric: 'NUMERIC',
@@ -567,6 +587,7 @@ interface FieldType extends Type {
567587
/**
568588
* @typedef {object} ParamType
569589
* @property {string} type The param type. Must be one of the following:
590+
* - float32
570591
* - float64
571592
* - int64
572593
* - numeric
@@ -601,6 +622,10 @@ function getType(value: Value): Type {
601622
const isSpecialNumber =
602623
is.infinite(value) || (is.number(value) && isNaN(value));
603624

625+
if (value instanceof Float32) {
626+
return {type: 'float32'};
627+
}
628+
604629
if (is.decimal(value) || isSpecialNumber || value instanceof Float) {
605630
return {type: 'float64'};
606631
}
@@ -780,6 +805,7 @@ export const codec = {
780805
convertProtoTimestampToDate,
781806
createTypeObject,
782807
SpannerDate,
808+
Float32,
783809
Float,
784810
Int,
785811
Numeric,

src/index.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import * as streamEvents from 'stream-events';
2626
import * as through from 'through2';
2727
import {
2828
codec,
29+
Float32,
2930
Float,
3031
Int,
3132
Numeric,
@@ -1671,6 +1672,22 @@ class Spanner extends GrpcService {
16711672
return new PreciseDate(value as number);
16721673
}
16731674

1675+
/**
1676+
* Helper function to get a Cloud Spanner Float32 object.
1677+
*
1678+
* @param {string|number} value The float as a number or string.
1679+
* @returns {Float32}
1680+
*
1681+
* @example
1682+
* ```
1683+
* const {Spanner} = require('@google-cloud/spanner');
1684+
* const float = Spanner.float32(10);
1685+
* ```
1686+
*/
1687+
static float32(value): Float32 {
1688+
return new codec.Float32(value);
1689+
}
1690+
16741691
/**
16751692
* Helper function to get a Cloud Spanner Float64 object.
16761693
*
@@ -1786,6 +1803,7 @@ class Spanner extends GrpcService {
17861803
promisifyAll(Spanner, {
17871804
exclude: [
17881805
'date',
1806+
'float32',
17891807
'float',
17901808
'instance',
17911809
'instanceConfig',
@@ -1946,4 +1964,4 @@ import * as protos from '../protos/protos';
19461964
import IInstanceConfig = instanceAdmin.spanner.admin.instance.v1.IInstanceConfig;
19471965
export {v1, protos};
19481966
export default {Spanner};
1949-
export {Float, Int, Struct, Numeric, PGNumeric, SpannerDate};
1967+
export {Float32, Float, Int, Struct, Numeric, PGNumeric, SpannerDate};

0 commit comments

Comments
 (0)