Skip to content
This repository was archived by the owner on Aug 30, 2026. It is now read-only.

fix: order normalization with descending query - #788

Merged
daniel-sanche merged 14 commits into
mainfrom
b_306472103
Nov 6, 2023
Merged

daniel-sanche merged 14 commits into
mainfrom
b_306472103

Conversation

@daniel-sanche

Copy link
Copy Markdown
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 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
Comment thread tests/unit/v1/test_base_query.py Outdated

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

Copy link
Copy Markdown
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?

Copy link
Copy Markdown
Contributor

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.

Comment thread google/cloud/firestore_v1/base_query.py Outdated
orders.append(self._make_order("__name__", direction))
# skip equality filters and filters on fields already ordered
if (
filter_.op not in _EQUALITY_OPERATORS

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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

Comment thread tests/unit/v1/test_base_query.py Outdated
_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]))

Copy link
Copy Markdown
Contributor

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

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.

3 participants