{"record":{"id":"5a581b3288490cb1","repo":"apache/cassandra","slug":"index-s-is-not-in-the-same-keyspace-as-the-querie","errorCode":null,"errorMessage":"Index %s is not in the same keyspace as the queried table.","messagePattern":"Index (.+?) is not in the same keyspace as the queried table\\.","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/db/filter/IndexHints.java","lineNumber":437,"sourceCode":"\n        Set<IndexMetadata> indexes = new HashSet<>(indexNames.size());\n\n        for (QualifiedName indexName : indexNames)\n        {\n            IndexMetadata index = fetchIndex(indexName, table, indexRegistry);\n            indexes.add(index);\n        }\n\n        return indexes;\n    }\n\n    private static IndexMetadata fetchIndex(QualifiedName indexName, TableMetadata table, IndexRegistry indexRegistry)\n    {\n        String name = indexName.getName();\n        String keyspace = indexName.getKeyspace();\n\n        if (keyspace != null && !table.keyspace.equals(keyspace))\n            throw new InvalidRequestException(format(WRONG_KEYSPACE_ERROR, indexName));\n\n        Index index = indexRegistry.getIndexByName(name);\n        if (index == null)\n            throw new InvalidRequestException(format(MISSING_INDEX_ERROR, table.name, name));\n\n        return index.getIndexMetadata();\n    }\n\n    /**\n     * Returns a comparator of index query plans based on which one has the most included indexes, so it can be used to\n     * select the plans that satisfy the index hints first, and the plans that are closest to satisfy them later.\n     *\n     * @return a comparator of index query plans based on which one has the most included indexes\n     */\n    public Comparator<Index.QueryPlan> comparator()\n    {\n        return Comparator.comparing(plan -> Sets.intersection(included, metadata(plan.getIndexes())).size());\n    }","sourceCodeStart":419,"sourceCodeEnd":455,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/db/filter/IndexHints.java#L419-L455","documentation":"Thrown as an InvalidRequestException when a query references an index with an explicit keyspace qualifier that does not match the keyspace of the table being queried. Cassandra requires that a named index belong to the same keyspace as the table, since indexes live in the table's keyspace. This guard in IndexHints.fetchIndex prevents cross-keyspace index resolution.","triggerScenarios":"Executing a SELECT with a USING INDEX (or ALLOW FILTERING index hint) clause like SELECT ... USING INDEX \"ks2.idx\" FROM ks1.tbl where the qualified index name's keyspace differs from table.keyspace.","commonSituations":"Copy-pasting queries between keyspaces without updating the index qualifier; multi-keyspace environments where identical index names exist in several keyspaces; client tools that template in a default keyspace for the index name.","solutions":["Remove the keyspace qualifier from the index name, or qualify it with the same keyspace as the queried table","USE the table's keyspace and reference the index by bare name","Recreate the index in the keyspace of the table if it genuinely doesn't exist there"],"exampleFix":"// before\nSELECT * FROM ks1.users USING INDEX ks2.users_email_idx;\n// after\nSELECT * FROM ks1.users USING INDEX ks1.users_email_idx;","handlingStrategy":"validation","validationCode":"if (indexName.getKeyspace() != null && !table.keyspace.equals(indexName.getKeyspace()))\n    throw new InvalidRequestException(\"Index \" + indexName + \" is not in keyspace \" + table.keyspace);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always qualify index names with the same keyspace as the queried table","Keep queries generated from config/templates keyspace-consistent","Test CQL statements against the target keyspace before production rollout"],"tags":["cql","invalid-request","keyspace"],"backgroundTag":"invalid-argument-value","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}