Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix: table-editor-negative-bigint-filter-precision (#47471)

Seid Muhammedf9fc5c802009
diff --git a/apps/studio/data/table-rows/utils.test.ts b/apps/studio/data/table-rows/utils.test.ts
new file mode 100644
index 00000000000..d6e449618d8
--- /dev/null
+++ b/apps/studio/data/table-rows/utils.test.ts
@@ -0,0 +1,59 @@
+import { describe, expect, it } from 'vitest'
+
+import { formatFilterValue } from './utils'
+import type { Filter } from '@/components/grid/types'
+
+const table = {
+ columns: [
+ { name: 'id', format: 'int8' },
+ { name: 'count', format: 'int4' },
+ { name: 'name', format: 'text' },
+ { name: 'price', format: 'numeric' },
+ ],
+}
+
+const makeFilter = (column: string, value: any): Filter => ({
+ column,
+ operator: '=',
+ value,
+})
+
+describe('formatFilterValue', () => {
+ it('returns the value untouched for non-numerical columns', () => {
+ expect(formatFilterValue(table, makeFilter('name', '123'))).toBe('123')
+ })
+
+ it('coerces in-range numerical values to a number', () => {
+ expect(formatFilterValue(table, makeFilter('count', '42'))).toBe(42)
+ expect(formatFilterValue(table, makeFilter('id', '-42'))).toBe(-42)
+ })
+
+ it('returns the original string when the value is not a valid number', () => {
+ expect(formatFilterValue(table, makeFilter('count', 'abc'))).toBe('abc')
+ })
+
+ it('preserves large positive bigints as strings to avoid precision loss', () => {
+ const int8Max = '9223372036854775807'
+ expect(formatFilterValue(table, makeFilter('id', int8Max))).toBe(int8Max)
+ })
+
+ // Regression: the previous implementation only guarded the upper bound
+ // (numberValue > MAX_SAFE_INTEGER), so large negative bigints were converted
+ // through Number() and silently rounded, producing a wrong filter value.
+ it('preserves large negative bigints as strings to avoid precision loss', () => {
+ const int8Min = '-9223372036854775808'
+ expect(formatFilterValue(table, makeFilter('id', int8Min))).toBe(int8Min)
+
+ const justBelowSafe = '-9007199254740993'
+ expect(formatFilterValue(table, makeFilter('id', justBelowSafe))).toBe(justBelowSafe)
+ })
+
+ it('keeps the exact safe-integer bounds as numbers', () => {
+ expect(formatFilterValue(table, makeFilter('id', String(Number.MAX_SAFE_INTEGER)))).toBe(
+ Number.MAX_SAFE_INTEGER
+ )
+ expect(formatFilterValue(table, makeFilter('id', String(Number.MIN_SAFE_INTEGER)))).toBe(
+ Number.MIN_SAFE_INTEGER
+ )
+ })
+})
diff --git a/apps/studio/data/table-rows/utils.ts b/apps/studio/data/table-rows/utils.ts
index 4feb6adb43d..80aa54d9ded 100644
--- a/apps/studio/data/table-rows/utils.ts
+++ b/apps/studio/data/table-rows/utils.ts
@@ -18,9 +18,9 @@ export function formatFilterValue(
const column = table.columns.find((x) => x.name == filter.column)
if (column && isNumericalColumn(column.format)) {
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)
+ if (Number.isNaN(numberValue) || Math.abs(numberValue) > Number.MAX_SAFE_INTEGER)
+ return filter.value
+ else return numberValue
}
return filter.value
}