apache/cassandra · error · InvalidRequestException

Cannot use aliases when defining a materialized view

Error message

Cannot use aliases when defining a materialized view (got %s)

What it means

Materialized view SELECT clauses may only project raw column names; column aliases (SELECT col AS something) are not supported because view columns must map 1:1 to base table columns. The statement throws when any selector has a non-null alias.

Solutions

  1. Remove the AS alias so the selector is the bare column name
  2. Keep base table column names in the view; rename only via a new base table maintained by the application

Example fix

// before
CREATE MATERIALIZED VIEW mv AS SELECT id AS user_id, ts FROM base WHERE id IS NOT NULL;
// after
CREATE MATERIALIZED VIEW mv AS SELECT id, ts FROM base WHERE id IS NOT NULL;
Defensive patterns

Strategy: validation

Validate before calling

for (String col : selectColumns) if (col.toLowerCase().contains(" as ")) throw new IllegalArgumentException("Aliases not allowed in MV select: " + col);

Try / catch

try { session.execute(createMvStmt); } catch (InvalidQueryException e) { if (e.getMessage().contains("Cannot use aliases")) { /* strip aliases and retry */ } }

Prevention

When it happens

Trigger: CREATE MATERIALIZED VIEW ... AS SELECT pk AS new_name, ... FROM base ...;

Common situations: Users copy SQL view habits of renaming columns; code generators that alias every projection; pasting SELECT lists from analytics queries.

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/872a20f6f11b5e51. Report an issue: GitHub.

Appendix: source

Thrown at src/java/org/apache/cassandra/cql3/statements/schema/CreateViewStatement.java:215

        if (table.params.pendingDrop)
            throw ire("Cannot create materialized view '%s' for base table " +
                      "'%s' as it is being dropped.",
                      viewName, tableName);

        /*
         * Process SELECT clause
         */

        Set<ColumnIdentifier> selectedColumns = new HashSet<>();

        if (rawColumns.isEmpty()) // SELECT *
            table.columns().forEach(c -> selectedColumns.add(c.name));

        rawColumns.forEach(selector ->
        {
            if (null != selector.alias)
                throw ire("Cannot use aliases when defining a materialized view (got %s)", selector);

            if (!(selector.selectable instanceof Selectable.RawIdentifier))
                throw ire("Can only select columns by name when defining a materialized view (got %s)", selector.selectable);

            // will throw IRE if the column doesn't exist in the base table
            Selectable.RawIdentifier rawIdentifier = (Selectable.RawIdentifier) selector.selectable;
            ColumnMetadata column = rawIdentifier.columnMetadata(table);

            selectedColumns.add(column.name);
        });

        selectedColumns.stream()
                       .map(table::getColumn)
                       .filter(ColumnMetadata::isStatic)
                       .findAny()
                       .ifPresent(c -> { throw ire("Cannot include static column '%s' in materialized view '%s'", c, viewName); });

        /*

View on GitHub (pinned to 88fd0f6a0e)