{"record":{"id":"23ffb87e83ec5c28","repo":"MyCATApache/Mycat-Server","slug":"all-columns-in-group-by-clause-should-be-in-the-se","errorCode":null,"errorMessage":"all columns in group by clause should be in the selected column list.!${columns[i]}","messagePattern":"all columns in group by clause should be in the selected column list\\.!(.+?)","errorType":"exception","errorClass":"java.lang.IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/sqlengine/mpp/AbstractDataNodeMerge.java","lineNumber":124,"sourceCode":"        addPack(data);\n\n        return false;\n    }\n\n\n    /**\n     * 将Map对应的col字段集，返回row中对应的index数组\n     * @param columns\n     * @param toIndexMap\n     * @return\n     */\n    protected static int[] toColumnIndex(String[] columns, Map<String, ColMeta> toIndexMap) {\n        int[] result = new int[columns.length];\n        ColMeta curColMeta;\n        for (int i = 0; i < columns.length; i++) {\n            curColMeta = toIndexMap.get(StringUtil.removeBackquote(columns[i]).toUpperCase());\n            if (curColMeta == null) {\n                throw new IllegalArgumentException(\n                        \"all columns in group by clause should be in the selected column list.!\"\n                                + columns[i]);\n            }\n            result[i] = curColMeta.colIndex;\n        }\n        return result;\n    }\n\n    @Override\n    public abstract void run();\n\n    public abstract void onRowMetaData(Map<String, ColMeta> columToIndx, int fieldCount) throws IOException;\n\n    public void outputMergeResult(NonBlockingSession session, byte[] eof) {\n        addPack(END_FLAG_PACK);\n    }\n\n    public RouteResultset getRrs() {","sourceCodeStart":106,"sourceCodeEnd":142,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/sqlengine/mpp/AbstractDataNodeMerge.java#L106-L142","documentation":"AbstractDataNodeMerge.toColumnIndex maps GROUP BY column names to result-set indexes. If a GROUP BY column (after removing backquotes and uppercasing) is not present in the selected-column metadata map, it throws IllegalArgumentException: merging across data nodes requires every GROUP BY column to appear in the SELECT list.","triggerScenarios":"Executing a query whose GROUP BY references a column or expression that is not in the SELECT list, so the merged result metadata (toIndexMap) has no entry for it.","commonSituations":"GROUP BY on an aliased or computed column not selected; GROUP BY using original column name while SELECT uses a different alias; backquote/case mismatches that removeBackquote+uppercase do not resolve.","solutions":["Add the GROUP BY column to the SELECT list, e.g. SELECT dept, COUNT(*) FROM t GROUP BY dept.","Match SELECT and GROUP BY expressions exactly (same names/aliases).","Remove backquotes or fix case so the column name matches the selected column.","Simplify the query or perform grouping client-side if the expression cannot be selected."],"exampleFix":"// before\nSELECT COUNT(*) FROM orders GROUP BY user_id;\n// after\nSELECT user_id, COUNT(*) FROM orders GROUP BY user_id;","handlingStrategy":"validation","validationCode":"// ensure each group-by column is in the select list\nList<String> select = List.of(\"user_id\", \"cnt\");\nfor (String g : groupByCols) {\n    if (!select.stream().anyMatch(s -> s.replace(\"`\",\"\").equalsIgnoreCase(g)))\n        throw new IllegalArgumentException(\"GROUP BY column not selected: \" + g);\n}","typeGuard":null,"tryCatchPattern":"try { runGroupedQuery(sql); } catch (IllegalArgumentException e) { if (e.getMessage().contains(\"group by clause\")) rewriteQueryAddingGroupCols(); else throw e; }","preventionTips":["Always SELECT the columns you GROUP BY.","Keep aliases consistent between SELECT and GROUP BY.","Avoid grouping on expressions not present in SELECT."],"tags":["sql","group-by","mpp-merge"],"backgroundTag":"sql-query-failed","analyzedSha":"65f8d8beb752f935752f2a0eec0ab017facab9ef","analyzedAt":"2026-09-11T00:12:21.696Z","contentChangedAt":"2026-09-11T00:12:21.696Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}