{"record":{"id":"c646baf25be32761","repo":"apache/druid","slug":"failed-to-handle-query-s","errorCode":null,"errorMessage":"Failed to handle query [%s]","messagePattern":"Failed to handle query \\[(.+?)\\]","errorType":"http","errorClass":"DruidException","httpStatus":500,"severity":"error","filePath":"multi-stage-query/src/main/java/org/apache/druid/msq/sql/resources/SqlStatementResource.java","lineNumber":259,"sourceCode":"    }\n    catch (DruidException e) {\n      stmt.reporter().failed(e);\n      return buildNonOkResponse(e);\n    }\n    catch (QueryException queryException) {\n      stmt.reporter().failed(queryException);\n      final DruidException underlyingException = DruidException.fromFailure(new QueryExceptionCompat(queryException));\n      return buildNonOkResponse(underlyingException);\n    }\n    catch (ForbiddenException e) {\n      log.debug(\"Got forbidden request for reason [%s]\", e.getErrorMessage());\n      return buildNonOkResponse(Forbidden.exception());\n    }\n    // Calcite throws java.lang.AssertionError at various points in planning/validation.\n    catch (AssertionError | Exception e) {\n      stmt.reporter().failed(e);\n      if (isDebug) {\n        log.warn(e, \"Failed to handle query [%s]\", sqlQueryId);\n      } else {\n        log.noStackTrace().warn(e, \"Failed to handle query [%s]\", sqlQueryId);\n      }\n      return buildNonOkResponse(\n          DruidException.forPersona(DruidException.Persona.DEVELOPER)\n                        .ofCategory(DruidException.Category.UNCATEGORIZED)\n                        .build(\"%s\", e.getMessage())\n      );\n    }\n    finally {\n      stmt.close();\n      Thread.currentThread().setName(currThreadName);\n    }\n  }\n\n\n  @GET\n  @Path(\"/{id}\")","sourceCodeStart":241,"sourceCodeEnd":277,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/multi-stage-query/src/main/java/org/apache/druid/msq/sql/resources/SqlStatementResource.java#L241-L277","documentation":"The /druid/v2/sql/statements POST endpoint caught an AssertionError or Exception while planning/submitting an async SQL query. The query is marked failed and the server responds with an UNCATEGORIZED DEVELOPER-persona DruidException wrapped as this message.","triggerScenarios":"POST /druid/v2/sql/statements where query validation/planning fails (SQL syntax errors, unknown columns/functions, authorization assertions, planning AssertionErrors from Calcite).","commonSituations":"Typos in SQL or object names; using functions not available in the MSQ engine; permission/authorization failures; Calcite assertion errors on unsupported query shapes; malformed JSON request body.","solutions":["Read the 'error' details in the HTTP response — the root cause (SQL parse/plan error) is included","Fix the SQL: check identifiers, function support, and syntax","Enable debug logging (isDebug) on the broker for full stack traces during development","Check authorizer config if the failure is a Forbidden/assertion error"],"exampleFix":"// before: query with unsupported function\nSELECT REGEXP_SUBSTR(col, 'a') FROM tbl\n\n// after: use an MSQ-supported equivalent\nSELECT REGEXP_EXTRACT(col, 'a', 1) FROM tbl","handlingStrategy":"validation","validationCode":"// Validate SQL locally before POSTing to /druid/v2/sql/statements\n// Ensure body has {query, context}; check identifiers exist via information_schema\nPOST /druid/v2/sql?wrapQueryInStatements=false body: {\"query\":\"EXPLAIN PLAN FOR \" + sql}","typeGuard":null,"tryCatchPattern":"try { postStatement(sql); } catch (DruidWebException e) { /* read response.error/errorMessage and correct the SQL accordingly */ }","preventionTips":["Test SQL with EXPLAIN PLAN or the sync /druid/v2/sql endpoint first","Use only MSQ-supported functions and syntax","Check column/table names against information_schema"],"tags":["sql","msq","rest-api","query-planning"],"backgroundTag":"sql-query-failed","analyzedSha":"9b90983fd291f26935af934383ce360473179e4d","analyzedAt":"2026-09-07T13:32:30.957Z","contentChangedAt":"2026-09-07T13:32:30.957Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}