v.21.11New Features
Add Functions for User-Defined Default Values on String Parsing Failures
Add FunctiontoUUIDOrDefault,toUInt8/16/32/64/256OrDefault,toInt8/16/32/64/128/256OrDefault, which enables user defining default value(not null) when string parsing is failed. #21330 (taiyang-li).
Why it matters
These functions address the need to safely parse string values into UUID or various integer types, providing a fallback default value instead of returningNULL or causing an error when the conversion fails. This improves data robustness and simplifies handling of malformed or unexpected input.How to use it
Use the new functions by passing the string to be converted and the desired default value as arguments. For example:SELECT toInt32OrDefault('123', 0);
SELECT toUUIDOrDefault('invalid-uuid', generateUUIDv4());If the string parsing fails, the function returns the specified default value instead of
NULL or error.