zio / zio-quill

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

Invliad Reduction for Optional Booleans #1686

Open deusaquilus opened 5 years ago

deusaquilus commented 5 years ago

Version: (e.g. 3.4.8) Module: (e.g. quill-sql) Database: (e.g. SQL Server)

val ctx = new SqlMirrorContext(SQLServerDialect, SnakeCase)
case class Something(id:Int, value:Option[Int])
run(query[Something].map(s => (s.id, s.value.map(vv => infix"${vv}".as[Boolean]).getOrElse(false))))
// SELECT s._1, s._2 FROM (SELECT s.id AS _1, s.value OR s.value IS NOT NULL AS _2 FROM something s) AS s

The statement s.value OR s.value IS NOT NULL is not valid in SQL Server.

@getquill/maintainers

deusaquilus commented 5 years ago

I think the following from FlattenOptionOperation clauses this.

case OptionGetOrElse(OptionMap(ast, alias, body), Constant(b: Boolean)) =>
        apply((BetaReduction(body, alias -> ast) +||+ emptyOrNot(b, ast)): Ast)

This construct works for Postgres so need to have a switch in FlattenOptionOperation to disable it in other contexts.