Skip to content

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

@Gyuchool

Description

@Gyuchool
@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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions