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 pathddl.py
More file actions
185 lines (162 loc) · 6.89 KB
/
ddl.py
File metadata and controls
185 lines (162 loc) · 6.89 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
# Copyright 2026 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
from typing import Any, Mapping, Optional, Sequence, Union
import bigframes_vendored.constants
import google.cloud.bigquery
import pandas as pd
import bigframes.core.logging.log_adapter as log_adapter
import bigframes.core.sql.ddl
import bigframes.session
def _get_table_metadata(
*,
bqclient: google.cloud.bigquery.Client,
table_name: str,
) -> pd.Series:
table_metadata = bqclient.get_table(table_name)
table_dict = table_metadata.to_api_repr()
return pd.Series(table_dict)
@log_adapter.method_logger(custom_base_name="bigquery_table")
def create_external_table(
table_name: str,
*,
replace: bool = False,
if_not_exists: bool = False,
columns: Optional[Mapping[str, str]] = None,
partition_columns: Optional[Mapping[str, str]] = None,
connection_name: Optional[str] = None,
options: Mapping[str, Union[str, int, float, bool, list]],
session: Optional[bigframes.session.Session] = None,
) -> pd.Series:
"""
Creates a BigQuery external table.
See the `BigQuery CREATE EXTERNAL TABLE DDL syntax
<https://docs.cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_external_table_statement>`_
for additional reference.
Args:
table_name (str):
The name of the table in BigQuery.
replace (bool, default False):
Whether to replace the table if it already exists.
if_not_exists (bool, default False):
Whether to ignore the error if the table already exists.
columns (Mapping[str, str], optional):
The table's schema.
partition_columns (Mapping[str, str], optional):
The table's partition columns.
connection_name (str, optional):
The connection to use for the table.
options (Mapping[str, Union[str, int, float, bool, list]]):
The OPTIONS clause, which specifies the table options.
session (bigframes.session.Session, optional):
The session to use. If not provided, the default session is used.
Returns:
pandas.Series:
A Series with object dtype containing the table metadata. Reference
the `BigQuery Table REST API reference
<https://cloud.google.com/bigquery/docs/reference/rest/v2/tables#Table>`_
for available fields.
"""
import bigframes.pandas as bpd
sql = bigframes.core.sql.ddl.create_external_table_ddl(
table_name=table_name,
replace=replace,
if_not_exists=if_not_exists,
columns=columns,
partition_columns=partition_columns,
connection_name=connection_name,
options=options,
)
if session is None:
bpd.read_gbq_query(sql)
session = bpd.get_global_session()
assert (
session is not None
), f"Missing connection to BigQuery. Please report how you encountered this error at {bigframes_vendored.constants.FEEDBACK_LINK}."
else:
session.read_gbq_query(sql)
return _get_table_metadata(bqclient=session.bqclient, table_name=table_name)
@log_adapter.method_logger(custom_base_name="bigquery_table")
def load_data(
uris: str | Sequence[str],
format: str,
destination_table: str,
*,
schema: Optional[Mapping[str, str]] = None,
cluster_by: Optional[Sequence[str]] = None,
partition_by: Optional[str] = None,
options: Optional[dict[str, Any]] = None,
load_options: Optional[dict[str, Any]] = None,
connection: Optional[str] = None,
hive_partition_columns: Optional[Mapping[str, str]] = None,
overwrite: bool = False,
session: Optional[bigframes.session.Session] = None,
) -> pd.Series:
"""
Loads data from external files into a BigQuery table using the `LOAD DATA` statement.
Args:
uris (str | List[str]):
The fully qualified URIs for the external data locations (e.g., 'gs://bucket/path/file.csv').
format (str):
The format of the external data (e.g., 'CSV', 'PARQUET', 'AVRO', 'JSON').
destination_table (str, optional):
The name of the destination table. If not specified, a temporary table will be created.
schema (List[google.cloud.bigquery.SchemaField], optional):
The schema of the destination table. If not provided, schema auto-detection will be used.
cluster_by (List[str], optional):
A list of columns to cluster the table by.
partition_by (str, optional):
The partition expression for the table.
options (dict[str, Any], optional):
Table options (e.g., {'description': 'my table'}).
load_options (dict[str, Any], optional):
Options for loading data (e.g., {'skip_leading_rows': 1}).
connection (str, optional):
The connection name to use for reading external data.
hive_partition_columns (List[google.cloud.bigquery.SchemaField], optional):
The external partitioning columns. If set to an empty list, partitioning is inferred.
overwrite (bool, default False):
If True, overwrites the destination table. If False, appends to it.
session (bigframes.session.Session, optional):
The session to use. If not provided, the default session is used.
Returns:
pandas.Series:
A Series with object dtype containing the table metadata. Reference
the `BigQuery Table REST API reference
<https://cloud.google.com/bigquery/docs/reference/rest/v2/tables#Table>`_
for available fields.
"""
import bigframes.pandas as bpd
if session is None:
session = bpd.get_global_session()
if isinstance(uris, str):
uris = [uris]
sql = bigframes.core.sql.ddl.load_data_ddl(
destination_table=destination_table,
uris=uris,
format=format,
schema_fields=schema,
cluster_by=cluster_by,
partition_by=partition_by,
table_options=options,
load_options=load_options,
connection=connection,
hive_partition_columns=hive_partition_columns,
overwrite=overwrite,
)
# Execute the LOAD DATA statement
session.read_gbq_query(sql)
# Return a DataFrame pointing to the destination table
# We use session.read_gbq to ensure it uses the same session
return session.read_gbq(destination_table)