This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathapi.py
More file actions
702 lines (590 loc) · 21.1 KB
/
api.py
File metadata and controls
702 lines (590 loc) · 21.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import functools
import inspect
import os
import threading
import typing
from typing import (
Any,
Callable,
Dict,
IO,
Iterable,
Literal,
MutableSequence,
Optional,
overload,
Sequence,
Tuple,
Union,
)
import warnings
import bigframes_vendored.constants as constants
import bigframes_vendored.pandas.io.gbq as vendored_pandas_gbq
from google.cloud import bigquery
import numpy
import pandas
from pandas._typing import (
CompressionOptions,
FilePath,
ReadPickleBuffer,
StorageOptions,
)
import pyarrow as pa
import bigframes._config as config
import bigframes._importing
import bigframes.core.global_session as global_session
import bigframes.core.indexes
import bigframes.dataframe
import bigframes.enums
import bigframes.series
import bigframes.session
from bigframes.session import dry_runs
import bigframes.session._io.bigquery
import bigframes.session.clients
import bigframes.session.metrics
# Note: the following methods are duplicated from Session. This duplication
# enables the following:
#
# 1. Static type checking knows the argument and return types, which is
# difficult to do with decorators. Aside: When we require Python 3.10, we
# can use Concatenate for generic typing in decorators. See:
# https://stackoverflow.com/a/68290080/101923
# 2. docstrings get processed by static processing tools, such as VS Code's
# autocomplete.
# 3. Positional arguments function as expected. If we were to pull in the
# methods directly from Session, a Session object would need to be the first
# argument, even if we allow a default value.
# 4. Allows to set BigQuery options for the BigFrames session based on the
# method and its arguments.
def read_arrow(pa_table: pa.Table) -> bigframes.dataframe.DataFrame:
"""Load a PyArrow Table to a BigQuery DataFrames DataFrame.
Args:
pa_table (pyarrow.Table):
PyArrow table to load data from.
Returns:
bigframes.dataframe.DataFrame:
A new DataFrame representing the data from the PyArrow table.
"""
session = global_session.get_global_session()
return session.read_arrow(pa_table=pa_table)
def read_csv(
filepath_or_buffer: str | IO["bytes"],
*,
sep: Optional[str] = ",",
header: Optional[int] = 0,
names: Optional[
Union[MutableSequence[Any], numpy.ndarray[Any, Any], Tuple[Any, ...], range]
] = None,
index_col: Optional[
Union[
int,
str,
Sequence[Union[str, int]],
bigframes.enums.DefaultIndexKind,
Literal[False],
]
] = None,
usecols: Optional[
Union[
MutableSequence[str],
Tuple[str, ...],
Sequence[int],
pandas.Series,
pandas.Index,
numpy.ndarray[Any, Any],
Callable[[Any], bool],
]
] = None,
dtype: Optional[Dict] = None,
engine: Optional[
Literal["c", "python", "pyarrow", "python-fwf", "bigquery"]
] = None,
encoding: Optional[str] = None,
write_engine: constants.WriteEngineType = "default",
**kwargs,
) -> bigframes.dataframe.DataFrame:
return global_session.with_default_session(
bigframes.session.Session.read_csv,
filepath_or_buffer=filepath_or_buffer,
sep=sep,
header=header,
names=names,
index_col=index_col,
usecols=usecols,
dtype=dtype,
engine=engine,
encoding=encoding,
write_engine=write_engine,
**kwargs,
)
read_csv.__doc__ = inspect.getdoc(bigframes.session.Session.read_csv)
def read_json(
path_or_buf: str | IO["bytes"],
*,
orient: Literal[
"split", "records", "index", "columns", "values", "table"
] = "columns",
dtype: Optional[Dict] = None,
encoding: Optional[str] = None,
lines: bool = False,
engine: Literal["ujson", "pyarrow", "bigquery"] = "ujson",
write_engine: constants.WriteEngineType = "default",
**kwargs,
) -> bigframes.dataframe.DataFrame:
return global_session.with_default_session(
bigframes.session.Session.read_json,
path_or_buf=path_or_buf,
orient=orient,
dtype=dtype,
encoding=encoding,
lines=lines,
engine=engine,
write_engine=write_engine,
**kwargs,
)
read_json.__doc__ = inspect.getdoc(bigframes.session.Session.read_json)
@overload
def read_gbq( # type: ignore[overload-overlap]
query_or_table: str,
*,
index_col: Iterable[str] | str | bigframes.enums.DefaultIndexKind = ...,
columns: Iterable[str] = ...,
configuration: Optional[Dict] = ...,
max_results: Optional[int] = ...,
filters: vendored_pandas_gbq.FiltersType = ...,
use_cache: Optional[bool] = ...,
col_order: Iterable[str] = ...,
dry_run: Literal[False] = ...,
allow_large_results: Optional[bool] = ...,
) -> bigframes.dataframe.DataFrame:
...
@overload
def read_gbq(
query_or_table: str,
*,
index_col: Iterable[str] | str | bigframes.enums.DefaultIndexKind = ...,
columns: Iterable[str] = ...,
configuration: Optional[Dict] = ...,
max_results: Optional[int] = ...,
filters: vendored_pandas_gbq.FiltersType = ...,
use_cache: Optional[bool] = ...,
col_order: Iterable[str] = ...,
dry_run: Literal[True] = ...,
allow_large_results: Optional[bool] = ...,
) -> pandas.Series:
...
def read_gbq(
query_or_table: str,
*,
index_col: Iterable[str] | str | bigframes.enums.DefaultIndexKind = (),
columns: Iterable[str] = (),
configuration: Optional[Dict] = None,
max_results: Optional[int] = None,
filters: vendored_pandas_gbq.FiltersType = (),
use_cache: Optional[bool] = None,
col_order: Iterable[str] = (),
dry_run: bool = False,
allow_large_results: Optional[bool] = None,
) -> bigframes.dataframe.DataFrame | pandas.Series:
_set_default_session_location_if_possible(query_or_table)
return global_session.with_default_session(
bigframes.session.Session.read_gbq,
query_or_table,
index_col=index_col,
columns=columns,
configuration=configuration,
max_results=max_results,
filters=filters,
use_cache=use_cache,
col_order=col_order,
dry_run=dry_run,
allow_large_results=allow_large_results,
)
read_gbq.__doc__ = inspect.getdoc(bigframes.session.Session.read_gbq)
def _run_read_gbq_colab_sessionless_dry_run(
query: str,
*,
pyformat_args: Dict[str, Any],
) -> pandas.Series:
"""Run a dry_run without a session."""
query_formatted = bigframes.core.pyformat.pyformat(
query,
pyformat_args=pyformat_args,
dry_run=True,
)
bqclient = _get_bqclient()
job = _dry_run(query_formatted, bqclient)
return dry_runs.get_query_stats_with_inferred_dtypes(job, (), ())
def _try_read_gbq_colab_sessionless_dry_run(
query: str,
*,
pyformat_args: Dict[str, Any],
) -> Optional[pandas.Series]:
"""Run a dry_run without a session, only if the session hasn't yet started."""
global _default_location_lock
# Avoid creating a session just for dry run. We don't want to bind to a
# location too early. This is especially important if the query only refers
# to local data and not any BigQuery tables.
with _default_location_lock:
if not config.options.bigquery._session_started:
return _run_read_gbq_colab_sessionless_dry_run(
query, pyformat_args=pyformat_args
)
# Explicitly return None to indicate that we didn't run the dry run query.
return None
@overload
def _read_gbq_colab( # type: ignore[overload-overlap]
query_or_table: str,
*,
pyformat_args: Optional[Dict[str, Any]] = ...,
dry_run: Literal[False] = ...,
) -> bigframes.dataframe.DataFrame:
...
@overload
def _read_gbq_colab(
query_or_table: str,
*,
pyformat_args: Optional[Dict[str, Any]] = ...,
dry_run: Literal[True] = ...,
) -> pandas.Series:
...
def _read_gbq_colab(
query_or_table: str,
*,
pyformat_args: Optional[Dict[str, Any]] = None,
dry_run: bool = False,
) -> bigframes.dataframe.DataFrame | pandas.Series:
"""A Colab-specific version of read_gbq.
Calls `_set_default_session_location_if_possible` and then delegates
to `bigframes.session.Session._read_gbq_colab`.
Args:
query_or_table (str):
SQL query or table ID (table ID not yet supported).
pyformat_args (Optional[Dict[str, Any]]):
Parameters to format into the query string.
dry_run (bool):
If True, estimates the query results size without returning data.
The return will be a pandas Series with query metadata.
Returns:
Union[bigframes.dataframe.DataFrame, pandas.Series]:
A BigQuery DataFrame if `dry_run` is False, otherwise a pandas Series.
"""
if pyformat_args is None:
pyformat_args = {}
# Only try to set the global location if it's not a dry run. We don't want
# to bind to a location too early. This is especially important if the query
# only refers to local data and not any BigQuery tables.
if dry_run:
result = _try_read_gbq_colab_sessionless_dry_run(
query_or_table, pyformat_args=pyformat_args
)
if result is not None:
return result
# If we made it this far, we must have a session that has already
# started. That means we can safely call the "real" _read_gbq_colab,
# which generates slightly nicer SQL.
else:
# Delay formatting the query with the special "session-less" logic. This
# avoids doing unnecessary work if the session already has a location or has
# already started.
create_query = functools.partial(
bigframes.core.pyformat.pyformat,
query_or_table,
pyformat_args=pyformat_args,
dry_run=True,
)
_set_default_session_location_if_possible_deferred_query(create_query)
if not config.options.bigquery._session_started:
with warnings.catch_warnings():
# Don't warning about Polars in SQL cell.
# Related to b/437090788.
try:
bigframes._importing.import_polars()
warnings.simplefilter("ignore", bigframes.exceptions.PreviewWarning)
config.options.bigquery.enable_polars_execution = True
except ImportError:
pass # don't fail if polars isn't available
return global_session.with_default_session(
bigframes.session.Session._read_gbq_colab,
query_or_table,
pyformat_args=pyformat_args,
dry_run=dry_run,
)
def read_gbq_model(model_name: str):
return global_session.with_default_session(
bigframes.session.Session.read_gbq_model,
model_name,
)
read_gbq_model.__doc__ = inspect.getdoc(bigframes.session.Session.read_gbq_model)
def read_gbq_object_table(
object_table: str, *, name: Optional[str] = None
) -> bigframes.dataframe.DataFrame:
return global_session.with_default_session(
bigframes.session.Session.read_gbq_object_table,
object_table,
name=name,
)
read_gbq_object_table.__doc__ = inspect.getdoc(
bigframes.session.Session.read_gbq_object_table
)
@overload
def read_gbq_query( # type: ignore[overload-overlap]
query: str,
*,
index_col: Iterable[str] | str | bigframes.enums.DefaultIndexKind = ...,
columns: Iterable[str] = ...,
configuration: Optional[Dict] = ...,
max_results: Optional[int] = ...,
use_cache: Optional[bool] = ...,
col_order: Iterable[str] = ...,
filters: vendored_pandas_gbq.FiltersType = ...,
dry_run: Literal[False] = ...,
allow_large_results: Optional[bool] = ...,
) -> bigframes.dataframe.DataFrame:
...
@overload
def read_gbq_query(
query: str,
*,
index_col: Iterable[str] | str | bigframes.enums.DefaultIndexKind = ...,
columns: Iterable[str] = ...,
configuration: Optional[Dict] = ...,
max_results: Optional[int] = ...,
use_cache: Optional[bool] = ...,
col_order: Iterable[str] = ...,
filters: vendored_pandas_gbq.FiltersType = ...,
dry_run: Literal[True] = ...,
allow_large_results: Optional[bool] = ...,
) -> pandas.Series:
...
def read_gbq_query(
query: str,
*,
index_col: Iterable[str] | str | bigframes.enums.DefaultIndexKind = (),
columns: Iterable[str] = (),
configuration: Optional[Dict] = None,
max_results: Optional[int] = None,
use_cache: Optional[bool] = None,
col_order: Iterable[str] = (),
filters: vendored_pandas_gbq.FiltersType = (),
dry_run: bool = False,
allow_large_results: Optional[bool] = None,
) -> bigframes.dataframe.DataFrame | pandas.Series:
_set_default_session_location_if_possible(query)
return global_session.with_default_session(
bigframes.session.Session.read_gbq_query,
query,
index_col=index_col,
columns=columns,
configuration=configuration,
max_results=max_results,
use_cache=use_cache,
col_order=col_order,
filters=filters,
dry_run=dry_run,
allow_large_results=allow_large_results,
)
read_gbq_query.__doc__ = inspect.getdoc(bigframes.session.Session.read_gbq_query)
@overload
def read_gbq_table( # type: ignore[overload-overlap]
query: str,
*,
index_col: Iterable[str] | str | bigframes.enums.DefaultIndexKind = ...,
columns: Iterable[str] = ...,
max_results: Optional[int] = ...,
filters: vendored_pandas_gbq.FiltersType = ...,
use_cache: bool = ...,
col_order: Iterable[str] = ...,
dry_run: Literal[False] = ...,
) -> bigframes.dataframe.DataFrame:
...
@overload
def read_gbq_table(
query: str,
*,
index_col: Iterable[str] | str | bigframes.enums.DefaultIndexKind = ...,
columns: Iterable[str] = ...,
max_results: Optional[int] = ...,
filters: vendored_pandas_gbq.FiltersType = ...,
use_cache: bool = ...,
col_order: Iterable[str] = ...,
dry_run: Literal[True] = ...,
) -> pandas.Series:
...
def read_gbq_table(
query: str,
*,
index_col: Iterable[str] | str | bigframes.enums.DefaultIndexKind = (),
columns: Iterable[str] = (),
max_results: Optional[int] = None,
filters: vendored_pandas_gbq.FiltersType = (),
use_cache: bool = True,
col_order: Iterable[str] = (),
dry_run: bool = False,
) -> bigframes.dataframe.DataFrame | pandas.Series:
_set_default_session_location_if_possible(query)
return global_session.with_default_session(
bigframes.session.Session.read_gbq_table,
query,
index_col=index_col,
columns=columns,
max_results=max_results,
filters=filters,
use_cache=use_cache,
col_order=col_order,
dry_run=dry_run,
)
read_gbq_table.__doc__ = inspect.getdoc(bigframes.session.Session.read_gbq_table)
@typing.overload
def read_pandas(
pandas_dataframe: pandas.DataFrame,
*,
write_engine: constants.WriteEngineType = "default",
) -> bigframes.dataframe.DataFrame:
...
@typing.overload
def read_pandas(
pandas_dataframe: pandas.Series,
*,
write_engine: constants.WriteEngineType = "default",
) -> bigframes.series.Series:
...
@typing.overload
def read_pandas(
pandas_dataframe: pandas.Index,
*,
write_engine: constants.WriteEngineType = "default",
) -> bigframes.core.indexes.Index:
...
def read_pandas(
pandas_dataframe: Union[pandas.DataFrame, pandas.Series, pandas.Index],
*,
write_engine: constants.WriteEngineType = "default",
):
return global_session.with_default_session(
bigframes.session.Session.read_pandas,
pandas_dataframe,
write_engine=write_engine,
)
read_pandas.__doc__ = inspect.getdoc(bigframes.session.Session.read_pandas)
def read_pickle(
filepath_or_buffer: FilePath | ReadPickleBuffer,
compression: CompressionOptions = "infer",
storage_options: StorageOptions = None,
*,
write_engine: constants.WriteEngineType = "default",
):
return global_session.with_default_session(
bigframes.session.Session.read_pickle,
filepath_or_buffer=filepath_or_buffer,
compression=compression,
storage_options=storage_options,
write_engine=write_engine,
)
read_pickle.__doc__ = inspect.getdoc(bigframes.session.Session.read_pickle)
def read_parquet(
path: str | IO["bytes"],
*,
engine: str = "auto",
write_engine: constants.WriteEngineType = "default",
) -> bigframes.dataframe.DataFrame:
return global_session.with_default_session(
bigframes.session.Session.read_parquet,
path,
engine=engine,
write_engine=write_engine,
)
read_parquet.__doc__ = inspect.getdoc(bigframes.session.Session.read_parquet)
def read_gbq_function(
function_name: str,
is_row_processor: bool = False,
):
return global_session.with_default_session(
bigframes.session.Session.read_gbq_function,
function_name=function_name,
is_row_processor=is_row_processor,
)
read_gbq_function.__doc__ = inspect.getdoc(bigframes.session.Session.read_gbq_function)
def from_glob_path(
path: str, *, connection: Optional[str] = None, name: Optional[str] = None
) -> bigframes.dataframe.DataFrame:
return global_session.with_default_session(
bigframes.session.Session.from_glob_path,
path=path,
connection=connection,
name=name,
)
from_glob_path.__doc__ = inspect.getdoc(bigframes.session.Session.from_glob_path)
_default_location_lock = threading.Lock()
def _get_bqclient() -> bigquery.Client:
# Address circular imports in doctest due to bigframes/session/__init__.py
# containing a lot of logic and samples.
from bigframes.session import clients
clients_provider = clients.ClientsProvider(
project=config.options.bigquery.project,
location=config.options.bigquery.location,
use_regional_endpoints=config.options.bigquery.use_regional_endpoints,
credentials=config.options.bigquery.credentials,
application_name=config.options.bigquery.application_name,
bq_kms_key_name=config.options.bigquery.kms_key_name,
client_endpoints_override=config.options.bigquery.client_endpoints_override,
requests_transport_adapters=config.options.bigquery.requests_transport_adapters,
)
return clients_provider.bqclient
def _dry_run(query, bqclient) -> bigquery.QueryJob:
# Address circular imports in doctest due to bigframes/session/__init__.py
# containing a lot of logic and samples.
from bigframes.session import metrics as bf_metrics
job = bqclient.query(query, bigquery.QueryJobConfig(dry_run=True))
# Fix for b/435183833. Log metrics even if a Session isn't available.
if bf_metrics.LOGGING_NAME_ENV_VAR in os.environ:
metrics = bf_metrics.ExecutionMetrics()
metrics.count_job_stats(job)
return job
def _set_default_session_location_if_possible(query):
_set_default_session_location_if_possible_deferred_query(lambda: query)
def _set_default_session_location_if_possible_deferred_query(create_query):
# Address circular imports in doctest due to bigframes/session/__init__.py
# containing a lot of logic and samples.
from bigframes.session._io import bigquery
# Set the location as per the query if this is the first query the user is
# running and:
# (1) Default session has not started yet, and
# (2) Location is not set yet, and
# (3) Use of regional endpoints is not set.
# If query is a table name, then it would be the location of the table.
# If query is a SQL with a table, then it would be table's location.
# If query is a SQL with no table, then it would be the BQ default location.
global _default_location_lock
with _default_location_lock:
if (
config.options.bigquery._session_started
or config.options.bigquery.location
or config.options.bigquery.use_regional_endpoints
):
return
query = create_query()
bqclient = _get_bqclient()
if bigquery.is_query(query):
# Intentionally run outside of the session so that we can detect the
# location before creating the session. Since it's a dry_run, labels
# aren't necessary.
job = _dry_run(query, bqclient)
config.options.bigquery.location = job.location
else:
table = bqclient.get_table(query)
config.options.bigquery.location = table.location