Skip to content

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

fix(studio): keep json formatting when truncating arrays (#34546)

Andrew Valleteaue36f1715b36d
diff --git a/packages/pg-meta/src/query/table-row-query.ts b/packages/pg-meta/src/query/table-row-query.ts
index 79cdbc4dbf0..82cf5202830 100644
--- a/packages/pg-meta/src/query/table-row-query.ts
+++ b/packages/pg-meta/src/query/table-row-query.ts
@@ -26,6 +26,7 @@ export interface BuildTableRowsQueryArgs {
// Text and JSON types that should be truncated
export const TEXT_TYPES = ['text', 'varchar', 'char', 'character varying', 'character']
export const JSON_TYPES = ['json', 'jsonb']
+const JSON_SET = new Set(JSON_TYPES)
// Additional PostgreSQL types that can hold large values and should be truncated
export const ADDITIONAL_LARGE_TYPES = [
@@ -112,7 +113,7 @@ export const getTableRowsSql = ({
const allColumnNames = table.columns
.sort((a, b) => a.ordinal_position - b.ordinal_position)
- .map((column) => column.name)
+ .map((column) => ({ name: column.name, format: column.format.toLowerCase() }))
// Identify columns that might need truncation
const columnsToTruncate = table.columns
@@ -120,7 +121,7 @@ export const getTableRowsSql = ({
.map((column) => column.name)
// Create select expressions for each column, applying truncation only to needed columns
- const selectExpressions = allColumnNames.map((columnName) => {
+ const selectExpressions = allColumnNames.map(({ name: columnName }) => {
const escapedColumnName = ident(columnName)
if (columnsToTruncate.includes(columnName)) {
@@ -135,16 +136,21 @@ export const getTableRowsSql = ({
})
// Handle array-based columns
- const arrayBasedColumnNames = table.columns
+ const arrayBasedColumns = table.columns
.filter((column) => column.data_type.toLowerCase() === 'array')
- .map((column) => column.name)
+ // remove the _ prefix for array based format
+ .map((column) => ({ name: column.name, format: column.format.toLowerCase().slice(1) }))
// Add array casting for array-based enum columns
- arrayBasedColumnNames.forEach((columnName) => {
+ arrayBasedColumns.forEach(({ name: columnName, format }) => {
// Find this column in our select expressions
const index = selectExpressions.findIndex(
(expr) => expr === ident(columnName) // if the column is selected without any truncation applied to it
)
+ // If the column is a json, the final cast remain an array of json
+ const typeCast = JSON_SET.has(format) ? `${format}[]` : 'text[]'
+ const lastElement =
+ typeCast === 'text[]' ? `array['...']` : `array['{"truncated": true}'::json]`
if (index >= 0) {
// We cast to text[] but limit the array size if the total size of the array is too large (same logic than for text fields)
// This returns the first MAX_ARRAY_SIZE elements of the array (adjustable) and adds '...' if truncated
@@ -153,8 +159,8 @@ export const getTableRowsSql = ({
selectExpressions[index] = `
case
when octet_length(${ident(columnName)}::text) > ${maxCharacters}
- then (select array_cat(${ident(columnName)}[1:${maxArraySize}]::text[], array['...']))::text[]
- else ${ident(columnName)}::text[]
+ then (select array_cat(${ident(columnName)}[1:${maxArraySize}]::${typeCast}, ${lastElement}::${typeCast}))::${typeCast}
+ else ${ident(columnName)}::${typeCast}
end
`
}
diff --git a/packages/pg-meta/test/query/table-row-query.test.ts b/packages/pg-meta/test/query/table-row-query.test.ts
index c8304318dbf..dd59529ac05 100644
--- a/packages/pg-meta/test/query/table-row-query.test.ts
+++ b/packages/pg-meta/test/query/table-row-query.test.ts
@@ -82,7 +82,7 @@ describe('Table Row Query', () => {
end as name,status,
case
when octet_length(history::text) > 10240
- then (select array_cat(history[1:50]::text[], array['...']))::text[]
+ then (select array_cat(history[1:50]::text[], array['...']::text[]))::text[]
else history::text[]
end
from public.test_enum_array order by test_enum_array.id asc nulls first limit 10 offset 0;"
@@ -147,7 +147,7 @@ describe('Table Row Query', () => {
end as name,status,
case
when octet_length(history::text) > 10240
- then (select array_cat(history[1:50]::text[], array['...']))::text[]
+ then (select array_cat(history[1:50]::text[], array['...']::text[]))::text[]
else history::text[]
end
from public.test_enum_array where status = 'active' order by test_enum_array.id asc nulls first limit 10 offset 0;"
@@ -185,7 +185,7 @@ describe('Table Row Query', () => {
end as name,status,
case
when octet_length(history::text) > 10240
- then (select array_cat(history[1:50]::text[], array['...']))::text[]
+ then (select array_cat(history[1:50]::text[], array['...']::text[]))::text[]
else history::text[]
end
from public.test_enum_array where history = ARRAY['active']::status_type[] order by test_enum_array.id asc nulls first limit 10 offset 0;"
@@ -230,7 +230,7 @@ describe('Table Row Query', () => {
end as name,
case
when octet_length(tags::text) > 10240
- then (select array_cat(tags[1:50]::text[], array['...']))::text[]
+ then (select array_cat(tags[1:50]::text[], array['...']::text[]))::text[]
else tags::text[]
end
from public.test_array_table order by test_array_table.id asc nulls first limit 10 offset 0;"
@@ -361,18 +361,18 @@ describe('Table Row Query', () => {
// Verify the SQL contains the array truncation logic
expect(sql).toMatchInlineSnapshot(`
- "select id,case
- when octet_length(name::text) > 2048
- then left(name::text, 2048) || '...'
- else name::text
- end as name,
- case
- when octet_length(large_array::text) > 2048
- then (select array_cat(large_array[1:10]::text[], array['...']))::text[]
- else large_array::text[]
- end
- from public.test_large_array_table order by test_large_array_table.id asc nulls first limit 10 offset 0;"
- `)
+ "select id,case
+ when octet_length(name::text) > 2048
+ then left(name::text, 2048) || '...'
+ else name::text
+ end as name,
+ case
+ when octet_length(large_array::text) > 2048
+ then (select array_cat(large_array[1:10]::text[], array['...']::text[]))::text[]
+ else large_array::text[]
+ end
+ from public.test_large_array_table order by test_large_array_table.id asc nulls first limit 10 offset 0;"
+ `)
// Execute the SQL and verify results
const queryResult = await db.executeQuery(sql)
@@ -515,6 +515,406 @@ describe('Table Row Query', () => {
}
)
+ withTestDatabase(
+ 'should truncate large arrays of jsonb and json to maxArraySize elements if their size is > maxCharacters',
+ async (db) => {
+ // Create test table with array column
+ await db.executeQuery(`
+ CREATE TABLE test_large_array_table (
+ id SERIAL PRIMARY KEY,
+ name TEXT,
+ large_array_jsonb jsonb[],
+ large_array_json json[]
+ );
+
+ -- Insert test data with a large array (>10KB)
+ -- Create arrays with JSON objects
+ INSERT INTO test_large_array_table (name, large_array_jsonb, large_array_json) VALUES
+ (
+ 'Large Array Item',
+ (SELECT array_agg(jsonb_build_object(
+ 'id', i,
+ 'name', 'element_' || i,
+ 'data', jsonb_build_object('value', i * 10, 'active', true)
+ )) FROM generate_series(1, 1000) i),
+ (SELECT array_agg(json_build_object(
+ 'id', i,
+ 'name', 'element_' || i,
+ 'data', json_build_object('value', i * 10, 'active', true)
+ )) FROM generate_series(1, 1000) i)
+ ),
+ (
+ 'Large Array Small items',
+ (SELECT array_agg(jsonb_build_object(
+ 'id', i,
+ 'value', i
+ )) FROM generate_series(1, 100) i),
+ (SELECT array_agg(json_build_object(
+ 'id', i,
+ 'value', i
+ )) FROM generate_series(1, 100) i)
+ ),
+ (
+ 'Normal Array Item',
+ ARRAY[
+ '{"id": 1, "tag": "tag1"}'::jsonb,
+ '{"id": 2, "tag": "tag2"}'::jsonb,
+ '{"id": 3, "tag": "tag3"}'::jsonb
+ ],
+ ARRAY[
+ '{"id": 1, "tag": "tag1"}'::json,
+ '{"id": 2, "tag": "tag2"}'::json,
+ '{"id": 3, "tag": "tag3"}'::json
+ ]
+ );
+ `)
+
+ // Get table metadata
+ const { sql: tablesSql, zod: tablesZod } = pgMeta.tables.list()
+ const tables = tablesZod.parse(await db.executeQuery(tablesSql))
+ const testTable = tables.find((table) => table.name === 'test_large_array_table')
+
+ expect(testTable).toBeDefined()
+
+ // Generate SQL with lower maxCharacters and maxArraySize limits
+ const sql = getTableRowsSql({
+ table: testTable!,
+ page: 1,
+ limit: 10,
+ maxCharacters: 2048,
+ maxArraySize: 10,
+ })
+
+ // Verify the SQL contains the array truncation logic
+ expect(sql).toMatchInlineSnapshot(`
+ "select id,case
+ when octet_length(name::text) > 2048
+ then left(name::text, 2048) || '...'
+ else name::text
+ end as name,
+ case
+ when octet_length(large_array_jsonb::text) > 2048
+ then (select array_cat(large_array_jsonb[1:10]::jsonb[], array['{"truncated": true}'::json]::jsonb[]))::jsonb[]
+ else large_array_jsonb::jsonb[]
+ end
+ ,
+ case
+ when octet_length(large_array_json::text) > 2048
+ then (select array_cat(large_array_json[1:10]::json[], array['{"truncated": true}'::json]::json[]))::json[]
+ else large_array_json::json[]
+ end
+ from public.test_large_array_table order by test_large_array_table.id asc nulls first limit 10 offset 0;"
+ `)
+
+ // Execute the SQL and verify results
+ const queryResult = await db.executeQuery(sql)
+ expect(queryResult).toMatchInlineSnapshot(`
+ [
+ {
+ "id": 1,
+ "large_array_json": [
+ {
+ "data": {
+ "active": true,
+ "value": 10,
+ },
+ "id": 1,
+ "name": "element_1",
+ },
+ {
+ "data": {
+ "active": true,
+ "value": 20,
+ },
+ "id": 2,
+ "name": "element_2",
+ },
+ {
+ "data": {
+ "active": true,
+ "value": 30,
+ },
+ "id": 3,
+ "name": "element_3",
+ },
+ {
+ "data": {
+ "active": true,
+ "value": 40,
+ },
+ "id": 4,
+ "name": "element_4",
+ },
+ {
+ "data": {
+ "active": true,
+ "value": 50,
+ },
+ "id": 5,
+ "name": "element_5",
+ },
+ {
+ "data": {
+ "active": true,
+ "value": 60,
+ },
+ "id": 6,
+ "name": "element_6",
+ },
+ {
+ "data": {
+ "active": true,
+ "value": 70,
+ },
+ "id": 7,
+ "name": "element_7",
+ },
+ {
+ "data": {
+ "active": true,
+ "value": 80,
+ },
+ "id": 8,
+ "name": "element_8",
+ },
+ {
+ "data": {
+ "active": true,
+ "value": 90,
+ },
+ "id": 9,
+ "name": "element_9",
+ },
+ {
+ "data": {
+ "active": true,
+ "value": 100,
+ },
+ "id": 10,
+ "name": "element_10",
+ },
+ {
+ "truncated": true,
+ },
+ ],
+ "large_array_jsonb": [
+ {
+ "data": {
+ "active": true,
+ "value": 10,
+ },
+ "id": 1,
+ "name": "element_1",
+ },
+ {
+ "data": {
+ "active": true,
+ "value": 20,
+ },
+ "id": 2,
+ "name": "element_2",
+ },
+ {
+ "data": {
+ "active": true,
+ "value": 30,
+ },
+ "id": 3,
+ "name": "element_3",
+ },
+ {
+ "data": {
+ "active": true,
+ "value": 40,
+ },
+ "id": 4,
+ "name": "element_4",
+ },
+ {
+ "data": {
+ "active": true,
+ "value": 50,
+ },
+ "id": 5,
+ "name": "element_5",
+ },
+ {
+ "data": {
+ "active": true,
+ "value": 60,
+ },
+ "id": 6,
+ "name": "element_6",
+ },
+ {
+ "data": {
+ "active": true,
+ "value": 70,
+ },
+ "id": 7,
+ "name": "element_7",
+ },
+ {
+ "data": {
+ "active": true,
+ "value": 80,
+ },
+ "id": 8,
+ "name": "element_8",
+ },
+ {
+ "data": {
+ "active": true,
+ "value": 90,
+ },
+ "id": 9,
+ "name": "element_9",
+ },
+ {
+ "data": {
+ "active": true,
+ "value": 100,
+ },
+ "id": 10,
+ "name": "element_10",
+ },
+ {
+ "truncated": true,
+ },
+ ],
+ "name": "Large Array Item",
+ },
+ {
+ "id": 2,
+ "large_array_json": [
+ {
+ "id": 1,
+ "value": 1,
+ },
+ {
+ "id": 2,
+ "value": 2,
+ },
+ {
+ "id": 3,
+ "value": 3,
+ },
+ {
+ "id": 4,
+ "value": 4,
+ },
+ {
+ "id": 5,
+ "value": 5,
+ },
+ {
+ "id": 6,
+ "value": 6,
+ },
+ {
+ "id": 7,
+ "value": 7,
+ },
+ {
+ "id": 8,
+ "value": 8,
+ },
+ {
+ "id": 9,
+ "value": 9,
+ },
+ {
+ "id": 10,
+ "value": 10,
+ },
+ {
+ "truncated": true,
+ },
+ ],
+ "large_array_jsonb": [
+ {
+ "id": 1,
+ "value": 1,
+ },
+ {
+ "id": 2,
+ "value": 2,
+ },
+ {
+ "id": 3,
+ "value": 3,
+ },
+ {
+ "id": 4,
+ "value": 4,
+ },
+ {
+ "id": 5,
+ "value": 5,
+ },
+ {
+ "id": 6,
+ "value": 6,
+ },
+ {
+ "id": 7,
+ "value": 7,
+ },
+ {
+ "id": 8,
+ "value": 8,
+ },
+ {
+ "id": 9,
+ "value": 9,
+ },
+ {
+ "id": 10,
+ "value": 10,
+ },
+ {
+ "truncated": true,
+ },
+ ],
+ "name": "Large Array Small items",
+ },
+ {
+ "id": 3,
+ "large_array_json": [
+ {
+ "id": 1,
+ "tag": "tag1",
+ },
+ {
+ "id": 2,
+ "tag": "tag2",
+ },
+ {
+ "id": 3,
+ "tag": "tag3",
+ },
+ ],
+ "large_array_jsonb": [
+ {
+ "id": 1,
+ "tag": "tag1",
+ },
+ {
+ "id": 2,
+ "tag": "tag2",
+ },
+ {
+ "id": 3,
+ "tag": "tag3",
+ },
+ ],
+ "name": "Normal Array Item",
+ },
+ ]
+ `)
+ }
+ )
+
withTestDatabase('should truncate fields to maxCharacters avoid', async (db) => {
// Create test table with array column
await db.executeQuery(`
@@ -556,7 +956,7 @@ describe('Table Row Query', () => {
end as name,
case
when octet_length(large_array::text) > 256
- then (select array_cat(large_array[1:50]::text[], array['...']))::text[]
+ then (select array_cat(large_array[1:50]::text[], array['...']::text[]))::text[]
else large_array::text[]
end
from public.test_large_array_table order by test_large_array_table.id asc nulls first limit 10 offset 0;"