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

Closed
wants to merge 4 commits into 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();
	}

sortKeys.put("f.id", Order.ASCENDING);
pagingQueryProvider.setSortKeys(sortKeys);

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 ((f.id > ?)) ORDER BY id ASC LIMIT "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

But this query would now produce a java.sql.SQLSyntaxErrorException with MySQL as f.id in the outer WHERE clause can't be resolved.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for feedback!

I fix the issue and then regenerate pull request :)

pongdangx2 and others added 2 commits January 9, 2025 09:35
Remove aliases of the sort keys in sort conditions when the PagingQueryProvider uses a sort key with an alias and a GROUP BY clause.
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.

2 participants