1515
1616from typing import Callable , Literal , Mapping , Optional , Tuple
1717
18+ import google .api_core .exceptions
1819import google .cloud .bigquery .job as bq_job
1920import google .cloud .bigquery .table as bq_table
21+ import google .cloud .bigquery_storage_v1
2022from google .cloud import bigquery
2123
24+ import bigframes .core .compile
2225import bigframes .core .compile .ibis_compiler .ibis_compiler as ibis_compiler
2326import bigframes .core .compile .sqlglot .compiler as sqlglot_compiler
2427import bigframes .core .events
25- import bigframes .session . metrics
28+ import bigframes .core . schema as schemata
2629import bigframes .session ._io .bigquery as bq_io
30+ import bigframes .session .metrics
31+ from bigframes import exceptions as bfe
2732from bigframes .core import bq_data , compile , nodes
28- import bigframes .core .compile
29- from bigframes .session import executor , semi_executor , execution_spec
3033from bigframes .core .compile .configs import CompileRequest , CompileResult
31- from bigframes import exceptions as bfe
32- import bigframes .core .schema as schemata
33- import google .cloud .bigquery_storage_v1
34-
35- import google .api_core .exceptions
36-
34+ from bigframes .session import execution_spec , executor , semi_executor
3735
3836_WRITE_DISPOSITIONS = {
3937 "fail" : bigquery .WriteDisposition .WRITE_EMPTY ,
@@ -66,20 +64,14 @@ def execute(
6664 spec : execution_spec .ExecutionSpec ,
6765 ) -> executor .ExecuteResult :
6866 """Just execute whatever plan as is, without further caching or decomposition."""
69-
70- og_schema = plan .schema
71- compile_request = CompileRequest (
72- plan ,
73- sort_rows = spec .ordered ,
74- peek_count = spec .peek ,
75- )
76-
7767 compiled = compile .compile_sql (
78- compile_request , compiler_name = self ._compiler_name
68+ CompileRequest (
69+ plan ,
70+ sort_rows = spec .ordered ,
71+ peek_count = spec .peek ,
72+ ),
73+ compiler_name = self ._compiler_name ,
7974 )
80- # might have more columns than og schema, for hidden ordering columns
81- compiled_schema = compiled .sql_schema
82-
8375 job_config = bigquery .QueryJobConfig ()
8476 dest_spec = spec .destination_spec
8577 cluster_cols = None
@@ -110,18 +102,16 @@ def execute(
110102 )
111103 result_bq_data = None
112104 if query_job and query_job .destination :
113- # we might add extra sql columns in compilation, esp if caching w ordering, infer a bigframes type for them
114- result_bf_schema = _result_schema (og_schema , list (compiled .sql_schema ))
115105 dst = query_job .destination
116106 result_bq_data = bq_data .BigqueryDataSource (
117107 table = bq_data .GbqNativeTable .from_ref_and_schema (
118108 dst ,
119- tuple (compiled_schema ),
109+ tuple (compiled . sql_schema ),
120110 cluster_cols = cluster_cols or (),
121111 location = iterator .location or self .bqclient .location ,
122112 table_type = "TABLE" ,
123113 ),
124- schema = result_bf_schema ,
114+ schema = plan . schema ,
125115 ordering = compiled .row_order ,
126116 n_rows = iterator .total_rows ,
127117 )
@@ -143,26 +133,25 @@ def execute(
143133 project_id = self .bqclient .project ,
144134 storage_client = self ._bqstoragereadclient ,
145135 execution_metadata = execution_metadata ,
146- selected_fields = tuple ((col , col ) for col in og_schema .names ),
136+ selected_fields = tuple ((col , col ) for col in plan . schema .names ),
147137 )
148138 else :
149139 return executor .LocalExecuteResult (
150- data = iterator .to_arrow ().select (og_schema .names ),
140+ data = iterator .to_arrow ().select (plan . schema .names ),
151141 bf_schema = plan .schema ,
152142 execution_metadata = execution_metadata ,
153143 )
154144
155145 def _run_execute_query (
156146 self ,
157147 sql : str ,
158- job_config : Optional [ bq_job .QueryJobConfig ] = None ,
159- query_with_job : bool = True ,
160- session = None ,
148+ job_config : bq_job .QueryJobConfig ,
149+ query_with_job : bool ,
150+ session ,
161151 ) -> Tuple [bq_table .RowIterator , Optional [bigquery .QueryJob ]]:
162152 """
163153 Starts BigQuery query job and waits for results.
164154 """
165- job_config = bq_job .QueryJobConfig () if job_config is None else job_config
166155 if bigframes .options .compute .maximum_bytes_billed is not None :
167156 job_config .maximum_bytes_billed = (
168157 bigframes .options .compute .maximum_bytes_billed
@@ -188,13 +177,3 @@ def _run_execute_query(
188177 raise bfe .QueryComplexityError (new_message ) from e
189178 else :
190179 raise
191-
192-
193- def _result_schema (
194- logical_schema : schemata .ArraySchema , sql_schema : list [bigquery .SchemaField ]
195- ) -> schemata .ArraySchema :
196- inferred_schema = bigframes .dtypes .bf_type_from_type_kind (sql_schema )
197- inferred_schema .update (logical_schema ._mapping )
198- return schemata .ArraySchema (
199- tuple (schemata .SchemaItem (col , dtype ) for col , dtype in inferred_schema .items ())
200- )
0 commit comments