zio-quill icon indicating copy to clipboard operation
zio-quill copied to clipboard

Semantics for Distinct On

Open deusaquilus opened this issue 5 years ago • 1 comments

Version: 3.3.0 Module: quill-sql Database: all

Create a Distinct On clause with the following semantics

case class Person(firstName: String, lastName:String, age:Int)

Select a single field:

quote {
  query[Person].distinctOn(p => p.firstName)
}
// select distinct on (firstName) firstName, lastName, age from Person

Select multiple fields:

quote {
  query[Person].distinctOn(p => (p.firstName, p.lastName))
}
// select distinct on (firstName, lastName) firstName, lastName, age from Person

Select concatenation of fields:

quote {
  query[Person].distinctOn(p => (p.firstName + p.lastName))
}
// select distinct on (firstName || lastName) firstName, lastName, age from Person

How distinctOn interacts with map.

quote {
  query[Person]
  .map(p => (p.firstName, p.lastName))
  .distinctOn(p => (p._1))
}
// select distinct on (_1) _1, _2 from (select firstName as _1, lastName as _2 from Person)
// Possibly beta-reduce to:
// select distinct on (firstName)firstName, lastName from Person

Should be possible to use UDFs and other operations inside distinctOn.

quote {
  query[Person].distinctOn(p => infix"myUDF(${p.firstName})")
}
// select distinct on myUDF(firstName) firstName, lastName, age from Person

Note how distinctOn should not change the type in Query[T]. The same T that existed before the distinctOn should come out afterward.

@getquill/maintainers

deusaquilus avatar Jul 26 '19 16:07 deusaquilus

Any updates on this, seems to have been stale for quite a while.

Yomanz avatar Aug 08 '21 15:08 Yomanz