{"record":{"id":"cef8f7e9bc3a9f7f","repo":"apache/shardingsphere","slug":"21","errorCode":"21","errorMessage":"Found different types for sharding value '%s'.","messagePattern":"Found different types for sharding value '(.+?)'\\.","errorType":"exception","errorClass":"ShardingValueDataTypeException","httpStatus":null,"severity":"error","filePath":"features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/WhereClauseShardingConditionEngine.java","lineNumber":145,"sourceCode":"                    continue;\n                }\n                result.computeIfAbsent(column, unused -> new LinkedList<>()).add(shardingConditionValue.get());\n            }\n        }\n        return result;\n    }\n    \n    private ShardingCondition createShardingCondition(final Map<HashColumn, Collection<ShardingConditionValue>> shardingConditionValues) {\n        ShardingCondition result = new ShardingCondition();\n        for (Entry<HashColumn, Collection<ShardingConditionValue>> entry : shardingConditionValues.entrySet()) {\n            try {\n                ShardingConditionValue shardingConditionValue = mergeShardingConditionValues(entry.getKey(), entry.getValue());\n                if (shardingConditionValue instanceof AlwaysFalseShardingConditionValue) {\n                    return new AlwaysFalseShardingCondition();\n                }\n                result.getValues().add(shardingConditionValue);\n            } catch (final ClassCastException ignored) {\n                throw new ShardingValueDataTypeException(entry.getKey());\n            }\n        }\n        return result;\n    }\n    \n    @SuppressWarnings({\"unchecked\", \"rawtypes\"})\n    private ShardingConditionValue mergeShardingConditionValues(final HashColumn column, final Collection<ShardingConditionValue> shardingConditionValues) {\n        Collection<Comparable<?>> listValue = null;\n        Range<Comparable<?>> rangeValue = null;\n        Set<Integer> parameterMarkerIndexes = new HashSet<>();\n        for (ShardingConditionValue each : shardingConditionValues) {\n            parameterMarkerIndexes.addAll(each.getParameterMarkerIndexes());\n            if (each instanceof ListShardingConditionValue) {\n                listValue = mergeListShardingValues(column, ((ListShardingConditionValue) each).getValues(), listValue);\n                if (listValue.isEmpty()) {\n                    return new AlwaysFalseShardingConditionValue();\n                }\n            } else if (each instanceof RangeShardingConditionValue) {","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/apache/shardingsphere/blob/e952770a215630a3659c75d64369168cd3e26b82/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/WhereClauseShardingConditionEngine.java#L127-L163","documentation":"ShardingValueDataTypeException ('Found different types for sharding value') is thrown by WhereClauseShardingConditionEngine when merging multiple ShardingConditionValues for the same hash column fails with ClassCastException. If a column has predicates whose literal/parameter types are mutually incompatible (e.g. one Integer and one String for the same sharding column), the merge in mergeShardingConditionValues cannot produce one comparable value and the catch block converts the failure into this diagnostic exception naming the column.","triggerScenarios":"A WHERE clause with two conditions on the same sharding column of incompatible Java types, e.g. WHERE order_id = 101 AND order_id = 'abc', or prepared-statement parameters bound with mixed types for the same column; also in-list plus range merges whose element types clash.","commonSituations":"String/numeric type drift between application and schema (VARCHAR sharding column compared to numbers), ORMs binding different parameter types for the same column in one statement, or dialect differences where literals parse to different types.","solutions":["Make every predicate on the sharding column use the same type as the column definition (cast literals/parameters, e.g. WHERE order_id = '101' consistently).","Fix ORM mappings so the same bind parameter type is used for the sharding column everywhere in the statement.","If the types are legitimately different predicates, move the non-sharding-key-condition to another column or drop it."],"exampleFix":"-- before: mixed types on sharding column order_id (INT)\nSELECT * FROM t_order WHERE order_id = 101 OR order_id = '102';\n\n-- after: consistent typing\nSELECT * FROM t_order WHERE order_id IN (101, 102);","handlingStrategy":"validation","validationCode":"// Normalize predicate types on the sharding column before building SQL\nint expectedJdbcType = Types.INTEGER; // matches column/rule definition\nfor (Param p : params) {\n    if (p.column.equals(\"order_id\") && !isCompatible(p.value.getClass(), expectedJdbcType)) {\n        p.value = normalize(p.value, expectedJdbcType); // e.g. Integer.parseInt(value.toString())\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    rs = ps.executeQuery();\n} catch (final ShardingValueDataTypeException ex) {\n    // ex names the offending column; rebind that column's parameters with a single type and retry\n}","preventionTips":["Declare one Java type for each sharding column and enforce it at the repository layer.","Use typed setters (setInt/setLong/setString) instead of setObject with varying types.","Add unit tests asserting bind types equal the sharding column type."],"tags":["sharding","condition","type-mismatch","where-clause","parameters"],"backgroundTag":null,"analyzedSha":"e952770a215630a3659c75d64369168cd3e26b82","analyzedAt":"2026-08-14T13:54:53.392Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}