Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Precise type for max_ aggregate function #689

Open
KAction opened this issue Jun 24, 2023 · 2 comments
Open

Precise type for max_ aggregate function #689

KAction opened this issue Jun 24, 2023 · 2 comments
Labels

Comments

@KAction
Copy link

KAction commented Jun 24, 2023

Say we have following table:

create table foo (id bigserial primary key, value bigint NOT NULL, name text NOT NULL);

In this query, max is nullable because table may not have specific name, and application must handle it:

select max(value) from foo where name = 'banana'

but in this query, return value of max is never null:

select name, max(value) from foo group by name

Beam seems to always assume that result of max aggregate is nullable with following type:

max_ :: QExpr be s a -> QAgg be s (Maybe a)

Is there any way to express this distinction and get precise type, or I have no choice but to use fromJust ?

@LaurentRDC
Copy link
Member

My understanding was that max_ must return a Maybe in case there isn't any data in the aggregation?

As per the Postgres documentation, for example:

It should be noted that except for count, [aggregate functions] return a null value when no rows are selected

@tathougies
Copy link
Collaborator

In the SQL standard, I believe all aggregates can return a null in certain situations (except for COUNT). The correct way (mentioned in the beam docs as well - https://haskell-beam.github.io/beam/user-guide/queries/aggregates/ -- see the note on returning null and use of coalesce_) is to use coalesce. One of the advantages / disadvantages of the type checker is that your queries must be NULL-correct!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants