@@ -554,9 +554,9 @@ select_ = select . List
554-- be subject to the elimination of duplicate rows using `selectDistinct`.
554-- be subject to the elimination of duplicate rows using `selectDistinct`.
555selectDistinct
555selectDistinct
556 :: (SListI columns, columns ~ (col ': cols))
556 :: (SListI columns, columns ~ (col ': cols))
557− => Selection outer commons 'Ungrouped schemas params from columns
557+ => Selection outer commons grp schemas params from columns
558 -- ^ selection
558 -- ^ selection
559− -> TableExpression outer commons 'Ungrouped schemas params from
559+ -> TableExpression outer commons grp schemas params from
560 -- ^ intermediate virtual table
560 -- ^ intermediate virtual table
561 -> Query outer commons schemas params columns
561 -> Query outer commons schemas params columns
562selectDistinct selection tabexpr = UnsafeQuery $
562selectDistinct selection tabexpr = UnsafeQuery $
@@ -568,9 +568,9 @@ selectDistinct selection tabexpr = UnsafeQuery $
568-- of a general `Selection`.
568-- of a general `Selection`.
569selectDistinct_
569selectDistinct_
570 :: (SListI columns, columns ~ (col ': cols))
570 :: (SListI columns, columns ~ (col ': cols))
571− => NP (Aliased (Expression outer commons 'Ungrouped schemas params from)) columns
571+ => NP (Aliased (Expression outer commons grp schemas params from)) columns
572 -- ^ select list
572 -- ^ select list
573− -> TableExpression outer commons 'Ungrouped schemas params from
573+ -> TableExpression outer commons grp schemas params from
574 -- ^ intermediate virtual table
574 -- ^ intermediate virtual table
575 -> Query outer commons schemas params columns
575 -> Query outer commons schemas params columns
576selectDistinct_ = selectDistinct . List
576selectDistinct_ = selectDistinct . List
@@ -580,14 +580,21 @@ selectDistinct_ = selectDistinct . List
580the given expressions evaluate to equal. The DISTINCT ON expressions are
580the given expressions evaluate to equal. The DISTINCT ON expressions are
581interpreted using the same rules as for ORDER BY. ORDER BY is used to
581interpreted using the same rules as for ORDER BY. ORDER BY is used to
582ensure that the desired row appears first.
582ensure that the desired row appears first.
584+The DISTINCT ON expression(s) must match the leftmost ORDER BY expression(s).
585+The ORDER BY clause will normally contain additional expression(s) that
586+determine the desired precedence of rows within each DISTINCT ON group.
588+In order to guarantee they match and reduce redundancy, this function
589+will prepend the The DISTINCT ON expressions to the ORDER BY clause.
584selectDistinctOn
591selectDistinctOn
585 :: (SListI columns, columns ~ (col ': cols))
592 :: (SListI columns, columns ~ (col ': cols))
586− => [SortExpression outer commons 'Ungrouped schemas params from]
593+ => [SortExpression outer commons grp schemas params from]
587− -- ^ distinct on and return the first row in ordering
594+ -- ^ DISTINCT ON expression(s) and prepended to ORDER BY clause
588− -> Selection outer commons 'Ungrouped schemas params from columns
595+ -> Selection outer commons grp schemas params from columns
589 -- ^ selection
596 -- ^ selection
590− -> TableExpression outer commons 'Ungrouped schemas params from
597+ -> TableExpression outer commons grp schemas params from
591 -- ^ intermediate virtual table
598 -- ^ intermediate virtual table
592 -> Query outer commons schemas params columns
599 -> Query outer commons schemas params columns
593selectDistinctOn distincts selection tab = UnsafeQuery $
600selectDistinctOn distincts selection tab = UnsafeQuery $
@@ -608,11 +615,11 @@ selectDistinctOn distincts selection tab = UnsafeQuery $
608-- of a general `Selection`.
615-- of a general `Selection`.
609selectDistinctOn_
616selectDistinctOn_
610 :: (SListI columns, columns ~ (col ': cols))
617 :: (SListI columns, columns ~ (col ': cols))
611− => [SortExpression outer commons 'Ungrouped schemas params from]
618+ => [SortExpression outer commons grp schemas params from]
612 -- ^ distinct on and return the first row in ordering
619 -- ^ distinct on and return the first row in ordering
613− -> NP (Aliased (Expression outer commons 'Ungrouped schemas params from)) columns
620+ -> NP (Aliased (Expression outer commons grp schemas params from)) columns
614 -- ^ selection
621 -- ^ selection
615− -> TableExpression outer commons 'Ungrouped schemas params from
622+ -> TableExpression outer commons grp schemas params from
616 -- ^ intermediate virtual table
623 -- ^ intermediate virtual table
617 -> Query outer commons schemas params columns
624 -> Query outer commons schemas params columns
618selectDistinctOn_ distincts = selectDistinctOn distincts . List
625selectDistinctOn_ distincts = selectDistinctOn distincts . List
@@ -750,7 +757,7 @@ instance RenderSQL (TableExpression outer commons grp schemas params from) where
751 :: FromClause outer commons schemas params from -- ^ table reference
758 :: FromClause outer commons schemas params from -- ^ table reference
752 -> TableExpression outer commons 'Ungrouped schemas params from
759 -> TableExpression outer commons 'Ungrouped schemas params from
753−from tab = TableExpression tab [] NoGroups NoHaving [] [] []
760+from tab = TableExpression tab [] noGroups NoHaving [] [] []
755-- | A `where_` is an endomorphism of `TableExpression`s which adds a
762-- | A `where_` is an endomorphism of `TableExpression`s which adds a
756-- search condition to the `whereClause`.
763-- search condition to the `whereClause`.
@@ -771,7 +778,7 @@ groupBy
771groupBy bys rels = TableExpression
778groupBy bys rels = TableExpression
772 { fromClause = fromClause rels
779 { fromClause = fromClause rels
773 , whereClause = whereClause rels
780 , whereClause = whereClause rels
774− , groupByClause = Group bys
781+ , groupByClause = group bys
775 , havingClause = Having []
782 , havingClause = Having []
776 , orderByClause = []
783 , orderByClause = []
777 , limitClause = limitClause rels
784 , limitClause = limitClause rels
@@ -985,19 +992,20 @@ instance (Has rel rels cols, Has col cols ty, bys ~ '[ '(rel, col)])
985-- done on @NoGroups@ while all output `Expression`s must be aggregated
992-- done on @NoGroups@ while all output `Expression`s must be aggregated
986-- in @Group Nil@. In general, all output `Expression`s in the
993-- in @Group Nil@. In general, all output `Expression`s in the
987-- complement of @bys@ must be aggregated in @Group bys@.
994-- complement of @bys@ must be aggregated in @Group bys@.
988−data GroupByClause grp from where
995+newtype GroupByClause grp from = UnsafeGroupByClause
989− NoGroups :: GroupByClause 'Ungrouped from
996+ { renderGroupByClause :: ByteString }
990− Group
997+ deriving (GHC.Generic,Show,Eq,Ord,NFData)
993− -> GroupByClause ('Grouped bys) from
995−-- | Renders a `GroupByClause`.
996instance RenderSQL (GroupByClause grp from) where
998instance RenderSQL (GroupByClause grp from) where
997− renderSQL = \case
999+ renderSQL = renderGroupByClause
998− NoGroups -> ""
1000+noGroups :: GroupByClause 'Ungrouped from
1001+noGroups = UnsafeGroupByClause ""
1004+ => NP (By from) bys
1005+ -> GroupByClause ('Grouped bys) from
1006+group bys = UnsafeGroupByClause $ case bys of
999− Group Nil -> ""
1007+ Nil -> ""
1000− Group bys -> " GROUP BY" <+> renderCommaSeparated renderSQL bys
1008+ bys' -> " GROUP BY" <+> renderCommaSeparated renderSQL bys'
1002-- | A `HavingClause` is used to eliminate groups that are not of interest.
1010-- | A `HavingClause` is used to eliminate groups that are not of interest.
1003-- An `Ungrouped` `TableExpression` may only use `NoHaving` while a `Grouped`
1011-- An `Ungrouped` `TableExpression` may only use `NoHaving` while a `Grouped`