apache/cassandra · error · InvalidRequestException

Durations are not allowed inside sets: %s

Error message

Durations are not allowed inside sets: %s

What it means

A set literal (or inferred set type) used in the selection clause contains duration elements. Cassandra forbids durations inside sets because duration is not orderable, which set elements must be. The request is rejected at prepare time.

Source

Thrown at src/java/org/apache/cassandra/cql3/selection/Selectable.java:1026

                                          VariableSpecifications boundNames)
        {
            AbstractType<?> type = getExactTypeIfKnown(cfm.keyspace);
            if (type == null)
            {
                type = expectedType;
                if (type == null)
                    throw invalidRequest("Cannot infer type for term %s in selection clause (try using a cast to force a type)",
                                         this);
            }

            // The parser treats empty Maps as Sets so if the type is a MapType we know that the Map is empty
            if (type instanceof MapType)
                return MapSelector.newFactory(type, Collections.emptyList());

            SetType<?> setType = (SetType<?>) type;

            if (setType.getElementsType() == DurationType.instance)
                throw invalidRequest("Durations are not allowed inside sets: %s", setType.asCQL3Type());

            List<AbstractType<?>> expectedTypes = new ArrayList<>(selectables.size());
            for (int i = 0, m = selectables.size(); i < m; i++)
                expectedTypes.add(setType.getElementsType());

            SelectorFactories factories = createFactoriesAndCollectColumnDefinitions(selectables,
                                                                                     expectedTypes,
                                                                                     cfm,
                                                                                     defs,
                                                                                     boundNames);

            return SetSelector.newFactory(type, factories);
        }

        @Override
        public AbstractType<?> getExactTypeIfKnown(String keyspace)
        {
            return Sets.getExactSetTypeIfKnown(selectables, p -> p.getExactTypeIfKnown(keyspace));

View on GitHub (pinned to 88fd0f6a0e)

Solutions

  1. Use a list<duration> (or map with non-key durations) instead of a set, since lists do not require ordering of elements.
  2. Change element type to an orderable duration representation, e.g. set<bigint> of milliseconds.
  3. If durations come from bind markers, cast the term to list<duration> instead of set<duration>.

Example fix

// before
SELECT {5s, 10s} FROM t;
// after
SELECT [5s, 10s] FROM t; -- list<duration>
Defensive patterns

Strategy: validation

Validate before calling

if (elementType.equals(DurationType.instance) && containerType instanceof SetType) throw new IllegalArgumentException("duration not allowed in set; use list<duration>");

Try / catch

try { session.execute(stmt); } catch (InvalidRequestException e) { if (e.getMessage().startsWith("Durations are not allowed inside sets")) { /* switch to list/map-value */ } else throw e; }

Prevention

When it happens

Trigger: `SELECT {1h, 2h} FROM t` — a set literal of durations; an empty or ambiguous literal inferred as set<duration>; passing a term cast to a set type whose element type is duration.

Common situations: Trying to store multiple durations in one column via a set; confusing list<duration> (allowed) with set<duration> (forbidden); typos like {5s,10s} where a list was intended.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10). Data as JSON: /api/errors/6ab2548c1985689c. Report an issue: GitHub.