-
Notifications
You must be signed in to change notification settings - Fork 154
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
feat(electric): Add support for the int8 column type #653
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
VAX-820 Implement support for int8 type
The caveat with int8 is that it cannot be represented natively by the Number type in JS. There is, however, the BigInt type.
|
alco
force-pushed
the
alco/vax-820-add-support-for-int8
branch
from
November 12, 2023 11:16
8701eca
to
bd6bb90
Compare
alco
commented
Nov 12, 2023
alco
force-pushed
the
alco/vax-820-add-support-for-int8
branch
from
November 13, 2023 15:55
bd6bb90
to
4fca4bd
Compare
magnetised
approved these changes
Nov 23, 2023
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks great. good to get those big-ol ints back.
This PR adds client-side support for int8 aka bigint. Supporting BigInts is tricky because of inconsistencies in the sqlite drivers. For example, better-sqlite3 requires BigInts to be explicitly enabled but then *all* integers are returned as BigInts because SQLite does not distinguish between them (they are all just 64bit integers). On the other hand, wa-sqlite returns a regular JS number if the integer fits, otherwise it returns a BigInt. To avoid these problems, we always cast BigInt to text when reading from the DB and then let the DAL turn it into a BigInt; i.e., when the Postgres type of a column "foo" is bigint, we read as follows: `SELECT cast(foo as TEXT) AS foo FROM table`. Similarly, we cast returned values: `INSERT INTO table VALUES (9223372036854775807) RETURNING cast(foo as TEXT) AS foo`. A similar problem occurs in the oplog triggers. These triggers insert an oplog entry for every insert/update/delete. This oplog entry contains the old and new rows as JSON objects. However, JSON does not support BigInts: ```sql sqlite> SELECT json_valid('{"a": 123n}'); 0 ``` We also can't write it as a regular number because when parsing the JSON it is read as a number and rounding errors occur: ```js js> JSON.parse('{"a": 9223372036854775807}') { a: 9223372036854776000 } ``` Hence, the triggers also need to cast BigInts to text and the deserialisation function for oplog entries (`deserialiseRow`) needs to correctly deserialise them as BigInts (when the column type is int8/bigint). --------- Co-authored-by: David Martos <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.