{"record":{"id":"7a708593ef6509ed","repo":"prestodb/presto","slug":"sql-statement-is-not-a-query","errorCode":null,"errorMessage":"SQL statement is not a query: ","messagePattern":"SQL statement is not a query: ","errorType":"exception","errorClass":"SQLException","httpStatus":null,"severity":"error","filePath":"presto-jdbc/src/main/java/com/facebook/presto/jdbc/PrestoStatement.java","lineNumber":80,"sourceCode":"        this.connection = new AtomicReference<>(requireNonNull(connection, \"connection is null\"));\n    }\n\n    public void setProgressMonitor(Consumer<QueryStats> progressMonitor)\n    {\n        progressCallback.set(Optional.of(requireNonNull(progressMonitor, \"progressMonitor is null\")));\n    }\n\n    public void clearProgressMonitor()\n    {\n        progressCallback.set(Optional.empty());\n    }\n\n    @Override\n    public ResultSet executeQuery(String sql)\n            throws SQLException\n    {\n        if (!execute(sql)) {\n            throw new SQLException(\"SQL statement is not a query: \" + sql);\n        }\n        return currentResult.get();\n    }\n\n    @Override\n    public void close()\n            throws SQLException\n    {\n        connection.set(null);\n        closeResultSet();\n    }\n\n    @Override\n    public int getMaxFieldSize()\n            throws SQLException\n    {\n        checkOpen();\n        return 0;","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-jdbc/src/main/java/com/facebook/presto/jdbc/PrestoStatement.java#L62-L98","documentation":"Statement.executeQuery() calls execute(sql) and, if it returns false (meaning the statement produced no ResultSet — i.e. it was an update/DDL statement), throws this SQLException including the offending SQL text. executeQuery is only valid for statements that return rows.","triggerScenarios":"Calling PrestoStatement.executeQuery with INSERT/UPDATE/DELETE/CREATE/DROP/ALTER/SET SESSION/CALL — any statement where execute() returns false because there is no result set.","commonSituations":"Running DDL or DML through executeQuery out of habit; framework code that always calls executeQuery; migration scripts executed via a query-only API; syntax confusion where a Presto utility statement has no result columns.","solutions":["Use executeUpdate() for DML, or execute()/executeLargeUpdate() for DDL and utility statements","Check execute()'s boolean return and only call getResultSet() when true","CAST the statement or add a trivial SELECT if you truly need rows, though the correct API is preferred"],"exampleFix":"// before\nResultSet rs = stmt.executeQuery(\"DROP TABLE t\");\n// after\nboolean hasRs = stmt.execute(\"DROP TABLE t\");\n// hasRs == false: statement is not a query","handlingStrategy":"validation","validationCode":"String s = sql.trim().toLowerCase();\nboolean nonQuery = s.startsWith(\"insert\") || s.startsWith(\"update\") || s.startsWith(\"delete\") || s.startsWith(\"create\") || s.startsWith(\"drop\") || s.startsWith(\"alter\") || s.startsWith(\"set \") || s.startsWith(\"call\") || s.startsWith(\"grant\") || s.startsWith(\"revoke\");\nif (nonQuery) throw new IllegalArgumentException(\"Use execute/executeUpdate, not executeQuery, for: \" + s);","typeGuard":null,"tryCatchPattern":"try (ResultSet rs = stmt.executeQuery(sql)) {\n    // consume rows\n} catch (SQLException e) {\n    if (e.getMessage() != null && e.getMessage().startsWith(\"SQL statement is not a query\")) {\n        stmt.execute(sql); // re-run as non-query\n    } else { throw e; }\n}","preventionTips":["Use executeUpdate for DML and execute for DDL/utility statements","In generic execution helpers, call execute() and branch on its boolean return","Review generated SQL to ensure query-only paths receive SELECT statements"],"tags":["jdbc","statement","wrong-api"],"backgroundTag":"statement-is-not-a-query","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}