20
20
from dataclasses import dataclass
21
21
from google .cloud .bigtable .data .row_filters import RowFilter
22
22
23
+ from google .cloud .bigtable_v2 .types import RowRange as RowRangePB
24
+ from google .cloud .bigtable_v2 .types import RowSet as RowSetPB
25
+ from google .cloud .bigtable_v2 .types import ReadRowsRequest as ReadRowsRequestPB
26
+
23
27
if TYPE_CHECKING :
24
28
from google .cloud .bigtable .data import RowKeySamples
25
29
from google .cloud .bigtable .data import ShardedQuery
@@ -279,16 +283,9 @@ def filter(self, row_filter: RowFilter | None):
279
283
280
284
Args:
281
285
- row_filter: a RowFilter to apply to this query
282
- Can be a RowFilter object or a dict representation
283
286
Returns:
284
287
- a reference to this query for chaining
285
288
"""
286
- if not (
287
- isinstance (row_filter , dict )
288
- or isinstance (row_filter , RowFilter )
289
- or row_filter is None
290
- ):
291
- raise ValueError ("row_filter must be a RowFilter or dict" )
292
289
self ._filter = row_filter
293
290
294
291
def add_key (self , row_key : str | bytes ):
@@ -312,20 +309,14 @@ def add_key(self, row_key: str | bytes):
312
309
313
310
def add_range (
314
311
self ,
315
- row_range : RowRange | dict [ str , bytes ] ,
312
+ row_range : RowRange ,
316
313
):
317
314
"""
318
315
Add a range of row keys to this query.
319
316
320
317
Args:
321
318
- row_range: a range of row keys to add to this query
322
- Can be a RowRange object or a dict representation in
323
- RowRange proto format
324
319
"""
325
- if not (isinstance (row_range , dict ) or isinstance (row_range , RowRange )):
326
- raise ValueError ("row_range must be a RowRange or dict" )
327
- if isinstance (row_range , dict ):
328
- row_range = RowRange ._from_dict (row_range )
329
320
self .row_ranges .add (row_range )
330
321
331
322
def shard (self , shard_keys : RowKeySamples ) -> ShardedQuery :
@@ -478,6 +469,22 @@ def _to_dict(self) -> dict[str, Any]:
478
469
final_dict ["rows_limit" ] = self .limit
479
470
return final_dict
480
471
472
+ def _to_pb (self , table ) -> ReadRowsRequestPB :
473
+ """
474
+ Convert this query into a dictionary that can be used to construct a
475
+ ReadRowsRequest protobuf
476
+ """
477
+ return ReadRowsRequestPB (
478
+ table_name = table .table_name ,
479
+ app_profile_id = table .app_profile_id ,
480
+ rows = RowSetPB (
481
+ row_keys = list (self .row_keys ),
482
+ row_ranges = [RowRangePB (* r ._to_dict ()) for r in self .row_ranges ],
483
+ ),
484
+ _filter = self ._filter ._to_pb () if self ._filter else None ,
485
+ rows_limit = self .limit ,
486
+ )
487
+
481
488
def __eq__ (self , other ):
482
489
"""
483
490
RowRanges are equal if they have the same row keys, row ranges,
0 commit comments