{"record":{"id":"14e8e4f1ac895c21","repo":"apache/cassandra","slug":"primary-key-columns-s-must-be-restricted-with-is","errorCode":null,"errorMessage":"Primary key columns %s must be restricted with 'IS NOT NULL' or otherwise","messagePattern":"Primary key columns (.+?) must be restricted with 'IS NOT NULL' or otherwise","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/schema/CreateViewStatement.java","lineNumber":324,"sourceCode":"            new StatementRestrictions(state,\n                                      StatementType.SELECT,\n                                      table,\n                                      IndexHints.NONE,\n                                      whereClause,\n                                      VariableSpecifications.empty(),\n                                      Collections.emptyList(),\n                                      null,\n                                      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)","sourceCodeStart":306,"sourceCodeEnd":342,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/CreateViewStatement.java#L306-L342","documentation":"Every primary key column of a materialized view must be explicitly restricted in the WHERE clause - either bound to a constant or restricted with IS NOT NULL. Unrestricted view key columns would be null-able and cannot form valid view row identities.","triggerScenarios":"CREATE MATERIALIZED VIEW ... PRIMARY KEY (a, b) WHERE a IS NOT NULL, omitting a restriction (e.g. IS NOT NULL or equality) for column b.","commonSituations":"Users forget one of several key columns when writing the view WHERE clause, especially after adding columns to the PRIMARY KEY clause.","solutions":["Add 'col IS NOT NULL' for each unrestricted primary key column in the WHERE clause.","Restrict the column with an equality condition instead (e.g. b = 'x').","Remove the column from the view primary key if it should not be restricted."],"exampleFix":"// before\nCREATE MATERIALIZED VIEW mv AS SELECT a, b, c FROM t WHERE a IS NOT NULL PRIMARY KEY (a, b, c);\n// after\nCREATE MATERIALIZED VIEW mv AS SELECT a, b, c FROM t WHERE a IS NOT NULL AND b IS NOT NULL AND c IS NOT NULL PRIMARY KEY (a, b, c);","handlingStrategy":"validation","validationCode":"List<String> unrestricted = viewPkColumns.stream()\n    .filter(c -> !whereRestricts(c))\n    .collect(toList());\nif (!unrestricted.isEmpty())\n    throw new IllegalArgumentException(\"add IS NOT NULL for: \" + unrestricted);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["After writing the view PRIMARY KEY, verify each column appears in WHERE (IS NOT NULL or equality).","Regenerate the WHERE clause whenever the view key changes."],"tags":["cassandra","cql","materialized-view","where-clause"],"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-14T16:17:12.679Z"}