File tree 6 files changed +69
-4
lines changed
main/java/tech/ydb/hibernate/dialect
test/java/tech/ydb/hibernate/student
6 files changed +69
-4
lines changed Original file line number Diff line number Diff line change 20
20
21
21
strategy :
22
22
matrix :
23
- java : [ '17' ]
23
+ java : [ '17', '21' ]
24
24
25
25
steps :
26
26
- uses : actions/checkout@v4
Original file line number Diff line number Diff line change
1
+ ## 0.9.5 ##
2
+
3
+ - Added query hint for view index for "select * from ... where" queries
4
+
1
5
## 0.9.4 ##
2
6
3
7
- Fixed the generated bool value
Original file line number Diff line number Diff line change 1
1
package tech .ydb .hibernate .dialect ;
2
2
3
3
import java .time .LocalDateTime ;
4
+ import java .util .List ;
4
5
import org .hibernate .boot .model .FunctionContributions ;
5
6
import org .hibernate .boot .model .TypeContributions ;
6
7
import org .hibernate .dialect .Dialect ;
51
52
import org .hibernate .type .StandardBasicTypes ;
52
53
import tech .ydb .hibernate .dialect .exporter .EmptyExporter ;
53
54
import tech .ydb .hibernate .dialect .exporter .YdbIndexExporter ;
55
+ import tech .ydb .hibernate .dialect .hint .IndexQueryHintHandler ;
54
56
import tech .ydb .hibernate .dialect .translator .YdbSqlAstTranslatorFactory ;
55
57
import tech .ydb .hibernate .dialect .types .LocalDateTimeJavaType ;
56
58
import tech .ydb .hibernate .dialect .types .LocalDateTimeJdbcType ;
@@ -118,6 +120,11 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
118
120
);
119
121
}
120
122
123
+ @ Override
124
+ public String getQueryHintString (String query , List <String > hintList ) {
125
+ return IndexQueryHintHandler .INSTANCE .addQueryHints (query , hintList );
126
+ }
127
+
121
128
@ Override
122
129
public LimitHandler getLimitHandler () {
123
130
return LimitOffsetLimitHandler .INSTANCE ;
Original file line number Diff line number Diff line change
1
+ package tech .ydb .hibernate .dialect .hint ;
2
+
3
+ import java .util .ArrayList ;
4
+ import java .util .List ;
5
+ import java .util .regex .Matcher ;
6
+ import java .util .regex .Pattern ;
7
+
8
+ /**
9
+ * @author Kirill Kurdyukov
10
+ */
11
+ public class IndexQueryHintHandler {
12
+ private static final Pattern SELECT_FROM_WHERE_QUERY_PATTERN = Pattern
13
+ .compile ("^\\ s*(select.+?from\\ s+\\ w+)(.+where.+)$" , Pattern .CASE_INSENSITIVE );
14
+
15
+ public static final String HINT_USE_INDEX = "use_index:" ;
16
+ public static final IndexQueryHintHandler INSTANCE = new IndexQueryHintHandler ();
17
+
18
+ public String addQueryHints (String query , List <String > hints ) {
19
+ if (hints .isEmpty ()) {
20
+ return query ;
21
+ }
22
+
23
+ var useIndexes = new ArrayList <String >();
24
+ hints .forEach (hint -> {
25
+ if (hint .startsWith (HINT_USE_INDEX )) {
26
+ useIndexes .add (hint .substring (HINT_USE_INDEX .length ()));
27
+ }
28
+ });
29
+
30
+ if (!useIndexes .isEmpty ()) {
31
+ Matcher matcher = SELECT_FROM_WHERE_QUERY_PATTERN .matcher (query );
32
+ if (matcher .matches () && matcher .groupCount () > 1 ) {
33
+ String startToken = matcher .group (1 );
34
+ String endToken = matcher .group (2 );
35
+
36
+ return startToken + " view " + String .join (", " , useIndexes ) + endToken ;
37
+ }
38
+ }
39
+
40
+ return query ;
41
+ }
42
+ }
Original file line number Diff line number Diff line change @@ -143,7 +143,6 @@ void studentsAndCourses_Lazy_ManyToManyTest() {
143
143
);
144
144
}
145
145
146
-
147
146
@ Test
148
147
void studentsByGroupName_Lazy_OneToManyTest () {
149
148
inTransaction (
@@ -160,6 +159,20 @@ void studentsByGroupName_Lazy_OneToManyTest() {
160
159
);
161
160
}
162
161
162
+ @ Test
163
+ void groupByGroupName_ViewIndex () {
164
+ inTransaction (
165
+ session -> {
166
+ Group group = session
167
+ .createQuery ("FROM Group g WHERE g.name = 'M3439'" , Group .class )
168
+ .addQueryHint ("use_index:group_name_index" )
169
+ .getSingleResult ();
170
+
171
+ assertEquals ("M3439" , group .getName ());
172
+ }
173
+ );
174
+ }
175
+
163
176
@ Test
164
177
void studentsByGroupName_Eager_OneToManyTest () {
165
178
inTransaction (
Original file line number Diff line number Diff line change 7
7
import jakarta .persistence .NamedQuery ;
8
8
import jakarta .persistence .OneToMany ;
9
9
import jakarta .persistence .Table ;
10
+ import java .util .List ;
10
11
import lombok .Getter ;
11
12
import lombok .Setter ;
12
13
13
- import java .util .List ;
14
-
15
14
/**
16
15
* @author Kirill Kurdyukov
17
16
*/
You can’t perform that action at this time.
0 commit comments