Permalink
Browse files

Another case of UNIONs and incorrect SQL.

Make the PruneProjections phase work correctly, assign the same type symbol on both sides of a Union.
  • Loading branch information...
iulianu committed Nov 10, 2015
1 parent f5409bf commit 6d7297e97abc193e78d9964ab2eb31ec51bcb651
@@ -157,4 +157,32 @@ class UnionTest extends AsyncTest[RelationalTestDB] {
)
}
def testCountWithUnionAndSort = {
case class Delivery(id: Long, dname: String, sentAt: Long)
class Deliveries(tag: Tag) extends Table[Delivery](tag, "d") {
val id = column[Long]("delivery_id")
val dname = column[String]("dname")
val sentAt = column[Long]("sent_at")
def * = (id, dname, sentAt) <> (Delivery.tupled, Delivery.unapply)
}
def leftSide = {
TableQuery[Deliveries].filter(_.sentAt >= 1400000000L)
}
def rightSide = {
TableQuery[Deliveries].filter(_.sentAt < 1400000000L)
}
val query =
leftSide.union(rightSide).sortBy(_.id.desc).length
DBIO.seq(
TableQuery[Deliveries].schema.create,
mark("q", query.result).map(_ shouldBe 0)
)
}
}
@@ -12,6 +12,7 @@ class RelabelUnions extends Phase {
def apply(state: CompilerState) = state.map(_.replace({
case u @ Union(Bind(_, _, Pure(StructNode(ls), lts)), rb @ Bind(_, _, Pure(StructNode(rs), rts)), _) =>
val rs2 = ls.zip(rs).map { case ((s, _), (_, n)) => (s, n) }
u.copy(right = rb.copy(select = Pure(StructNode(rs2), rts))).infer()
val unifiedTs = lts // Use same type symbol on both sides of the Union
u.copy(right = rb.copy(select = Pure(StructNode(rs2), unifiedTs))).infer()
}, keepType = true, bottomUp = true))
}

0 comments on commit 6d7297e

Please sign in to comment.