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. #4743

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

Conversation

pongdangx2
Copy link

@pongdangx2 pongdangx2 commented Jan 9, 2025

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();
	}

@fmbenhassine
Copy link
Contributor

Can you please edit your initial comment and explain the problem this PR addresses. I know you linked the previous PR in which the problem is explained, but since we might include this PR in the change log it would be better to have the problem explained here.

Moreover, is this specific to MySQL only?

@fmbenhassine fmbenhassine added pr-for: bug status: waiting-for-reporter Issues for which we are waiting for feedback from the reporter in: infrastructure labels Feb 25, 2025
@pongdangx2
Copy link
Author

pongdangx2 commented Feb 26, 2025

@fmbenhassine Thank you for the comment.
I edit the comment as you requested.

Using a table alias from a subquery in an outer query is not compliant with the ANSI SQL standard. Given this, it is likely that all DBMSs providing an implementation of PagingQueryProvider in Spring Batch follow this behavior consistently.

Since I'm not familiar with other DBMSs, I only created a test for MysqlPagingQueryProvider.

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]>
This reverts commit a851d71.

Signed-off-by: KyeongHoon Lee <[email protected]>
@fmbenhassine fmbenhassine added status: feedback-provided Issues for which the feedback requested from the reporter was provided and removed status: waiting-for-reporter Issues for which we are waiting for feedback from the reporter labels Feb 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: infrastructure pr-for: bug status: feedback-provided Issues for which the feedback requested from the reporter was provided
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants