Skip to content

Fix quoted sort key lookup in JdbcPagingItemReader - #5688

Open
BDARJH wants to merge 1 commit into
spring-projects:mainfrom
BDARJH:fix/gh-4641
Open

BDARJH wants to merge 1 commit into
spring-projects:mainfrom
BDARJH:fix/gh-4641

Conversation

@BDARJH

@BDARJH BDARJH commented Nov 27, 2026

Copy link
Copy Markdown

Fixes #4641

Summary

  • Resolve the sort key to a result set column label in JdbcPagingItemReader.PagingRowMapper (getSortKeyLabel), dropping ANSI double-quote delimiters while preserving the case of the identifier and spaces inside it. startAfterValues stays keyed by the original sort key for restart compatibility.
  • Fall back from rs.findColumn(key) to a label lookup over ResultSetMetaData, rethrowing the original SQLException when the label is missing or duplicated. A single lookup decides the match: a quoted key matches the exact label only, because a delimited identifier is case sensitive, while an unquoted qualified key matches a label ignoring case and only when that match is unique. A plain unquoted key with no qualifier keeps the main behavior of throwing as soon as findColumn fails, without a metadata scan.
  • Because the lookup is a single pass, a result set holding both id and "id" rejects an unquoted p.id sort key as ambiguous instead of silently binding it to the quoted column and dropping rows 3~5.
  • Use Spring's braced named-parameter syntax (:{...}) in AbstractSqlPagingQueryProvider.getSortKeyPlaceHolder when a sort key contains ", preventing NamedParameterUtils from splitting at the quote character on page 2+.

Tests

  • Extended JdbcPagingItemReaderSortKeyTests (H2) with the quoted sort key cases: reading with positional and named parameters, restarting with a qualified quoted key (p."A"), composite quoted keys with and without named parameters, a delimited identifier containing spaces (" A "), and a rejection of select p.id, p."id" with an unquoted p.id sort key.
  • Added JdbcPagingItemReaderSortKeyLabelTests unit tests for the label resolution, including outer whitespace, t."A" and a plain key.
  • Added JdbcPagingItemReaderPostgresQuotedSortKeyIntegrationTests (Testcontainers postgres:17.5) for a read with named parameters and a restart.
  • Added AbstractSqlPagingQueryProviderSortKeyPlaceHolderTests, which covers the sort key placeholder once with a mock DataSource, so the dialect subclasses no longer start an embedded H2 database for it.

Scope

  • ANSI double-quoted identifiers (") only. Identifiers delimited with backticks (MySQL) or brackets (SQL Server) are left out of scope and can be addressed in a follow-up, because those databases may still compare quoted identifiers case-insensitively, which this lookup assumes they do not.
  • In named-parameter mode the :{...} placeholder cannot contain }, { or :, so sort keys with such characters are not supported there; they keep working with positional (?) parameters.

Backport

  • This patch targets main. findSortKeyColumn was introduced on main, so a 6.0.x backport needs a separate patch.

Checklist

  • Rebased changes on the latest main branch and squashed commits
  • Added/Updated unit tests as needed
  • Ran build and verified all tests pass prior to submission
  • Signed-off commits according to the Developer Certificate of Origin (git commit -s)

The sort key is a SQL column reference, not a result set column label:
for a delimited identifier such as "A" or t."A", ResultSet.findColumn
never matches because the quote characters are part of the lookup
string. PostgreSQL does not lower case quoted identifiers, so lower
casing the sort key would neither fix the lookup nor be safe for drivers
that compare identifiers case sensitively.

Resolve the configured sort key to a column label instead (unquoting the
last identifier and preserving dots and spaces inside quotes) while
keeping the configured key for restart checkpoints. A quoted key matches
the exact label only, because a delimited identifier is case sensitive.
An unquoted key with a qualifier matches a label ignoring case, and only
when that match is unique. A plain unquoted key keeps the behavior of
failing as soon as findColumn does, without a metadata scan. The lookup
is a single pass, so a result set holding both id and "id" rejects the
unquoted p.id key instead of silently binding it to the quoted column
and dropping the rows read so far. Also fix the named parameter
placeholder for such keys, which previously produced :_"A" and failed to
parse.

Scope is limited to identifiers delimited with ANSI double quotes.
Backtick and bracket quoting are out of scope, because those databases
may still compare quoted identifiers case insensitively.

Resolves spring-projects#4641

Signed-off-by: Jinhui Kim <46351067+BDARJH@users.noreply.github.com>

This branch has not been deployed

No deployments
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.

Issue with Sort Keys and Case Sensitivity in Spring Batch with PostgreSQL

1 participant