bitemyapp / bitemyapp/esqueleto

count with composite key emits COUNT(*) instead of counting the number of those rows

Open
#239 7 comments 1 reaction 0 assignees View on GitHub
Dominant language
Haskell
Stars
399
Forks
107
Avg merge
1d 17h
Merged PRs (30d)
1

Description

I explicitly want to count non nulls so I assume if I hand the `count` function a nullable column (via `?.`) it will count the number of non null rows. However that is not the case. `count` using a non null column (via `^.`) works but not a nullable column.

Am I trying to find the number of non null rows incorrectly?

Also the documentation for `count` and the other count functions is nonexistent so I would assume it would work like in SQL.

Version: lts 16.5, esqueleto ^>= 3.3.3
Using the experimental syntax/version though `count` comes from the standard part of the library. Uses `BlockArguments`. See sections delineated by comments.
```haskell
select do
(event :& game :& match) <-
from $ Table @Event
`InnerJoin` Table @Game
`on` (\(event :& game) ->
event ^. EventGame ==. game ^. GameId)

--
-- deliberate join of 0 or more rows with a desire to count them
`LeftOuterJoin` Table @Match
`on` (\(event :& _ :& match) ->
just (event ^. EventId) ==. match ?. MatchEventId)
--

limit . fromIntegral $ rangeLimit endTimeRange
offset . fromIntegral $ rangeOffset endTimeRange
let mEndTime = rangeValue endTimeRange
case rangeOrder endTimeRange of
RangeDesc -> do
orderBy [desc $ event ^. EventEndsOn]
when (isJust mEndTime) .
where_ $ just (event ^. EventEndsOn) <=. val mEndTime
RangeAsc -> do
orderBy [asc $ event ^. EventEndsOn]
when (isJust mEndTime) .
where_ $ just (event ^. EventEndsOn) >=. val mEndTime

--
-- group by all columns other than match ones
let theGameName = game ^. GameName
groupBy (event ^. EventId, theGameName)
--return (event, theGameName, count $ event ^. EventId) -- uses COUNT("event"."id")
return (event, theGameName, count $ match ?. MatchId) -- uses COUNT(*)
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.