I am trying to hack together a solution that would allow me to use DISTINCT ON clause in Postgres. As is well known, it does not work out of the box in Slick. I am trying to implement it using SimpleExpression but cannot find a way to make it accept tuple as return type.
case class Item(name: String, param: String, ord: Int)
class Items(tag: Tag) extends Table[Item](tag, "item") {
def name = column[String]("name")
def param = column[String]("param")
def ord = column[Int]("ord")
def * = (name, param, ord).mapTo[Item]
}
val items = TableQuery[Items]
val distinctOn = SimpleExpression.unary[String, (String, String)] { (col, qb) =>
import slick.util.MacroSupport._
import qb._
b"distinct on ($col) name, param"
}
val query = items.sortBy(i => (i.name, i.ord.desc)).map(i => distinctOn(i.name))
It does work when return type is a single column, but if I try to return more than one column (or the whole row, using "*") I get "could not find implicit value for evidence parameter of type slick.ast.TypedType[...]". I do realize that SimpleExpression probably wasn't meant for such scenarios but is it even possible to achieve what I am trying to?