apache/shardingsphere · error · ClassCastException
Unsupported type conversion from %s to %s
Error message
Unsupported type conversion from %s to %s
What it means
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.
Source
Thrown at features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/util/ShardingValueTypeConvertUtils.java:123
return (T) convertToTimestamp(value);
} else if (LocalDate.class == targetType) {
return (T) convertToLocalDate(value);
} else if (LocalTime.class == targetType) {
return (T) convertToLocalTime(value);
} else if (LocalDateTime.class == targetType) {
return (T) convertToLocalDateTime(value);
} else if (Instant.class == targetType) {
return (T) convertToInstant(value);
} else if (Year.class == targetType) {
return (T) convertToYear(value);
} else if (YearMonth.class == targetType) {
return (T) convertToYearMonth(value);
} else if (MonthDay.class == targetType) {
return (T) convertToMonthDay(value);
} else if (Duration.class == targetType) {
return (T) convertToDuration(value);
}
throw new ClassCastException("Unsupported type conversion from " + value.getClass().getName() + " to " + targetType.getName());
}
private static Integer convertToInteger(final Comparable<?> value) {
if (value instanceof Number) {
return ((Number) value).intValue();
}
return Integer.parseInt(value.toString());
}
private static Long convertToLong(final Comparable<?> value) {
if (value instanceof Number) {
return ((Number) value).longValue();
}
return Long.parseLong(value.toString());
}
private static Short convertToShort(final Comparable<?> value) {
if (value instanceof Number) {View on GitHub (pinned to e952770a21)
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.
Example fix
// before: java.util.Date sharding value, target LocalDateTime ps.setDate(1, new java.util.Date(millis)); // after: bind a supported java.time type ps.setObject(1, LocalDateTime.ofInstant(Instant.ofEpochMilli(millis), ZoneId.systemDefault()));
Defensive patterns
Strategy: type-guard
Validate before calling
// Convert bind values to supported types before setting
Object v = toSupported(value);
Object toSupported(Object value) {
if (value instanceof java.util.Date d) return LocalDateTime.ofInstant(d.toInstant(), ZoneId.systemDefault());
return value;
} Type guard
boolean isConvertibleShardingValue(final Object value) {
return value instanceof String
|| value instanceof Number
|| value instanceof java.time.LocalDate
|| value instanceof java.time.LocalDateTime
|| value instanceof java.time.LocalTime
|| value instanceof java.time.Instant
|| value instanceof java.time.Year
|| value instanceof java.time.YearMonth
|| value instanceof java.time.MonthDay
|| value instanceof java.time.Duration
|| value instanceof Boolean;
} Try / catch
try {
return ShardingValueTypeConvertUtils.convert(value, targetType);
} catch (final ClassCastException ex) {
// message names source and target classes; convert value manually and retry
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- 21
- Unsupported Firebird format code `%s`
- Sharding algorithm class '%s' should be implement '%s'.
- Could not load class: %s
- Invalid %s, datetime pattern should be '%s', value is '%s'.
AI-assisted analysis of apache/shardingsphere@e952770a21 (2026-08-14).
Data as JSON: /api/errors/136bd164b8f8cd19.
Report an issue: GitHub.