Skip to content

Commit 96bdb44

Browse files
fix: Widen YawnProjections args to YawnDef from YawnTableDef (#113)
1 parent 5e8d42e commit 96bdb44

File tree

2 files changed

+125
-19
lines changed

2 files changed

+125
-19
lines changed

yawn-api/src/main/kotlin/com/faire/yawn/project/YawnProjections.kt

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.faire.yawn.project
22

3-
import com.faire.yawn.YawnTableDef
3+
import com.faire.yawn.YawnDef
44
import com.faire.yawn.query.YawnCompilationContext
55
import org.hibernate.criterion.Projection
66
import org.hibernate.criterion.Projections
@@ -28,7 +28,7 @@ object YawnProjections {
2828
}
2929

3030
internal class Count<SOURCE : Any, FROM : Any?>(
31-
private val columnDef: YawnTableDef<SOURCE, *>.ColumnDef<FROM>,
31+
private val columnDef: YawnDef<SOURCE, *>.YawnColumnDef<FROM>,
3232
) : YawnQueryProjection<SOURCE, Long> {
3333
override fun compile(
3434
context: YawnCompilationContext,
@@ -38,13 +38,13 @@ object YawnProjections {
3838
}
3939

4040
fun <SOURCE : Any, FROM : Any?> count(
41-
columnDef: YawnTableDef<SOURCE, *>.ColumnDef<FROM>,
41+
columnDef: YawnDef<SOURCE, *>.YawnColumnDef<FROM>,
4242
): YawnQueryProjection<SOURCE, Long> {
4343
return Count(columnDef)
4444
}
4545

4646
internal class CountDistinct<SOURCE : Any, FROM : Any?>(
47-
private val columnDef: YawnTableDef<SOURCE, *>.ColumnDef<FROM>,
47+
private val columnDef: YawnDef<SOURCE, *>.YawnColumnDef<FROM>,
4848
) : YawnQueryProjection<SOURCE, Long> {
4949
override fun compile(
5050
context: YawnCompilationContext,
@@ -54,13 +54,13 @@ object YawnProjections {
5454
}
5555

5656
fun <SOURCE : Any, FROM : Any?> countDistinct(
57-
columnDef: YawnTableDef<SOURCE, *>.ColumnDef<FROM>,
57+
columnDef: YawnDef<SOURCE, *>.YawnColumnDef<FROM>,
5858
): YawnQueryProjection<SOURCE, Long> {
5959
return CountDistinct(columnDef)
6060
}
6161

6262
internal class SumNullable<SOURCE : Any, FROM : Number?>(
63-
private val columnDef: YawnTableDef<SOURCE, *>.ColumnDef<FROM>,
63+
private val columnDef: YawnDef<SOURCE, *>.YawnColumnDef<FROM>,
6464
) : YawnQueryProjection<SOURCE, Long?> {
6565
override fun compile(
6666
context: YawnCompilationContext,
@@ -71,13 +71,13 @@ object YawnProjections {
7171

7272
@JvmName("sumNullable")
7373
fun <SOURCE : Any, FROM : Number?> sum(
74-
columnDef: YawnTableDef<SOURCE, *>.ColumnDef<FROM>,
74+
columnDef: YawnDef<SOURCE, *>.YawnColumnDef<FROM>,
7575
): YawnQueryProjection<SOURCE, Long?> {
7676
return SumNullable(columnDef)
7777
}
7878

7979
internal class Sum<SOURCE : Any, FROM : Number>(
80-
private val columnDef: YawnTableDef<SOURCE, *>.ColumnDef<FROM>,
80+
private val columnDef: YawnDef<SOURCE, *>.YawnColumnDef<FROM>,
8181
) : YawnQueryProjection<SOURCE, Long> {
8282
override fun compile(
8383
context: YawnCompilationContext,
@@ -87,13 +87,13 @@ object YawnProjections {
8787
}
8888

8989
fun <SOURCE : Any, FROM : Number> sum(
90-
columnDef: YawnTableDef<SOURCE, *>.ColumnDef<FROM>,
90+
columnDef: YawnDef<SOURCE, *>.YawnColumnDef<FROM>,
9191
): YawnQueryProjection<SOURCE, Long> {
9292
return Sum(columnDef)
9393
}
9494

9595
internal class AvgNullable<SOURCE : Any, FROM : Any?>(
96-
private val columnDef: YawnTableDef<SOURCE, *>.ColumnDef<FROM>,
96+
private val columnDef: YawnDef<SOURCE, *>.YawnColumnDef<FROM>,
9797
) : YawnQueryProjection<SOURCE, Double?> {
9898
override fun compile(
9999
context: YawnCompilationContext,
@@ -104,13 +104,13 @@ object YawnProjections {
104104

105105
@JvmName("avgNullable")
106106
fun <SOURCE : Any, FROM : Number?> avg(
107-
columnDef: YawnTableDef<SOURCE, *>.ColumnDef<FROM>,
107+
columnDef: YawnDef<SOURCE, *>.YawnColumnDef<FROM>,
108108
): YawnQueryProjection<SOURCE, Double?> {
109109
return AvgNullable(columnDef)
110110
}
111111

112112
internal class Avg<SOURCE : Any, FROM : Number>(
113-
private val columnDef: YawnTableDef<SOURCE, *>.ColumnDef<FROM>,
113+
private val columnDef: YawnDef<SOURCE, *>.YawnColumnDef<FROM>,
114114
) : YawnQueryProjection<SOURCE, Double> {
115115
override fun compile(
116116
context: YawnCompilationContext,
@@ -120,13 +120,13 @@ object YawnProjections {
120120
}
121121

122122
fun <SOURCE : Any, FROM : Number> avg(
123-
columnDef: YawnTableDef<SOURCE, *>.ColumnDef<FROM>,
123+
columnDef: YawnDef<SOURCE, *>.YawnColumnDef<FROM>,
124124
): YawnQueryProjection<SOURCE, Double> {
125125
return Avg(columnDef)
126126
}
127127

128128
internal class Max<SOURCE : Any, FROM : Comparable<FROM>?>(
129-
private val columnDef: YawnTableDef<SOURCE, *>.ColumnDef<FROM>,
129+
private val columnDef: YawnDef<SOURCE, *>.YawnColumnDef<FROM>,
130130
) : YawnQueryProjection<SOURCE, FROM> {
131131
override fun compile(
132132
context: YawnCompilationContext,
@@ -137,13 +137,13 @@ object YawnProjections {
137137
}
138138

139139
fun <SOURCE : Any, FROM : Comparable<FROM>?> max(
140-
columnDef: YawnTableDef<SOURCE, *>.ColumnDef<FROM>,
140+
columnDef: YawnDef<SOURCE, *>.YawnColumnDef<FROM>,
141141
): YawnQueryProjection<SOURCE, FROM> {
142142
return Max(columnDef)
143143
}
144144

145145
internal class Min<SOURCE : Any, FROM : Comparable<FROM>?>(
146-
private val columnDef: YawnTableDef<SOURCE, *>.ColumnDef<FROM>,
146+
private val columnDef: YawnDef<SOURCE, *>.YawnColumnDef<FROM>,
147147
) : YawnQueryProjection<SOURCE, FROM> {
148148
override fun compile(
149149
context: YawnCompilationContext,
@@ -154,13 +154,13 @@ object YawnProjections {
154154
}
155155

156156
fun <SOURCE : Any, FROM : Comparable<FROM>?> min(
157-
columnDef: YawnTableDef<SOURCE, *>.ColumnDef<FROM>,
157+
columnDef: YawnDef<SOURCE, *>.YawnColumnDef<FROM>,
158158
): YawnQueryProjection<SOURCE, FROM> {
159159
return Min(columnDef)
160160
}
161161

162162
internal class GroupBy<SOURCE : Any, FROM : Any?>(
163-
private val columnDef: YawnTableDef<SOURCE, *>.ColumnDef<FROM>,
163+
private val columnDef: YawnDef<SOURCE, *>.YawnColumnDef<FROM>,
164164
) : YawnQueryProjection<SOURCE, FROM> {
165165
override fun compile(
166166
context: YawnCompilationContext,
@@ -171,7 +171,7 @@ object YawnProjections {
171171
}
172172

173173
fun <SOURCE : Any, FROM : Any?> groupBy(
174-
columnDef: YawnTableDef<SOURCE, *>.ColumnDef<FROM>,
174+
columnDef: YawnDef<SOURCE, *>.YawnColumnDef<FROM>,
175175
): YawnQueryProjection<SOURCE, FROM> {
176176
return GroupBy(columnDef)
177177
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.faire.yawn.database
2+
3+
import com.faire.yawn.project.YawnProjections
4+
import com.faire.yawn.setup.entities.BookTable
5+
import org.assertj.core.api.Assertions.assertThat
6+
import org.junit.jupiter.api.Test
7+
8+
internal class YawnAggregationNullabilityTest : BaseYawnDatabaseTest() {
9+
@Test
10+
fun `count with addIsNotNull`() {
11+
transactor.open { session ->
12+
val count = session.project(BookTable) { books ->
13+
val notes = addIsNotNull(books.notes)
14+
project(YawnProjections.count(notes))
15+
}.uniqueResult()!!
16+
17+
// 3 books have notes: harry potter, lord of the rings, the hobbit
18+
assertThat(count).isEqualTo(3)
19+
}
20+
}
21+
22+
@Test
23+
fun `countDistinct with addIsNotNull`() {
24+
transactor.open { session ->
25+
val distinctCount = session.project(BookTable) { books ->
26+
val notes = addIsNotNull(books.notes)
27+
project(YawnProjections.countDistinct(notes))
28+
}.uniqueResult()!!
29+
30+
// harry potter and the hobbit share the same note
31+
assertThat(distinctCount).isEqualTo(2)
32+
}
33+
}
34+
35+
@Test
36+
fun `sum with addIsNotNull`() {
37+
transactor.open { session ->
38+
val total = session.project(BookTable) { books ->
39+
val rating = addIsNotNull(books.rating)
40+
project(YawnProjections.sum(rating))
41+
}.uniqueResult()!!
42+
43+
// lord of the rings (10) + the hobbit (9)
44+
assertThat(total).isEqualTo(19)
45+
}
46+
}
47+
48+
@Test
49+
fun `avg with addIsNotNull`() {
50+
transactor.open { session ->
51+
val average = session.project(BookTable) { books ->
52+
val rating = addIsNotNull(books.rating)
53+
project(YawnProjections.avg(rating))
54+
}.uniqueResult()!!
55+
56+
// (10 + 9) / 2
57+
assertThat(average).isEqualTo(9.5)
58+
}
59+
}
60+
61+
@Test
62+
fun `max with addIsNotNull`() {
63+
transactor.open { session ->
64+
val maxRating = session.project(BookTable) { books ->
65+
val rating = addIsNotNull(books.rating)
66+
project(YawnProjections.max(rating))
67+
}.uniqueResult()!!
68+
69+
assertThat(maxRating).isEqualTo(10)
70+
}
71+
}
72+
73+
@Test
74+
fun `min with addIsNotNull`() {
75+
transactor.open { session ->
76+
val minRating = session.project(BookTable) { books ->
77+
val rating = addIsNotNull(books.rating)
78+
project(YawnProjections.min(rating))
79+
}.uniqueResult()!!
80+
81+
assertThat(minRating).isEqualTo(9)
82+
}
83+
}
84+
85+
@Test
86+
fun `groupBy with addIsNotNull`() {
87+
transactor.open { session ->
88+
val results = session.project(BookTable) { books ->
89+
val notes = addIsNotNull(books.notes)
90+
val rating = addIsNotNull(books.rating)
91+
project(
92+
YawnProjections.pair(
93+
YawnProjections.groupBy(notes),
94+
YawnProjections.sum(rating),
95+
),
96+
)
97+
}.list()
98+
99+
// lord of the rings has rating 10, the hobbit has rating 9
100+
assertThat(results).containsExactlyInAnyOrder(
101+
"Note for The Hobbit and Harry Potter" to 9L,
102+
"Note for Lord of the Rings" to 10L,
103+
)
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)