fix Spanner custom query ordering with interleaved fields - #4585
Open
arnabnandy7 wants to merge 1 commit into
Open
fix Spanner custom query ordering with interleaved fields#4585arnabnandy7 wants to merge 1 commit into
arnabnandy7 wants to merge 1 commit into
Conversation
Signed-off-by: Arnab Nandy <arnab_nandy7@yahoo.com>
arnabnandy7
force-pushed
the
fix/spanner-interleaved-sort
branch
from
July 18, 2026 10:51
4987b21 to
1c779df
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Custom Spanner repository queries returning entities with eager
@Interleavedproperties 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 BYclause in the custom query does not reliably determine the order of the final results. This also affects queries usingORDER BYwithLIMITorOFFSET.Fixes #4244.
Solution
Do not wrap a custom SQL query solely to fetch eager interleaved properties when no external
SortorPageableoptions require an outer query.The original query is executed unchanged, preserving its
ORDER BY,LIMIT, andOFFSETsemantics. Missing eager interleaved properties are then populated through the existingSpannerTemplatechild-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
SqlSpannerQueryTestswith a regression case verifying that a custom query containingORDER BY ... LIMITis not wrapped for an entity with eager interleaved properties.Executed:
Results: