Skip to content

Commit 43d786d

Browse files
committed
Update SqlPagingQueryUtils
Remove table aliases of the sort keys when the PagingQueryProvider uses sort keys with table aliases and a GROUP BY clause. Signed-off-by: KyeongHoon Lee <[email protected]>
1 parent 910ca1d commit 43d786d

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/support/SqlPagingQueryUtils.java

+20-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ public static String generateLimitGroupedSqlQuery(AbstractSqlPagingQueryProvider
109109
buildGroupByClause(provider, sql);
110110
sql.append(") AS MAIN_QRY ");
111111
sql.append("WHERE ");
112-
buildSortConditions(provider, sql);
113-
sql.append(" ORDER BY ").append(buildSortClause(provider));
112+
buildSortConditionsWithoutTableAliases(provider, sql);
113+
sql.append(" ORDER BY ").append(buildSortClause(provider.getSortKeysWithoutAliases()));
114114
sql.append(" ").append(limitClause);
115115

116116
return sql.toString();
@@ -270,6 +270,19 @@ public static String buildSortClause(Map<String, Order> sortKeys) {
270270
return builder.toString();
271271
}
272272

273+
/**
274+
* Appends the where conditions required to query for the subsequent pages, without
275+
* using table aliases in sort keys.
276+
* @param provider the {@link AbstractSqlPagingQueryProvider} to be used for
277+
* pagination.
278+
* @param sql {@link StringBuilder} containing the sql to be used for the query.
279+
*/
280+
public static void buildSortConditionsWithoutTableAliases(AbstractSqlPagingQueryProvider provider,
281+
StringBuilder sql) {
282+
List<Map.Entry<String, Order>> keys = new ArrayList<>(provider.getSortKeysWithoutAliases().entrySet());
283+
buildDetailSortConditions(keys, provider, sql);
284+
}
285+
273286
/**
274287
* Appends the where conditions required to query for the subsequent pages.
275288
* @param provider the {@link AbstractSqlPagingQueryProvider} to be used for
@@ -278,6 +291,11 @@ public static String buildSortClause(Map<String, Order> sortKeys) {
278291
*/
279292
public static void buildSortConditions(AbstractSqlPagingQueryProvider provider, StringBuilder sql) {
280293
List<Map.Entry<String, Order>> keys = new ArrayList<>(provider.getSortKeys().entrySet());
294+
buildDetailSortConditions(keys, provider, sql);
295+
}
296+
297+
private static void buildDetailSortConditions(List<Map.Entry<String, Order>> keys,
298+
AbstractSqlPagingQueryProvider provider, StringBuilder sql) {
281299
List<String> clauses = new ArrayList<>();
282300

283301
for (int i = 0; i < keys.size(); i++) {

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/support/MySqlPagingQueryProviderTests.java

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717

1818
import java.util.HashMap;
1919
import java.util.Map;
20+
import java.util.LinkedHashMap;
2021

2122
import org.junit.jupiter.api.Test;
2223

@@ -27,6 +28,7 @@
2728
/**
2829
* @author Thomas Risberg
2930
* @author Michael Minella
31+
* @author Kyeonghoon Lee
3032
*/
3133
class MySqlPagingQueryProviderTests extends AbstractSqlPagingQueryProviderTests {
3234

@@ -68,6 +70,22 @@ void testGenerateRemainingPagesQueryWithGroupBy() {
6870
assertEquals(sql, s);
6971
}
7072

73+
@Test
74+
void testGenerateRemainingPagesQueryWithGroupByWithAlias() {
75+
pagingQueryProvider.setSelectClause("SELECT f.id, f.name, f.age");
76+
pagingQueryProvider.setFromClause("FROM foo f");
77+
pagingQueryProvider.setWhereClause("f.bar = 1");
78+
pagingQueryProvider.setGroupClause("dep");
79+
Map<String, Order> sortKeys = new LinkedHashMap<>();
80+
sortKeys.put("f.id", Order.ASCENDING);
81+
pagingQueryProvider.setSortKeys(sortKeys);
82+
83+
String sql = "SELECT * FROM (SELECT f.id, f.name, f.age FROM foo f WHERE f.bar = 1 GROUP BY dep) AS MAIN_QRY WHERE ((id > ?)) ORDER BY id ASC LIMIT "
84+
+ pageSize;
85+
String s = pagingQueryProvider.generateRemainingPagesQuery(pageSize);
86+
assertEquals(sql, s);
87+
}
88+
7189
@Test
7290
void testFirstPageSqlWithAliases() {
7391
Map<String, Order> sorts = new HashMap<>();

0 commit comments

Comments
 (0)