Skip to content

Commit 9cbe8de

Browse files
committed
docs(readme): update readme to mention the BigInt parsing and precision errors
1 parent c933c9e commit 9cbe8de

File tree

1 file changed

+46
-4
lines changed

1 file changed

+46
-4
lines changed

presto-client/README.md

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,18 @@ try {
9797

9898
### Additional notes
9999

100-
Additional notes
100+
Additional notes on the `query` method:
101101

102-
- The `query` method is asynchronous and will return a promise that resolves to a PrestoQuery object.
103-
- The `query` method will automatically retry the query if it fails due to a transient error.
104-
- The `query` method will cancel the query if the client is destroyed.
102+
- It's asynchronous and will return a promise that resolves to a PrestoQuery object.
103+
- It will automatically retry the query if it fails due to a transient error.
104+
- It will cancel the query if the client is destroyed.
105+
- \*It parses big numbers with the BigInt JavaScript primitive. If your Presto response includes a number bigger than `Number.MAX_SAFE_INTEGER`, it will be parsed into a bigint, so you may need to consider that when handling the response, or serializing it.
106+
107+
\* Only if the current JavaScript environment supports the reviver with context in the JSON.parse callback. Check compatibility here:
108+
109+
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#browser_compatibility
110+
111+
Otherwise, bigger numbers will lose precision due to the default JavaScript JSON parsing.
105112

106113
## Get Query metadata information
107114

@@ -240,3 +247,38 @@ const client = new PrestoClient({
240247
user: 'root',
241248
})
242249
```
250+
251+
## Troubleshooting
252+
253+
### Do not know how to serialize a BigInt
254+
255+
Example error message:
256+
257+
```
258+
Do not know how to serialize a BigInt
259+
TypeError: Do not know how to serialize a BigInt
260+
at JSON.stringify (<anonymous>)
261+
```
262+
263+
Make sure to write a custom replacer and handle the serialization of BigInts if your Presto query returns a number bigger than `Number.MAX_SAFE_INTEGER`.
264+
Example JSON.stringify replacer:
265+
266+
```javascript
267+
const results = await client.query(`SELECT 1234567890123456623`)
268+
return {
269+
columns: results.columns,
270+
rows: JSON.stringify(results.data, (key, value) => {
271+
if (typeof value !== 'bigint') return value
272+
273+
return value.toString()
274+
}),
275+
}
276+
```
277+
278+
### Numbers lose precision
279+
280+
Known issue:
281+
If working with numbers bigger than `Number.MAX_SAFE_INTEGER`, and your environment does not support the `JSON.parse` with the context in the reviver (Node.js > 21.0.0, and certain browser versions), the default JSON.parse will make the number lose precision.
282+
Check compatibility here:
283+
284+
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#browser_compatibility

0 commit comments

Comments
 (0)