{-# options_ghc -fno-warn-redundant-constraints #-}

module Rel8.Query.Distinct
  ( distinct
  , distinctOn
  , distinctOnBy
  )
where

-- base
import Prelude

-- opaleye
import qualified Opaleye.Distinct as Opaleye
import qualified Opaleye.Internal.Order as Opaleye
import qualified Opaleye.Internal.QueryArr as Opaleye

-- rel8
import Rel8.Order ( Order( Order ) )
import Rel8.Query ( Query )
import Rel8.Query.Opaleye ( mapOpaleye )
import Rel8.Table.Eq ( EqTable )
import Rel8.Table.Opaleye ( distinctspec, unpackspec )


-- | Select all distinct rows from a query, removing duplicates.  @distinct q@
-- is equivalent to the SQL statement @SELECT DISTINCT q@.
distinct :: EqTable a => Query a -> Query a
distinct :: Query a -> Query a
distinct = (Select a -> Select a) -> Query a -> Query a
forall a b. (Select a -> Select b) -> Query a -> Query b
mapOpaleye (Distinctspec a a -> Select a -> Select a
forall fields fields'.
Distinctspec fields fields' -> Select fields -> Select fields'
Opaleye.distinctExplicit Distinctspec a a
forall a. Table Expr a => Distinctspec a a
distinctspec)


-- | Select all distinct rows from a query, where rows are equivalent according
-- to a projection. If multiple rows have the same projection, it is
-- unspecified which row will be returned. If this matters, use 'distinctOnBy'.
distinctOn :: EqTable b => (a -> b) -> Query a -> Query a
distinctOn :: (a -> b) -> Query a -> Query a
distinctOn a -> b
proj =
  (Select a -> Select a) -> Query a -> Query a
forall a b. (Select a -> Select b) -> Query a -> Query b
mapOpaleye (\Select a
q -> (((), Tag) -> (a, PrimQuery, Tag)) -> Select a
forall a b. ((a, Tag) -> (b, PrimQuery, Tag)) -> QueryArr a b
Opaleye.productQueryArr (Unpackspec b b
-> (a -> b) -> (a, PrimQuery, Tag) -> (a, PrimQuery, Tag)
forall b a.
Unpackspec b b
-> (a -> b) -> (a, PrimQuery, Tag) -> (a, PrimQuery, Tag)
Opaleye.distinctOn Unpackspec b b
forall a. Table Expr a => Unpackspec a a
unpackspec a -> b
proj ((a, PrimQuery, Tag) -> (a, PrimQuery, Tag))
-> (((), Tag) -> (a, PrimQuery, Tag))
-> ((), Tag)
-> (a, PrimQuery, Tag)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Select a -> ((), Tag) -> (a, PrimQuery, Tag)
forall a b. QueryArr a b -> (a, Tag) -> (b, PrimQuery, Tag)
Opaleye.runSimpleQueryArr Select a
q))


-- | Select all distinct rows from a query, where rows are equivalent according
-- to a projection. If there are multiple rows with the same projection, the
-- first row according to the specified 'Order' will be returned.
distinctOnBy :: EqTable b => (a -> b) -> Order a -> Query a -> Query a
distinctOnBy :: (a -> b) -> Order a -> Query a -> Query a
distinctOnBy a -> b
proj (Order Order a
order) =
  (Select a -> Select a) -> Query a -> Query a
forall a b. (Select a -> Select b) -> Query a -> Query b
mapOpaleye (\Select a
q -> (((), Tag) -> (a, PrimQuery, Tag)) -> Select a
forall a b. ((a, Tag) -> (b, PrimQuery, Tag)) -> QueryArr a b
Opaleye.productQueryArr (Unpackspec b b
-> (a -> b)
-> Order a
-> (a, PrimQuery, Tag)
-> (a, PrimQuery, Tag)
forall b a.
Unpackspec b b
-> (a -> b)
-> Order a
-> (a, PrimQuery, Tag)
-> (a, PrimQuery, Tag)
Opaleye.distinctOnBy Unpackspec b b
forall a. Table Expr a => Unpackspec a a
unpackspec a -> b
proj Order a
order ((a, PrimQuery, Tag) -> (a, PrimQuery, Tag))
-> (((), Tag) -> (a, PrimQuery, Tag))
-> ((), Tag)
-> (a, PrimQuery, Tag)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Select a -> ((), Tag) -> (a, PrimQuery, Tag)
forall a b. QueryArr a b -> (a, Tag) -> (b, PrimQuery, Tag)
Opaleye.runSimpleQueryArr Select a
q))