From 4befa359c84e8c94b402b5a12b856b45c9bae7f1 Mon Sep 17 00:00:00 2001 From: Katerina Skroumpelou Date: Mon, 6 Apr 2026 19:36:06 +0300 Subject: [PATCH 1/2] fix(postgrest): fix scalar computed column type inference for isNotNullable and SETOF scalar --- .../src/select-query-parser/result.ts | 30 +++++-- .../src/select-query-parser/utils.ts | 78 +++++++++++++------ .../test/embeded_functions_join.test.ts | 26 +++++++ .../core/postgrest-js/test/types.generated.ts | 19 +++++ 4 files changed, 121 insertions(+), 32 deletions(-) diff --git a/packages/core/postgrest-js/src/select-query-parser/result.ts b/packages/core/postgrest-js/src/select-query-parser/result.ts index 22207afdf34..9ed3ea8f705 100644 --- a/packages/core/postgrest-js/src/select-query-parser/result.ts +++ b/packages/core/postgrest-js/src/select-query-parser/result.ts @@ -291,7 +291,11 @@ type ProcessFieldNode< Relationships extends GenericRelationship[], Field extends Ast.FieldNode, > = Field['children'] extends [] - ? {} + ? // Empty `()` — could be a scalar computed column (e.g. `user_count()`). + // Route through ProcessEmbeddedResource so the scalar path returns the correct + // primitive type. For non-scalar embedded resources with empty selection, + // ProcessNodes([]) returns {} which preserves the same prior behavior. + ProcessEmbeddedResource : IsNonEmptyArray extends true // Has embedded resource? ? ProcessEmbeddedResource : ProcessSimpleField @@ -366,13 +370,25 @@ export type ProcessEmbeddedResource< > = ResolveRelationship extends infer Resolved ? Resolved extends { - referencedTable: Pick - relation: GenericRelationship & { match: 'refrel' | 'col' | 'fkname' | 'func' } - direction: string + scalarType: infer ScalarType + relation: { isSetofReturn?: boolean; isNotNullable?: boolean } } - ? ProcessEmbeddedResourceResult - : // Otherwise the Resolved is a SelectQueryError return it - { [K in GetFieldNodeResultName]: Resolved } + ? // Scalar computed column: bypass ProcessNodes and return the primitive type directly. + { + [K in GetFieldNodeResultName]: Resolved['relation']['isSetofReturn'] extends true + ? ScalarType + : Resolved['relation']['isNotNullable'] extends true + ? ScalarType + : ScalarType | null + } + : Resolved extends { + referencedTable: Pick + relation: GenericRelationship & { match: 'refrel' | 'col' | 'fkname' | 'func' } + direction: string + } + ? ProcessEmbeddedResourceResult + : // Otherwise the Resolved is a SelectQueryError return it + { [K in GetFieldNodeResultName]: Resolved } : { [K in GetFieldNodeResultName]: SelectQueryError<'Failed to resolve relationship.'> & string diff --git a/packages/core/postgrest-js/src/select-query-parser/utils.ts b/packages/core/postgrest-js/src/select-query-parser/utils.ts index 09e37f3dadf..5a454046063 100644 --- a/packages/core/postgrest-js/src/select-query-parser/utils.ts +++ b/packages/core/postgrest-js/src/select-query-parser/utils.ts @@ -464,30 +464,58 @@ export type ResolveForwardRelationship< CurrentTableOrView, Field['name'] > extends infer FoundEmbededFunctionJoinTableRelation - ? FoundEmbededFunctionJoinTableRelation extends GenericSetofOption - ? { - referencedTable: TablesAndViews[FoundEmbededFunctionJoinTableRelation['to']] - relation: { - foreignKeyName: `${Field['name']}_${CurrentTableOrView}_${FoundEmbededFunctionJoinTableRelation['to']}_forward` - columns: [] - isOneToOne: FoundEmbededFunctionJoinTableRelation['isOneToOne'] extends true - ? true - : false - referencedColumns: [] - referencedRelation: FoundEmbededFunctionJoinTableRelation['to'] - } & { - match: 'func' - isNotNullable: FoundEmbededFunctionJoinTableRelation['isNotNullable'] extends true - ? true - : FoundEmbededFunctionJoinTableRelation['isSetofReturn'] extends true - ? false - : true - isSetofReturn: FoundEmbededFunctionJoinTableRelation['isSetofReturn'] - } - direction: 'forward' - from: CurrentTableOrView - type: 'found-by-embeded-function' - } + ? FoundEmbededFunctionJoinTableRelation extends GenericFunction + ? FoundEmbededFunctionJoinTableRelation['SetofOptions'] extends GenericSetofOption + ? FoundEmbededFunctionJoinTableRelation['SetofOptions']['to'] extends '' + ? // Scalar computed column: function returns a primitive (not a table row). + // `to` is '' because there is no target table — the value is returned directly. + { + referencedTable: { Row: Record; Relationships: [] } + relation: { + foreignKeyName: `${Field['name']}_${CurrentTableOrView}_scalar_forward` + columns: [] + isOneToOne: false + referencedColumns: [] + referencedRelation: '' + } & { + match: 'func' + isNotNullable: FoundEmbededFunctionJoinTableRelation['SetofOptions']['isNotNullable'] extends true + ? true + : FoundEmbededFunctionJoinTableRelation['SetofOptions']['isSetofReturn'] extends true + ? false + : true + isSetofReturn: FoundEmbededFunctionJoinTableRelation['SetofOptions']['isSetofReturn'] + } + scalarType: FoundEmbededFunctionJoinTableRelation['Returns'] + direction: 'forward' + from: CurrentTableOrView + type: 'found-by-embeded-scalar-function' + } + : // Table-valued function: `to` names the target table/view. + { + referencedTable: TablesAndViews[FoundEmbededFunctionJoinTableRelation['SetofOptions']['to']] + relation: { + foreignKeyName: `${Field['name']}_${CurrentTableOrView}_${FoundEmbededFunctionJoinTableRelation['SetofOptions']['to']}_forward` + columns: [] + isOneToOne: FoundEmbededFunctionJoinTableRelation['SetofOptions']['isOneToOne'] extends true + ? true + : false + referencedColumns: [] + referencedRelation: FoundEmbededFunctionJoinTableRelation['SetofOptions']['to'] + } & { + match: 'func' + isNotNullable: FoundEmbededFunctionJoinTableRelation['SetofOptions']['isNotNullable'] extends true + ? true + : FoundEmbededFunctionJoinTableRelation['SetofOptions']['isSetofReturn'] extends true + ? false + : true + isSetofReturn: FoundEmbededFunctionJoinTableRelation['SetofOptions']['isSetofReturn'] + } + direction: 'forward' + from: CurrentTableOrView + type: 'found-by-embeded-function' + } + : SelectQueryError<`could not find the relation between ${CurrentTableOrView} and ${Field['name']}`> : SelectQueryError<`could not find the relation between ${CurrentTableOrView} and ${Field['name']}`> : SelectQueryError<`could not find the relation between ${CurrentTableOrView} and ${Field['name']}`> : SelectQueryError<`could not find the relation between ${CurrentTableOrView} and ${Field['name']}`> @@ -542,7 +570,7 @@ type ResolveEmbededFunctionJoinTableRelationship< CurrentTableOrView > extends infer Fn ? Fn extends GenericFunction - ? Fn['SetofOptions'] + ? Fn : false : false diff --git a/packages/core/postgrest-js/test/embeded_functions_join.test.ts b/packages/core/postgrest-js/test/embeded_functions_join.test.ts index c5e8ed6d50f..006264ea8c0 100644 --- a/packages/core/postgrest-js/test/embeded_functions_join.test.ts +++ b/packages/core/postgrest-js/test/embeded_functions_join.test.ts @@ -1433,4 +1433,30 @@ describe('embeded functions select', () => { > >(true) }) + + test('scalar_computed_count - non-SETOF non-nullable scalar returns number (not null)', async () => { + const res = await postgrest.from('users').select('username, scalar_computed_count()') + let result: Exclude + const ExpectedSchema = z.array( + z.object({ + username: z.string(), + scalar_computed_count: z.number(), + }) + ) + let expected: z.infer + expectType>(true) + }) + + test('scalar_computed_ids - SETOF scalar returns string[] (not null)', async () => { + const res = await postgrest.from('users').select('username, scalar_computed_ids()') + let result: Exclude + const ExpectedSchema = z.array( + z.object({ + username: z.string(), + scalar_computed_ids: z.array(z.string()), + }) + ) + let expected: z.infer + expectType>(true) + }) }) diff --git a/packages/core/postgrest-js/test/types.generated.ts b/packages/core/postgrest-js/test/types.generated.ts index c3700b3d5e7..68a907fea2c 100644 --- a/packages/core/postgrest-js/test/types.generated.ts +++ b/packages/core/postgrest-js/test/types.generated.ts @@ -716,6 +716,25 @@ export type Database = { isSetofReturn: true } } + scalar_computed_count: { + Args: { '': Database['public']['Tables']['users']['Row'] } + Returns: number + SetofOptions: { + from: 'users' + to: '' + isSetofReturn: false + isNotNullable: true + } + } + scalar_computed_ids: { + Args: { '': Database['public']['Tables']['users']['Row'] } + Returns: string[] + SetofOptions: { + from: 'users' + to: '' + isSetofReturn: true + } + } function_returning_single_row: { Args: { messages: Database['public']['Tables']['messages']['Row'] } Returns: { From b4b048f4c6c8a805d77fb5dce0195597571755e2 Mon Sep 17 00:00:00 2001 From: Katerina Skroumpelou Date: Tue, 7 Apr 2026 15:06:50 +0300 Subject: [PATCH 2/2] fix(postgrest): regression --- .../postgrest-js/src/select-query-parser/result.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/core/postgrest-js/src/select-query-parser/result.ts b/packages/core/postgrest-js/src/select-query-parser/result.ts index 9ed3ea8f705..7ac9dd03557 100644 --- a/packages/core/postgrest-js/src/select-query-parser/result.ts +++ b/packages/core/postgrest-js/src/select-query-parser/result.ts @@ -294,7 +294,7 @@ type ProcessFieldNode< ? // Empty `()` — could be a scalar computed column (e.g. `user_count()`). // Route through ProcessEmbeddedResource so the scalar path returns the correct // primitive type. For non-scalar embedded resources with empty selection, - // ProcessNodes([]) returns {} which preserves the same prior behavior. + // ProcessEmbeddedResource returns {} (no contribution to result type). ProcessEmbeddedResource : IsNonEmptyArray extends true // Has embedded resource? ? ProcessEmbeddedResource @@ -386,7 +386,17 @@ export type ProcessEmbeddedResource< relation: GenericRelationship & { match: 'refrel' | 'col' | 'fkname' | 'func' } direction: string } - ? ProcessEmbeddedResourceResult + ? Field['children'] extends [] + ? // Empty `()` on a regular table embed — no fields selected, contribute nothing. + // This preserves the prior behavior: `users()` does not add a `users` key to the result. + {} + : ProcessEmbeddedResourceResult< + ClientOptions, + Schema, + Resolved, + Field, + CurrentTableOrView + > : // Otherwise the Resolved is a SelectQueryError return it { [K in GetFieldNodeResultName]: Resolved } : {