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

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

Error message

all columns in group by clause should be in the selected column list.!${columns[i]}

What it means

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.

Solutions

  1. Add the GROUP BY column to the SELECT list, e.g. SELECT dept, COUNT(*) FROM t GROUP BY dept.
  2. Match SELECT and GROUP BY expressions exactly (same names/aliases).
  3. Remove backquotes or fix case so the column name matches the selected column.
  4. Simplify the query or perform grouping client-side if the expression cannot be selected.

Example fix

// before
SELECT COUNT(*) FROM orders GROUP BY user_id;
// after
SELECT user_id, COUNT(*) FROM orders GROUP BY user_id;
Defensive patterns

Strategy: validation

Validate before calling

// ensure each group-by column is in the select list
List<String> select = List.of("user_id", "cnt");
for (String g : groupByCols) {
    if (!select.stream().anyMatch(s -> s.replace("`","").equalsIgnoreCase(g)))
        throw new IllegalArgumentException("GROUP BY column not selected: " + g);
}

Try / catch

try { runGroupedQuery(sql); } catch (IllegalArgumentException e) { if (e.getMessage().contains("group by clause")) rewriteQueryAddingGroupCols(); else throw e; }

Prevention

When it happens

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

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

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/23ffb87e83ec5c28. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/io/mycat/sqlengine/mpp/AbstractDataNodeMerge.java:124

        addPack(data);

        return false;
    }


    /**
     * 将Map对应的col字段集,返回row中对应的index数组
     * @param columns
     * @param toIndexMap
     * @return
     */
    protected static int[] toColumnIndex(String[] columns, Map<String, ColMeta> toIndexMap) {
        int[] result = new int[columns.length];
        ColMeta curColMeta;
        for (int i = 0; i < columns.length; i++) {
            curColMeta = toIndexMap.get(StringUtil.removeBackquote(columns[i]).toUpperCase());
            if (curColMeta == null) {
                throw new IllegalArgumentException(
                        "all columns in group by clause should be in the selected column list.!"
                                + columns[i]);
            }
            result[i] = curColMeta.colIndex;
        }
        return result;
    }

    @Override
    public abstract void run();

    public abstract void onRowMetaData(Map<String, ColMeta> columToIndx, int fieldCount) throws IOException;

    public void outputMergeResult(NonBlockingSession session, byte[] eof) {
        addPack(END_FLAG_PACK);
    }

    public RouteResultset getRrs() {

View on GitHub (pinned to 65f8d8beb7)