@@ -92,44 +92,42 @@ absl::optional<Operator> Query::FindOpInsideFilters(
9292 return absl::nullopt ;
9393}
9494
95- const std::vector<OrderBy>& Query::normalized_order_bys () const {
96- return memoized_normalized_order_bys_.memoize ([&]() {
97- // Any explicit order by fields should be added as is.
98- auto result = absl::make_unique<std::vector<OrderBy>>(explicit_order_bys_);
99- std::set<FieldPath> fieldsNormalized;
100- for (const OrderBy& order_by : explicit_order_bys_) {
101- fieldsNormalized.insert (order_by.field ());
102- }
95+ std::shared_ptr<std::vector<OrderBy>> Query::calculate_normalized_order_bys () const {
96+ auto result = std::make_shared<std::vector<OrderBy>>(explicit_order_bys_);
10397
104- // The order of the implicit ordering always matches the last explicit order
105- // by.
106- Direction last_direction = explicit_order_bys_.empty ()
107- ? Direction::Ascending
108- : explicit_order_bys_.back ().direction ();
109-
110- // Any inequality fields not explicitly ordered should be implicitly ordered
111- // in a lexicographical order. When there are multiple inequality filters on
112- // the same field, the field should be added only once. Note:
113- // `std::set<model::FieldPath>` sorts the key field before other fields.
114- // However, we want the key field to be sorted last.
115- const std::set<model::FieldPath> inequality_fields =
116- InequalityFilterFields ();
117-
118- for (const model::FieldPath& field : inequality_fields) {
119- if (fieldsNormalized.find (field) == fieldsNormalized.end () &&
120- !field.IsKeyFieldPath ()) {
121- result->push_back (OrderBy (field, last_direction));
122- }
123- }
98+ std::set<FieldPath> fieldsNormalized;
99+ for (const OrderBy& order_by : explicit_order_bys_) {
100+ fieldsNormalized.insert (order_by.field ());
101+ }
124102
125- // Add the document key field to the last if it is not explicitly ordered.
126- if (fieldsNormalized.find (FieldPath::KeyFieldPath ()) ==
127- fieldsNormalized.end ()) {
128- result->push_back (OrderBy (FieldPath::KeyFieldPath (), last_direction));
129- }
103+ // The order of the implicit ordering always matches the last explicit order
104+ // by.
105+ Direction last_direction = explicit_order_bys_.empty ()
106+ ? Direction::Ascending
107+ : explicit_order_bys_.back ().direction ();
108+
109+ // Any inequality fields not explicitly ordered should be implicitly ordered
110+ // in a lexicographical order. When there are multiple inequality filters on
111+ // the same field, the field should be added only once. Note:
112+ // `std::set<model::FieldPath>` sorts the key field before other fields.
113+ // However, we want the key field to be sorted last.
114+ const std::set<model::FieldPath> inequality_fields =
115+ InequalityFilterFields ();
116+
117+ for (const model::FieldPath& field : inequality_fields) {
118+ if (fieldsNormalized.find (field) == fieldsNormalized.end () &&
119+ !field.IsKeyFieldPath ()) {
120+ result->push_back (OrderBy (field, last_direction));
121+ }
122+ }
130123
131- return result;
132- });
124+ // Add the document key field to the last if it is not explicitly ordered.
125+ if (fieldsNormalized.find (FieldPath::KeyFieldPath ()) ==
126+ fieldsNormalized.end ()) {
127+ result->push_back (OrderBy (FieldPath::KeyFieldPath (), last_direction));
128+ }
129+
130+ return result;
133131}
134132
135133LimitType Query::limit_type () const {
@@ -297,16 +295,12 @@ std::string Query::ToString() const {
297295 return absl::StrCat (" Query(canonical_id=" , CanonicalId (), " )" );
298296}
299297
300- const Target& Query::ToTarget () const & {
301- return memoized_target_.memoize ([&]() {
302- return absl::make_unique<Target>(ToTarget (normalized_order_bys ()));
303- });
298+ std::shared_ptr<Target> Query::calculate_target () const {
299+ return std::make_shared<Target>(ToTarget (normalized_order_bys ()));
304300}
305301
306- const Target& Query::ToAggregateTarget () const & {
307- return memoized_aggregate_target_.memoize ([&]() {
308- return absl::make_unique<Target>(ToTarget (explicit_order_bys_));
309- });
302+ std::shared_ptr<Target> Query::calculate_aggregate_target () const {
303+ return std::make_shared<Target>(ToTarget (explicit_order_bys_));
310304}
311305
312306Target Query::ToTarget (const std::vector<OrderBy>& order_bys) const {
0 commit comments