Skip to content

Aggregation interacts incorrectly with union #17

@tomjaguarpaw

Description

@tomjaguarpaw
import Database.HaskellDB
import Database.HaskellDB.PrimQuery
import Database.HaskellDB.Query
import Database.HaskellDB.DBLayout

data Name' = Name'
instance FieldTag Name' where
  fieldName _ = "name"

data Apples = Apples
instance FieldTag Apples where
  fieldName _ = "age"

name :: Attr Name' String
name = mkAttr Name'

apples :: Attr Apples Int
apples = mkAttr Apples

applesTable :: Table (RecCons Name' (Expr String) (RecCons Apples (Expr Int) RecNil))
applesTable = Table "apples" [ ("name", AttrExpr "namecol")
                             , ("apples", AttrExpr "applecol")]

totalApples :: Query (Rel (RecCons Name' (Expr String) (RecCons Apples (Expr Int) RecNil)))
totalApples = do
  t <- table applesTable `union` table applesTable
  project (name << t!name #
           apples << _sum(t!apples))

The aggregation is done before the union. It should be after.

*Main> putStrLn $ showSql totalApples 
(SELECT namecol as name,
        SUM(age3) as age
 FROM apples as T1
 GROUP BY namecol)
UNION
(SELECT namecol as name,
        SUM(age3) as age
 FROM apples as T1
 GROUP BY namecol)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions