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