Closed posa88 closed 7 years ago
I rewrite it as follow and it works.But use a duplicated function and it's ugly i'm afraid.
def find(condition: FilterQuery): List[Any] = {
val distinctAndSum1= quote {
new {
def apply(xs: Query[StatItem])(p: StatItem => Boolean) =
xs.filter(p(_)).map(s => (s.mid, s.realpid, s.gender, s.age, s.day, s.pv, s.click, s.uv)).distinct
.nested
.groupBy(x => (x._1, x._2)).map{
case (group, items) =>
(group._1, group._2,
items.map(_._6).sum, items.map(_._7).sum, items.map(_._8).sum)
}
}
}
val distinctAndSum2 = quote {
new {
def apply(xs: Query[StatItem])(p: StatItem => Boolean) =
xs.filter(p(_)).map(s => (s.mid, s.realpid, s.gender, s.age, s.day, s.pv, s.click, s.uv)).distinct
.nested
.groupBy(y => (y._1, y._2)).map{
case (group, items) =>
(group._1, group._2,
items.map(_._6).sum, items.map(_._7).sum, items.map(_._8).sum)
}
}
}
val q = quote {
for {
audienceSum <- distinctAndSum1(query[StatItem])(c =>
c.day > lift(condition.startDay) && c.day < lift(condition.endDay)
&& (liftQuery(condition.labels.split(",").toList).contains(c.tags)) // default should be all labels
&& liftQuery(condition.gender.split(",").toList).contains(c.gender)
&& liftQuery(condition.age.split(",").toList).contains(c.age)
)
adPlaceSum <- distinctAndSum2(query[StatItem])(d =>
d.day > lift(condition.start) && d.day < lift(condition.endDay))
if (audienceSum._1 == adPlaceSum._1 && audienceSum._2 == adPlaceSum._2)
} yield {
(audienceSum._1)
}
}
ctx.run(q)
}
simplified code:
object Bug extends App {
// val ctx = io.getquill.testContext
val ctx = io.getquill.context.sql.testContext
import ctx._
val q = quote {
for {
a <- qr1.nested.groupBy(a => a.i).map { case (i, l) => (i, l.map(_.i).sum) }
b <- qr1.nested.groupBy(a => a.i).map { case (i, l) => (i, l.map(_.i).sum) }
} yield {
(a, b)
}
}
println(ctx.run(q.dynamic).string)
/*
SELECT
a._1_1,
a._1_2,
a._2_1,
a._2_2
FROM (SELECT
a.i _1_1,
a.i _1_2,
SUM(a.i) _2_1,
SUM(a.i) _2_2
FROM (SELECT
x.i
FROM TestEntity x) a
GROUP BY a.i) a,
(SELECT
a.i _1_1,
a.i _1_2,
SUM(a.i) _2_1,
SUM(a.i) _2_2
FROM (SELECT
x.i
FROM TestEntity x) a
GROUP BY a.i) a
*/
}
Note that both sub-queries have identifier a
Version: (e.g.
0.4.1-SNAPSHOT
) Module: (e.g.quill-jdbc
) Database: (e.g.mysql
)Expected behavior
use high-order functions like:
Actual behavior
I think we should generate different name for the generated table if we call a high-order function twice or more.
@getquill/maintainers