{"record":{"id":"6fae9fee520a5b71","repo":"apache/cassandra","slug":"non-primary-key-columns-can-only-be-restricted-wit","errorCode":null,"errorMessage":"Non-primary key columns can only be restricted with 'IS NOT NULL' (got: %s restricted illegally)","messagePattern":"Non-primary key columns can only be restricted with 'IS NOT NULL' \\(got: (.+?) restricted illegally\\)","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/schema/CreateViewStatement.java","lineNumber":332,"sourceCode":"                                      false,\n                                      false,\n                                      true,\n                                      true);\n\n        List<ColumnIdentifier> nonRestrictedPrimaryKeyColumns =\n            Lists.newArrayList(filter(primaryKeyColumns, name -> !restrictions.isRestricted(table.getColumn(name))));\n\n        if (!nonRestrictedPrimaryKeyColumns.isEmpty())\n        {\n            throw ire(\"Primary key columns %s must be restricted with 'IS NOT NULL' or otherwise\",\n                      join(\", \", transform(nonRestrictedPrimaryKeyColumns, ColumnIdentifier::toString)));\n        }\n\n        // See CASSANDRA-13798\n        Set<ColumnMetadata> restrictedNonPrimaryKeyColumns = restrictions.nonPKRestrictedColumns(false);\n        if (!restrictedNonPrimaryKeyColumns.isEmpty() && !MV_ALLOW_FILTERING_NONKEY_COLUMNS_UNSAFE.getBoolean())\n        {\n            throw ire(\"Non-primary key columns can only be restricted with 'IS NOT NULL' (got: %s restricted illegally)\",\n                      join(\",\", transform(restrictedNonPrimaryKeyColumns, ColumnMetadata::toString)));\n        }\n\n        /*\n         * Validate WITH params\n         */\n\n        attrs.validate();\n\n        if (attrs.hasOption(TableParams.Option.DEFAULT_TIME_TO_LIVE)\n            && attrs.getInt(TableParams.Option.DEFAULT_TIME_TO_LIVE.toString(), 0) != 0)\n        {\n            throw ire(\"Cannot set default_time_to_live for a materialized view. \" +\n                      \"Data in a materialized view always expire at the same time than \" +\n                      \"the corresponding data in the parent table.\");\n        }\n\n        /*","sourceCodeStart":314,"sourceCodeEnd":350,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/CreateViewStatement.java#L314-L350","documentation":"In a materialized view's WHERE clause, non-primary-key columns may only be restricted with IS NOT NULL (unless the unsafe flag MV_ALLOW_FILTERING_NONKEY_COLUMNS_UNSAFE is enabled). Other predicates on non-key columns would require view rows to be recomputed/moved on updates, which Cassandra refuses.","triggerScenarios":"CREATE MATERIALIZED VIEW ... WHERE regular_col = 5 (or any range/inequality on a regular column not in the view primary key), without the unsafe flag set.","commonSituations":"Users attempt to pre-filter views by value conditions (e.g. status = 'active') on regular columns, expecting SQL-view semantics.","solutions":["Change the restriction on the non-key column to IS NOT NULL only.","Move the regular column into the view primary key and restrict it (at most one non-PK base column is allowed).","Set the unsafe flag (MV_ALLOW_FILTERING_NONKEY_COLUMNS_UNSAFE) only if you fully understand the consistency implications.","Handle the value filtering at query time on the view."],"exampleFix":"// before\nCREATE MATERIALIZED VIEW mv AS SELECT id, status FROM t WHERE status = 'active' PRIMARY KEY (id, status);\n// after\nCREATE MATERIALIZED VIEW mv AS SELECT id, status FROM t WHERE id IS NOT NULL PRIMARY KEY (id, status);\n// then query with: SELECT * FROM mv WHERE status = 'active';","handlingStrategy":"validation","validationCode":"for (ColumnMetadata col : restrictedColumns) {\n    if (!col.isPrimaryKeyColumn() && !restrictionIsNotNullOnly(col))\n        throw new IllegalArgumentException(\"non-PK column \" + col.name + \" may only be restricted with IS NOT NULL\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Restrict non-key view columns with IS NOT NULL only.","Push value filters (equality/range) to read queries against the view."],"tags":["cassandra","cql","materialized-view","filtering"],"backgroundTag":"schema-validation-failed","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"}