From a28c50fef11d84efaae78000727a7581838c9db1 Mon Sep 17 00:00:00 2001 From: Oleksii Sholik Date: Fri, 29 Sep 2023 15:43:58 +0300 Subject: [PATCH 1/4] String encoding of REAL must always include the decimal point --- clients/typescript/src/satellite/client.ts | 4 ++++ clients/typescript/src/util/common.ts | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/clients/typescript/src/satellite/client.ts b/clients/typescript/src/satellite/client.ts index 40ab58b885..00a2e00a77 100644 --- a/clients/typescript/src/satellite/client.ts +++ b/clients/typescript/src/satellite/client.ts @@ -1060,6 +1060,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) } diff --git a/clients/typescript/src/util/common.ts b/clients/typescript/src/util/common.ts index da1eeae6a2..86d73e5a2a 100644 --- a/clients/typescript/src/util/common.ts +++ b/clients/typescript/src/util/common.ts @@ -26,6 +26,7 @@ export const typeDecoder = { export const typeEncoder = { bool: boolToBytes, number: numberToBytes, + real: realToBytes, text: (string: string) => new TextEncoder().encode(string), } @@ -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' + } 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()) { From 1cd287ccb7bf1132370d4c3e400bfda185191ccb Mon Sep 17 00:00:00 2001 From: Oleksii Sholik Date: Fri, 29 Sep 2023 15:52:25 +0300 Subject: [PATCH 2/4] Verify encoding of REAL values in tests --- .../typescript/test/satellite/serialization.test.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/clients/typescript/test/satellite/serialization.test.ts b/clients/typescript/test/satellite/serialization.test.ts index 398c5e02fc..a13a2d871f 100644 --- a/clients/typescript/test/satellite/serialization.test.ts +++ b/clients/typescript/test/satellite/serialization.test.ts @@ -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 }, @@ -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) }) From a4143885bf21cafa39234d7bb415eceebd754036 Mon Sep 17 00:00:00 2001 From: Oleksii Sholik Date: Fri, 29 Sep 2023 15:52:47 +0300 Subject: [PATCH 3/4] Add missing REAL case in deserializeColumnData() --- clients/typescript/src/satellite/client.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/clients/typescript/src/satellite/client.ts b/clients/typescript/src/satellite/client.ts index 00a2e00a77..8969b38f6a 100644 --- a/clients/typescript/src/satellite/client.ts +++ b/clients/typescript/src/satellite/client.ts @@ -1037,6 +1037,7 @@ function deserializeColumnData( return typeDecoder.text(column) case 'BOOL': return typeDecoder.bool(column) + case 'REAL': case 'FLOAT4': case 'FLOAT8': case 'INT': From 85b494dd0c1c4555415d8e8d0c328596c1a6c687 Mon Sep 17 00:00:00 2001 From: Oleksii Sholik Date: Wed, 4 Oct 2023 11:14:09 +0300 Subject: [PATCH 4/4] Cleaner num to str conversion --- clients/typescript/src/util/common.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/clients/typescript/src/util/common.ts b/clients/typescript/src/util/common.ts index 86d73e5a2a..a4db34edb0 100644 --- a/clients/typescript/src/util/common.ts +++ b/clients/typescript/src/util/common.ts @@ -68,13 +68,10 @@ export function numberToBytes(i: number) { } export function realToBytes(num: number) { - let num_str = '' + 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 += num + '.0' - } else { - // num has a fractional part, so the default string conversion produces correct result - num_str += num + num_str += '.0' } return new TextEncoder().encode(num_str) }