{"record":{"id":"121cacdef7219ad4","repo":"apache/cassandra","slug":"cannot-filter-top-k-tables-without-specifying-a-so","errorCode":null,"errorMessage":"cannot filter top K tables without specifying a sort key.","messagePattern":"cannot filter top K tables without specifying a sort key\\.","errorType":"validation","errorClass":"java.lang.IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/tools/nodetool/TableStats.java","lineNumber":102,"sourceCode":"    private boolean locationCheck = false;\n\n    @Override\n    public void execute(NodeProbe probe)\n    {\n        if (!outputFormat.isEmpty() && !\"json\".equals(outputFormat) && !\"yaml\".equals(outputFormat))\n        {\n            throw new IllegalArgumentException(\"arguments for -F are json,yaml only.\");\n        }\n\n        if (!sortKey.isEmpty() && !Arrays.asList(StatsTableComparator.supportedSortKeys).contains(sortKey))\n        {\n            throw new IllegalArgumentException(String.format(\"argument for sort must be one of: %s\",\n                                               String.join(\", \", StatsTableComparator.supportedSortKeys)));\n        }\n\n        if (top > 0 && sortKey.isEmpty())\n        {\n            throw new IllegalArgumentException(\"cannot filter top K tables without specifying a sort key.\");\n        }\n\n        if (top < 0)\n        {\n            throw new IllegalArgumentException(\"argument for top must be a positive integer.\");\n        }\n\n        StatsHolder holder = new TableStatsHolder(probe, humanReadable, ignore, tableNames, sortKey, top, locationCheck);\n        // print out the keyspace and table statistics\n        StatsPrinter printer = TableStatsPrinter.from(outputFormat, !sortKey.isEmpty());\n        printer.print(holder, probe.output().out);\n    }\n\n}\n","sourceCodeStart":84,"sourceCodeEnd":117,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/tools/nodetool/TableStats.java#L84-L117","documentation":"The --top N option of TableStats only makes sense together with a sort key, because 'top K' is defined by a sort order. If -t/--top is given a positive value while no sort key was supplied, execute() throws IllegalArgumentException. The ordering is what makes 'top' meaningful.","triggerScenarios":"`nodetool tablestats --top 5` without also passing a valid -s/--sort-key.","commonSituations":"Users assuming --top works standalone; scripts copied from examples that included -s elsewhere; documentation examples that omitted the sort flag.","solutions":["Add a valid -s/--sort-key alongside --top (e.g. `nodetool tablestats -s read_latency_ms --top 5`).","Remove --top if you want the full unsorted listing.","Consult `nodetool help tablestats` for the exact flag pairing."],"exampleFix":"// before\nnodetool tablestats --top 5\n// after\nnodetool tablestats -s read_latency_ms --top 5","handlingStrategy":"validation","validationCode":"if (top > 0 && (sortKey == null || sortKey.isEmpty()))\n    throw new IllegalArgumentException(\"--top requires -s <sortKey>\");","typeGuard":null,"tryCatchPattern":"try { runNodetool(\"tablestats\", \"--top\", String.valueOf(top)); }\ncatch (IllegalArgumentException e) { if (e.getMessage().contains(\"top K tables\")) { /* add sort key and retry */ } else throw e; }","preventionTips":["Always pair --top with -s in scripts and documentation.","Validate flag combinations in wrapper functions before invoking nodetool.","Test CLI wrappers with all flag permutations used in production."],"tags":["nodetool","cli","argument-combination"],"backgroundTag":"missing-required-argument","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}