diff --git a/providers/google/src/airflow/providers/google/cloud/hooks/bigquery.py b/providers/google/src/airflow/providers/google/cloud/hooks/bigquery.py index 52ba8ef588005..5731baec73dd8 100644 --- a/providers/google/src/airflow/providers/google/cloud/hooks/bigquery.py +++ b/providers/google/src/airflow/providers/google/cloud/hooks/bigquery.py @@ -1990,18 +1990,19 @@ def _format_schema_for_description(schema: dict) -> list: internal_size, precision, scale, null_ok. """ description = [] - for field in schema["fields"]: - mode = field.get("mode", "NULLABLE") - field_description = ( - field["name"], - field["type"], - None, - None, - None, - None, - mode == "NULLABLE", - ) - description.append(field_description) + if "fields" in schema: + for field in schema["fields"]: + mode = field.get("mode", "NULLABLE") + field_description = ( + field["name"], + field["type"], + None, + None, + None, + None, + mode == "NULLABLE", + ) + description.append(field_description) return description diff --git a/providers/google/tests/unit/google/cloud/hooks/test_bigquery.py b/providers/google/tests/unit/google/cloud/hooks/test_bigquery.py index ddadebf14a07f..6d55aabd34a99 100644 --- a/providers/google/tests/unit/google/cloud/hooks/test_bigquery.py +++ b/providers/google/tests/unit/google/cloud/hooks/test_bigquery.py @@ -1043,6 +1043,11 @@ def test_format_schema_for_description(self): ("field_3", "STRING", None, None, None, None, False), ] + def test_format_schema_for_description_no_fields_key(self): + test_query_result = {"schema": {}} + description = _format_schema_for_description(test_query_result["schema"]) + assert description == [] + @mock.patch("airflow.providers.google.cloud.hooks.bigquery.build") @mock.patch("airflow.providers.google.cloud.hooks.bigquery.BigQueryHook.insert_job") def test_description(self, mock_insert, mock_build):