3939 Optional ,
4040 Sequence ,
4141 Tuple ,
42- TYPE_CHECKING ,
4342 Union ,
4443)
4544import warnings
6867import bigframes .core .window_spec as windows
6968import bigframes .dtypes
7069import bigframes .exceptions as bfe
71- import bigframes .features
7270import bigframes .operations as ops
7371import bigframes .operations .aggregations as agg_ops
74- import bigframes .session ._io .pandas as io_pandas
75-
76- if TYPE_CHECKING :
77- import bigframes .session .executor
7872
7973# Type constraint for wherever column labels are used
8074Label = typing .Hashable
@@ -221,7 +215,7 @@ def shape(self) -> typing.Tuple[int, int]:
221215 except Exception :
222216 pass
223217
224- row_count = self .session ._executor .get_row_count (self .expr )
218+ row_count = self .session ._executor .execute (self .expr . row_count ()). to_py_scalar ( )
225219 return (row_count , len (self .value_columns ))
226220
227221 @property
@@ -485,7 +479,7 @@ def to_arrow(
485479 * ,
486480 ordered : bool = True ,
487481 allow_large_results : Optional [bool ] = None ,
488- ) -> Tuple [pa .Table , bigquery .QueryJob ]:
482+ ) -> Tuple [pa .Table , Optional [ bigquery .QueryJob ] ]:
489483 """Run query and download results as a pyarrow Table."""
490484 execute_result = self .session ._executor .execute (
491485 self .expr , ordered = ordered , use_explicit_destination = allow_large_results
@@ -580,7 +574,7 @@ def try_peek(
580574 result = self .session ._executor .peek (
581575 self .expr , n , use_explicit_destination = allow_large_results
582576 )
583- df = io_pandas . arrow_to_pandas ( result .to_arrow_table (), self . expr . schema )
577+ df = result .to_pandas ( )
584578 self ._copy_index_to_pandas (df )
585579 return df
586580 else :
@@ -604,8 +598,7 @@ def to_pandas_batches(
604598 page_size = page_size ,
605599 max_results = max_results ,
606600 )
607- for record_batch in execute_result .arrow_batches ():
608- df = io_pandas .arrow_to_pandas (record_batch , self .expr .schema )
601+ for df in execute_result .to_pandas_batches ():
609602 self ._copy_index_to_pandas (df )
610603 if squeeze :
611604 yield df .squeeze (axis = 1 )
@@ -659,7 +652,7 @@ def _materialize_local(
659652
660653 # TODO: Maybe materialize before downsampling
661654 # Some downsampling methods
662- if fraction < 1 :
655+ if fraction < 1 and ( execute_result . total_rows is not None ) :
663656 if not sample_config .enable_downsampling :
664657 raise RuntimeError (
665658 f"The data size ({ table_mb :.2f} MB) exceeds the maximum download limit of "
@@ -690,9 +683,7 @@ def _materialize_local(
690683 MaterializationOptions (ordered = materialize_options .ordered )
691684 )
692685 else :
693- total_rows = execute_result .total_rows
694- arrow = execute_result .to_arrow_table ()
695- df = io_pandas .arrow_to_pandas (arrow , schema = self .expr .schema )
686+ df = execute_result .to_pandas ()
696687 self ._copy_index_to_pandas (df )
697688
698689 return df , execute_result .query_job
@@ -1570,12 +1561,11 @@ def retrieve_repr_request_results(
15701561
15711562 # head caches full underlying expression, so row_count will be free after
15721563 head_result = self .session ._executor .head (self .expr , max_results )
1573- count = self .session ._executor .get_row_count (self .expr )
1564+ row_count = self .session ._executor .execute (self .expr . row_count ()). to_py_scalar ( )
15741565
1575- arrow = head_result .to_arrow_table ()
1576- df = io_pandas .arrow_to_pandas (arrow , schema = self .expr .schema )
1566+ df = head_result .to_pandas ()
15771567 self ._copy_index_to_pandas (df )
1578- return df , count , head_result .query_job
1568+ return df , row_count , head_result .query_job
15791569
15801570 def promote_offsets (self , label : Label = None ) -> typing .Tuple [Block , str ]:
15811571 expr , result_id = self ._expr .promote_offsets ()
0 commit comments