@@ -72,26 +72,27 @@ def __init__(self, dataframe: bigframes.dataframe.DataFrame):
7272 self ._table_id = str (uuid .uuid4 ())
7373 self ._all_data_loaded = False
7474 self ._batch_iter : Optional [Iterator [pd .DataFrame ]] = None
75- self ._batches : Optional [bigframes .core .blocks .PandasBatches ] = None
7675 self ._cached_batches : List [pd .DataFrame ] = []
7776
7877 # Respect display options for initial page size
7978 initial_page_size = bigframes .options .display .max_rows
8079
81- # Fetches initial data batches and row count for display.
8280 batches = dataframe .to_pandas_batches (
8381 page_size = initial_page_size ,
8482 )
85- self ._batches = cast (bigframes .core .blocks .PandasBatches , batches )
83+ self ._batches : bigframes .core .blocks .PandasBatches = cast (
84+ bigframes .core .blocks .PandasBatches , batches
85+ )
8686
87- # Use total_rwos from batches directly
87+ # The query issued by `to_pandas_batches()` already contains metadata
88+ # about how many results there were. Use that to avoid doing an extra
89+ # COUNT(*) query that `len(...)` would do.
8890 self .row_count = self ._batches .total_rows or 0
8991
9092 # Set page_size after _batches is available since traitlets observers
9193 # may depend on _batches being initialized when the change trigger happens
9294 self .page_size = initial_page_size
9395
94- # Generates the initial HTML table content
9596 self ._set_table_html ()
9697
9798 @functools .cached_property
@@ -182,11 +183,7 @@ def _get_next_batch(self) -> bool:
182183 @property
183184 def _batch_iterator (self ) -> Iterator [pd .DataFrame ]:
184185 """Lazily initializes and returns the batch iterator."""
185- if self ._batch_iter is None :
186- if self ._batches is None :
187- self ._batch_iter = iter ([])
188- else :
189- self ._batch_iter = iter (self ._batches )
186+ self ._batch_iter = iter (self ._batches )
190187 return self ._batch_iter
191188
192189 @property
0 commit comments