|
37 | 37 | "DATE": types.StandardSqlDataType.DATE,
|
38 | 38 | "TIME": types.StandardSqlDataType.TIME,
|
39 | 39 | "DATETIME": types.StandardSqlDataType.DATETIME,
|
| 40 | + # no direct conversion from ARRAY, the latter is represented by mode="REPEATED" |
40 | 41 | }
|
41 | 42 | """String names of the legacy SQL types to integer codes of Standard SQL types."""
|
42 | 43 |
|
@@ -179,13 +180,25 @@ def to_standard_sql(self):
|
179 | 180 | An instance of :class:`~google.cloud.bigquery_v2.types.StandardSqlField`.
|
180 | 181 | """
|
181 | 182 | sql_type = types.StandardSqlDataType()
|
182 |
| - sql_type.type_kind = LEGACY_TO_STANDARD_TYPES.get( |
183 |
| - self.field_type, types.StandardSqlDataType.TYPE_KIND_UNSPECIFIED |
184 |
| - ) |
185 | 183 |
|
186 |
| - # NOTE: No need to also handle the "ARRAY" composed type, the latter |
187 |
| - # does not exist in legacy SQL types. |
188 |
| - if sql_type.type_kind == types.StandardSqlDataType.STRUCT: # noqa: E721 |
| 184 | + if self.mode == "REPEATED": |
| 185 | + sql_type.type_kind = types.StandardSqlDataType.ARRAY |
| 186 | + else: |
| 187 | + sql_type.type_kind = LEGACY_TO_STANDARD_TYPES.get( |
| 188 | + self.field_type, types.StandardSqlDataType.TYPE_KIND_UNSPECIFIED |
| 189 | + ) |
| 190 | + |
| 191 | + if sql_type.type_kind == types.StandardSqlDataType.ARRAY: # noqa: E721 |
| 192 | + array_element_type = LEGACY_TO_STANDARD_TYPES.get( |
| 193 | + self.field_type, types.StandardSqlDataType.TYPE_KIND_UNSPECIFIED |
| 194 | + ) |
| 195 | + # ARRAY cannot directly contain other arrays, only scalar types and STRUCTs |
| 196 | + # https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#array-type |
| 197 | + if array_element_type != types.StandardSqlDataType.STRUCT: # noqa: E721 |
| 198 | + sql_type.array_element_type.type_kind = array_element_type |
| 199 | + else: |
| 200 | + raise NotImplementedError("no support yet for array of STRUCT") |
| 201 | + elif sql_type.type_kind == types.StandardSqlDataType.STRUCT: # noqa: E721 |
189 | 202 | sql_type.struct_type.fields.extend(
|
190 | 203 | field.to_standard_sql() for field in self.fields
|
191 | 204 | )
|
|
0 commit comments