|
| 1 | +# Copyright 2025 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 bigframes_vendored.constants |
| 20 | +import google.cloud.bigquery |
| 21 | +import pandas as pd |
| 22 | + |
| 23 | +import bigframes.core.logging.log_adapter as log_adapter |
| 24 | +import bigframes.core.sql.table |
| 25 | +import bigframes.session |
| 26 | + |
| 27 | + |
| 28 | +def _get_table_metadata( |
| 29 | + *, |
| 30 | + bqclient: google.cloud.bigquery.Client, |
| 31 | + table_name: str, |
| 32 | +) -> pd.Series: |
| 33 | + table_metadata = bqclient.get_table(table_name) |
| 34 | + table_dict = table_metadata.to_api_repr() |
| 35 | + return pd.Series(table_dict) |
| 36 | + |
| 37 | + |
| 38 | +@log_adapter.method_logger(custom_base_name="bigquery_table") |
| 39 | +def create_external_table( |
| 40 | + table_name: str, |
| 41 | + *, |
| 42 | + replace: bool = False, |
| 43 | + if_not_exists: bool = False, |
| 44 | + columns: Optional[Mapping[str, str]] = None, |
| 45 | + partition_columns: Optional[Mapping[str, str]] = None, |
| 46 | + connection_name: Optional[str] = None, |
| 47 | + options: Mapping[str, Union[str, int, float, bool, list]], |
| 48 | + session: Optional[bigframes.session.Session] = None, |
| 49 | +) -> pd.Series: |
| 50 | + """ |
| 51 | + Creates a BigQuery external table. |
| 52 | +
|
| 53 | + See the `BigQuery CREATE EXTERNAL TABLE DDL syntax |
| 54 | + <https://docs.cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_external_table_statement>`_ |
| 55 | + for additional reference. |
| 56 | +
|
| 57 | + Args: |
| 58 | + table_name (str): |
| 59 | + The name of the table in BigQuery. |
| 60 | + replace (bool, default False): |
| 61 | + Whether to replace the table if it already exists. |
| 62 | + if_not_exists (bool, default False): |
| 63 | + Whether to ignore the error if the table already exists. |
| 64 | + columns (Mapping[str, str], optional): |
| 65 | + The table's schema. |
| 66 | + partition_columns (Mapping[str, str], optional): |
| 67 | + The table's partition columns. |
| 68 | + connection_name (str, optional): |
| 69 | + The connection to use for the table. |
| 70 | + options (Mapping[str, Union[str, int, float, bool, list]]): |
| 71 | + The OPTIONS clause, which specifies the table options. |
| 72 | + session (bigframes.session.Session, optional): |
| 73 | + The session to use. If not provided, the default session is used. |
| 74 | +
|
| 75 | + Returns: |
| 76 | + pandas.Series: |
| 77 | + A Series with object dtype containing the table metadata. Reference |
| 78 | + the `BigQuery Table REST API reference |
| 79 | + <https://cloud.google.com/bigquery/docs/reference/rest/v2/tables#Table>`_ |
| 80 | + for available fields. |
| 81 | + """ |
| 82 | + import bigframes.pandas as bpd |
| 83 | + |
| 84 | + sql = bigframes.core.sql.table.create_external_table_ddl( |
| 85 | + table_name=table_name, |
| 86 | + replace=replace, |
| 87 | + if_not_exists=if_not_exists, |
| 88 | + columns=columns, |
| 89 | + partition_columns=partition_columns, |
| 90 | + connection_name=connection_name, |
| 91 | + options=options, |
| 92 | + ) |
| 93 | + |
| 94 | + if session is None: |
| 95 | + bpd.read_gbq_query(sql) |
| 96 | + session = bpd.get_global_session() |
| 97 | + assert ( |
| 98 | + session is not None |
| 99 | + ), f"Missing connection to BigQuery. Please report how you encountered this error at {bigframes_vendored.constants.FEEDBACK_LINK}." |
| 100 | + else: |
| 101 | + session.read_gbq_query(sql) |
| 102 | + |
| 103 | + return _get_table_metadata(bqclient=session.bqclient, table_name=table_name) |
0 commit comments