Skip to content

fix Spanner custom query ordering with interleaved fields - #4585

Open
arnabnandy7 wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
arnabnandy7:fix/spanner-interleaved-sort
Open

fix Spanner custom query ordering with interleaved fields#4585
arnabnandy7 wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
arnabnandy7:fix/spanner-interleaved-sort

Conversation

@arnabnandy7

Copy link
Copy Markdown

Problem

Custom Spanner repository queries returning entities with eager @Interleaved properties are wrapped in an outer query to fetch the interleaved children.

Cloud Spanner does not preserve the ordering of a derived table. Consequently, an ORDER BY clause in the custom query does not reliably determine the order of the final results. This also affects queries using ORDER BY with LIMIT or OFFSET.

Fixes #4244.

Solution

Do not wrap a custom SQL query solely to fetch eager interleaved properties when no external Sort or Pageable options require an outer query.

The original query is executed unchanged, preserving its ORDER BY, LIMIT, and OFFSET semantics. Missing eager interleaved properties are then populated through the existing SpannerTemplate child-resolution flow.

This may result in additional queries for interleaved properties, but avoids returning incorrectly ordered results.

The documentation has been updated to explain this behavior and its performance trade-off.

Testing

Updated SqlSpannerQueryTests with a regression case verifying that a custom query containing ORDER BY ... LIMIT is not wrapped for an entity with eager interleaved properties.

Executed:

./mvnw -pl spring-cloud-gcp-data-spanner -DskipITs \
  -Dtest=SqlSpannerQueryTests,SpannerStatementQueryTests test

Results:

  • Tests run: 28
  • Failures: 0
  • Errors: 0
  • Skipped: 0

@arnabnandy7
arnabnandy7 requested a review from a team as a code owner July 18, 2026 10:50
Signed-off-by: Arnab Nandy <arnab_nandy7@yahoo.com>
@arnabnandy7
arnabnandy7 force-pushed the fix/spanner-interleaved-sort branch from 4987b21 to 1c779df Compare July 18, 2026 10:51
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.

SpannerRepository methods having @Query annotations with ORDER BY clauses don't sort properly for entities with @Interleaved fields

1 participant