-
Notifications
You must be signed in to change notification settings - Fork 359
fix(rtdb): Match backend key ordering in order_by_key() query results #984
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1100,6 +1100,17 @@ def test_invalid_sort(self, value): | |||||
| ({'k1' : 1, 'k2' : 2, 'k3' : 3}, ['k1', 'k2', 'k3']), | ||||||
| ({'k3' : 3, 'k2' : 2, 'k1' : 1}, ['k1', 'k2', 'k3']), | ||||||
| ({'k1' : 3, 'k3' : 1, 'k2' : 2}, ['k1', 'k2', 'k3']), | ||||||
| # Keys that parse as integers are ordered numerically, matching the backend | ||||||
| # (https://github.com/firebase/firebase-admin-python/issues/677). | ||||||
| ({'100001' : 1, '100002' : 2, '123' : 3, '100003' : 4}, | ||||||
| ['123', '100001', '100002', '100003']), | ||||||
| ({'2' : 1, '10' : 2, '1' : 3}, ['1', '2', '10']), | ||||||
| ({'-5' : 1, '3' : 2, '-10' : 3}, ['-10', '-5', '3']), | ||||||
| ({'01' : 1, '1' : 2, '2' : 3}, ['1', '01', '2']), | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since keys with leading zeros (like
Suggested change
|
||||||
| # Integer keys come before string keys; the rest are lexicographic. | ||||||
| ({'b' : 1, '10' : 2, 'a' : 3, '2' : 4}, ['2', '10', 'a', 'b']), | ||||||
| # Keys outside the signed 32-bit integer range are ordered lexicographically. | ||||||
| ({'2147483648' : 1, '9' : 2, '2147483647' : 3}, ['9', '2147483647', '2147483648']), | ||||||
| ]) | ||||||
| def test_order_by_key(self, result, expected): | ||||||
| ordered = db._Sorter(result, '$key').get() | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the Firebase Realtime Database, keys with leading zeros (except for
'0'itself) are not treated as 32-bit integers; they are treated as strings and sorted lexicographically after all integer keys.Currently,
_parse_int_keyparses'01'as the integer1because_INTEGER_KEY_PATTERNmatches it andint('01')succeeds. To ensure only canonical integer representations are treated as integers (matching the backend behavior), we should verify thatstr(value) == key.