The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Deprecate formatFilterValue, consistently use formatPatternMatchFilterValue across all table-row-operations (#51918)
Joshen Lim3e8d7fa795d8
diff --git a/apps/studio/data/table-rows/table-row-delete-all-mutation.ts b/apps/studio/data/table-rows/table-row-delete-all-mutation.tsindex 00ae2f14c69..ff5c9416866 100644--- a/apps/studio/data/table-rows/table-row-delete-all-mutation.ts+++ b/apps/studio/data/table-rows/table-row-delete-all-mutation.ts@@ -1,9 +1,9 @@import { Query } from '@supabase/pg-meta/src/query'+import { formatPatternMatchFilterValue } from '@supabase/pg-meta/src/query/table-row-query'import { useMutation, useQueryClient } from '@tanstack/react-query'import { toast } from 'sonner'import { tableRowKeys } from './keys'-import { formatFilterValue } from './utils'import type { Filter } from '@/components/grid/types'import { executeSql } from '@/data/sql/execute-sql-mutation'import { Entity } from '@/data/table-editor/table-editor-types'@@ -28,7 +28,7 @@ export function getTableRowDeleteAllSql({filters.filter((x) => x.value && x.value !== '').forEach((x) => {- const value = formatFilterValue(table, x)+ const value = formatPatternMatchFilterValue(x.value, x.operator)queryChains = queryChains.filter(x.column, x.operator, value)})diff --git a/apps/studio/data/table-rows/table-row-filters.test.ts b/apps/studio/data/table-rows/table-row-filters.test.tsnew file mode 100644index 00000000000..b7cf0049534--- /dev/null+++ b/apps/studio/data/table-rows/table-row-filters.test.ts@@ -0,0 +1,81 @@+import { formatPatternMatchFilterValue } from '@supabase/pg-meta/src/query/table-row-query'+import { describe, expect, it } from 'vitest'++import { getTableRowDeleteAllSql } from './table-row-delete-all-mutation'+import { getAllTableRowsSql } from './table-rows-query'+import type { Filter, SupaTable } from '@/components/grid/types'+import type { Entity } from '@/data/table-editor/table-editor-types'++const makeFilter = (column: string, value: any, operator: Filter['operator'] = '='): Filter => ({+ column,+ operator,+ value,+})++const table = {+ id: 1,+ name: 'precise_values',+ schema: 'public',+ columns: [+ { name: 'id', dataType: 'bigint', format: 'int8' },+ { name: 'amount', dataType: 'numeric', format: 'numeric' },+ { name: 'name', dataType: 'text', format: 'text' },+ ],+} as unknown as SupaTable++const sqlFor = (filter: Filter) =>+ String(getAllTableRowsSql({ table, filters: [filter] }).sql.toSql())++describe('table row filters keep numeric values exact', () => {+ // High-precision numerics lose digits when coerced through Number(), so the+ // generated `=` comparison would no longer match the stored value.+ const preciseValues = [+ '0.123456789012345678901', // numeric(30, 21)+ '12345678901234567890.5',+ '9223372036854775807', // int8 max+ '-9223372036854775808', // int8 min+ '-9007199254740993', // just below MIN_SAFE_INTEGER+ ]++ it.each(preciseValues)('rows query emits %s as a quoted literal', (value) => {+ expect(sqlFor(makeFilter('amount', value))).toContain(`amount = '${value}'`)+ })++ it.each(preciseValues)('delete-all query emits %s as a quoted literal', (value) => {+ const sql = String(+ getTableRowDeleteAllSql({+ table: table as unknown as Entity,+ filters: [makeFilter('amount', value)],+ })+ )+ expect(sql).toContain(`amount = '${value}'`)+ })++ it('does not rewrite the numeric form of the input', () => {+ expect(sqlFor(makeFilter('amount', '1.0'))).toContain(`amount = '1.0'`)+ expect(sqlFor(makeFilter('amount', '1e3'))).toContain(`amount = '1e3'`)+ })+})++describe('formatPatternMatchFilterValue', () => {+ it('returns the value untouched for non-pattern operators', () => {+ expect(formatPatternMatchFilterValue('123', '=')).toBe('123')+ expect(formatPatternMatchFilterValue('abc', '=')).toBe('abc')+ })++ it.each(['~~', '~~*', '!~~', '!~~*'] as const)(+ 'wraps the value in %%...%% for the %s operator when no wildcard is present',+ (operator) => {+ expect(formatPatternMatchFilterValue('al', operator)).toBe('%al%')+ }+ )++ it.each(['~~', '~~*', '!~~', '!~~*'] as const)(+ 'leaves the value untouched for the %s operator when it already contains a % or _ wildcard',+ (operator) => {+ expect(formatPatternMatchFilterValue('al%', operator)).toBe('al%')+ expect(formatPatternMatchFilterValue('%al', operator)).toBe('%al')+ expect(formatPatternMatchFilterValue('j_hn', operator)).toBe('j_hn')+ }+ )+})diff --git a/apps/studio/data/table-rows/table-rows-count-query.ts b/apps/studio/data/table-rows/table-rows-count-query.tsindex e1ba0ec16d1..c8aa1dfa45a 100644--- a/apps/studio/data/table-rows/table-rows-count-query.ts+++ b/apps/studio/data/table-rows/table-rows-count-query.ts@@ -1,10 +1,10 @@import { getTableRowsCountSql } from '@supabase/pg-meta'+import { formatPatternMatchFilterValue } from '@supabase/pg-meta/src/query/table-row-query'import { PermissionAction } from '@supabase/shared-types/out/constants'import { QueryClient, useQuery, useQueryClient } from '@tanstack/react-query'import { IS_PLATFORM, useFlag } from 'common'import { tableRowKeys } from './keys'-import { formatFilterValue } from './utils'import { parseSupaTable } from '@/components/grid/SupabaseGrid.utils'import type { Filter, SupaTable } from '@/components/grid/types'import { useConnectionStringForReadOps } from '@/data/read-replicas/replicas-query'@@ -67,7 +67,10 @@ export async function getTableRowsCount(const table = parseSupaTable(entity)- const formattedFilters = filters?.map((x) => ({ ...x, value: formatFilterValue(table, x) }))+ const formattedFilters = filters?.map((x) => ({+ ...x,+ value: formatPatternMatchFilterValue(x.value, x.operator),+ }))const sql = wrapWithRoleImpersonation(getTableRowsCountSql({table,diff --git a/apps/studio/data/table-rows/table-rows-query.ts b/apps/studio/data/table-rows/table-rows-query.tsindex be7c8be4d06..9c04104a3a3 100644--- a/apps/studio/data/table-rows/table-rows-query.ts+++ b/apps/studio/data/table-rows/table-rows-query.ts@@ -1,11 +1,13 @@import { ident, joinSqlFragments, ROLE_IMPERSONATION_NO_RESULTS, safeSql } from '@supabase/pg-meta'import { Query, type QueryFilter } from '@supabase/pg-meta/src/query'-import { getTableRowsSql } from '@supabase/pg-meta/src/query/table-row-query'+import {+ formatPatternMatchFilterValue,+ getTableRowsSql,+} from '@supabase/pg-meta/src/query/table-row-query'import { useQuery, useQueryClient, type QueryClient } from '@tanstack/react-query'import { IS_PLATFORM, useFlag } from 'common'import { tableRowKeys } from './keys'-import { formatFilterValue } from './utils'import { parseSupaTable } from '@/components/grid/SupabaseGrid.utils'import { Filter, Sort, SupaRow, SupaTable } from '@/components/grid/types'import { ENTITY_TYPE } from '@/data/entity-types/entity-type-constants'@@ -147,7 +149,7 @@ export const getAllTableRowsSql = ({filters.filter((filter) => filter.value && filter.value !== '').forEach((filter) => {- const value = formatFilterValue(table, filter)+ const value = formatPatternMatchFilterValue(filter.value, filter.operator)queryChains = queryChains.filter(filter.column, filter.operator, value)})diff --git a/apps/studio/data/table-rows/utils.test.ts b/apps/studio/data/table-rows/utils.test.tsdeleted file mode 100644index 220fdfcbc48..00000000000--- a/apps/studio/data/table-rows/utils.test.ts+++ /dev/null@@ -1,75 +0,0 @@-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, operator: Filter['operator'] = '='): 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- )- })-- it.each(['~~', '~~*', '!~~', '!~~*'] as const)(- 'wraps the value in %%...%% for the %s operator when no wildcard is present',- (operator) => {- expect(formatFilterValue(table, makeFilter('name', 'al', operator))).toBe('%al%')- }- )-- it.each(['~~', '~~*', '!~~', '!~~*'] as const)(- 'leaves the value untouched for the %s operator when it already contains a % or _ wildcard',- (operator) => {- expect(formatFilterValue(table, makeFilter('name', 'al%', operator))).toBe('al%')- expect(formatFilterValue(table, makeFilter('name', '%al', operator))).toBe('%al')- expect(formatFilterValue(table, makeFilter('name', 'j_hn', operator))).toBe('j_hn')- }- )-})diff --git a/apps/studio/data/table-rows/utils.ts b/apps/studio/data/table-rows/utils.tsindex 996ef1f2c6e..e573a071e08 100644--- a/apps/studio/data/table-rows/utils.ts+++ b/apps/studio/data/table-rows/utils.ts@@ -1,32 +1,6 @@-import { formatPatternMatchFilterValue } from '@supabase/pg-meta/src/query/table-row-query'--import type { Filter, ServiceError } from '@/components/grid/types'-import { isNumericalColumn } from '@/components/grid/utils/types'+import type { ServiceError } from '@/components/grid/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 formatPatternMatchFilterValue(filter.value, filter.operator)-}-export function getPrimaryKeys({ table }: { table: Entity }): {primaryKeys?: string[]error?: ServiceError