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

JdbcPagingItemReader - When using sortKeys with alias, I think it should paging by column name rather than alias in the select clause. #4573

Open
Gyuchool opened this issue Apr 4, 2024 · 0 comments
Labels
status: waiting-for-triage Issues that we did not analyse yet type: feature

Comments

@Gyuchool
Copy link

Gyuchool commented Apr 4, 2024

@Bean
@StepScope
public JdbcPagingItemReader<Point> reader() {

    return new JdbcPagingItemReaderBuilder<Point>()
            .name("reader")
            .pageSize(chunkSize)
            .fetchSize(chunkSize)
            .dataSource(datasource)
            .rowMapper(pointRowMapper)
            .parameterValues(parameters)
            .queryProvider(pagingQueryProvider())
            .build();
}

@Bean
public PagingQueryProvider pagingQueryProvider() {

    SqlPagingQueryProviderFactoryBean queryProvider = new SqlPagingQueryProviderFactoryBean();
    queryProvider.setDataSource(datasource);
    queryProvider.setSelectClause("place_id, user_id as member_id, points");
    queryProvider.setFromClause("user_point");
    queryProvider.setWhereClause("place_id = :place_id");
    queryProvider.setSortKeys(sortKeyAsc("place_id", "member_id"));

    try {
        return queryProvider.getObject();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

In mysqal datasource,
When you run the above code, the queries written in Current Behavior are sorted.

However, I think it should be done like the query in Expected Behavior

Expected Behavior

SELECT place_id, user_id as member_id, point 
FROM user_point 
WHERE (user_point.place_id = ?) 
AND ((place_id > ?) OR (place_id = ? AND user_id > ?)) # member_id -> user_id
ORDER BY place_id ASC, member_id ASC 
LIMIT ?

Current Behavior

SELECT place_id, user_id as member_id, point 
FROM user_point 
WHERE (user_point.place_id = ?) 
AND ((place_id > ?) OR (place_id = ? AND member_id > ?)) 
ORDER BY place_id ASC, member_id ASC 
LIMIT ?

Context

when using jdbcPagingItemReader and PagingQueryProvider, paging and sorting are done based on the sortKey in the where clause.
If an alias is used in the select clause and designated as the sortKey, the column name used in the where clause becomes the alias, leading to an exception since the database cannot find it.

This situation is awkward. Typically, the reason for using aliases in a select clause is for use in an order by clause. However, using aliases for paging causes problems. So I think it might be necessary to modify the paging logic to use actual column names rather than aliases.

@Gyuchool Gyuchool added status: waiting-for-triage Issues that we did not analyse yet type: feature labels Apr 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage Issues that we did not analyse yet type: feature
Projects
None yet
Development

No branches or pull requests

1 participant