Skip to content

Commit

Permalink
fix(satellite): Ensure decimal point in real number encoding (#508)
Browse files Browse the repository at this point in the history
Fixes VAX-1078.

Related: #507
  • Loading branch information
alco authored Oct 4, 2023
1 parent b561d5c commit 8dbd8bd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
5 changes: 5 additions & 0 deletions clients/typescript/src/satellite/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,7 @@ function deserializeColumnData(
return typeDecoder.text(column)
case 'BOOL':
return typeDecoder.bool(column)
case 'REAL':
case 'FLOAT4':
case 'FLOAT8':
case 'INT':
Expand All @@ -1061,6 +1062,10 @@ function serializeColumnData(
switch (col_type.toUpperCase()) {
case 'BOOL':
return typeEncoder.bool(col_val as number)
case 'REAL':
case 'FLOAT4':
case 'FLOAT8':
return typeEncoder.real(col_val as number)
default:
return typeEncoder.text(col_val as string)
}
Expand Down
10 changes: 10 additions & 0 deletions clients/typescript/src/util/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const typeDecoder = {
export const typeEncoder = {
bool: boolToBytes,
number: numberToBytes,
real: realToBytes,
text: (string: string) => new TextEncoder().encode(string),
}

Expand Down Expand Up @@ -66,6 +67,15 @@ export function numberToBytes(i: number) {
)
}

export function realToBytes(num: number) {
let num_str = num.toString()
if (Math.trunc(num) === num) {
// num is an integer, we need to explicitly append the ".0" to it.
num_str += '.0'
}
return new TextEncoder().encode(num_str)
}

export function bytesToNumber(bytes: Uint8Array) {
let n = 0
for (const byte of bytes.values()) {
Expand Down
11 changes: 8 additions & 3 deletions clients/typescript/test/satellite/serialization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test('serialize/deserialize row data', async (t) => {
{ name: 'name3', type: 'TEXT', isNullable: true },
{ name: 'int1', type: 'INTEGER', isNullable: true },
{ name: 'int2', type: 'INTEGER', isNullable: true },
{ name: 'float1', type: 'FLOAT4', isNullable: true },
{ name: 'float1', type: 'REAL', isNullable: true },
{ name: 'float2', type: 'FLOAT4', isNullable: true },
{ name: 'bool1', type: 'BOOL', isNullable: true },
{ name: 'bool2', type: 'BOOL', isNullable: true },
Expand All @@ -29,15 +29,20 @@ test('serialize/deserialize row data', async (t) => {
name3: null,
int1: 1,
int2: -30,
float1: 1.1,
float1: 1.0,
float2: -30.3,
bool1: 1,
bool2: 0,
bool3: null,
}

const s_row = serializeRow(record, rel)
const d_row = deserializeRow(s_row, rel)
t.deepEqual(
s_row.values.map((bytes) => new TextDecoder().decode(bytes)),
['Hello', 'World!', '', '1', '-30', '1.0', '-30.3', 't', 'f', '']
)

const d_row = deserializeRow(s_row, rel)
t.deepEqual(record, d_row)
})

Expand Down

0 comments on commit 8dbd8bd

Please sign in to comment.