Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove alias of sort key when PagingQueryProvider has sort key with alias and group by clause. #4730

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

pongdangx2
Copy link

@pongdangx2 pongdangx2 commented Dec 5, 2024

Hello.

Motivation

I tried to build a batch application.

Environment

  • Spring boot, Spring batch
  • mysql
  • kotlin

Problem

I use MySqlPagingQueryProvider like below, and Query built by this provider only works for first page query.
(Because of sort key's alias)

 provider.setSelectClause(
        """
            SELECT 
                  t1.member_id         AS member_id
                , t2.kyc_id            AS kyc_id
                , SUM(t1.balance) AS total_balance
        """.trimIndent()
        )
        provider.setFromClause(
        """
            FROM cash_history t1
             LEFT JOIN kyc t2
               ON t1.member_id = t2.member_id
        """.trimIndent()
        )
        provider.setWhereClause(
        """
            WHERE t1.balance_year_month = '202412'
                  AND kyc_id is NOT NULL
        """.trimIndent()
        )
        provider.setGroupClause("GROUP BY t1.member_id")
        provider.setSortKeys(mapOf("t1.member_id" to Order.ASCENDING))

Generated Remaining Query

SELECT *
FROM (SELECT t1.member_id         AS member_id,
             t2.kyc_id            AS kyc_id,
             SUM(t1.balance) AS total_balance
      FROM cash_history t1
               LEFT JOIN kyc t2
                         ON t1.member_id = t2.member_id
      WHERE t1.balance_year_month = '202412'
        AND kyc_id is NOT NULL
      GROUP BY t1.member_id
     ) AS MAIN_QRY
WHERE ((MAIN_QRY.member_id > ?))
ORDER BY t1.member_id ASC
LIMIT 100

This generated remaining query has order by clause with t1.memeber, and valid scope of t1 alias is in MAIN_QRY.

Solution

  • Use getSortKeysWithoutAliases method for building order by clause after MAIN_QRY appended.
public static String generateLimitGroupedSqlQuery(AbstractSqlPagingQueryProvider provider, String limitClause) {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT * ");
		sql.append(" FROM (");
		sql.append("SELECT ").append(provider.getSelectClause());
		sql.append(" FROM ").append(provider.getFromClause());
		sql.append(provider.getWhereClause() == null ? "" : " WHERE " + provider.getWhereClause());
		buildGroupByClause(provider, sql);
		sql.append(") AS MAIN_QRY ");
		sql.append("WHERE ");
		buildSortConditions(provider, sql);
		sql.append(" ORDER BY ").append(buildSortClause(provider.getSortKeysWithoutAliases()));
		sql.append(" ").append(limitClause);

		return sql.toString();
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant