-
Notifications
You must be signed in to change notification settings - Fork 910
InstantAsStringAttributeConverter does not appear to have lexicographically correct sorting as claimed #2219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@andrewparmet I'm trying to understand what's the issue exactly. In the current behavior the results are listed in lexicographically order "since Would this be a feature request for the result be sorted like in the expected behavior? |
@debora-ito For ISO 8601-formatted instants, which is what the converter specifies as its destination format, you can get lexicographical ordering that matches natural ordering. One requirement to get that guarantee is that the precision after the decimal is consistent among elements. In particular the sentence "This serialization is lexicographically orderable when the year is not negative," by indicating the other necessary condition on the elements - namely, that the year is not negative - implies that it's meant to be used in this way. (Actually, you can still get natural lexicographical ordering if all years are negative and you reverse the sort.) This guarantee makes it useful for mapping Instants used as sort keys, since a time-based query will translate into a sort key query on a string attribute. All UTF-8 text is trivially lexicographically orderable. I think if the current behavior is desired there should be a disclaimer saying that Instants mapped this way should not be used as sort keys with the intent to be used with time-based queries. |
+1 - this also has implications for people who wish to use Instant values in ConditionExpressions. It is easy to fall into the trap of assuming that the Instant value will be strictly increasing lexicographically, but it leaves corner cases where, e.g. As a developer my general assumption is that all "normal" instants (those between As for whether this is a bug or a feature request - it feels like it is in the grey area between the two. At very least a warning should be added to the documentation. |
…cb9324ba0 Pull request: release <- staging/e85a5651-0af8-4dce-8dc6-425cb9324ba0
Describe the bug
InstantAsStringAttributeConverter claims that its provided serialization is lexicographically sortable given that the year of the Instant is non-negative. It seems to also not be lexicographically sortable if any value falls directly on a second, since InstantAsStringAttributeConverter simply calls
toString()
, which uses an adapting ISO 8601 formatter that will truncate 3-groups of zero values:From Instant's toString():
From DateTimeFormatter's ISO_INSTANT:
The manifestation of this problem was a query on a sort key that, when re-sorted in application code, changed its order.
Expected Behavior
When using an Instant as a sort key on a DynamoDbBean, I expect sort orderings like this to be returned by a query:
Current Behavior
Instead I'm getting orderings like this:
since
Z
comes after.
lexicographically. This disagrees with the natural sort order of the corresponding Instants.Steps to Reproduce
Create a Dynamo schema with a string sort key, write two instants that share a second, but ensure one of them falls directly on the second while the other has some fractional component.
Possible Solution
Use
new DateTimeFormatterBuilder().appendInstant(9).toFormatter()
to control the number of zeros. For any values taken fromSystem.currentTimeMillis()
there will be a lot of unnecessary zeros, but I don't think there's any other correct default behavior.Context
It's nice to combine the enhanced client's ability to use native Java types with querying by time. In this case the workaround is pretty straightforward for my case.
Your Environment
The text was updated successfully, but these errors were encountered: