You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "nrnavaneet (via GitHub)" <gi...@apache.org> on 2025/07/10 13:42:33 UTC

[PR] Fix KeyError in BigQueryHook when schema or fields are missing [airflow]

nrnavaneet opened a new pull request, #53138:
URL: https://github.com/apache/airflow/pull/53138

   ### What
   Fixes a KeyError that occurs when BigQuery job results return an empty schema or no 'fields' key.
   
   ### Why
   Some queries like external table creation return an empty schema ({}), causing `schema["fields"]` to raise a KeyError.
   
   ### How
   - Updated `_format_schema_for_description` to safely handle None or missing 'fields' key.
   - Added unit tests to cover:
     - Normal schema
     - Empty schema
     - Schema with missing 'fields'
   
   Closes: #53127


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix KeyError in BigQueryHook when schema or fields are missing [airflow]

Posted by "nrnavaneet (via GitHub)" <gi...@apache.org>.
nrnavaneet commented on PR #53138:
URL: https://github.com/apache/airflow/pull/53138#issuecomment-3058043295

   Thanks for the feedback!  I’ve removed the redundant checks.
   	•	Instead of checking if "schema" in query_results and later re-fetching it with query_results.get("schema"), I now directly retrieve the schema once and pass it to _format_schema_for_description.
   	•	The function itself (_format_schema_for_description) handles None, type mismatches, and missing fields, so upstream validation is no longer necessary.
   
   This simplifies the logic and ensures the single-responsibility principle is followed.
   
   Let me know if you’d like me to adjust further!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix KeyError in BigQueryHook when schema or fields are missing [airflow]

Posted by "nrnavaneet (via GitHub)" <gi...@apache.org>.
nrnavaneet closed pull request #53138: Fix KeyError in BigQueryHook when schema or fields are missing
URL: https://github.com/apache/airflow/pull/53138


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix KeyError in BigQueryHook when schema or fields are missing [airflow]

Posted by "jroachgolf84 (via GitHub)" <gi...@apache.org>.
jroachgolf84 commented on code in PR #53138:
URL: https://github.com/apache/airflow/pull/53138#discussion_r2198094707


##########
providers/google/src/airflow/providers/google/cloud/hooks/bigquery.py:
##########
@@ -1660,8 +1660,9 @@ def execute(self, operation: str, parameters: dict | None = None) -> None:
         self.job_id = job.job_id
         self.location = self.location or job.location
         query_results = self._get_query_result()
-        if "schema" in query_results:
-            self.description = _format_schema_for_description(query_results["schema"])
+        schema = query_results.get("schema")
+        if schema and isinstance(schema, dict) and "fields" in schema:

Review Comment:
   Please see other comment.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix KeyError in BigQueryHook when schema or fields are missing [airflow]

Posted by "jroachgolf84 (via GitHub)" <gi...@apache.org>.
jroachgolf84 commented on code in PR #53138:
URL: https://github.com/apache/airflow/pull/53138#discussion_r2198091120


##########
providers/google/src/airflow/providers/google/cloud/hooks/bigquery.py:
##########
@@ -2055,22 +2056,31 @@ def _format_schema_for_description(schema: dict) -> list:
     """
     Reformat the schema to match cursor description standard.
 
-    The description should be a tuple of 7 elemenbts: name, type, display_size,
+    The description should be a tuple of 7 elements: name, type, display_size,
     internal_size, precision, scale, null_ok.
     """
     description = []
-    for field in schema["fields"]:
+
+    if not schema or not isinstance(schema, dict):
+        return description
+
+    fields = schema.get("fields")

Review Comment:
   This logic seems redundant. We've already determined that `schema` is a dictionary and that `fields` is a key in that dictionary. I think it's best to only choose one place to implement this logic.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix KeyError in BigQueryHook when schema or fields are missing [airflow]

Posted by "jroachgolf84 (via GitHub)" <gi...@apache.org>.
jroachgolf84 commented on code in PR #53138:
URL: https://github.com/apache/airflow/pull/53138#discussion_r2198091120


##########
providers/google/src/airflow/providers/google/cloud/hooks/bigquery.py:
##########
@@ -2055,22 +2056,31 @@ def _format_schema_for_description(schema: dict) -> list:
     """
     Reformat the schema to match cursor description standard.
 
-    The description should be a tuple of 7 elemenbts: name, type, display_size,
+    The description should be a tuple of 7 elements: name, type, display_size,
     internal_size, precision, scale, null_ok.
     """
     description = []
-    for field in schema["fields"]:
+
+    if not schema or not isinstance(schema, dict):
+        return description
+
+    fields = schema.get("fields")

Review Comment:
   This logic seems redundant. You've already determined that `schema` is a dictionary and that `fields` is a key in that dictionary. I think it's best to only choose one place to implement this logic.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org