|
| 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 bigframes_vendored.sqlglot as sg |
| 20 | +import bigframes_vendored.sqlglot.expressions as sge |
| 21 | + |
| 22 | +from bigframes.core.compile.sqlglot.sql import base |
| 23 | + |
| 24 | + |
| 25 | +def _loaddata_sql(self: sg.Generator, expression: sge.LoadData) -> str: |
| 26 | + out = ["LOAD DATA"] |
| 27 | + if expression.args.get("overwrite"): |
| 28 | + out.append("OVERWRITE") |
| 29 | + |
| 30 | + out.append(f"INTO {self.sql(expression, 'this').strip()}") |
| 31 | + |
| 32 | + # We ignore inpath as it's just a dummy to satisfy sqlglot requirements |
| 33 | + # but BigQuery uses FROM FILES instead. |
| 34 | + |
| 35 | + columns = self.sql(expression, "columns").strip() |
| 36 | + if columns: |
| 37 | + out.append(columns) |
| 38 | + |
| 39 | + partition_by = self.sql(expression, "partition_by").strip() |
| 40 | + if partition_by: |
| 41 | + out.append(partition_by) |
| 42 | + |
| 43 | + cluster_by = self.sql(expression, "cluster_by").strip() |
| 44 | + if cluster_by: |
| 45 | + out.append(cluster_by) |
| 46 | + |
| 47 | + options = self.sql(expression, "options").strip() |
| 48 | + if options: |
| 49 | + out.append(options) |
| 50 | + |
| 51 | + from_files = self.sql(expression, "from_files").strip() |
| 52 | + if from_files: |
| 53 | + out.append(f"FROM FILES {from_files}") |
| 54 | + |
| 55 | + with_partition_columns = self.sql(expression, "with_partition_columns").strip() |
| 56 | + if with_partition_columns: |
| 57 | + out.append(f"WITH PARTITION COLUMNS {with_partition_columns}") |
| 58 | + |
| 59 | + connection = self.sql(expression, "connection").strip() |
| 60 | + if connection: |
| 61 | + out.append(f"WITH CONNECTION {connection}") |
| 62 | + |
| 63 | + return " ".join(out) |
| 64 | + |
| 65 | + |
| 66 | +# Register the transform for BigQuery generator |
| 67 | +sg.dialects.bigquery.BigQuery.Generator.TRANSFORMS[sge.LoadData] = _loaddata_sql |
| 68 | + |
| 69 | + |
| 70 | +def load_data( |
| 71 | + table_name: str, |
| 72 | + *, |
| 73 | + write_disposition: str = "INTO", |
| 74 | + columns: Optional[Mapping[str, str]] = None, |
| 75 | + partition_by: Optional[list[str]] = None, |
| 76 | + cluster_by: Optional[list[str]] = None, |
| 77 | + table_options: Optional[Mapping[str, Union[str, int, float, bool, list]]] = None, |
| 78 | + from_files_options: Mapping[str, Union[str, int, float, bool, list]], |
| 79 | + with_partition_columns: Optional[Mapping[str, str]] = None, |
| 80 | + connection_name: Optional[str] = None, |
| 81 | +) -> sge.LoadData: |
| 82 | + """Generates the LOAD DATA DDL statement.""" |
| 83 | + # We use a Table with a simple identifier for the table name. |
| 84 | + # Quoting is handled by the dialect. |
| 85 | + table_expr = sge.Table(this=base.identifier(table_name)) |
| 86 | + |
| 87 | + sge_columns = ( |
| 88 | + sge.Schema( |
| 89 | + this=None, |
| 90 | + expressions=[ |
| 91 | + sge.ColumnDef( |
| 92 | + this=base.identifier(name), |
| 93 | + kind=sge.DataType.build(typ, dialect="bigquery"), |
| 94 | + ) |
| 95 | + for name, typ in columns.items() |
| 96 | + ], |
| 97 | + ) |
| 98 | + if columns |
| 99 | + else None |
| 100 | + ) |
| 101 | + |
| 102 | + sge_partition_by = ( |
| 103 | + sge.PartitionedByProperty( |
| 104 | + this=base.identifier(partition_by[0]) |
| 105 | + if len(partition_by) == 1 |
| 106 | + else sge.Tuple(expressions=[base.identifier(col) for col in partition_by]) |
| 107 | + ) |
| 108 | + if partition_by |
| 109 | + else None |
| 110 | + ) |
| 111 | + |
| 112 | + sge_cluster_by = ( |
| 113 | + sge.Cluster(expressions=[base.identifier(col) for col in cluster_by]) |
| 114 | + if cluster_by |
| 115 | + else None |
| 116 | + ) |
| 117 | + |
| 118 | + sge_table_options = ( |
| 119 | + sge.Properties( |
| 120 | + expressions=[ |
| 121 | + sge.Property(this=base.identifier(k), value=base.literal(v)) |
| 122 | + for k, v in table_options.items() |
| 123 | + ] |
| 124 | + ) |
| 125 | + if table_options |
| 126 | + else None |
| 127 | + ) |
| 128 | + |
| 129 | + sge_from_files = sge.Tuple( |
| 130 | + expressions=[ |
| 131 | + sge.Property(this=base.identifier(k), value=base.literal(v)) |
| 132 | + for k, v in from_files_options.items() |
| 133 | + ] |
| 134 | + ) |
| 135 | + |
| 136 | + sge_with_partition_columns = ( |
| 137 | + sge.Schema( |
| 138 | + this=None, |
| 139 | + expressions=[ |
| 140 | + sge.ColumnDef( |
| 141 | + this=base.identifier(name), |
| 142 | + kind=sge.DataType.build(typ, dialect="bigquery"), |
| 143 | + ) |
| 144 | + for name, typ in with_partition_columns.items() |
| 145 | + ], |
| 146 | + ) |
| 147 | + if with_partition_columns |
| 148 | + else None |
| 149 | + ) |
| 150 | + |
| 151 | + sge_connection = base.identifier(connection_name) if connection_name else None |
| 152 | + |
| 153 | + return sge.LoadData( |
| 154 | + this=table_expr, |
| 155 | + overwrite=(write_disposition == "OVERWRITE"), |
| 156 | + inpath=sge.convert("fake"), # satisfy sqlglot's required inpath arg |
| 157 | + columns=sge_columns, |
| 158 | + partition_by=sge_partition_by, |
| 159 | + cluster_by=sge_cluster_by, |
| 160 | + options=sge_table_options, |
| 161 | + from_files=sge_from_files, |
| 162 | + with_partition_columns=sge_with_partition_columns, |
| 163 | + connection=sge_connection, |
| 164 | + ) |
0 commit comments