@@ -231,33 +231,45 @@ def columns(
231231 ) -> t .Dict [str , exp .DataType ]:
232232 """Fetches column names and types for the target table."""
233233
234- def dtype_to_sql (dtype : t .Optional [StandardSqlDataType ]) -> str :
234+ def dtype_to_sql (
235+ dtype : t .Optional [StandardSqlDataType ], field : bigquery .SchemaField
236+ ) -> str :
235237 assert dtype
238+ assert field
236239
237240 kind = dtype .type_kind
238241 assert kind
239242
240243 # Not using the enum value to preserve compatibility with older versions
241244 # of the BigQuery library.
242245 if kind .name == "ARRAY" :
243- return f"ARRAY<{ dtype_to_sql (dtype .array_element_type )} >"
246+ return f"ARRAY<{ dtype_to_sql (dtype .array_element_type , field )} >"
244247 if kind .name == "STRUCT" :
245248 struct_type = dtype .struct_type
246249 assert struct_type
247250 fields = ", " .join (
248- f"{ field .name } { dtype_to_sql (field .type )} " for field in struct_type .fields
251+ f"{ struct_field .name } { dtype_to_sql (struct_field .type , nested_field )} "
252+ for struct_field , nested_field in zip (struct_type .fields , field .fields )
249253 )
250254 return f"STRUCT<{ fields } >"
251255 if kind .name == "TYPE_KIND_UNSPECIFIED" :
252- return "JSON"
256+ field_type = field .field_type
257+
258+ if field_type == "RANGE" :
259+ # If the field is a RANGE then `range_element_type` should be set to
260+ # one of `"DATE"`, `"DATETIME"` or `"TIMESTAMP"`.
261+ return f"RANGE<{ field .range_element_type .element_type } >"
262+
263+ return field_type
264+
253265 return kind .name
254266
255267 def create_mapping_schema (
256268 schema : t .Sequence [bigquery .SchemaField ],
257269 ) -> t .Dict [str , exp .DataType ]:
258270 return {
259271 field .name : exp .DataType .build (
260- dtype_to_sql (field .to_standard_sql ().type ), dialect = self .dialect
272+ dtype_to_sql (field .to_standard_sql ().type , field ), dialect = self .dialect
261273 )
262274 for field in schema
263275 }
0 commit comments