Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions foundations/server/packages/postgres/src/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,23 @@ describe('utils - inferType', () => {
expect(inferType([1, 2, 3])).toBe('::numeric[]')
})

it('should handle empty arrays', () => {
// BUG: Empty arrays are treated as objects and return '::jsonb'
// Expected behavior would be to return '' or handle specially
expect(inferType([])).toBe('::jsonb')
it('should handle empty arrays as text arrays', () => {
// Empty arrays fall back to '::text[]' so `= ANY` stays valid on CockroachDB.
expect(inferType([])).toBe('::text[]')
})

it('should handle arrays with null first element', () => {
expect(inferType([null, 'text'])).toBe('::text[]')
})

it('should handle arrays with all null elements', () => {
// BUG: Arrays with only null elements return '::jsonb[]'
// Expected: Should probably return '' or handle as empty array
expect(inferType([null, null])).toBe('::jsonb[]')
it('should handle single-null arrays as text arrays', () => {
// [null] is what the github integration submits when a workspace has the
// App installed but no repositories enabled; must not infer ::jsonb.
expect(inferType([null])).toBe('::text[]')
})

it('should handle arrays with all null elements as text arrays', () => {
expect(inferType([null, null])).toBe('::text[]')
})

it('should infer Date type as text', () => {
Expand Down
13 changes: 10 additions & 3 deletions foundations/server/packages/postgres/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,17 @@ export function inferType (val: any): string {
return '::boolean'
}
if (Array.isArray(val)) {
const type = inferType(val[0] ?? val[1])
if (type !== '') {
return type + '[]'
const sample = val.find((v) => v !== null && v !== undefined)
if (sample !== undefined) {
const inner = inferType(sample)
if (inner !== '') {
return inner + '[]'
}
}
// Empty arrays and all-null arrays: caller is almost always running
// $in on a Ref/string column. ::jsonb[] would be incompatible with
// CockroachDB's `= ANY`, so fall back to a real array type.
return '::text[]'
}
if (typeof val === 'object') {
if (val instanceof Date) {
Expand Down
Loading