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
164 lines (136 loc) · 5.04 KB
/
ddl.py
File metadata and controls
164 lines (136 loc) · 5.04 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
# 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 Mapping, Optional, Union
import bigframes_vendored.sqlglot as sg
import bigframes_vendored.sqlglot.expressions as sge
from bigframes.core.compile.sqlglot.sql import base
def _loaddata_sql(self: sg.Generator, expression: sge.LoadData) -> str:
out = ["LOAD DATA"]
if expression.args.get("overwrite"):
out.append("OVERWRITE")
out.append(f"INTO {self.sql(expression, 'this').strip()}")
# We ignore inpath as it's just a dummy to satisfy sqlglot requirements
# but BigQuery uses FROM FILES instead.
columns = self.sql(expression, "columns").strip()
if columns:
out.append(columns)
partition_by = self.sql(expression, "partition_by").strip()
if partition_by:
out.append(partition_by)
cluster_by = self.sql(expression, "cluster_by").strip()
if cluster_by:
out.append(cluster_by)
options = self.sql(expression, "options").strip()
if options:
out.append(options)
from_files = self.sql(expression, "from_files").strip()
if from_files:
out.append(f"FROM FILES {from_files}")
with_partition_columns = self.sql(expression, "with_partition_columns").strip()
if with_partition_columns:
out.append(f"WITH PARTITION COLUMNS {with_partition_columns}")
connection = self.sql(expression, "connection").strip()
if connection:
out.append(f"WITH CONNECTION {connection}")
return " ".join(out)
# Register the transform for BigQuery generator
sg.dialects.bigquery.BigQuery.Generator.TRANSFORMS[sge.LoadData] = _loaddata_sql
def load_data(
table_name: str,
*,
write_disposition: str = "INTO",
columns: Optional[Mapping[str, str]] = None,
partition_by: Optional[list[str]] = None,
cluster_by: Optional[list[str]] = None,
table_options: Optional[Mapping[str, Union[str, int, float, bool, list]]] = None,
from_files_options: Mapping[str, Union[str, int, float, bool, list]],
with_partition_columns: Optional[Mapping[str, str]] = None,
connection_name: Optional[str] = None,
) -> sge.LoadData:
"""Generates the LOAD DATA DDL statement."""
# We use a Table with a simple identifier for the table name.
# Quoting is handled by the dialect.
table_expr = sge.Table(this=base.identifier(table_name))
sge_columns = (
sge.Schema(
this=None,
expressions=[
sge.ColumnDef(
this=base.identifier(name),
kind=sge.DataType.build(typ, dialect="bigquery"),
)
for name, typ in columns.items()
],
)
if columns
else None
)
sge_partition_by = (
sge.PartitionedByProperty(
this=base.identifier(partition_by[0])
if len(partition_by) == 1
else sge.Tuple(expressions=[base.identifier(col) for col in partition_by])
)
if partition_by
else None
)
sge_cluster_by = (
sge.Cluster(expressions=[base.identifier(col) for col in cluster_by])
if cluster_by
else None
)
sge_table_options = (
sge.Properties(
expressions=[
sge.Property(this=base.identifier(k), value=base.literal(v))
for k, v in table_options.items()
]
)
if table_options
else None
)
sge_from_files = sge.Tuple(
expressions=[
sge.Property(this=base.identifier(k), value=base.literal(v))
for k, v in from_files_options.items()
]
)
sge_with_partition_columns = (
sge.Schema(
this=None,
expressions=[
sge.ColumnDef(
this=base.identifier(name),
kind=sge.DataType.build(typ, dialect="bigquery"),
)
for name, typ in with_partition_columns.items()
],
)
if with_partition_columns
else None
)
sge_connection = base.identifier(connection_name) if connection_name else None
return sge.LoadData(
this=table_expr,
overwrite=(write_disposition == "OVERWRITE"),
inpath=sge.convert("fake"), # satisfy sqlglot's required inpath arg
columns=sge_columns,
partition_by=sge_partition_by,
cluster_by=sge_cluster_by,
options=sge_table_options,
from_files=sge_from_files,
with_partition_columns=sge_with_partition_columns,
connection=sge_connection,
)