zio / zio-quill

Compile-time Language Integrated Queries for Scala
https://zio.dev/zio-quill
Apache License 2.0
2.15k stars 346 forks source link

GetOrElse on Optional Tables works and returns ridiculous result #1301

Open deusaquilus opened 5 years ago

deusaquilus commented 5 years ago

Version: (e.g. 3.0.0-SNAPSHOT) Module: (e.g. quill-sql) Database: (e.g. any)

Expected behavior

The following should fail to generate SQL since null checking a table alias is completely invalid in SQL:

case class Person(name:String, age:Int)
case class Address(owner:String, street:String)
run(query[Person]
    .leftJoin(query[Address]).on(_.name == _.owner)
    .map { case(p, a) => a.getOrElse(Address("a","a")) })

Actual behavior

Instead not only does it not fail, but it produces a completely ridiculous query:

SELECT CASE WHEN x2 IS NOT NULL THEN x2 ELSE 'a', 'a' END.owner, CASE WHEN x2 IS NOT NULL THEN x2 ELSE 'a', 'a' END.street FROM Person x1 LEFT JOIN Address x2 ON x1.name = x2.owner

The FlattenOptionOperation must be passed additional information to know whether it is working with a table or a column. This is related to #1053

Workaround

Do not use getOrElse on a table

@getquill/maintainers

deusaquilus commented 5 years ago

I was going to fix this in #1302 but I don't think that I will. The chief problem here is that for some operations like Option[T].getOrElse, it is very difficult to determine whether there is actually a row type or column type involved. Even if I check to see that T is a case class, it is possible that the case class is just the result of a custom encoder as opposed to an actual row type. For this reason I think the issue will remain for now.