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

fix(satellite): Ensure decimal point in real number encoding #508

Merged
merged 4 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions clients/typescript/src/satellite/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,7 @@ function deserializeColumnData(
return typeDecoder.text(column)
case 'BOOL':
return typeDecoder.bool(column)
case 'REAL':
case 'FLOAT4':
case 'FLOAT8':
case 'INT':
Expand All @@ -1060,6 +1061,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
13 changes: 13 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,18 @@ export function numberToBytes(i: number) {
)
}

export function realToBytes(num: number) {
let num_str = ''
if (Math.trunc(num) === num) {
// num is an integer, we need to explicitly append the ".0" to it.
num_str += num + '.0'
alco marked this conversation as resolved.
Show resolved Hide resolved
} else {
// num has a fractional part, so the default string conversion produces correct result
num_str += num
}
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
Loading