Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Embedded with Distinct can cause Incorrect and Duplicate Column Aliases #1602

Closed
deusaquilus opened this issue Sep 10, 2019 · 0 comments · Fixed by #1604
Closed

Using Embedded with Distinct can cause Incorrect and Duplicate Column Aliases #1602

deusaquilus opened this issue Sep 10, 2019 · 0 comments · Fixed by #1604

Comments

@deusaquilus
Copy link
Collaborator

Note that this issue involves the fixes from #1597 or else #1564 with occour before this problem even has a chance to manifest itself.

Version: (e.g. 3.4.5-SNAPSHOT)
Module: (e.g. quill-sql)
Database: (e.g. ALL)

When using an Embedded entity to represent a coproduct instead of a tuple, since aliases include embedded entity property (in ExpandSelect) names but properties are not expanded with embedded property names (in SqlIdiom) the set aliases select from the outer query will not be the same as the set of aliases being expressed.

case class TestEntity(s: String, i: Int, l: Long, o: Option[Int]) extends Embedded
case class Dual(ta: TestEntity, tb: TestEntity)

val qr1 = quote {
  query[TestEntity]
}

val q = quote {
  qr1.join(qr1).on((a, b) => a.i == b.i).nested.map(both => both match { case (a, b) => Dual(a, b) }).distinct.nested
}

println(run(q).string)
// SELECT x.s, x.i, x.l, x.o, x.s, x.i, x.l, x.o FROM (
//   SELECT DISTINCT both._1s AS tas, both._1i AS tai, both._1l AS tal, both._1o AS tao, both._2s AS tbs, both._2i AS tbi, both._2l AS tbl, both._2o AS tbo FROM (
//    SELECT a.s AS _1s, a.i AS _1i, a.l AS _1l, a.o AS _1o, b.s AS _2s, b.i AS _2i, b.l AS _2l, b.o AS _2o FROM test_entity a INNER JOIN test_entity b ON a.i = b.i) AS both) AS x

Notice that s, i, l, and o are not actually the aliases inside the 1st inner query. Also, s, i, l, and o are all selected twice.

Workaround

Use tuple co-products instead.

@getquill/maintainers

@deusaquilus deusaquilus changed the title Using Embedded with Distinct can cause Duplicate Column Aliases Using Embedded with Distinct can cause Incorrect and Duplicate Column Aliases Sep 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant