haskell-beam / haskell-beam/beam

Lone left join doesn't compile to left join

Open
#332 4 comments 1 reaction 0 assignees View on GitHub
Dominant language
Haskell
Stars
635
Forks
193
PR merge metrics
No merged PRs in 30d

Description

Hey, and thanks for a great library!

I ran into something surprising today.

Consider the following (nonsensical) queries:

```haskell
test :: IO ()
test = dumpSqlSelect $ do
dep <- all_ (_departments employeeDbSettings)
role <- leftJoin_
(all_ (_roles employeeDbSettings))
(\_ -> val_ False)
pure (dep, role)

test2 :: IO ()
test2 = dumpSqlSelect $ do
leftJoin_
(all_ (_roles employeeDbSettings))
(\_ -> val_ False)
```

They result in:

```sql
λ> test
SELECT `t0`.`name` AS "res0", `t0`.`head__first_name` AS "res1", `t0`.`head__last_name` AS "res2", `t0`.`head__created` AS "res3", `t1`.`for_employee__first_name` AS "res4", `t1`.`for_employee__last_name` AS "res5", `t1`.`for_employee__created` AS "res6", `t1`.`name` AS "res7", `t1`.`started` AS "res8"
FROM "departments" AS "t0"
LEFT JOIN "roles" AS "t1" ON (FALSE)
λ> test2
SELECT `t0`.`for_employee__first_name` AS "res0", `t0`.`for_employee__last_name` AS "res1", `t0`.`for_employee__created` AS "res2", `t0`.`name` AS "res3", `t0`.`started` AS "res4"
FROM "roles" AS "t0"
WHERE FALSE
```

The second does an ordinary select, so it won't return a null `role` row when the condition fails.

Is this the intended behaviour? It was quite surprising to me. I suppose a left join without a left table is an odd thing to do, but the types allow it after all...

For context, I had a query parameterised by what to select before a `leftJoin_`, and in some cases I already knew what row I needed so I passed a `pure $ val_ something`. I.e. I sometimes did `do x <- all_ blah; leftJoin_ ...`, and sometimes just `do x <- pure (val_ something); leftJoin_ ...`. I would have expected the second version to work the same as when the `all_` in the first version returns a single element `something`.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.