Skip to content
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

fix: order normalization with descending query #788

Merged
merged 14 commits into from
Nov 6, 2023
Merged

Conversation

daniel-sanche
Copy link
Contributor

This PR fixes a bug in order normalization logic, to be more in line with nodejs:

  • all equality operators should be skipped when normalizing (previously, "in" and "array_contains_any" would still be added)
  • always use the last direction when normalizing a query, instead of adding "ASCENDING" sections

@daniel-sanche daniel-sanche requested review from a team as code owners November 2, 2023 23:34
@product-auto-label product-auto-label bot added size: m Pull request size is medium. api: firestore Issues related to the googleapis/python-firestore API. labels Nov 2, 2023

normalized_w_snapshot = query_w_snapshot._normalize_orders()
expected_w_snapshot = expected + [
query._make_order("b", "DESCENDING"),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let me know if this test case looks right.

Previously we were always using ASCENDING for all added orders. My understanding is we should always use the previous direction?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: IMO it would be more readable just to spell out the whole expected list here instead of appending the previous expected.

orders.append(self._make_order("__name__", direction))
# skip equality filters and filters on fields already ordered
if (
filter_.op not in _EQUALITY_OPERATORS

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, it would be better to create a list of INEQUALITY filters such that a new filter is default opted out rather than default opted in. This matches how other SDKs handle this more closely. I also think it's more likely someone will notice that an inequality is not having the order-by normalized rather than an equality filter is accidentally sneaking into the order-by normalization (Since it's an equality, the ordering is not easily noticed in most situations.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense, fixed

_make_base_query(collection)
.where(filter=FieldFilter("a", "==", 1))
.where(filter=FieldFilter("b", "in", [1, 2, 3]))
.where(filter=FieldFilter("b", "not-in", [4, 5, 6]))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: We have two 'b' values here, an equality and inequality on the same field, it would probably make more sense to have this be a separate property value.

@daniel-sanche daniel-sanche merged commit dbe8ef7 into main Nov 6, 2023
19 checks passed
@daniel-sanche daniel-sanche deleted the b_306472103 branch November 6, 2023 22:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: firestore Issues related to the googleapis/python-firestore API. size: m Pull request size is medium.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants