Skip to content

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

Chore/optimizations for table editor query (#27553)

Joshen Limae8dafd0a439
diff --git a/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.test.ts b/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.test.ts
index a8a3a4bafda..0f966c253de 100644
--- a/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.test.ts
+++ b/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.test.ts
@@ -19,6 +19,24 @@ describe('SQLEditor.utils.ts:checkIfAppendLimitRequired', () => {
const { appendAutoLimit } = checkIfAppendLimitRequired(sql, limit)
expect(appendAutoLimit).toBe(false)
})
+ test('Should return false if query already has a limit (check for case-insensitiveness)', () => {
+ const sql = 'SELECT * FROM countries LIMIT 10;'
+ const limit = 100
+ const { appendAutoLimit } = checkIfAppendLimitRequired(sql, limit)
+ expect(appendAutoLimit).toBe(false)
+ })
+ test('Should return false if query already has a limit and offset', () => {
+ const sql = 'select * from countries limit 10 offset 0;'
+ const limit = 100
+ const { appendAutoLimit } = checkIfAppendLimitRequired(sql, limit)
+ expect(appendAutoLimit).toBe(false)
+ })
+ test('Should return false if query already has a limit and offset (flip order of limit and offset)', () => {
+ const sql = 'select * from countries offset 0 limit 1;'
+ const limit = 100
+ const { appendAutoLimit } = checkIfAppendLimitRequired(sql, limit)
+ expect(appendAutoLimit).toBe(false)
+ })
test('Should return false if query already has a limit, even if no value provided for limit', () => {
const sql = 'select * from countries limit'
const limit = 100
diff --git a/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.ts b/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.ts
index cb778fcdb57..8dc6bf5a84c 100644
--- a/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.ts
+++ b/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.ts
@@ -137,6 +137,7 @@ export const checkIfAppendLimitRequired = (sql: string, limit: number = 0) => {
!cleanedSql.toLowerCase().match(/fetch\s+first/i) &&
!cleanedSql.match(/limit$/i) &&
!cleanedSql.match(/limit;$/i) &&
+ !cleanedSql.match(/limit [0-9]* offset [0-9]*[;]?$/i) &&
!cleanedSql.match(/limit [0-9]*[;]?$/i)
return { cleanedSql, appendAutoLimit }
}
diff --git a/apps/studio/data/table-rows/table-rows-query.ts b/apps/studio/data/table-rows/table-rows-query.ts
index 5d0e3e56c54..5ce5bf5d7cc 100644
--- a/apps/studio/data/table-rows/table-rows-query.ts
+++ b/apps/studio/data/table-rows/table-rows-query.ts
@@ -125,28 +125,15 @@ export const getTableRowsSqlQuery = ({
if (!table) return ``
- // [Joshen] Only truncate text/json based columns as their length could go really big
- // Note: Risk of payload being too large if the user has many many text/json based columns
- // although possibly negligible risk.
- const truncatedColumns = table.columns
- .filter((column) => {
- return (
- ((column?.enum ?? []).length > 0 && column.dataType.toLowerCase() === 'array') ||
- TEXT_TYPES.includes(column.format) ||
- JSON_TYPES.includes(column.format)
- )
- })
- .map((column) => {
- if ((column?.enum ?? []).length > 0 && column.dataType.toLowerCase() === 'array') {
- return `"${column.name}"::text[]`
- } else {
- return `case when length("${column.name}"::text) > ${MAX_CHARACTERS} then concat(left("${column.name}"::text, ${MAX_CHARACTERS}), '...') else "${column.name}"::text end "${column.name}"`
- }
- })
+ const arrayBasedColumns = table.columns
+ .filter(
+ (column) => (column?.enum ?? []).length > 0 && column.dataType.toLowerCase() === 'array'
+ )
+ .map((column) => `"${column.name}"::text[]`)
let queryChains = query
.from(table.name, table.schema ?? undefined)
- .select(truncatedColumns.length > 0 ? `*,${truncatedColumns.join(',')}` : '*')
+ .select(arrayBasedColumns.length > 0 ? `*,${arrayBasedColumns.join(',')}` : '*')
filters
.filter((x) => x.value && x.value != '')
@@ -157,10 +144,10 @@ export const getTableRowsSqlQuery = ({
// If sorts is empty, use the primary key as the default sort
if (sorts.length === 0) {
- const primaryKey = getDefaultOrderByColumn(table)
+ const defaultOrderByColumn = getDefaultOrderByColumn(table)
- if (primaryKey) {
- queryChains = queryChains.order(table.name, primaryKey, true, true)
+ if (defaultOrderByColumn) {
+ queryChains = queryChains.order(table.name, defaultOrderByColumn, true, true)
}
} else {
sorts.forEach((x) => {
@@ -170,9 +157,22 @@ export const getTableRowsSqlQuery = ({
// getPagination is expecting to start from 0
const { from, to } = getPagination((page ?? 1) - 1, limit)
- const sql = queryChains.range(from, to).toSql()
+ const baseSql = queryChains.range(from, to).toSql()
+
+ // [Joshen] Only truncate text/json based columns as their length could go really big
+ // Note: Risk of payload being too large if the user has many many text/json based columns
+ // although possibly negligible risk.
+ const truncatedColumns = table.columns
+ .filter((column) => TEXT_TYPES.includes(column.format) || JSON_TYPES.includes(column.format))
+ .map((column) => {
+ return `case when length("${column.name}"::text) > ${MAX_CHARACTERS} then concat(left("${column.name}"::text, ${MAX_CHARACTERS}), '...') else "${column.name}"::text end "${column.name}"`
+ })
+ const outputSql =
+ truncatedColumns.length > 0
+ ? `with _temp as (${baseSql.slice(0, -1)}) select *, ${truncatedColumns.join(',')} from _temp`
+ : baseSql
- return sql
+ return outputSql
}
export type TableRows = {