Files
supabase/apps/studio/data/table-rows/utils.ts
Seid Muhammed f9fc5c8020 fix: table-editor-negative-bigint-filter-precision (#47471)
Fixes: #47470

## I have read the
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
file.

YES

## What kind of change does this PR introduce?

Bug fix.

## What is the current behavior?

In the Table Editor, filtering a `bigint` (`int8`) column by a large
**negative** value
returns the wrong results (the matching row does not appear), while the
equivalent large
**positive** value works correctly.

`formatFilterValue` (`apps/studio/data/table-rows/utils.ts`) keeps
out-of-range bigint
filter values as strings so they reach Postgres without precision loss,
but it only guards
the upper end of the JS safe-integer range:

```ts
const numberValue = Number(filter.value)
// Supports BigInt filter values
if (Number.isNaN(numberValue) || numberValue > Number.MAX_SAFE_INTEGER) return filter.value
else return Number(filter.value)
```

`numberValue > Number.MAX_SAFE_INTEGER` is always `false` for negative
numbers, so large
negative bigints (e.g. the int8 minimum `-9223372036854775808`) fall
through and get rounded
by `Number()` (`Number('-9223372036854775808')` →
`-9223372036854776000`). The rounded value
is then sent to SQL, so the filter no longer matches the intended row.
The same helper feeds
the row count and "delete all matching" queries.

Steps to reproduce:

1. Create a table with a `bigint` column `id`.
2. Insert a row with `id = -9223372036854775808`.
3. In the Table Editor, filter `id = -9223372036854775808`.
4. The row is not returned. Filtering by `9223372036854775807` works as
expected.

## What is the new behavior?

Large negative bigints are now preserved as strings just like large
positive ones, so the
literal sent to Postgres matches what the user typed and the filter
returns the correct rows.

The fix guards the safe-integer range by magnitude:

```ts
if (Number.isNaN(numberValue) || Math.abs(numberValue) > Number.MAX_SAFE_INTEGER)
  return filter.value
else return numberValue
```

In-range values and large positive bigints are unaffected.

## Additional context

- Added unit tests in `apps/studio/data/table-rows/utils.test.ts`
covering non-numerical
passthrough, in-range coercion (positive and negative), `NaN`
passthrough, large positive
bigints (existing behavior), large negative bigints (regression), and
the exact
  safe-integer bounds.
- The negative-bigint test fails on `master` and passes with this
change.

Verify locally:

```bash
pnpm --filter studio exec vitest run data/table-rows/utils.test.ts
```

No API, schema, or infrastructure changes.


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Bug Fixes**
* Improved filter value formatting to keep the original input when
numeric conversion would be unsafe (invalid numbers or values outside
safe-integer bounds), including large negative inputs.

* **Tests**
* Added automated coverage for filter value formatting across
non-numeric values, valid numeric coercion, invalid numeric strings, and
bigint-like edge cases (including a large negative regression case).
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Ali Waseem <waseema393@gmail.com>
2026-07-02 10:05:13 -06:00

46 lines
1.3 KiB
TypeScript

import type { Filter, ServiceError } from '@/components/grid/types'
import { isNumericalColumn } from '@/components/grid/utils/types'
import { Entity, isTableLike } from '@/data/table-editor/table-editor-types'
/**
* temporary fix until we implement a better filter UI
* which validate input value base on the column type
*/
export function formatFilterValue(
table: {
columns: {
name: string
format: string
}[]
},
filter: Filter
) {
const column = table.columns.find((x) => x.name == filter.column)
if (column && isNumericalColumn(column.format)) {
const numberValue = Number(filter.value)
if (Number.isNaN(numberValue) || Math.abs(numberValue) > Number.MAX_SAFE_INTEGER)
return filter.value
else return numberValue
}
return filter.value
}
export function getPrimaryKeys({ table }: { table: Entity }): {
primaryKeys?: string[]
error?: ServiceError
} {
if (!isTableLike(table)) {
return {
error: { message: 'Only table rows can be updated or deleted' },
}
}
const pkColumns = table.primary_keys
if (!pkColumns || pkColumns.length == 0) {
return {
error: { message: 'Please add a primary key column to your table to update or delete rows' },
}
}
return { primaryKeys: pkColumns.map((x) => x.name) }
}