I'm using Spring-batch version 3.0.6.RELEASE to query a MySQL DB for some data and then process it.
Details about usage:
- query provider is
MySqlPagingQueryProvider
- when setting up the query provider I'm specifying an alias of one of the columns in the query as the
sort key
(please check the query below for more details)
Query:
SELECT
target.tx_timestamp AS event_datetime,
....
FROM
some_table AS target
....
WHERE
target.tx_timestamp > :startTime AND target.tx_timestamp <= :endTime;
The code:
Map<String, Order> sortKeys = new HashMap<>();
sortKeys.put("event_datetime", Order.ASCENDING);
MySqlPagingQueryProvider queryProvider = new MySqlPagingQueryProvider();
queryProvider.setSortKeys(sortKeys);
The generated query (please note the part with event_datetime):
WHERE
(target.tx_timestamp > ? AND target.tx_timestamp <= ?) AND
((event_datetime > ?))
The error:
org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar .... Unknown column 'event_datetime' in 'where clause'.
Is it possible to use alias as the sort key ?
Also worth noting are this thread with same issue and this Spring batch Jira ticket which is marked as resolved for version 3.0.4.
I have also tried using 3.0.4.RELEASE, 3.0.5.RELEASE and 3.0.7.RELEASE versions with the same result.
Edit: After a few more tries I can add that the issue arises only when there is enough records for more than one page. The query for the first page passes without issues but the queries for subsequent pages fail.
Edit2: As pointed out by Sabir, the issue is in fact with column alias in the where clause. Spring batch is working correctly in this case.
For people running into the same issue: You can work around this by nesting the whole query in another select like this:
SELECT * FROM (
SELECT
target.tx_timestamp AS event_datetime,
....
FROM
some_table AS target
....
WHERE
target.tx_timestamp > :startTime AND target.tx_timestamp <= :endTime) our_inner_select;
Unknown to me at the moment when I posted the question, the alias cannot be used in WHERE clause and since spring batch was given the alias event_datetime
as sort key, the queries for chunks after the 1st one were generated with additional conditions based on sort keys.
With the query above, the inner select is evaluated before the spring batch added WHERE condition and then the usage of column alias for sort key is possible.