{"record":{"id":"26342bac5a326fe0","repo":"apache/cassandra","slug":"invalid-value-for-initcond-of-type-s","errorCode":null,"errorMessage":"Invalid value for INITCOND of type %s","messagePattern":"Invalid value for INITCOND of type (.+?)","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/schema/CreateAggregateStatement.java","lineNumber":187,"sourceCode":"        /*\n         * Validate initial condition\n         */\n\n        ByteBuffer initialValue = null;\n        if (null != rawInitialValue)\n        {\n            String term = rawInitialValue.toString();\n            initialValue = Term.asBytes(keyspaceName, term, stateType);\n\n            if (null != initialValue)\n            {\n                try\n                {\n                    stateType.validate(initialValue);\n                }\n                catch (MarshalException e)\n                {\n                    throw ire(\"Invalid value for INITCOND of type %s\", stateType.asCQL3Type());\n                }\n            }\n\n            // Converts initcond to a CQL literal and parse it back to avoid another CASSANDRA-11064\n            String initialValueString = stateType.asCQL3Type().toCQLLiteral(initialValue);\n            if (!Objects.equal(initialValue, stateType.asCQL3Type().fromCQLLiteral(initialValueString)))\n                throw new AssertionError(String.format(\"CQL literal '%s' (from type %s) parsed with a different value\", initialValueString, stateType.asCQL3Type()));\n\n            if (Constants.NULL_LITERAL != rawInitialValue && isNullOrEmpty(stateType, initialValue))\n                throw ire(\"INITCOND must not be empty for all types except TEXT, ASCII, BLOB\");\n        }\n\n        if (!((UDFunction) stateFunction).isCalledOnNullInput() && null == initialValue)\n        {\n            throw ire(\"Cannot create aggregate '%s' without INITCOND because state function %s does not accept 'null' arguments\",\n                      aggregateName,\n                      stateFunctionName);\n        }","sourceCodeStart":169,"sourceCodeEnd":205,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/CreateAggregateStatement.java#L169-L205","documentation":"Thrown when the literal supplied as INITCOND cannot be validated as a value of the aggregate's STYPE (an AbstractType.MarshalException was raised while validating the serialized bytes). The initcond must be a legal value of the state type.","triggerScenarios":"CREATE AGGREGATE ... INITCOND <value> where <value> does not marshal to the declared STYPE, e.g. a string for an int state type, an out-of-domain value for the type, or a wrongly shaped collection/tuple literal.","commonSituations":"Copy-pasted initcond from an aggregate with a different STYPE; type changed in STYPE but old INITCOND left in place; locale/precision issues with decimal or floating-point literals.","solutions":["Align the INITCOND literal with the declared STYPE (e.g. 0 for int, 0.0 for double, quoted string for text).","Update STYPE and INITCOND together when changing the state type.","Omit INITCOND if the state function is CALLED ON NULL INPUT and null initialization is acceptable."],"exampleFix":"// before\nCREATE AGGREGATE ks.sum(int) SFUNC add_one STYPE int INITCOND 'zero';\n// after\nCREATE AGGREGATE ks.sum(int) SFUNC add_one STYPE int INITCOND 0;","handlingStrategy":"validation","validationCode":"// validate initcond literal against STYPE before issuing DDL\ntry {\n    Object v = stateType.asCQL3Type().fromCQLLiteral(initcondLiteral);\n    stateType.validate(stateType.getSerializer().serialize(v));\n} catch (MarshalException e) {\n    throw new IllegalArgumentException(\"INITCOND \" + initcondLiteral + \" invalid for STYPE \" + stateType);\n}","typeGuard":null,"tryCatchPattern":"try { session.execute(ddl); }\ncatch (InvalidRequestException e) {\n    if (e.getMessage().startsWith(\"Invalid value for INITCOND\")) { /* fix literal to match STYPE */ }\n    else throw e;\n}","preventionTips":["Derive INITCOND from the STYPE in generated DDL (0, 0.0, '', etc.).","Update STYPE and INITCOND together in schema migrations.","Test DDL against a local cluster before applying to production."],"tags":["cql","initcond","type-mismatch"],"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-17T15:17:12.973Z"}