18
18
from google .cloud import bigquery_storage_v1
19
19
import pyarrow as pa
20
20
21
- from bigframes .core import bigframe_node , rewrite
21
+ from bigframes .core import bigframe_node , nodes , pyarrow_utils , rewrite
22
22
from bigframes .session import executor , semi_executor
23
23
24
24
@@ -39,14 +39,11 @@ def execute(
39
39
ordered : bool ,
40
40
peek : Optional [int ] = None ,
41
41
) -> Optional [executor .ExecuteResult ]:
42
- node = rewrite . try_reduce_to_table_scan (plan )
42
+ node = self . _try_adapt_plan (plan , ordered )
43
43
if not node :
44
44
return None
45
45
if node .explicitly_ordered and ordered :
46
46
return None
47
- if peek :
48
- # TODO: Support peeking
49
- return None
50
47
51
48
import google .cloud .bigquery_storage_v1 .types as bq_storage_types
52
49
from google .protobuf import timestamp_pb2
@@ -92,16 +89,39 @@ def execute(
92
89
93
90
def process_page (page ):
94
91
pa_batch = page .to_arrow ()
92
+ pa_batch = pa_batch .select (
93
+ [item .source_id for item in node .scan_list .items ]
94
+ )
95
95
return pa .RecordBatch .from_arrays (
96
96
pa_batch .columns , names = [id .sql for id in node .ids ]
97
97
)
98
98
99
99
batches = map (process_page , rowstream .pages )
100
100
101
+ if peek :
102
+ batches = pyarrow_utils .truncate_pyarrow_iterable (batches , max_results = peek )
103
+
104
+ rows = node .source .n_rows
105
+ if peek and rows :
106
+ rows = min (peek , rows )
107
+
101
108
return executor .ExecuteResult (
102
109
arrow_batches = batches ,
103
110
schema = plan .schema ,
104
111
query_job = None ,
105
112
total_bytes = None ,
106
- total_rows = node . source . n_rows ,
113
+ total_rows = rows ,
107
114
)
115
+
116
+ def _try_adapt_plan (
117
+ self ,
118
+ plan : bigframe_node .BigFrameNode ,
119
+ ordered : bool ,
120
+ ) -> Optional [nodes .ReadTableNode ]:
121
+ """
122
+ Tries to simplify the plan to an equivalent single ReadTableNode. Otherwise, returns None.
123
+ """
124
+ if not ordered :
125
+ # gets rid of order_by ops
126
+ plan = rewrite .bake_order (plan )
127
+ return rewrite .try_reduce_to_table_scan (plan )
0 commit comments