{"record":{"id":"9da7e01a92ad89e3","repo":"apache/beam","slug":"could-not-execute-the-query-please-check-if-the-query-is","errorCode":null,"errorMessage":"Could not execute the query. Please check if the query is properly formatted and the table exists. {e}","messagePattern":"Could not execute the query\\. Please check if the query is properly formatted and the table exists\\. (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/transforms/enrichment_handlers/cloudsql.py","lineNumber":407,"sourceCode":"        if params:\n          result = connection.execute(text(query), params)\n        else:\n          result = connection.execute(text(query))\n        # Materialize results while transaction is active.\n        data: Union[list[dict[str, Any]], dict[str, Any]]\n        if is_batch:\n          data = [row._asdict() for row in result]\n        else:\n          result_row = result.first()\n          data = result_row._asdict() if result_row else {}\n        # Explicitly commit the transaction.\n        transaction.commit()\n        return data\n      except Exception as e:\n        transaction.rollback()\n        raise RuntimeError(f\"Database operation failed: {e}\") from e\n    except Exception as e:\n      raise Exception(\n          f'Could not execute the query. Please check if the query is properly '\n          f'formatted and the table exists. {e}') from e\n    finally:\n      if connection:\n        connection.close()\n\n  def _build_batch_query(\n      self, requests: list[beam.Row], batch_size: int) -> str:\n    \"\"\"Build batched query with unique parameter names for multiple requests.\n\n    This method extracts parameter placeholders from the where_clause_template\n    using regex and creates unique parameter names for each batch item. The\n    parameter names in the template can be any valid identifiers (e.g., :id,\n    :param_0, :user_name) and don't need to match field names exactly.\n\n    For batch queries, placeholders are replaced with unique names like\n    :batch_0_id, :batch_1_param_0, etc., based on the actual parameter names\n    found in the template.","sourceCodeStart":389,"sourceCodeEnd":425,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/transforms/enrichment_handlers/cloudsql.py#L389-L425","documentation":"The outer except in _execute_query converts any exception (including the RuntimeError above) into Exception('Could not execute the query. Please check if the query is properly formatted and the table exists. ...'), hinting that the SQL itself or the target table is the likely cause.","triggerScenarios":"Invalid SQL syntax, non-existent table or schema, wrong dialect for the configured db_adapter, or any wrapped database failure reaching the outer handler.","commonSituations":"Typo in the table name; using MySQL dialect against PostgreSQL; the table living in a different database/db_id; missing SELECT permission making the table appear absent.","solutions":["Inspect the chained message for the exact DB error and fix the SQL.","Confirm table_name/db_id point to an existing table in the connected database.","Verify db_adapter matches the actual database engine.","Catch the exception and log e.__cause__ for diagnostics."],"exampleFix":"# before\nCustomQueryConfig(query=\"SELECT * FROM userz WHERE id = {id}\")\n# after\nCustomQueryConfig(query=\"SELECT * FROM users WHERE id = {id}\")","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":["python","sql","cloudsql","query"],"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"}