Skip to content

Aggregation is optimized incorrectly #18

@tomjaguarpaw

Description

@tomjaguarpaw

If I have a table A, and I sum a numeric column while grouping by a string column (say) the string column in the resulting table B has the property that no string appears twice. If I then project out the string column to get C then C contains all the distinct strings from A.

However, HaskellDB's query optimizer seems to get rid of the aggregation entirely without noticing the that the strings in C should be distinct.

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
  project (name << t!name #
           apples << _sum(t!apples))

justNames :: Query (Rel (RecCons Name' (Expr String) RecNil))
justNames = do
  t <- totalApples
  project (name << t!name)


*Main> putStrLn $ showSql justNames
SELECT namecol as name
FROM apples as T1

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