I have got the following Python code to retrieve a BigQuery dataset. Then I execute two queries working_query and bad_query on a Jupyter Notebook on kaggle.
The only difference is an added whitespace on line 3 after
...posts_questions` as q which makes the bad_query fail with the message
Query exceeded limit for bytes billed: 10000000000. 24460132352 or higher required.
I know that cost controls are enabled but do not see how this could be the issue here. How can I avoid such traps in the future and can someone explain the problem?
from google.cloud import bigquery
client = bigquery.Client()
dataset_ref = client.dataset("stackoverflow", project="bigquery-public-data")
dataset = client.get_dataset(dataset_ref)
safe_config = bigquery.QueryJobConfig(maximum_bytes_billed=10**10)
answers_query_job = client.query(working_query, job_config=safe_config)
answers_query_job.to_dataframe()
working_query = """
SELECT a.id, a.body, a.owner_user_id
FROM `bigquery-public-data.stackoverflow.posts_answers` AS a
INNER JOIN `bigquery-public-data.stackoverflow.posts_questions` AS q
ON q.id = a.parent_id
WHERE q.tags LIKE '%bigquery%'
"""
bad_query = """
SELECT a.id, a.body, a.owner_user_id
FROM `bigquery-public-data.stackoverflow.posts_answers` AS a
INNER JOIN `bigquery-public-data.stackoverflow.posts_questions` AS q
ON q.id = a.parent_id
WHERE q.tags LIKE '%bigquery%'
"""
UPDATE:
It was a caching problem as the working_query was run before activating cost control. That way it could retrieve data from cache even with cost control enabled. The queries have to be absolutely identical to share the cache so even added whitespace can prevent that.