From b6fe3c09d21cb7b5250ea6637f9ae1c027a1458c Mon Sep 17 00:00:00 2001 From: Alexey Orlenko Date: Thu, 28 Sep 2023 10:07:42 +0200 Subject: [PATCH] adapter-libsql: fix failing apply_number_ops_for_int test (#4296) --- .../js/adapter-libsql/src/conversion.ts | 12 +++++++++++- .../driver-adapters/js/adapter-libsql/src/libsql.ts | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/query-engine/driver-adapters/js/adapter-libsql/src/conversion.ts b/query-engine/driver-adapters/js/adapter-libsql/src/conversion.ts index cf1af825db5e..b2fa4b5b4095 100644 --- a/query-engine/driver-adapters/js/adapter-libsql/src/conversion.ts +++ b/query-engine/driver-adapters/js/adapter-libsql/src/conversion.ts @@ -125,7 +125,7 @@ class UnexpectedTypeError extends Error { } } -export function mapRow(row: Row): unknown[] { +export function mapRow(row: Row, columnTypes: ColumnType[]): unknown[] { // `Row` doesn't have map, so we copy the array once and modify it in-place // to avoid allocating and copying twice if we used `Array.from(row).map(...)`. const result: unknown[] = Array.from(row) @@ -145,6 +145,16 @@ export function mapRow(row: Row): unknown[] { if (isArrayBuffer(value)) { result[i] = Array.from(new Uint8Array(value)) } + + // If an integer is required and the current number isn't one, + // discard the fractional part. + if ( + typeof value === 'number' && + (columnTypes[i] === ColumnTypeEnum.Int32 || columnTypes[i] === ColumnTypeEnum.Int64) && + !Number.isInteger(value) + ) { + result[i] = Math.trunc(value) + } } return result diff --git a/query-engine/driver-adapters/js/adapter-libsql/src/libsql.ts b/query-engine/driver-adapters/js/adapter-libsql/src/libsql.ts index fab2480ea5b8..5d104e8e2949 100644 --- a/query-engine/driver-adapters/js/adapter-libsql/src/libsql.ts +++ b/query-engine/driver-adapters/js/adapter-libsql/src/libsql.ts @@ -40,7 +40,7 @@ class LibSqlQueryable implements const resultSet: ResultSet = { columnNames: columns, columnTypes, - rows: rows.map(mapRow), + rows: rows.map((row) => mapRow(row, columnTypes)), } return ok(resultSet)