@@ -2289,25 +2289,51 @@ def to_json(
2289
2289
2290
2290
def to_gbq (
2291
2291
self ,
2292
- destination_table : str ,
2292
+ destination_table : Optional [ str ] = None ,
2293
2293
* ,
2294
- if_exists : Optional [Literal ["fail" , "replace" , "append" ]] = "fail" ,
2294
+ if_exists : Optional [Literal ["fail" , "replace" , "append" ]] = None ,
2295
2295
index : bool = True ,
2296
2296
ordering_id : Optional [str ] = None ,
2297
- ) -> None :
2298
- if "." not in destination_table :
2299
- raise ValueError (
2300
- "Invalid Table Name. Should be of the form 'datasetId.tableId' or "
2301
- "'projectId.datasetId.tableId'"
2302
- )
2303
-
2297
+ ) -> str :
2304
2298
dispositions = {
2305
2299
"fail" : bigquery .WriteDisposition .WRITE_EMPTY ,
2306
2300
"replace" : bigquery .WriteDisposition .WRITE_TRUNCATE ,
2307
2301
"append" : bigquery .WriteDisposition .WRITE_APPEND ,
2308
2302
}
2303
+
2304
+ if destination_table is None :
2305
+ # TODO(swast): If there have been no modifications to the DataFrame
2306
+ # since the last time it was written (cached), then return that.
2307
+ # For `read_gbq` nodes, return the underlying table clone.
2308
+ destination_table = bigframes .session ._io .bigquery .create_temp_table (
2309
+ self ._session .bqclient ,
2310
+ self ._session ._anonymous_dataset ,
2311
+ # TODO(swast): allow custom expiration times, probably via session configuration.
2312
+ constants .DEFAULT_EXPIRATION ,
2313
+ )
2314
+
2315
+ if if_exists is not None and if_exists != "replace" :
2316
+ raise ValueError (
2317
+ f"Got invalid value { repr (if_exists )} for if_exists. "
2318
+ "When no destination table is specified, a new table is always created. "
2319
+ "None or 'replace' are the only valid options in this case."
2320
+ )
2321
+ if_exists = "replace"
2322
+
2323
+ if "." not in destination_table :
2324
+ raise ValueError (
2325
+ f"Got invalid value for destination_table { repr (destination_table )} . "
2326
+ "Should be of the form 'datasetId.tableId' or 'projectId.datasetId.tableId'."
2327
+ )
2328
+
2329
+ if if_exists is None :
2330
+ if_exists = "fail"
2331
+
2309
2332
if if_exists not in dispositions :
2310
- raise ValueError ("'{0}' is not valid for if_exists" .format (if_exists ))
2333
+ raise ValueError (
2334
+ f"Got invalid value { repr (if_exists )} for if_exists. "
2335
+ f"Valid options include None or one of { dispositions .keys ()} ."
2336
+ )
2311
2337
2312
2338
job_config = bigquery .QueryJobConfig (
2313
2339
write_disposition = dispositions [if_exists ],
@@ -2318,6 +2344,7 @@ def to_gbq(
2318
2344
)
2319
2345
2320
2346
self ._run_io_query (index = index , ordering_id = ordering_id , job_config = job_config )
2347
+ return destination_table
2321
2348
2322
2349
def to_numpy (
2323
2350
self , dtype = None , copy = False , na_value = None , ** kwargs
0 commit comments