valderman / selda

A type-safe, high-level SQL library for Haskell
https://selda.link
MIT License
478 stars 58 forks source link

Crash when inductive tuple has non-nullable column after left join row #157

Open alexmingoia opened 3 years ago

alexmingoia commented 3 years ago

If a query with a left join returns non-nullable columns after a left join row in the inductive tuple, a crash occurs.

This gist reproduces the crash.

This crashes with fromSql: text column with non-text value: SqlNull:

person <- select people
pet <- leftJoin (\p -> p ! #owner .== person ! #name) $ select pets
return (pet :*: person ! #name)

But this doesn't (inductive tuple order is flipped):

person <- select people
pet <- leftJoin (\p -> p ! #owner .== person ! #name) $ select pets
return (person ! #name :*: pet)
kamoii commented 3 years ago

I think I found the cause of this bug.

https://github.com/valderman/selda/blob/master/selda/src/Database/Selda/SqlRow.hs#L69

    then return Nothing

When the whole Row is null, it doesn't consume SqlValue's in the ResultReader state monad. This code should be something like:

    then Nothing <$ R (put (drop (nestedCols (Proxy :: Proxy a)) xs))