|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +from typing import Mapping, Optional, Union |
| 18 | + |
| 19 | +import pandas as pd |
| 20 | + |
| 21 | +from bigframes.bigquery._operations.table import _get_table_metadata |
| 22 | +import bigframes.core.logging.log_adapter as log_adapter |
| 23 | +import bigframes.core.sql.io |
| 24 | +import bigframes.session |
| 25 | + |
| 26 | + |
| 27 | +@log_adapter.method_logger(custom_base_name="bigquery_io") |
| 28 | +def load_data( |
| 29 | + table_name: str, |
| 30 | + *, |
| 31 | + write_disposition: str = "INTO", |
| 32 | + columns: Optional[Mapping[str, str]] = None, |
| 33 | + partition_by: Optional[list[str]] = None, |
| 34 | + cluster_by: Optional[list[str]] = None, |
| 35 | + table_options: Optional[Mapping[str, Union[str, int, float, bool, list]]] = None, |
| 36 | + from_files_options: Mapping[str, Union[str, int, float, bool, list]], |
| 37 | + with_partition_columns: Optional[Mapping[str, str]] = None, |
| 38 | + connection_name: Optional[str] = None, |
| 39 | + session: Optional[bigframes.session.Session] = None, |
| 40 | +) -> pd.Series: |
| 41 | + """ |
| 42 | + Loads data into a BigQuery table. |
| 43 | + See the `BigQuery LOAD DATA DDL syntax |
| 44 | + <https://docs.cloud.google.com/bigquery/docs/reference/standard-sql/load-statements>`_ |
| 45 | + for additional reference. |
| 46 | + Args: |
| 47 | + table_name (str): |
| 48 | + The name of the table in BigQuery. |
| 49 | + write_disposition (str, default "INTO"): |
| 50 | + Whether to replace the table if it already exists ("OVERWRITE") or append to it ("INTO"). |
| 51 | + columns (Mapping[str, str], optional): |
| 52 | + The table's schema. |
| 53 | + partition_by (list[str], optional): |
| 54 | + A list of partition expressions to partition the table by. See https://docs.cloud.google.com/bigquery/docs/reference/standard-sql/load-statements#partition_expression. |
| 55 | + cluster_by (list[str], optional): |
| 56 | + A list of columns to cluster the table by. |
| 57 | + table_options (Mapping[str, Union[str, int, float, bool, list]], optional): |
| 58 | + The table options. |
| 59 | + from_files_options (Mapping[str, Union[str, int, float, bool, list]]): |
| 60 | + The options for loading data from files. |
| 61 | + with_partition_columns (Mapping[str, str], optional): |
| 62 | + The table's partition columns. |
| 63 | + connection_name (str, optional): |
| 64 | + The connection to use for the table. |
| 65 | + session (bigframes.session.Session, optional): |
| 66 | + The session to use. If not provided, the default session is used. |
| 67 | + Returns: |
| 68 | + pandas.Series: |
| 69 | + A Series with object dtype containing the table metadata. Reference |
| 70 | + the `BigQuery Table REST API reference |
| 71 | + <https://cloud.google.com/bigquery/docs/reference/rest/v2/tables#Table>`_ |
| 72 | + for available fields. |
| 73 | + """ |
| 74 | + import bigframes.pandas as bpd |
| 75 | + |
| 76 | + sql = bigframes.core.sql.io.load_data_ddl( |
| 77 | + table_name=table_name, |
| 78 | + write_disposition=write_disposition, |
| 79 | + columns=columns, |
| 80 | + partition_by=partition_by, |
| 81 | + cluster_by=cluster_by, |
| 82 | + table_options=table_options, |
| 83 | + from_files_options=from_files_options, |
| 84 | + with_partition_columns=with_partition_columns, |
| 85 | + connection_name=connection_name, |
| 86 | + ) |
| 87 | + |
| 88 | + if session is None: |
| 89 | + bpd.read_gbq_query(sql) |
| 90 | + session = bpd.get_global_session() |
| 91 | + else: |
| 92 | + session.read_gbq_query(sql) |
| 93 | + |
| 94 | + return _get_table_metadata(bqclient=session.bqclient, table_name=table_name) |
0 commit comments