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(spanner): add support for float32 #2020

Merged
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
5de854b
feat(spanner): add support for float32
alkatrivedi Mar 8, 2024
348e965
refactor: codec.ts
alkatrivedi Mar 12, 2024
a1d57a6
refactor: codec.ts
alkatrivedi Mar 12, 2024
794f384
refactor: add sample to fetch float32 value from table
alkatrivedi Mar 12, 2024
a854b46
feat: add support for float32
alkatrivedi Mar 14, 2024
0289e31
fix: lint errors
alkatrivedi Mar 14, 2024
0adae71
fix: presubmit errors
alkatrivedi Mar 14, 2024
b3d0fff
fix: presubmit errors
alkatrivedi Mar 14, 2024
d8b584d
fix: presubmit errors
alkatrivedi Mar 14, 2024
cfe2dfc
fix: presubmit errors
alkatrivedi Mar 14, 2024
b96f8a9
fix: lint errors
alkatrivedi Mar 15, 2024
736f7d9
chore: add system-test for float32
alkatrivedi Mar 18, 2024
8679cf5
refactor: skip the integration test for float32
alkatrivedi Mar 18, 2024
fdf3ec8
fix: presubmit error
alkatrivedi Mar 18, 2024
59ce148
refactor: remove sample for float32 query
alkatrivedi Mar 18, 2024
e774e98
chore: add system tests for float32
alkatrivedi Mar 18, 2024
bae30ed
refactor tests
alkatrivedi Mar 19, 2024
fa068b3
fix: presubmit error
alkatrivedi Mar 19, 2024
94ece6a
fix: presubmit error
alkatrivedi Mar 19, 2024
59e4bb6
fix: presubmit error
alkatrivedi Mar 19, 2024
9fa5ab4
fix: presubmit error
alkatrivedi Mar 19, 2024
22d33f7
fix: presubmit error
alkatrivedi Mar 19, 2024
27740b0
Merge branch 'googleapis:main' into spanner-float32-support
alkatrivedi Mar 20, 2024
667dea9
refactor: system test for float32
alkatrivedi Mar 25, 2024
fe957b4
refactor: system test
alkatrivedi Mar 25, 2024
4a98236
refactor: system test
alkatrivedi Mar 25, 2024
b546880
fix: presubmit errors
alkatrivedi Mar 25, 2024
a9052d7
refactor: system-test
alkatrivedi Mar 25, 2024
dccaa32
skip float32 test
alkatrivedi Mar 25, 2024
69ffa96
skip float32 test
alkatrivedi Mar 25, 2024
a0e7cf1
fix: presubmit error
alkatrivedi Mar 25, 2024
a890603
chore: add test cases for float32 tests
alkatrivedi Mar 26, 2024
620c8c4
fix: lint errors
alkatrivedi Mar 26, 2024
8992dd9
fix: presubmit error
alkatrivedi Mar 26, 2024
03a7360
refactor: add comments
alkatrivedi Mar 26, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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',
alkatrivedi marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
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};
Loading
Loading