1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15+ from __future__ import annotations
16+
1517import datetime
1618import logging
1719import threading
2325import google .cloud .bigquery as bigquery
2426
2527from bigframes .core .compile import googlesql
28+ import bigframes .core .events
2629from bigframes .session import temporary_storage
30+ import bigframes .session ._io .bigquery as bfbqio
2731
2832KEEPALIVE_QUERY_TIMEOUT_SECONDS = 5.0
2933
@@ -38,12 +42,19 @@ class SessionResourceManager(temporary_storage.TemporaryStorageManager):
3842 Responsible for allocating and cleaning up temporary gbq tables used by a BigFrames session.
3943 """
4044
41- def __init__ (self , bqclient : bigquery .Client , location : str ):
45+ def __init__ (
46+ self ,
47+ bqclient : bigquery .Client ,
48+ location : str ,
49+ * ,
50+ publisher : bigframes .core .events .Publisher ,
51+ ):
4252 self .bqclient = bqclient
4353 self ._location = location
4454 self ._session_id : Optional [str ] = None
4555 self ._sessiondaemon : Optional [RecurringTaskDaemon ] = None
4656 self ._session_lock = threading .RLock ()
57+ self ._publisher = publisher
4758
4859 @property
4960 def location (self ):
@@ -84,21 +95,38 @@ def create_temp_table(
8495
8596 ddl = f"CREATE TEMP TABLE `_SESSION`.{ googlesql .identifier (table_ref .table_id )} ({ fields_string } ){ cluster_string } "
8697
87- job = self .bqclient .query (
88- ddl , job_config = job_config , location = self .location
98+ _ , job = bfbqio .start_query_with_client (
99+ self .bqclient ,
100+ ddl ,
101+ job_config = job_config ,
102+ location = self .location ,
103+ project = None ,
104+ timeout = None ,
105+ metrics = None ,
106+ query_with_job = True ,
107+ publisher = self ._publisher ,
89108 )
90109 job .result ()
91110 # return the fully qualified table, so it can be used outside of the session
92- return job .destination
111+ destination = job .destination
112+ assert destination is not None , "Failure to create temp table."
113+ return destination
93114
94115 def close (self ):
95116 if self ._sessiondaemon is not None :
96117 self ._sessiondaemon .stop ()
97118
98119 if self ._session_id is not None and self .bqclient is not None :
99- self .bqclient .query_and_wait (
120+ bfbqio .start_query_with_client (
121+ self .bqclient ,
100122 f"CALL BQ.ABORT_SESSION('{ self ._session_id } ')" ,
123+ job_config = bigquery .QueryJobConfig (),
101124 location = self .location ,
125+ project = None ,
126+ timeout = None ,
127+ metrics = None ,
128+ query_with_job = False ,
129+ publisher = self ._publisher ,
102130 )
103131
104132 def _get_session_id (self ) -> str :
@@ -109,8 +137,16 @@ def _get_session_id(self) -> str:
109137 job_config = bigquery .QueryJobConfig (create_session = True )
110138 # Make sure the session is a new one, not one associated with another query.
111139 job_config .use_query_cache = False
112- query_job = self .bqclient .query (
113- "SELECT 1" , job_config = job_config , location = self .location
140+ _ , query_job = bfbqio .start_query_with_client (
141+ self .bqclient ,
142+ "SELECT 1" ,
143+ job_config = job_config ,
144+ location = self .location ,
145+ project = None ,
146+ timeout = None ,
147+ metrics = None ,
148+ query_with_job = True ,
149+ publisher = self ._publisher ,
114150 )
115151 query_job .result () # blocks until finished
116152 assert query_job .session_info is not None
@@ -133,11 +169,16 @@ def _keep_session_alive(self):
133169 ]
134170 )
135171 try :
136- self .bqclient .query_and_wait (
172+ bfbqio .start_query_with_client (
173+ self .bqclient ,
137174 "SELECT 1" ,
138- location = self .location ,
139175 job_config = job_config ,
140- wait_timeout = KEEPALIVE_QUERY_TIMEOUT_SECONDS ,
176+ location = self .location ,
177+ project = None ,
178+ timeout = KEEPALIVE_QUERY_TIMEOUT_SECONDS ,
179+ metrics = None ,
180+ query_with_job = False ,
181+ publisher = self ._publisher ,
141182 )
142183 except Exception as e :
143184 logging .warning ("BigQuery session keep-alive query errored : %s" , e )
0 commit comments