@@ -89,6 +89,8 @@ public final class Template {
89
89
= Set .of ("date" , "time" );
90
90
private static final Set <String > LITERAL_PREFIXES
91
91
= Set .of ("n" , "x" , "varbyte" , "bx" , "bytea" , "date" , "time" , "timestamp" , "zone" );
92
+ private static final Set <String > FETCH_BIGRAMS
93
+ = Set .of ("first" , "next" );
92
94
93
95
private static final String PUNCTUATION = "=><!+-*/()',|&`" ;
94
96
@@ -166,6 +168,7 @@ public static String renderWhereStringTemplate(
166
168
boolean inExtractOrTrim = false ;
167
169
boolean inCast = false ;
168
170
boolean afterCastAs = false ;
171
+ boolean afterFetch = false ;
169
172
170
173
boolean hasMore = tokens .hasMoreTokens ();
171
174
String nextToken = hasMore ? tokens .nextToken () : null ;
@@ -215,10 +218,14 @@ else if ( quotedIdentifier && dialect.closeQuote()==token.charAt(0) ) {
215
218
}
216
219
}
217
220
218
- final boolean quotedOrWhitespace =
219
- quoted || quotedIdentifier || isQuoteCharacter
220
- || token .isBlank ();
221
- if ( quotedOrWhitespace ) {
221
+ final boolean isWhitespace = token .isBlank ();
222
+
223
+ final boolean wasAfterFetch = afterFetch ;
224
+ afterFetch = afterFetch && isWhitespace ;
225
+
226
+ final boolean isQuoted =
227
+ quoted || quotedIdentifier || isQuoteCharacter ;
228
+ if ( isQuoted || isWhitespace ) {
222
229
result .append ( token );
223
230
}
224
231
else if ( beforeTable ) {
@@ -247,6 +254,13 @@ else if ( inCast && ("as".equals( lcToken ) || afterCastAs) ) {
247
254
result .append ( token );
248
255
afterCastAs = true ;
249
256
}
257
+ else if ( isFetch ( dialect , lcToken ) ) {
258
+ result .append ( token );
259
+ afterFetch = true ;
260
+ }
261
+ else if ( wasAfterFetch && FETCH_BIGRAMS .contains ( lcToken ) ) {
262
+ result .append ( token );
263
+ }
250
264
else if ( !inFromClause // don't want to append alias to tokens inside the FROM clause
251
265
&& isIdentifier ( token )
252
266
&& !isFunctionOrKeyword ( lcToken , nextToken , dialect , typeConfiguration )
@@ -256,7 +270,7 @@ && isIdentifier( token )
256
270
.append ( dialect .quote (token ) );
257
271
}
258
272
else {
259
- if ( ")" .equals ( lcToken ) ) {
273
+ if ( ")" .equals (lcToken ) ) {
260
274
inExtractOrTrim = false ;
261
275
inCast = false ;
262
276
afterCastAs = false ;
@@ -286,6 +300,11 @@ else if ( inFromClause && ",".equals(lcToken) ) {
286
300
return result .toString ();
287
301
}
288
302
303
+ private static boolean isFetch (Dialect dialect , String lcToken ) {
304
+ return "fetch" .equals ( lcToken )
305
+ && dialect .getKeywords ().contains ( "fetch" );
306
+ }
307
+
289
308
private static boolean endsWithDot (String token ) {
290
309
return token != null && token .endsWith ( "." );
291
310
}
0 commit comments