MyCATApache/Mycat-Server · error · java.lang.IllegalArgumentException

all columns in order by clause should be in the selected…

Error message

all columns in order by clause should be in the selected column list!${entry.getKey()}

What it means

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.

Solutions

  1. Add the ORDER BY column to the SELECT list.
  2. Reference the selected alias exactly in ORDER BY.
  3. Normalize case/backquotes so names match the result metadata.
  4. Push ordering to a single-node query if possible.

Example fix

// before
SELECT id FROM t ORDER BY t.create_time;
// after
SELECT id, create_time FROM t ORDER BY create_time;
Defensive patterns

Strategy: validation

Validate before calling

// same guard as DataMergeService, applied before dispatch
for (String o : orders.keySet()) {
    if (!selectedColumns.contains(o.replace("`","").toUpperCase()))
        throw new IllegalArgumentException("ORDER BY column not selected: " + o);
}

Try / catch

try { mergeManager.handle(rs); } catch (IllegalArgumentException e) { if (e.getMessage().contains("order by clause")) { fixSqlSelectList(); } else throw e; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "query failed", "%w: SQL error" — wrapped database query errors in Go libraries explained — this error's family across 3 libraries.

Related errors


AI-assisted analysis of MyCATApache/Mycat-Server@65f8d8beb7 (2026-09-11). Data as JSON: /api/errors/6994f1d98c966175. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/io/mycat/sqlengine/mpp/DataNodeMergeManager.java:214

    			mergeColsIndex = new int[mergColsArrays.length];
    			for(int i = 0;i<mergColsArrays.length;i++){
    				mergeColsIndex[i] = mergColsArrays[i].colMeta.colIndex;
    			}
    			Arrays.sort(mergeColsIndex);
    		}
        }


        if (rrs.getOrderByCols() != null) {
            LinkedHashMap<String, Integer> orders = rrs.getOrderByCols();
            orderCols = new OrderCol[orders.size()];
            int i = 0;
            for (Map.Entry<String, Integer> entry : orders.entrySet()) {
                String key = StringUtil.removeBackquote(entry.getKey()
                        .toUpperCase());
                ColMeta colMeta = columToIndx.get(key);
                if (colMeta == null) {
                    throw new IllegalArgumentException(
                            "all columns in order by clause should be in the selected column list!"
                                    + entry.getKey());
                }
                orderCols[i++] = new OrderCol(colMeta, entry.getValue());
            }

            /**
             * 构造全局排序器
             */
            schema = new StructType(columToIndx,fieldCount);
            schema.setOrderCols(orderCols);

            prefixComputer = new RowPrefixComputer(schema);

//            if(orderCols.length>0
//                    && orderCols[0].getOrderType()
//                    == OrderCol.COL_ORDER_TYPE_ASC){
//                prefixComparator = PrefixComparators.LONG;

View on GitHub (pinned to 65f8d8beb7)