SAFE_CAST
The SAFE_CAST function in MaxCompute converts an expression (expr) to a target data type (type).
Description
The CAST function converts the result of an expression (expr) to a target data type (type). The key difference between SAFE_CAST and CAST is how they handle conversion failures:
CAST: If an expression cannot be converted to the target data type, such as converting a non-numeric string to an integer, it raises a runtime error, causing the entire query to fail.
SAFE_CAST: If an expression cannot be converted to the target data type, it returns NULL instead of raising an error. This allows the query to complete successfully.
Therefore, SAFE_CAST is particularly useful for handling data of uncertain quality or dirty data. It prevents a task from failing due to a few invalid records.
The SAFE_CAST function supports converting an expression (expr) to the INT and BIGINT data types:
Currently, SAFE_CAST only supports INT and BIGINT as target data types. It does not support other data types that CAST supports, such as STRING, DOUBLE, or DATETIME.
SAFE_CAST AS INT
Syntax
SAFE_CAST (<expr> AS INT)Parameters
expr: Required. The expression to convert.
Return value
Returns an INT value. If the conversion is successful, the function returns the corresponding value. If the conversion fails, it returns NULL instead of raising an error. This is the core behavior that distinguishes SAFE_CAST from CAST.
For a string with non-numeric characters, such as '123abc':
In non-strict mode (the default,
odps.sql.udf.strict.mode=false), the operation returns the numeric part before the letters.In strict mode (
odps.sql.udf.strict.mode=true), NULL is returned.
For a string with only non-numeric characters, such as 'abc':
In non-strict mode (default) (
odps.sql.udf.strict.mode=false), NULL is returned.In strict mode (
odps.sql.udf.strict.mode=true), it returns NULL.
Examples
-- Non-strict mode (default)
SELECT SAFE_CAST('123abc' AS INT) AS toint
,SAFE_CAST('abc' AS INT) AS toint2
;
-- Result
+-------+--------+
| toint | toint2 |
+-------+--------+
| 123 | NULL |
+-------+--------+
-- Strict mode
SET odps.sql.udf.strict.mode=true;
SELECT SAFE_CAST('123abc' AS INT) AS toint
,SAFE_CAST('abc' AS INT) AS toint2
;
-- Result
+-------+--------+
| toint | toint2 |
+-------+--------+
| NULL | NULL |
+-------+--------+SAFE_CAST AS BIGINT
Syntax
SAFE_CAST (<expr> AS BIGINT)Parameters
expr: Required. The expression to convert.
Return value
Returns a BIGINT value. If the conversion is successful, the function returns the corresponding value. If the conversion fails, it returns NULL instead of raising an error. This is the core behavior that distinguishes SAFE_CAST from CAST.
For a string with non-numeric characters, such as '123abc':
In non-strict mode (default) (
odps.sql.udf.strict.mode=false), the function returns the numeric part before the letters.In strict mode (
odps.sql.udf.strict.mode=true), NULL is returned.
For a string with only non-numeric characters, such as 'abc':
In non-strict mode (default) (
odps.sql.udf.strict.mode=false), it returns 0.In strict mode (
odps.sql.udf.strict.mode=true), it returns NULL.
Examples
-- Non-strict mode (default)
SELECT SAFE_CAST('123abc' AS BIGINT) AS tobigint
,SAFE_CAST('abc' AS BIGINT) AS tobigint2
;
-- Result
+------------+------------+
| tobigint | tobigint2 |
+------------+------------+
| 123 | 0 |
+------------+------------+
-- Strict mode
SET odps.sql.udf.strict.mode=true;
SELECT SAFE_CAST('123abc' AS BIGINT) AS tobigint
,SAFE_CAST('abc' AS BIGINT) AS tobigint2
;
-- Result
+------------+------------+
| tobigint | tobigint2 |
+------------+------------+
| NULL | NULL |
+------------+------------+