{"record":{"id":"6994f1d98c966175","repo":"MyCATApache/Mycat-Server","slug":"all-columns-in-order-by-clause-should-be-in-the-se-6994f1","errorCode":null,"errorMessage":"all columns in order by clause should be in the selected column list!${entry.getKey()}","messagePattern":"all columns in order 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/DataNodeMergeManager.java","lineNumber":214,"sourceCode":"    \t\t\tmergeColsIndex = new int[mergColsArrays.length];\n    \t\t\tfor(int i = 0;i<mergColsArrays.length;i++){\n    \t\t\t\tmergeColsIndex[i] = mergColsArrays[i].colMeta.colIndex;\n    \t\t\t}\n    \t\t\tArrays.sort(mergeColsIndex);\n    \t\t}\n        }\n\n\n        if (rrs.getOrderByCols() != null) {\n            LinkedHashMap<String, Integer> orders = rrs.getOrderByCols();\n            orderCols = new OrderCol[orders.size()];\n            int i = 0;\n            for (Map.Entry<String, Integer> entry : orders.entrySet()) {\n                String key = StringUtil.removeBackquote(entry.getKey()\n                        .toUpperCase());\n                ColMeta colMeta = columToIndx.get(key);\n                if (colMeta == null) {\n                    throw new IllegalArgumentException(\n                            \"all columns in order by clause should be in the selected column list!\"\n                                    + entry.getKey());\n                }\n                orderCols[i++] = new OrderCol(colMeta, entry.getValue());\n            }\n\n            /**\n             * 构造全局排序器\n             */\n            schema = new StructType(columToIndx,fieldCount);\n            schema.setOrderCols(orderCols);\n\n            prefixComputer = new RowPrefixComputer(schema);\n\n//            if(orderCols.length>0\n//                    && orderCols[0].getOrderType()\n//                    == OrderCol.COL_ORDER_TYPE_ASC){\n//                prefixComparator = PrefixComparators.LONG;","sourceCodeStart":196,"sourceCodeEnd":232,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/sqlengine/mpp/DataNodeMergeManager.java#L196-L232","documentation":"DataNodeMergeManager.onRowMetaData performs the same ORDER BY column validation as DataMergeService: each ORDER BY entry must exist in the merged column metadata map, otherwise IllegalArgumentException is thrown. This variant runs in the manager-based merge path for queries fanned out to multiple data nodes.","triggerScenarios":"Multi-node SELECT with ORDER BY on a column absent from the SELECT list or with a name that does not match the result metadata after backquote removal and uppercasing.","commonSituations":"ORDER BY on a non-selected column in sharded queries; ORDER BY with table-qualified names like t.id while metadata exposes 'id'; alias case mismatch.","solutions":["Add the ORDER BY column to the SELECT list.","Reference the selected alias exactly in ORDER BY.","Normalize case/backquotes so names match the result metadata.","Push ordering to a single-node query if possible."],"exampleFix":"// before\nSELECT id FROM t ORDER BY t.create_time;\n// after\nSELECT id, create_time FROM t ORDER BY create_time;","handlingStrategy":"validation","validationCode":"// same guard as DataMergeService, applied before dispatch\nfor (String o : orders.keySet()) {\n    if (!selectedColumns.contains(o.replace(\"`\",\"\").toUpperCase()))\n        throw new IllegalArgumentException(\"ORDER BY column not selected: \" + o);\n}","typeGuard":null,"tryCatchPattern":"try { mergeManager.handle(rs); } catch (IllegalArgumentException e) { if (e.getMessage().contains(\"order by clause\")) { fixSqlSelectList(); } else throw e; }","preventionTips":["Standardize on lowercase column names to avoid case mismatches.","Audit generated SQL so SELECT covers all ORDER BY columns.","Test sharded queries against Mycat before production."],"tags":["sql","order-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"}