{"record":{"id":"c41dc60c67257534","repo":"apache/beam","slug":"could-not-execute-the-query-query-please-check-if-the-query","errorCode":null,"errorMessage":"Could not execute the query: {query}. Please check if the query is properly formatted and the BigQuery table exists. {e}","messagePattern":"Could not execute the query: (.+?)\\. Please check if the query is properly formatted and the BigQuery table exists\\. (.+?)","errorType":"exception","errorClass":"BadRequest","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/transforms/enrichment_handlers/bigquery.py","lineNumber":182,"sourceCode":"      if max_batch_duration_secs is not None:\n        self._batching_kwargs[\n            'max_batch_duration_secs'] = max_batch_duration_secs\n\n  def __enter__(self):\n    self.client = bigquery.Client(project=self.project, **self.kwargs)\n\n  def _execute_query(self, query: str):\n    try:\n      results = self.client.query(query=query).result()\n      row_list = [dict(row.items()) for row in results]\n      if not row_list:\n        return None\n      if self._batching_kwargs:\n        return row_list\n      else:\n        return row_list[0]\n    except BadRequest as e:\n      raise BadRequest(\n          f'Could not execute the query: {query}. Please check if '\n          f'the query is properly formatted and the BigQuery '\n          f'table exists. {e}')\n    except RuntimeError as e:\n      raise RuntimeError(f\"Could not complete the query request: {query}. {e}\")\n\n  def create_row_key(self, row: beam.Row):\n    if self.condition_value_fn:\n      return tuple(self.condition_value_fn(row))\n    if self.fields:\n      row_dict = row._asdict()\n      return (tuple(row_dict[field] for field in self.fields))\n    raise ValueError(\"Either fields or condition_value_fn must be specified\")\n\n  def __call__(self, request: Union[beam.Row, list[beam.Row]], *args, **kwargs):\n    if isinstance(request, list):\n      values = []\n      responses = []","sourceCodeStart":164,"sourceCodeEnd":200,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/transforms/enrichment_handlers/bigquery.py#L164-L200","documentation":"_execute_query() wraps google.api_core.exceptions.BadRequest raised while running the enrichment query in BigQuery, re-raising it with the formatted query text to help diagnose formatting errors or a missing table.","triggerScenarios":"__call__ executes a malformed SQL query (bad query_template placeholders/format args), or the query references a nonexistent table/dataset, and BigQuery returns BadRequest.","commonSituations":"query_template placeholders not matching the number of format values; typos in table or column names; dataset deleted or in a different project.","solutions":["Print/inspect the embedded query text and run it manually in the BigQuery console to find the syntax problem","Verify the table's fully qualified name (project.dataset.table) and that it exists","Check that the number/order of format placeholders in the template matches the values passed","Catch BadRequest around enrichment calls if empty/bad results are expected"],"exampleFix":"// before\nquery_template = 'SELECT * FROM `proj.ds.tbl` WHERE id = {}'\n// after (quote and match placeholders)\nquery_template = \"SELECT * FROM `proj.ds.tbl` WHERE id = '{}'\"","handlingStrategy":"try-catch","validationCode":"def validate_template(template, n_args):\n    if template.count('{}') != n_args:\n        raise ValueError(f'Query template has {template.count(\"{}\")} placeholders, expected {n_args}')","typeGuard":null,"tryCatchPattern":"from google.api_core.exceptions import BadRequest\ntry:\n    out = handler(row)\nexcept BadRequest as e:\n    logger.error('BigQuery enrichment query failed: %s', e)\n    out = beam.Row()","preventionTips":["Run the rendered query manually in the BigQuery console before shipping","Keep placeholders in query_template aligned with the number of format values","Pin table references to project.dataset.table and verify existence in CI"],"tags":["apache-beam","bigquery","sql"],"backgroundTag":"sql-query-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}