{"record":{"id":"7c6ce75c469af377","repo":"apache/cassandra","slug":"durations-are-not-allowed-as-map-keys-s","errorCode":null,"errorMessage":"Durations are not allowed as map keys: %s","messagePattern":"Durations are not allowed as map keys: (.+?)","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/selection/Selectable.java","lineNumber":1143,"sourceCode":"                    throw invalidRequest(\"Cannot infer type for term %s in selection clause (try using a cast to force a type)\",\n                                         this);\n            }\n\n            if (type.isUDT())\n                return newUdtSelectorFactory(cfm, expectedType, defs, boundNames);\n\n            return newMapSelectorFactory(cfm, defs, boundNames, type);\n        }\n\n        private Factory newMapSelectorFactory(TableMetadata cfm,\n                                              List<ColumnMetadata> defs,\n                                              VariableSpecifications boundNames,\n                                              AbstractType<?> type)\n        {\n            MapType<?, ?> mapType = (MapType<?, ?>) type;\n\n            if (mapType.getKeysType() == DurationType.instance)\n                throw invalidRequest(\"Durations are not allowed as map keys: %s\", mapType.asCQL3Type());\n\n            return MapSelector.newFactory(type, getMapEntries(cfm).stream()\n                                                                  .map(p -> Pair.create(p.left.newSelectorFactory(cfm, mapType.getKeysType(), defs, boundNames),\n                                                                                        p.right.newSelectorFactory(cfm, mapType.getValuesType(), defs, boundNames)))\n                                                                  .collect(Collectors.toList()));\n        }\n\n        private Factory newUdtSelectorFactory(TableMetadata cfm,\n                                              AbstractType<?> expectedType,\n                                              List<ColumnMetadata> defs,\n                                              VariableSpecifications boundNames)\n        {\n            UserType ut = (UserType) expectedType;\n            Map<FieldIdentifier, Factory> factories = new LinkedHashMap<>(ut.size());\n\n            for (Pair<Selectable.Raw, Selectable.Raw> raw : raws)\n            {\n                if (!(raw.left instanceof RawIdentifier))","sourceCodeStart":1125,"sourceCodeEnd":1161,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/selection/Selectable.java#L1125-L1161","documentation":"A map literal/type in the selection clause uses duration as the key type. Durations cannot be map keys because they are not orderable, which map key ordering requires. The request is rejected at prepare time.","triggerScenarios":"`SELECT {1h:'a'} FROM t` — a map literal with duration keys; a term cast to map<duration, X>; schema where a map column's key type is duration and the literal is selected.","commonSituations":"Attempting to index durations by map keys; confusing map<duration,X> (invalid) with map<X,duration> (valid); typos swapping key/value types in the cast.","solutions":["Move durations to the value side: map<text, duration> with durations as values.","Use list<duration> or frozen<list<duration>> if an ordered container of durations is needed.","Encode durations as orderable values (e.g. bigint seconds) for keys."],"exampleFix":"// before\nSELECT {5s:'a', 10s:'b'} FROM t; -- duration keys\n// after\nSELECT {'a':5s, 'b':10s} FROM t; -- durations as values","handlingStrategy":"validation","validationCode":"if (mapType.getKeysType().equals(DurationType.instance)) throw new IllegalArgumentException(\"duration cannot be a map key\");","typeGuard":null,"tryCatchPattern":"try { session.execute(stmt); } catch (InvalidRequestException e) { if (e.getMessage().startsWith(\"Durations are not allowed as map keys\")) { /* move durations to values */ } else throw e; }","preventionTips":["Use map<orderable-type, duration>; never duration keys.","Encode durations as bigint/timestamp if keying is required.","Add schema-level linting that rejects duration key types."],"tags":["cql","duration","map","unsupported-type"],"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"}