Skip to content

Commit 9ed1119

Browse files
committed
HHH-18069 - Add test
1 parent 7ca9d0a commit 9ed1119

File tree

1 file changed

+96
-14
lines changed

1 file changed

+96
-14
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/set/UnionOfPartitionResultsTest.java

Lines changed: 96 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
1212
import org.hibernate.testing.orm.junit.DomainModel;
13-
import org.hibernate.testing.orm.junit.FailureExpected;
13+
import org.hibernate.testing.orm.junit.JiraKey;
1414
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
1515
import org.hibernate.testing.orm.junit.SessionFactory;
1616
import org.hibernate.testing.orm.junit.SessionFactoryScope;
@@ -25,41 +25,112 @@
2525
/**
2626
* @author Jan Schatteman
2727
*/
28-
@DomainModel (
29-
annotatedClasses = { UnionOfPartitionResultsTest.Apple.class, UnionOfPartitionResultsTest.Pie.class }
28+
@DomainModel(
29+
annotatedClasses = {UnionOfPartitionResultsTest.Apple.class, UnionOfPartitionResultsTest.Pie.class}
3030
)
3131
@SessionFactory
32+
@JiraKey( "HHH-18069" )
3233
public class UnionOfPartitionResultsTest {
3334

3435
@Test
35-
@FailureExpected
3636
@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsUnion.class)
37+
public void testSubqueryWithUnion(SessionFactoryScope scope) {
38+
scope.inTransaction(
39+
session -> {
40+
String q = """
41+
SELECT id
42+
FROM
43+
(
44+
( SELECT id id, bakedPie bakedPie
45+
FROM Apple c
46+
)
47+
UNION ALL
48+
( SELECT id id, bakedPie bakedPie
49+
FROM Apple a
50+
)
51+
)
52+
""";
53+
54+
Query query = session.createQuery( q );
55+
56+
query.list();
57+
}
58+
);
59+
}
60+
61+
@Test
62+
public void testSubquery(SessionFactoryScope scope) {
63+
scope.inTransaction(
64+
session -> {
65+
String q = """
66+
SELECT id
67+
FROM
68+
(
69+
SELECT id id, bakedPie bakedPie
70+
FROM Apple c
71+
)
72+
""";
73+
74+
Query query = session.createQuery( q );
75+
76+
query.list();
77+
}
78+
);
79+
}
80+
81+
@Test
82+
@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsUnion.class)
83+
public void testUnionQuery(SessionFactoryScope scope) {
84+
scope.inTransaction(
85+
session -> {
86+
String q = """
87+
( SELECT id id, bakedPie bakedPie
88+
FROM Apple c
89+
)
90+
UNION ALL
91+
( SELECT id id, bakedPie bakedPie
92+
FROM Apple c
93+
)
94+
""";
95+
96+
Query query = session.createQuery( q );
97+
98+
query.list();
99+
}
100+
);
101+
}
102+
103+
104+
@Test
105+
@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsUnion.class)
106+
@RequiresDialectFeature(feature = DialectFeatureChecks.SupportPartitionBy.class)
37107
public void testUnionOfPartitionResults(SessionFactoryScope scope) {
38108
scope.inTransaction(
39109
session -> {
40110
String q =
41111
"SELECT new CurrentApple(id, bakedPie.id, dir) " +
42112
"FROM (" +
43-
"(" +
44-
"SELECT id id, bakedPie bakedPie, bakedOn bakedOn, MAX(bakedOn) OVER (PARTITION BY bakedPie.id) mbo, -1 dir " +
45-
"FROM Apple c " +
46-
"WHERE bakedPie.id IN (1,2,3,4) AND bakedOn <= :now" +
47-
") UNION ALL (" +
48-
"SELECT id id, bakedPie bakedPie, bakedOn bakedOn, MIN(bakedOn) OVER (PARTITION BY bakedPie.id) mbo, 1 dir " +
49-
"FROM Apple c " +
50-
"WHERE bakedPie.id IN (1,2,3,4) AND bakedOn > :now" +
51-
")" +
113+
"(" +
114+
"SELECT id id, bakedPie bakedPie, bakedOn bakedOn, MAX(bakedOn) OVER (PARTITION BY bakedPie.id) mbo, -1 dir " +
115+
"FROM Apple c " +
116+
"WHERE bakedPie.id IN (1,2,3,4) AND bakedOn <= :now" +
117+
") UNION ALL (" +
118+
"SELECT id id, bakedPie bakedPie, bakedOn bakedOn, MIN(bakedOn) OVER (PARTITION BY bakedPie.id) mbo, 1 dir " +
119+
"FROM Apple c " +
120+
"WHERE bakedPie.id IN (1,2,3,4) AND bakedOn > :now" +
121+
")" +
52122
") " +
53123
"WHERE bakedOn = mbo ORDER BY dir";
54124

55125
Query<CurrentApple> query = session.createQuery( q, CurrentApple.class );
56126
query.setParameter( "now", LocalDate.now());
57127

58-
query.getSingleResult();
128+
query.list();
59129
}
60130
);
61131
}
62132

133+
63134
public static class CurrentApple {
64135
private final int id;
65136
private final int pieId;
@@ -70,12 +141,15 @@ public CurrentApple(int id, int pieId, int dir) {
70141
this.pieId = pieId;
71142
this.dir = dir;
72143
}
144+
73145
public int getDir() {
74146
return dir;
75147
}
148+
76149
public int getId() {
77150
return id;
78151
}
152+
79153
public int getPieId() {
80154
return pieId;
81155
}
@@ -93,20 +167,25 @@ public static class Apple {
93167
public Integer getId() {
94168
return id;
95169
}
170+
96171
public Apple setId(Integer id) {
97172
this.id = id;
98173
return this;
99174
}
175+
100176
public LocalDate getBakedOn() {
101177
return bakedOn;
102178
}
179+
103180
public Apple setBakedOn(LocalDate bakedOn) {
104181
this.bakedOn = bakedOn;
105182
return this;
106183
}
184+
107185
public Pie getBakedPie() {
108186
return bakedPie;
109187
}
188+
110189
public Apple setBakedPie(Pie bakedPie) {
111190
this.bakedPie = bakedPie;
112191
return this;
@@ -123,13 +202,16 @@ public static class Pie {
123202
public Integer getId() {
124203
return id;
125204
}
205+
126206
public Pie setId(Integer id) {
127207
this.id = id;
128208
return this;
129209
}
210+
130211
public String getTaste() {
131212
return taste;
132213
}
214+
133215
public Pie setTaste(String taste) {
134216
this.taste = taste;
135217
return this;

0 commit comments

Comments
 (0)