{"record":{"id":"d247a369e016296c","repo":"pagehelper-org/Mybatis-PageHelper","slug":"unable-to-process-the-sql-you-can-submit-issues-i","errorCode":null,"errorMessage":"Unable to process the SQL, you can submit issues in GitHub for help.!","messagePattern":"Unable to process the SQL, you can submit issues in GitHub for help\\.!","errorType":"exception","errorClass":"PageException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/github/pagehelper/parser/defaults/DefaultSqlServerSqlParser.java","lineNumber":202,"sourceCode":"\n        if (isNotEmptyList(select.getWithItemsList())) {\n            newSelect.setWithItemsList(select.getWithItemsList());\n            select.setWithItemsList(null);\n        }\n        return newSelect;\n    }\n\n    /**\n     * 包装SetOperationList\n     *\n     * @param setOperationList\n     * @return\n     */\n    protected Select wrapSetOperationList(SetOperationList setOperationList) {\n        //获取最后一个plainSelect\n        Select setSelectBody = setOperationList.getSelects().get(setOperationList.getSelects().size() - 1);\n        if (!(setSelectBody instanceof PlainSelect)) {\n            throw new PageException(\"Unable to process the SQL, you can submit issues in GitHub for help.!\");\n        }\n        PlainSelect plainSelect = (PlainSelect) setSelectBody;\n        PlainSelect selectBody = new PlainSelect();\n        List<SelectItem<?>> selectItems = getSelectItems(plainSelect);\n        selectBody.setSelectItems(selectItems);\n\n        //设置fromIterm\n        ParenthesedSelect fromItem = new ParenthesedSelect();\n        fromItem.setSelect(setOperationList);\n        fromItem.setAlias(new Alias(WRAP_TABLE));\n        selectBody.setFromItem(fromItem);\n        //order by\n        if (isNotEmptyList(setOperationList.getOrderByElements())) {\n            selectBody.setOrderByElements(setOperationList.getOrderByElements());\n            setOperationList.setOrderByElements(null);\n        }\n        return selectBody;\n    }","sourceCodeStart":184,"sourceCodeEnd":220,"githubUrl":"https://github.com/pagehelper-org/Mybatis-PageHelper/blob/c692616c5bc95b41aa779b502f991825c8e5acbc/src/main/java/com/github/pagehelper/parser/defaults/DefaultSqlServerSqlParser.java#L184-L220","documentation":"PageHelper's SqlServer parser rewrites UNION/INTERSECT/EXCEPT (set operation) queries to add pagination (OFFSET/TOP). It takes the last SELECT in the set operation list and expects it to be a simple PlainSelect; if it is not (e.g. a nested set operation or another non-plain body), the parser cannot safely wrap it and throws this PageException.","triggerScenarios":"Executing a page query (PageHelper.startPage + query) against SQL Server whose statement is a set-operation list (UNION/UNION ALL/INTERSECT/EXCEPT/MINUS) where the final select body is itself not a PlainSelect — e.g. nested parentheses producing a nested SetOperationList, or JSqlParser parsing the SQL into an unexpected body type.","commonSituations":"Paging over UNION queries where the last UNION branch is itself parenthesized or contains another UNION (e.g. '(a UNION b) UNION c'); SQL Server dialect with complex generated SQL from ORM tools; older JSqlParser versions producing different AST shapes than expected.","solutions":["Restructure the SQL so the set operation is a flat list of plain SELECTs without nested/parenthesized set operations, e.g. 'SELECT ... UNION ALL SELECT ... UNION ALL SELECT ...'","Wrap the whole set operation in a subquery and page over that: SELECT * FROM (original union sql) t","Compute the count query separately (countColumn or a hand-written count MS) so PageHelper does not have to rewrite the set-operation SQL","If the SQL is valid but fails, check the JSqlParser version bundled with pagehelper and upgrade pagehelper (parser bugs are often fixed in newer releases)","Report the SQL to PageHelper's GitHub issues as the message suggests if it should be supported"],"exampleFix":"// before (nested set op fails to parse into PlainSelect)\nSELECT * FROM (SELECT a FROM t1 UNION SELECT a FROM t2) x UNION SELECT a FROM t3\n\n// after (flat set operation, each branch a plain select)\nSELECT a FROM t1 UNION SELECT a FROM t2 UNION SELECT a FROM t3","handlingStrategy":"validation","validationCode":"// Before calling the query with PageHelper.startPage, reject nested set operations:\nString normalized = sql.toUpperCase();\nif (normalized.matches(\"(?s).*UNION\\s*\\(.*\") || normalized.matches(\"(?s).*\\)\\s*UNION.*\")) {\n    throw new IllegalArgumentException(\"Wrap the set operation in a subquery before pagination: SELECT * FROM ( \" + sql + \" ) t\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    return mapper.selectPaged(params);\n} catch (PageException e) {\n    if (e.getMessage().startsWith(\"Unable to process the SQL\")) {\n        return mapper.selectFlatRewritten(params); // fallback to subquery-wrapped SQL\n    }\n    throw e;\n}","preventionTips":["Keep UNION/INTERSECT/EXCEPT queries flat — no parenthesized nested set operations","Prefer wrapping complex set SQL in an outer subquery and paging over that","Provide an explicit hand-written count query so the parser touches the union SQL less","Test pagination of every union query in CI"],"tags":["sql","sqlserver","pagination","parser"],"backgroundTag":"unsupported-operation","analyzedSha":"c692616c5bc95b41aa779b502f991825c8e5acbc","analyzedAt":"2026-09-08T03:50:49.192Z","contentChangedAt":"2026-09-08T03:50:49.192Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}