tomjaguarpaw / haskell-opaleye

Other
599 stars 115 forks source link

Should asc/desc/ascNullsFirst/descNullsLast have a less restrictive type? #560

Closed abigailalice closed 1 year ago

abigailalice commented 1 year ago

I just upgraded to a more recent version of Opaleye, and noticed that these types were changed from SqlOrd b => (a -> Column b) -> Order a to SqlOrd b => (a -> Field b) -> Order a, and don't understand why these types were made more restrictive. This was changed in c5d5b5094847f52267821029cb2af3fd0f242537, but I don't see how the types could actually allow the different ordering based on nulls to even be exposed to the database, since the projection function will necessarily have to have removed the null before the value actually gets sorted, and the a could be a record with multiple null fields.

I have a record representing a database row which contains a FieldNullable SqlDate, but unless I'm failing to see something very simple I couldn't see how to achieve the old behaviour without doing something like O.asc $ O.matchNullable (O.toFields (ModifiedJulianDate largeNegativeInteger)) nullFieldSelector, which isn't even really achieving the old behaviour in the first place.

I'm not sure if #545 might possibly be related longterm, if FieldNullable ever becoming deprecated implies anything for Field_. I also noticed the type of matchNullable was made more restrictive as well (not allowing a FieldNullable to be returned, when previously it did), so I'm still kind of figuring out the results of using MaybeFields (Fields a) in practice.

tomjaguarpaw commented 1 year ago

Thanks for reporting this. I'm pretty sure this is just an oversight on my part. I'll patch this in a new release soon, but in the meantime you might like to use unsafeCoerceField to work around it.

tomjaguarpaw commented 1 year ago

Should be fixed in https://hackage.haskell.org/package/opaleye-0.9.4.0. Let me know if this doesn't solve the problem for you.

tomjaguarpaw commented 1 year ago

Oops, actually fixed in https://hackage.haskell.org/package/opaleye-0.9.4.1