Skip to content

Commit

Permalink
Mark Postgres aggregate return types as nullable (#498)
Browse files Browse the repository at this point in the history
### What

As discussed internally, a particular quirk of SQL (or Postgres, at the
very least) is that, with the exception of `COUNT`, an aggregate of an
empty row set returns `NULL`[^1] (rather than, for example, returning
`0` for `SUM`). This means that all aggregate functions over scalar
types exposed by the Postgres connector should be labelled as nullable.

This PR addresses
[APG-131](https://hasurahq.atlassian.net/browse/APG-131).

NB: this will be a breaking change for anyone generating (for example)
types from our /schema output.

### How

This PR updates the `/schema` endpoint to mark these types as nullable.

[^1]: The entire work effort of this PR was trying to verify this
statement.


[APG-131]:
https://hasurahq.atlassian.net/browse/APG-131?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
  • Loading branch information
i-am-tom committed Jun 11, 2024
1 parent d7e054a commit 188322e
Show file tree
Hide file tree
Showing 5 changed files with 1,520 additions and 610 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

### Changed

- Aggregate functions (except COUNT) are now all marked as having nullable
return types, as they will return null for empty row sets.
([#498])(https://github.com/hasura/ndc-postgres/pull/498))

### Fixed

- Rows and aggregates parts of the query should operate on the same query parameters (where, order by, limit and offset).
Expand Down
10 changes: 8 additions & 2 deletions crates/connectors/ndc-postgres/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,14 @@ pub fn get_schema(
(
function_name.clone(),
models::AggregateFunctionDefinition {
result_type: models::Type::Named {
name: function_definition.return_type.0.clone(),
result_type: models::Type::Nullable {
// It turns out that all aggregates defined for postgres
// (_except_ `COUNT`) will return `NULL` for an empty row set.
// Thus, we mark all aggregates as having a nullable return
// type.
underlying_type: Box::new(models::Type::Named {
name: function_definition.return_type.0.clone(),
}),
},
},
)
Expand Down
Loading

0 comments on commit 188322e

Please sign in to comment.