Skip to content

Double aggregation leads to malformed SQL #15

@tomjaguarpaw

Description

@tomjaguarpaw

If I aggregate twice in a row I seem to get malformed SQL. For example with this code

import Database.HaskellDB.PrimQuery
import Database.HaskellDB.Query
import Database.HaskellDB.DBLayout

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

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

apples :: Attr Apples Int
apples = mkAttr Apples

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

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

summed :: Query (Rel (RecCons Apples (Expr Int) (RecCons Name' (Expr String) RecNil)))
summed = do
  b <- table appleTable
  project (apples << _sum(b!apples) #
          name << b!name)

summed' :: Query (Rel (RecCons Apples (Expr Int) (RecCons Name' (Expr String) RecNil)))
summed' = do
  b <- summed
  project (apples << _sum(b!apples) #
          name << b!name)

showStrange = putStrLn $ showSql summed'

I get the result

*Main> showStrange 
SELECT SUM(SUM(applecol)) as apples,
       namecol as name
FROM apple_table 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