{"record":{"id":"876a6ddfdb6794aa","repo":"apache/shardingsphere","slug":"either-key-generator-name-or-algorithm-segment-mus","errorCode":null,"errorMessage":"Either key generator name or algorithm segment must be provided.","messagePattern":"Either key generator name or algorithm segment must be provided\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/converter/ShardingKeyGenerateStrategyStatementConverter.java","lineNumber":65,"sourceCode":"                : new SequenceKeyGenerateStrategiesRuleConfiguration(keyGeneratorName, ((SequenceKeyGenerateStrategyDefinitionSegment) keyGenerateStrategySegment).getSequenceName());\n    }\n    \n    /**\n     * Get key generator name.\n     *\n     * @param strategyName strategy name\n     * @param keyGenerateStrategySegment key generate strategy segment\n     * @return key generator name\n     * @throws IllegalArgumentException when both key generator name and algorithm segment are absent\n     */\n    public static String getKeyGeneratorName(final String strategyName, final AbstractKeyGenerateStrategyDefinitionSegment keyGenerateStrategySegment) {\n        if (keyGenerateStrategySegment.getKeyGeneratorName().isPresent()) {\n            return keyGenerateStrategySegment.getKeyGeneratorName().get();\n        }\n        if (keyGenerateStrategySegment.getAlgorithmSegment().isPresent()) {\n            return createKeyGeneratorName(strategyName, keyGenerateStrategySegment.getAlgorithmSegment().get());\n        }\n        throw new IllegalArgumentException(\"Either key generator name or algorithm segment must be provided.\");\n    }\n    \n    private static String createKeyGeneratorName(final String strategyName, final AlgorithmSegment algorithmSegment) {\n        return String.format(\"%s_%s\", strategyName, algorithmSegment.getName()).toLowerCase();\n    }\n}\n","sourceCodeStart":47,"sourceCodeEnd":72,"githubUrl":"https://github.com/apache/shardingsphere/blob/e952770a215630a3659c75d64369168cd3e26b82/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/converter/ShardingKeyGenerateStrategyStatementConverter.java#L47-L72","documentation":"IllegalArgumentException thrown by ShardingKeyGenerateStrategyStatementConverter.getKeyGeneratorName when a key-generate strategy segment in a DistSQL statement provides neither a keyGeneratorName nor an inline algorithmSegment. The converter must return a generator name; with both optionals empty there is nothing to name or construct, so it rejects the malformed statement.","triggerScenarios":"CREATE/ALTER SHARDING TABLE RULE with a KEY_GENERATE_STRATEGY clause that specifies a column but omits both KEY_GENERATOR_NAME and the (GENERATED_KEY_ALGORITHM ...) segment; or a custom strategy definition segment parsed with both fields empty.","commonSituations":"Hand-written DistSQL with an incomplete KEY_GENERATE_STRATEGY; copy-paste from docs where the algorithm line was dropped; upgrading DistSQL syntax and losing the generator clause.","solutions":["Provide either a named generator (KEY_GENERATOR_NAME(snowflake)) or an inline algorithm segment (GENERATED_KEY_ALGORITHM(NAME=uuid)) in the strategy.","If you do not want key generation for the table, remove the KEY_GENERATE_STRATEGY clause entirely.","Validate the DistSQL against the current grammar before applying."],"exampleFix":"-- before: strategy lacks generator name and algorithm\nCREATE SHARDING TABLE RULE t_order (\n  DATANODES(\"ds_${0..1}.t_order_${0..3}\"),\n  SHARDING_COLUMN(order_id), TYPE(NAME=hash_mod, PROPERTIES(\"sharding-count\"=4)),\n  KEY_GENERATE_STRATEGY(COLUMN=order_id)\n);\n\n-- after: explicit generator\nCREATE SHARDING TABLE RULE t_order (\n  DATANODES(\"ds_${0..1}.t_order_${0..3}\"),\n  SHARDING_COLUMN(order_id), TYPE(NAME=hash_mod, PROPERTIES(\"sharding-count\"=4)),\n  KEY_GENERATE_STRATEGY(COLUMN=order_id, KEY_GENERATOR_NAME(snowflake))\n);","handlingStrategy":"validation","validationCode":"// Validate a parsed key-generate strategy before submission\nvoid validate(KeyGenerateStrategySegment s) {\n    if (s.getKeyGeneratorName().isEmpty() && s.getAlgorithmSegment().isEmpty()) {\n        throw new IllegalArgumentException(\"KEY_GENERATE_STRATEGY needs KEY_GENERATOR_NAME or GENERATED_KEY_ALGORITHM\");\n    }\n}","typeGuard":"boolean isCompleteKeyGenerateStrategy(final AbstractKeyGenerateStrategyDefinitionSegment s) {\n    return s.getKeyGeneratorName().isPresent() || s.getAlgorithmSegment().isPresent();\n}","tryCatchPattern":"try {\n    getKeyGeneratorName(strategyName, segment);\n} catch (final IllegalArgumentException ex) {\n    // report which strategy is incomplete so the DistSQL can be fixed\n}","preventionTips":["Use DistSQL linting (parse + validate) in deployment pipelines.","Keep documented templates for KEY_GENERATE_STRATEGY clauses."],"tags":["sharding","distsql","key-generate","validation","configuration"],"backgroundTag":null,"analyzedSha":"e952770a215630a3659c75d64369168cd3e26b82","analyzedAt":"2026-08-14T13:54:53.392Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}