Skip to content

Commit

Permalink
adapter-libsql: fix failing apply_number_ops_for_int test (#4296)
Browse files Browse the repository at this point in the history
  • Loading branch information
aqrln authored Sep 28, 2023
1 parent f5cd0c1 commit b6fe3c0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
12 changes: 11 additions & 1 deletion query-engine/driver-adapters/js/adapter-libsql/src/conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class LibSqlQueryable<ClientT extends StdClient | TransactionClient> implements
const resultSet: ResultSet = {
columnNames: columns,
columnTypes,
rows: rows.map(mapRow),
rows: rows.map((row) => mapRow(row, columnTypes)),
}

return ok(resultSet)
Expand Down

0 comments on commit b6fe3c0

Please sign in to comment.