Skip to content

Commit ea5d862

Browse files
committed
Added a test case for a simpler version of #746
1 parent 860bdf2 commit ea5d862

File tree

1 file changed

+58
-2
lines changed
  • beam-core/test/Database/Beam/Test

1 file changed

+58
-2
lines changed

beam-core/test/Database/Beam/Test/SQL.hs

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ tests = testGroup "SQL generation tests"
3131
, leftJoinSingle
3232
, aggregates
3333
, orderBy
34+
, innerNub
3435

3536
, joinHaving
3637

@@ -127,7 +128,7 @@ simpleWhereNoFrom :: TestTree
127128
simpleWhereNoFrom =
128129
testCase "WHERE clause not dropped if there is no FROM" $ do
129130
SqlSelect Select { selectTable = SelectTable { .. }, .. } <- pure $ selectMock simple
130-
131+
131132
selectGrouping @?= Nothing
132133
selectOrdering @?= []
133134
selectLimit @?= Nothing
@@ -137,7 +138,7 @@ simpleWhereNoFrom =
137138
-- Important point: no FROM clause, yet WHERE clause should still be here
138139
selectFrom @?= Nothing
139140
selectWhere @?= (Just (ExpressionValue (Value False)))
140-
141+
141142
where
142143
simple :: Q (MockSqlBackend Command) EmptyDb s (QExpr (MockSqlBackend Command) s Bool)
143144
simple = do
@@ -770,6 +771,61 @@ orderBy =
770771
(FromTable (TableFromSubSelect subselect) (Just ("t1", Nothing)))
771772
Nothing)
772773

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+
773829
-- | HAVING clause should not be floated out of a join
774830

775831
joinHaving :: TestTree

0 commit comments

Comments
 (0)