{"record":{"id":"136bd164b8f8cd19","repo":"apache/shardingsphere","slug":"unsupported-type-conversion-from-s-to-s","errorCode":null,"errorMessage":"Unsupported type conversion from %s to %s","messagePattern":"Unsupported type conversion from (.+?) to (.+?)","errorType":"exception","errorClass":"ClassCastException","httpStatus":null,"severity":"error","filePath":"features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/util/ShardingValueTypeConvertUtils.java","lineNumber":123,"sourceCode":"            return (T) convertToTimestamp(value);\n        } else if (LocalDate.class == targetType) {\n            return (T) convertToLocalDate(value);\n        } else if (LocalTime.class == targetType) {\n            return (T) convertToLocalTime(value);\n        } else if (LocalDateTime.class == targetType) {\n            return (T) convertToLocalDateTime(value);\n        } else if (Instant.class == targetType) {\n            return (T) convertToInstant(value);\n        } else if (Year.class == targetType) {\n            return (T) convertToYear(value);\n        } else if (YearMonth.class == targetType) {\n            return (T) convertToYearMonth(value);\n        } else if (MonthDay.class == targetType) {\n            return (T) convertToMonthDay(value);\n        } else if (Duration.class == targetType) {\n            return (T) convertToDuration(value);\n        }\n        throw new ClassCastException(\"Unsupported type conversion from \" + value.getClass().getName() + \" to \" + targetType.getName());\n    }\n    \n    private static Integer convertToInteger(final Comparable<?> value) {\n        if (value instanceof Number) {\n            return ((Number) value).intValue();\n        }\n        return Integer.parseInt(value.toString());\n    }\n    \n    private static Long convertToLong(final Comparable<?> value) {\n        if (value instanceof Number) {\n            return ((Number) value).longValue();\n        }\n        return Long.parseLong(value.toString());\n    }\n    \n    private static Short convertToShort(final Comparable<?> value) {\n        if (value instanceof Number) {","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/apache/shardingsphere/blob/e952770a215630a3659c75d64369168cd3e26b82/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/util/ShardingValueTypeConvertUtils.java#L105-L141","documentation":"A plain ClassCastException ('Unsupported type conversion from X to Y') thrown by ShardingValueTypeConvertUtils.convert when the runtime value's class has no conversion branch to the requested temporal target (LocalDateTime, Instant, Year, YearMonth, MonthDay, Duration — and the earlier branches). The utility converts sharding/route values between Java types; unsupported source/target pairs fall through to this explicit throw instead of failing obscurely inside JDK casts.","triggerScenarios":"A sharding value whose Java type does not match the strategy/algorithm's expected type — e.g. passing a java.util.Date or byte[] where the sharding algorithm asks to convert to LocalDateTime, or a target type added to the enum without a corresponding converter branch in this utility.","commonSituations":"Custom sharding algorithms typed to a specific temporal class while the application binds a different temporal type; dialect upgrades changing the parsed literal class; using an exotic Comparable (e.g. YearWeek) with a strategy that requests a supported-but-unconvertible target.","solutions":["Bind sharding parameters using a type this utility converts (String, Number, java.time types listed in its branches — LocalDate/LocalDateTime/Instant/Year/YearMonth/MonthDay/Duration).","Convert the value in application code before sending (e.g. java.util.Date -> java.time.LocalDateTime) so the utility receives a supported class.","For custom algorithms, do the conversion inside the algorithm rather than relying on ShardingValueTypeConvertUtils for unsupported pairs."],"exampleFix":"// before: java.util.Date sharding value, target LocalDateTime\nps.setDate(1, new java.util.Date(millis));\n\n// after: bind a supported java.time type\nps.setObject(1, LocalDateTime.ofInstant(Instant.ofEpochMilli(millis), ZoneId.systemDefault()));","handlingStrategy":"type-guard","validationCode":"// Convert bind values to supported types before setting\nObject v = toSupported(value);\nObject toSupported(Object value) {\n    if (value instanceof java.util.Date d) return LocalDateTime.ofInstant(d.toInstant(), ZoneId.systemDefault());\n    return value;\n}","typeGuard":"boolean isConvertibleShardingValue(final Object value) {\n    return value instanceof String\n            || value instanceof Number\n            || value instanceof java.time.LocalDate\n            || value instanceof java.time.LocalDateTime\n            || value instanceof java.time.LocalTime\n            || value instanceof java.time.Instant\n            || value instanceof java.time.Year\n            || value instanceof java.time.YearMonth\n            || value instanceof java.time.MonthDay\n            || value instanceof java.time.Duration\n            || value instanceof Boolean;\n}","tryCatchPattern":"try {\n    return ShardingValueTypeConvertUtils.convert(value, targetType);\n} catch (final ClassCastException ex) {\n    // message names source and target classes; convert value manually and retry\n}","preventionTips":["Standardize on java.time types for temporal sharding columns end to end.","In custom sharding algorithms, handle conversion explicitly instead of relying on the utility for exotic types."],"tags":["sharding","type-conversion","class-cast","temporal","parameters"],"backgroundTag":null,"analyzedSha":"e952770a215630a3659c75d64369168cd3e26b82","analyzedAt":"2026-08-14T13:54:53.392Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}