@@ -31,6 +31,7 @@ tests = testGroup "SQL generation tests"
31
31
, leftJoinSingle
32
32
, aggregates
33
33
, orderBy
34
+ , innerNub
34
35
35
36
, joinHaving
36
37
@@ -127,7 +128,7 @@ simpleWhereNoFrom :: TestTree
127
128
simpleWhereNoFrom =
128
129
testCase " WHERE clause not dropped if there is no FROM" $ do
129
130
SqlSelect Select { selectTable = SelectTable { .. }, .. } <- pure $ selectMock simple
130
-
131
+
131
132
selectGrouping @?= Nothing
132
133
selectOrdering @?= []
133
134
selectLimit @?= Nothing
@@ -137,7 +138,7 @@ simpleWhereNoFrom =
137
138
-- Important point: no FROM clause, yet WHERE clause should still be here
138
139
selectFrom @?= Nothing
139
140
selectWhere @?= (Just (ExpressionValue (Value False )))
140
-
141
+
141
142
where
142
143
simple :: Q (MockSqlBackend Command ) EmptyDb s (QExpr (MockSqlBackend Command ) s Bool )
143
144
simple = do
@@ -770,6 +771,61 @@ orderBy =
770
771
(FromTable (TableFromSubSelect subselect) (Just (" t1" , Nothing )))
771
772
Nothing )
772
773
774
+
775
+ -- | Ensure that a SQL DISTINCT in a subquery does not propagate up (see issue #746)
776
+ innerNub :: TestTree
777
+ innerNub =
778
+ testCase " DISTINCT clause on inner SELECT" $ do
779
+ stmt@ (SqlSelect Select {selectTable = SelectTable {.. }, .. }) <- pure $ selectMock query
780
+
781
+ selectGrouping @?= Nothing
782
+ selectOrdering @?= []
783
+ selectLimit @?= Nothing
784
+ selectOffset @?= Nothing
785
+ selectHaving @?= Nothing
786
+ selectQuantifier @?= Nothing -- quantifier should be in the subquery
787
+ selectFrom
788
+ @?= Just
789
+ ( FromTable
790
+ ( TableFromSubSelect
791
+ ( Select
792
+ { selectTable =
793
+ SelectTable
794
+ { selectQuantifier = Just SetQuantifierDistinct
795
+ , selectProjection = ProjExprs [(ExpressionFieldName (QualifiedField " t0" " first_name" ), Just " res0" )]
796
+ , selectFrom = Just (FromTable (TableNamed (TableName Nothing " employees" )) (Just (" t0" , Nothing )))
797
+ , selectWhere = Nothing
798
+ , selectGrouping = Nothing
799
+ , selectHaving = Nothing
800
+ }
801
+ , selectOrdering = []
802
+ , selectLimit = Nothing
803
+ , selectOffset = Nothing
804
+ }
805
+ )
806
+ )
807
+ (Just (" t0" , Nothing ))
808
+ )
809
+ selectWhere
810
+ @?= Just
811
+ ( ExpressionCompOp
812
+ " =="
813
+ Nothing
814
+ (ExpressionFieldName (QualifiedField " t0" " res0" ))
815
+ (ExpressionValue (Value @ Text " Alice" ))
816
+ )
817
+ where
818
+ query :: Q (MockSqlBackend Command ) EmployeeDb s (QExpr (MockSqlBackend Command ) s Text )
819
+ query = do
820
+ name <- subQuery
821
+ guard_ (name ==. val_ " Alice" )
822
+ pure name
823
+
824
+ -- This sub query contains a DISTINCT clause via `nub_`
825
+ subQuery :: Q (MockSqlBackend Command ) EmployeeDb s (QExpr (MockSqlBackend Command ) s Text )
826
+ subQuery = nub_ $ _employeeFirstName <$> (all_ (_employees employeeDbSettings))
827
+
828
+
773
829
-- | HAVING clause should not be floated out of a join
774
830
775
831
joinHaving :: TestTree
0 commit comments