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

Commit e628ca9

Browse files
committed
made query.pipeline private
1 parent 9a35dfe commit e628ca9

File tree

7 files changed

+37
-43
lines changed

7 files changed

+37
-43
lines changed

google/cloud/firestore_v1/base_aggregation.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
from google.cloud.firestore_v1.stream_generator import (
4949
StreamGenerator,
5050
)
51+
from google.cloud.firestore_v1.pipeline_source import PipelineSource
5152

5253
import datetime
5354

@@ -356,19 +357,20 @@ def stream(
356357
A generator of the query results.
357358
"""
358359

359-
def pipeline(self):
360+
def _build_pipeline(self, source: "PipelineSource"):
360361
"""
361362
Convert this query into a Pipeline
362363
363364
Queries containing a `cursor` or `limit_to_last` are not currently supported
364365
366+
Args:
367+
source: the PipelineSource to build the pipeline off of
365368
Raises:
366-
- ValueError: raised if Query wasn't created with an associated client
367369
- NotImplementedError: raised if the query contains a `cursor` or `limit_to_last`
368370
Returns:
369371
a Pipeline representing the query
370372
"""
371373
# use autoindexer to keep track of which field number to use for un-aliased fields
372374
autoindexer = itertools.count(start=1)
373375
exprs = [a._to_pipeline_expr(autoindexer) for a in self._aggregations]
374-
return self._nested_query.pipeline().aggregate(*exprs)
376+
return self._nested_query._build_pipeline(source).aggregate(*exprs)

google/cloud/firestore_v1/base_query.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
from google.cloud.firestore_v1.query_profile import ExplainOptions
6868
from google.cloud.firestore_v1.query_results import QueryResultsList
6969
from google.cloud.firestore_v1.stream_generator import StreamGenerator
70+
from google.cloud.firestore_v1.pipeline_source import PipelineSource
7071

7172
import datetime
7273

@@ -1129,24 +1130,23 @@ def recursive(self: QueryType) -> QueryType:
11291130

11301131
return copied
11311132

1132-
def pipeline(self):
1133+
def _build_pipeline(self, source: "PipelineSource"):
11331134
"""
11341135
Convert this query into a Pipeline
11351136
11361137
Queries containing a `cursor` or `limit_to_last` are not currently supported
11371138
1139+
Args:
1140+
source: the PipelineSource to build the pipeline off of
11381141
Raises:
1139-
- ValueError: raised if Query wasn't created with an associated client
11401142
- NotImplementedError: raised if the query contains a `cursor` or `limit_to_last`
11411143
Returns:
11421144
a Pipeline representing the query
11431145
"""
1144-
if not self._client:
1145-
raise ValueError("Query does not have an associated client")
11461146
if self._all_descendants:
1147-
ppl = self._client.pipeline().collection_group(self._parent.id)
1147+
ppl = source.collection_group(self._parent.id)
11481148
else:
1149-
ppl = self._client.pipeline().collection(self._parent._path)
1149+
ppl = source.collection(self._parent._path)
11501150

11511151
# Filters
11521152
for filter_ in self._field_filters:

tests/unit/v1/test_aggregation.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,7 @@ def test_aggreation_to_pipeline_sum(field, in_alias, out_alias):
10401040
query = make_query(parent)
10411041
aggregation_query = make_aggregation_query(query)
10421042
aggregation_query.sum(field, alias=in_alias)
1043-
pipeline = aggregation_query.pipeline()
1043+
pipeline = aggregation_query._build_pipeline(client.pipeline())
10441044
assert isinstance(pipeline, Pipeline)
10451045
assert len(pipeline.stages) == 2
10461046
assert isinstance(pipeline.stages[0], Collection)
@@ -1071,7 +1071,7 @@ def test_aggreation_to_pipeline_avg(field, in_alias, out_alias):
10711071
query = make_query(parent)
10721072
aggregation_query = make_aggregation_query(query)
10731073
aggregation_query.avg(field, alias=in_alias)
1074-
pipeline = aggregation_query.pipeline()
1074+
pipeline = aggregation_query._build_pipeline(client.pipeline())
10751075
assert isinstance(pipeline, Pipeline)
10761076
assert len(pipeline.stages) == 2
10771077
assert isinstance(pipeline.stages[0], Collection)
@@ -1102,7 +1102,7 @@ def test_aggreation_to_pipeline_count(in_alias, out_alias):
11021102
query = make_query(parent)
11031103
aggregation_query = make_aggregation_query(query)
11041104
aggregation_query.count(alias=in_alias)
1105-
pipeline = aggregation_query.pipeline()
1105+
pipeline = aggregation_query._build_pipeline(client.pipeline())
11061106
assert isinstance(pipeline, Pipeline)
11071107
assert len(pipeline.stages) == 2
11081108
assert isinstance(pipeline.stages[0], Collection)
@@ -1127,7 +1127,7 @@ def test_aggreation_to_pipeline_count_increment():
11271127
aggregation_query = make_aggregation_query(query)
11281128
for _ in range(n):
11291129
aggregation_query.count()
1130-
pipeline = aggregation_query.pipeline()
1130+
pipeline = aggregation_query._build_pipeline(client.pipeline())
11311131
aggregate_stage = pipeline.stages[1]
11321132
assert len(aggregate_stage.accumulators) == n
11331133
for i in range(n):
@@ -1146,7 +1146,7 @@ def test_aggreation_to_pipeline_complex():
11461146
aggregation_query.count()
11471147
aggregation_query.avg("other")
11481148
aggregation_query.sum("final")
1149-
pipeline = aggregation_query.pipeline()
1149+
pipeline = aggregation_query._build_pipeline(client.pipeline())
11501150
assert isinstance(pipeline, Pipeline)
11511151
assert len(pipeline.stages) == 3
11521152
assert isinstance(pipeline.stages[0], Collection)

tests/unit/v1/test_async_aggregation.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ def test_async_aggreation_to_pipeline_sum(field, in_alias, out_alias):
716716
query = make_async_query(parent)
717717
aggregation_query = make_async_aggregation_query(query)
718718
aggregation_query.sum(field, alias=in_alias)
719-
pipeline = aggregation_query.pipeline()
719+
pipeline = aggregation_query._build_pipeline(client.pipeline())
720720
assert isinstance(pipeline, AsyncPipeline)
721721
assert len(pipeline.stages) == 2
722722
assert isinstance(pipeline.stages[0], Collection)
@@ -747,7 +747,7 @@ def test_async_aggreation_to_pipeline_avg(field, in_alias, out_alias):
747747
query = make_async_query(parent)
748748
aggregation_query = make_async_aggregation_query(query)
749749
aggregation_query.avg(field, alias=in_alias)
750-
pipeline = aggregation_query.pipeline()
750+
pipeline = aggregation_query._build_pipeline(client.pipeline())
751751
assert isinstance(pipeline, AsyncPipeline)
752752
assert len(pipeline.stages) == 2
753753
assert isinstance(pipeline.stages[0], Collection)
@@ -778,7 +778,7 @@ def test_async_aggreation_to_pipeline_count(in_alias, out_alias):
778778
query = make_async_query(parent)
779779
aggregation_query = make_async_aggregation_query(query)
780780
aggregation_query.count(alias=in_alias)
781-
pipeline = aggregation_query.pipeline()
781+
pipeline = aggregation_query._build_pipeline(client.pipeline())
782782
assert isinstance(pipeline, AsyncPipeline)
783783
assert len(pipeline.stages) == 2
784784
assert isinstance(pipeline.stages[0], Collection)
@@ -803,7 +803,7 @@ def test_aggreation_to_pipeline_count_increment():
803803
aggregation_query = make_async_aggregation_query(query)
804804
for _ in range(n):
805805
aggregation_query.count()
806-
pipeline = aggregation_query.pipeline()
806+
pipeline = aggregation_query._build_pipeline(client.pipeline())
807807
aggregate_stage = pipeline.stages[1]
808808
assert len(aggregate_stage.accumulators) == n
809809
for i in range(n):
@@ -822,7 +822,7 @@ def test_async_aggreation_to_pipeline_complex():
822822
aggregation_query.count()
823823
aggregation_query.avg("other")
824824
aggregation_query.sum("final")
825-
pipeline = aggregation_query.pipeline()
825+
pipeline = aggregation_query._build_pipeline(client.pipeline())
826826
assert isinstance(pipeline, AsyncPipeline)
827827
assert len(pipeline.stages) == 3
828828
assert isinstance(pipeline.stages[0], Collection)

tests/unit/v1/test_async_query.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ def test_asyncquery_collection_pipeline_type():
917917
client = make_async_client()
918918
parent = client.collection("test")
919919
query = parent._query()
920-
ppl = query.pipeline()
920+
ppl = query._build_pipeline(client.pipeline())
921921
assert isinstance(ppl, AsyncPipeline)
922922

923923

@@ -926,5 +926,5 @@ def test_asyncquery_collectiongroup_pipeline_type():
926926

927927
client = make_async_client()
928928
query = client.collection_group("test")
929-
ppl = query.pipeline()
929+
ppl = query._build_pipeline(client.pipeline())
930930
assert isinstance(ppl, AsyncPipeline)

tests/unit/v1/test_base_query.py

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1994,18 +1994,10 @@ def test__collection_group_query_response_to_snapshot_response():
19941994
assert snapshot.update_time == response_pb._pb.document.update_time
19951995

19961996

1997-
def test__query_pipeline_no_client():
1998-
mock_parent = mock.Mock()
1999-
mock_parent._client = None
2000-
query = _make_base_query(mock_parent)
2001-
with pytest.raises(ValueError, match="client"):
2002-
query.pipeline()
2003-
2004-
20051997
def test__query_pipeline_decendants():
20061998
client = make_client()
20071999
query = client.collection_group("my_col")
2008-
pipeline = query.pipeline()
2000+
pipeline = query._build_pipeline(client.pipeline())
20092001

20102002
assert len(pipeline.stages) == 1
20112003
stage = pipeline.stages[0]
@@ -2025,7 +2017,7 @@ def test__query_pipeline_no_decendants(in_path, out_path):
20252017
client = make_client()
20262018
collection = client.collection(in_path)
20272019
query = collection._query()
2028-
pipeline = query.pipeline()
2020+
pipeline = query._build_pipeline(client.pipeline())
20292021

20302022
assert len(pipeline.stages) == 1
20312023
stage = pipeline.stages[0]
@@ -2043,7 +2035,7 @@ def test__query_pipeline_composite_filter():
20432035
with mock.patch.object(
20442036
expr.BooleanExpression, "_from_query_filter_pb"
20452037
) as convert_mock:
2046-
pipeline = query.pipeline()
2038+
pipeline = query._build_pipeline(client.pipeline())
20472039
convert_mock.assert_called_once_with(in_filter._to_pb(), client)
20482040
assert len(pipeline.stages) == 2
20492041
stage = pipeline.stages[1]
@@ -2054,7 +2046,7 @@ def test__query_pipeline_composite_filter():
20542046
def test__query_pipeline_projections():
20552047
client = make_client()
20562048
query = client.collection("my_col").select(["field_a", "field_b.c"])
2057-
pipeline = query.pipeline()
2049+
pipeline = query._build_pipeline(client.pipeline())
20582050

20592051
assert len(pipeline.stages) == 2
20602052
stage = pipeline.stages[1]
@@ -2069,7 +2061,7 @@ def test__query_pipeline_order_exists_multiple():
20692061

20702062
client = make_client()
20712063
query = client.collection("my_col").order_by("field_a").order_by("field_b")
2072-
pipeline = query.pipeline()
2064+
pipeline = query._build_pipeline(client.pipeline())
20732065

20742066
# should have collection, where, and sort
20752067
# we're interested in where
@@ -2089,7 +2081,7 @@ def test__query_pipeline_order_exists_multiple():
20892081
def test__query_pipeline_order_exists_single():
20902082
client = make_client()
20912083
query_single = client.collection("my_col").order_by("field_c")
2092-
pipeline_single = query_single.pipeline()
2084+
pipeline_single = query_single._build_pipeline(client.pipeline())
20932085

20942086
# should have collection, where, and sort
20952087
# we're interested in where
@@ -2110,7 +2102,7 @@ def test__query_pipeline_order_sorts():
21102102
.order_by("field_a", direction=BaseQuery.ASCENDING)
21112103
.order_by("field_b", direction=BaseQuery.DESCENDING)
21122104
)
2113-
pipeline = query.pipeline()
2105+
pipeline = query._build_pipeline(client.pipeline())
21142106

21152107
assert len(pipeline.stages) == 3
21162108
sort_stage = pipeline.stages[2]
@@ -2128,21 +2120,21 @@ def test__query_pipeline_unsupported():
21282120
client = make_client()
21292121
query_start = client.collection("my_col").start_at({"field_a": "value"})
21302122
with pytest.raises(NotImplementedError, match="cursors"):
2131-
query_start.pipeline()
2123+
query_start._build_pipeline(client.pipeline())
21322124

21332125
query_end = client.collection("my_col").end_at({"field_a": "value"})
21342126
with pytest.raises(NotImplementedError, match="cursors"):
2135-
query_end.pipeline()
2127+
query_end._build_pipeline(client.pipeline())
21362128

21372129
query_limit_last = client.collection("my_col").limit_to_last(10)
21382130
with pytest.raises(NotImplementedError, match="limit_to_last"):
2139-
query_limit_last.pipeline()
2131+
query_limit_last._build_pipeline(client.pipeline())
21402132

21412133

21422134
def test__query_pipeline_limit():
21432135
client = make_client()
21442136
query = client.collection("my_col").limit(15)
2145-
pipeline = query.pipeline()
2137+
pipeline = query._build_pipeline(client.pipeline())
21462138

21472139
assert len(pipeline.stages) == 2
21482140
stage = pipeline.stages[1]
@@ -2153,7 +2145,7 @@ def test__query_pipeline_limit():
21532145
def test__query_pipeline_offset():
21542146
client = make_client()
21552147
query = client.collection("my_col").offset(5)
2156-
pipeline = query.pipeline()
2148+
pipeline = query._build_pipeline(client.pipeline())
21572149

21582150
assert len(pipeline.stages) == 2
21592151
stage = pipeline.stages[1]

tests/unit/v1/test_query.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,7 @@ def test_asyncquery_collection_pipeline_type():
10541054
client = make_client()
10551055
parent = client.collection("test")
10561056
query = parent._query()
1057-
ppl = query.pipeline()
1057+
ppl = query._build_pipeline(client.pipeline())
10581058
assert isinstance(ppl, Pipeline)
10591059

10601060

@@ -1063,5 +1063,5 @@ def test_asyncquery_collectiongroup_pipeline_type():
10631063

10641064
client = make_client()
10651065
query = client.collection_group("test")
1066-
ppl = query.pipeline()
1066+
ppl = query._build_pipeline(client.pipeline())
10671067
assert isinstance(ppl, Pipeline)

0 commit comments

Comments
 (0)