Skip to content

Commit 824f00a

Browse files
waleedlatif1claude
andcommitted
fix(logs): mirror SQL NULLS LAST in JS merge for cursor consistency
The in-memory merge of workflow + job pages negated the comparator for DESC, which placed null sort values at the start. SQL orders both ASC and DESC with NULLS LAST, so DESC pages emitted a cursor {v: <last non-null>, id: ...} while null rows still satisfied the cursor predicate (OR sort_expr IS NULL) on the next page — producing duplicate null rows across pages on cost/duration sorts. Handle nulls explicitly in the JS comparator so they always sort last regardless of direction, matching the SQL ordering. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent b0c1862 commit 824f00a

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

apps/sim/app/api/logs/route.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,6 @@ export const GET = withRouteHandler(async (request: NextRequest) => {
401401
})
402402

403403
const compareSortValues = (a: unknown, b: unknown): number => {
404-
if (a === null || a === undefined) return b === null || b === undefined ? 0 : 1
405-
if (b === null || b === undefined) return -1
406404
if (a instanceof Date && b instanceof Date) return a.getTime() - b.getTime()
407405
if (typeof a === 'number' && typeof b === 'number') return a - b
408406
const aStr = String(a)
@@ -417,8 +415,15 @@ export const GET = withRouteHandler(async (request: NextRequest) => {
417415
}
418416

419417
const merged = [...workflowMapped, ...jobMapped].sort((a, b) => {
420-
const cmp = compareSortValues(a.sortValue, b.sortValue)
421-
if (cmp !== 0) return sortOrder === 'asc' ? cmp : -cmp
418+
const aNull = a.sortValue === null || a.sortValue === undefined
419+
const bNull = b.sortValue === null || b.sortValue === undefined
420+
// Mirror SQL's NULLS LAST for both ASC and DESC so the cursor stays consistent.
421+
if (aNull && !bNull) return 1
422+
if (!aNull && bNull) return -1
423+
if (!aNull && !bNull) {
424+
const cmp = compareSortValues(a.sortValue, b.sortValue)
425+
if (cmp !== 0) return sortOrder === 'asc' ? cmp : -cmp
426+
}
422427
const idCmp = a.id.localeCompare(b.id)
423428
return sortOrder === 'asc' ? idCmp : -idCmp
424429
})

0 commit comments

Comments
 (0)