{"record":{"id":"3d99438c78aaf5f5","repo":"cube-js/cube","slug":"query-failed-e-query-id-queryid","errorCode":null,"errorMessage":"Query failed: ${e}; query id: ${queryId}","messagePattern":"Query failed: (.+?); query id: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/cubejs-clickhouse-driver/src/ClickHouseDriver.ts","lineNumber":302,"sourceCode":"          query: formattedQuery,\n          query_id: queryId,\n          format,\n          clickhouse_settings: this.config.clickhouseSettings,\n          abort_signal: signal,\n        });\n\n        // response_headers['x-clickhouse-format'] is optional, but if it exists,\n        // it should match the requested format.\n        if (resultSet.response_headers['x-clickhouse-format'] && resultSet.response_headers['x-clickhouse-format'] !== format) {\n          throw new Error(`Unexpected x-clickhouse-format in response: expected ${format}, received ${resultSet.response_headers['x-clickhouse-format']}`);\n        }\n\n        // We used format JSON, so we expect each row to be Record with column names as keys\n        const results = await resultSet.json<Record<string, unknown>>();\n        return results;\n      } catch (e) {\n        // TODO replace string formatting with proper cause\n        throw new Error(`Query failed: ${e}; query id: ${queryId}`);\n      }\n    });\n  }\n\n  protected normaliseResponse<R = unknown>(res: ResponseJSON<Record<string, unknown>>): Array<R> {\n    if (res.data) {\n      const meta = (res.meta ?? []).reduce<Record<string, { name: string; type: string; }>>(\n        (state, element) => ({ [element.name]: element, ...state }),\n        {}\n      );\n\n      // TODO maybe use row-based format here as well?\n      res.data.forEach((row) => {\n        transformRow(row, meta);\n      });\n    }\n    return res.data as Array<R>;\n  }","sourceCodeStart":284,"sourceCodeEnd":320,"githubUrl":"https://github.com/cube-js/cube/blob/7d981676b36392fec34088b9afab6bdcad40207c/packages/cubejs-clickhouse-driver/src/ClickHouseDriver.ts#L284-L320","documentation":"queryResponse wraps the entire execute/json parsing of a ClickHouse query in try/catch and rethrows as `Query failed: <original error>; query id: <queryId>`. This normalizes all client errors (network, HTTP status, parsing, ClickHouse SQL errors) into one message while preserving the query id so the failing query can be found in ClickHouse's query_log.","triggerScenarios":"Any failure during query() execution in queryResponse: ClickHouse SQL error (syntax, unknown identifier), HTTP 4xx/5xx from the server, aborted request, or resultSet.json() parse failure.","commonSituations":"Typos in generated SQL from a bad data model; ClickHouse throwing UNKNOWN_IDENTIFIER for a missing column; auth failures (403 from ClickHouse user); queries killed by user-level timeout; schema drift after table changes.","solutions":["Copy the query id from the message and run `SELECT * FROM system.query_log WHERE query_id = '<id>'` (or check the server log) for the real ClickHouse error","Fix the underlying SQL/model issue indicated by the nested error text after 'Query failed:'","Verify ClickHouse user permissions and database/table existence","Check clickhouse server logs at the timestamp for full stack/exception text"],"exampleFix":"// before: measure references missing column\n-- Code: 47 UNKNOWN_IDENTIFIER: amount_typo\n// after: fix the sql in the cube schema\nmeasure: { sql: `amount`, type: `sum` }","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  const rows = await cube.query(query);\n} catch (e) {\n  const m = e.message.match(/^Query failed: (.*); query id: ([\\w-]+)/s);\n  if (m) {\n    const [, cause, queryId] = m;\n    logger.error({ cause, queryId }, 'ClickHouse query failed'); // look up queryId in system.query_log\n  }\n  throw e;\n}","preventionTips":["Validate schema SQL against ClickHouse (dry run) after model changes","Monitor ClickHouse system.query_log for exceptions","Grant the Cube ClickHouse user sufficient table/database permissions"],"tags":["clickhouse","query-execution","error-wrapping"],"backgroundTag":"sql-query-failed","analyzedSha":"7d981676b36392fec34088b9afab6bdcad40207c","analyzedAt":"2026-09-02T03:45:10.400Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}