Skip to content

fix(query): carry BIGINT as text and quote from column metadata - #510

Merged
EVWorth merged 1 commit into
mainfrom
feat/502-numeric-precision-via-column-metadata
Aug 28, 2026
Merged

EVWorth merged 1 commit into
mainfrom
feat/502-numeric-precision-via-column-metadata

Conversation

@EVWorth

@EVWorth EVWorth commented Aug 28, 2026

Copy link
Copy Markdown
Owner

Closes #502. Step 2 of the restructure, after #509.

The bug

BIGINT exceeds what a JSON number can represent, so any value above 2^53 reached the grid with a silently wrong final digit:

9007199254740993  ->  9007199254740992

No error, nothing logged. Worst in copy-cell and copy-as-INSERT, where a truncated id gets pasted straight into another query.

The fix, and the trap in it

Carry 64-bit integers as text, as DECIMAL already is after #509. Types up to 32 bits still travel as JSON numbers — every value of those fits exactly, so only BIGINT needs this. mysql2 draws the line in exactly the same place (SHORT/LONG/INT24 use parseLengthCodedIntNoBigCheck, only LONGLONG gets the treatment), and its bigNumberStrings is what Beekeeper Studio turns on for this.

The trap: that breaks the assumption typeof value === "number" identifies a number, which two places relied on to decide quoting — SqlValueGuard.toSqlLiteral and formatSqlValue. Left alone they would emit '9007199254740993' quoted, turning a precision fix into a correctness regression in generated SQL.

So quoting now comes from ColumnMeta.data_type. That is what SQLyog does — IS_NUM(field->type) drives both right-alignment and whether a generated INSERT quotes — and it's the reason a client can hand back big numbers as strings without breaking its SQL generation. New src/lib/sql-types.ts holds that predicate so the grid and the generator share one definition.

Safety

Unquoting requires both that the column claims to be numeric and that the text parses as a number:

if (isNumericSqlType(dataType) && isNumericLiteral(val)) return val;

Trusting the declared type alone would splice arbitrary text into a statement unquoted if the two ever disagreed. Covered by tests, including "1; DROP TABLE users" against a bigint column, which stays quoted.

Tests

  • sql-types.test.ts — type parsing (DECIMAL(20,4), BIGINT UNSIGNED), and the numeric-literal guard against injection-shaped input
  • sql-generator.test.ts — INSERT/UPDATE/DELETE keep a stringified BIGINT unquoted in both SET and WHERE; a varchar column holding digits stays quoted; and a test pinning that without column types the id would be quoted, so the regression this prevents is visible
  • type_coverage.rs — bigint_keeps_every_digit asserts 2^53+1 survives, including across the JSON round-trip where the truncation actually happened

Verification

1589 frontend unit tests (+23), 290 browser tests, 5 type-coverage and 39 integration tests against real MySQL, cargo clippy --all-features -D warnings and cargo fmt --check clean, type-check clean, lint unchanged at 180 warnings / 0 errors.

BIGINT exceeds what a JSON number can represent, so a value above 2^53
arrived at the grid with a silently wrong final digit — and got pasted
into another query from copy-as-INSERT. 9007199254740993 became
...992, with no error anywhere.

Carry 64-bit integers as text, as DECIMAL already is after #508. Types
up to 32 bits still travel as JSON numbers, since every value of those
fits exactly; only BIGINT needs it. mysql2 draws the line in the same
place, and its bigNumberStrings option is what Beekeeper Studio enables
for this exact reason.

That breaks the assumption `typeof value === "number"` means numeric,
which both toSqlLiteral and formatSqlValue relied on to decide quoting.
Left alone they would emit '9007199254740993' — quoted — turning a
precision fix into a correctness regression. Quoting now comes from
ColumnMeta.data_type instead, which is what SQLyog does: IS_NUM on the
field drives both alignment and whether a generated INSERT quotes.

Unquoting requires the column to claim numeric *and* the text to parse
as a number. Trusting the declared type alone would splice arbitrary
text into a statement unquoted if the two ever disagreed.

closes #502

Co-Authored-By: Claude Opus 5 <[email protected]>
@EVWorth
EVWorth merged commit 1d2f883 into main Aug 28, 2026
9 checks passed
@EVWorth
EVWorth deleted the feat/502-numeric-precision-via-column-metadata branch August 28, 2026 01:37
EVWorth added a commit that referenced this pull request Aug 28, 2026
…511)

There were two definitions of "is this column numeric" that disagreed.
EditableCell had a substring regex, /int|decimal|.../, matched against
the lowercased type — so POINT and MULTIPOINT counted as numeric
(po·int) while YEAR did not. #510 then added a second, stricter one in
sql-types without noticing the first.

Consolidated into sql-types, matching the base type rather than a
substring, which fixes both misclassifications on the way past.
isBooleanType and isLongTextType move there too; the latter was
duplicated verbatim in EditableCell and TruncatedCell.

Consolidating surfaced a third instance of the precision bug: on commit,
parseEditValue pushed any numeric column through Number(), so editing a
BIGINT wrote back a value with the last digit dropped. isExactNumericType
now keeps BIGINT and DECIMAL as text, matching how they arrive from Rust,
and those cells use a text input since a number input normalises them
too.

Alignment, which the grid had none of: numeric columns are right-aligned
with tabular figures, headers follow their column. Driven by the declared
type, since BIGINT and DECIMAL arrive as strings and the value's
JavaScript type no longer identifies a number — the same reason SQLyog
aligns from IS_NUM(field->type). For DECIMAL this is decimal-point
alignment for free, as MySQL returns a fixed scale.

NULL is styled muted and italic instead of being left-aligned. It read
identically to a VARCHAR containing the text "NULL", and styling
separates them without breaking a column's run of digits.

Co-authored-by: Claude Opus 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[kind/bug] BIGINT cell values lose precision past 2^53

1 participant