"Conversion" includes, but is not limited to, casting and coercion.
- Casting is explicit conversion and uses the
CAST()
function. - Coercion is implicit conversion, which ZetaSQL performs automatically under the conditions described below.
There are also conversions that have their own function names, such as
PARSE_DATE()
. To learn more about these functions, see
Conversion functions
The table below summarizes all possible CAST
and coercion possibilities for
ZetaSQL data types. "Coercion To" applies to all expressions of a
given data type, (for example, a
column), but literals
and parameters can also be coerced. See Literal Coercion and
Parameter Coercion for details.
From Type | CAST to | Coercion To |
---|---|---|
INT32 | BOOL INT32 INT64 UINT32 UINT64 NUMERIC BIGNUMERIC FLOAT DOUBLE STRING ENUM |
INT64 NUMERIC BIGNUMERIC DOUBLE |
INT64 | BOOL INT32 INT64 UINT32 UINT64 NUMERIC BIGNUMERIC FLOAT DOUBLE STRING ENUM |
NUMERIC BIGNUMERIC DOUBLE |
UINT32 | BOOL INT32 INT64 UINT32 UINT64 NUMERIC BIGNUMERIC FLOAT DOUBLE STRING ENUM |
INT64 UINT64 NUMERIC BIGNUMERIC DOUBLE |
UINT64 | BOOL INT32 INT64 UINT32 UINT64 NUMERIC BIGNUMERIC FLOAT DOUBLE STRING ENUM |
NUMERIC BIGNUMERIC DOUBLE |
NUMERIC | INT32 INT64 UINT32 UINT64 NUMERIC BIGNUMERIC FLOAT DOUBLE STRING |
BIGNUMERIC DOUBLE |
BIGNUMERIC | INT32 INT64 UINT32 UINT64 NUMERIC BIGNUMERIC FLOAT DOUBLE STRING |
DOUBLE |
FLOAT | INT32 INT64 UINT32 UINT64 NUMERIC BIGNUMERIC FLOAT DOUBLE STRING |
DOUBLE |
DOUBLE | INT32 INT64 UINT32 UINT64 NUMERIC BIGNUMERIC FLOAT DOUBLE STRING |
|
BOOL | BOOL INT32 INT64 UINT32 UINT64 STRING |
|
STRING | BOOL INT32 INT64 UINT32 UINT64 NUMERIC BIGNUMERIC FLOAT DOUBLE STRING BYTES DATE DATETIME TIME TIMESTAMP ENUM PROTO |
|
BYTES | STRING BYTES PROTO |
|
DATE | STRING DATE DATETIME TIMESTAMP |
DATETIME |
DATETIME | STRING DATE DATETIME TIME TIMESTAMP |
|
TIME | STRING TIME |
|
TIMESTAMP | STRING DATE DATETIME TIME TIMESTAMP |
|
ARRAY | ARRAY | |
ENUM |
ENUM
(with the same ENUM name)
INT32 INT64 UINT32 UINT64 STRING |
ENUM (with the same ENUM name) |
STRUCT | STRUCT | |
PROTO |
PROTO
(with the same PROTO name)
STRING BYTES |
PROTO (with the same PROTO name) |
Most data types can be cast from one type to another with the CAST
function.
When using CAST
, a query can fail if ZetaSQL is unable to perform
the cast. If you want to protect your queries from these types of errors, you
can use SAFE_CAST
. To learn more about the rules for CAST
, SAFE_CAST
and
other casting functions, see
Conversion functions.
ZetaSQL coerces the result type of an argument expression to another type if needed to match function signatures. For example, if function func() is defined to take a single argument of type DOUBLE and an expression is used as an argument that has a result type of INT64, then the result of the expression will be coerced to DOUBLE type before func() is computed.
ZetaSQL supports the following literal coercions:
Input Data Type | Result Data Type | Notes |
---|---|---|
Integer literal | INT32 UINT32 UINT64 ENUM |
Integer literals will implicitly coerce to ENUM type when necessary, or can be explicitly CAST to a specific ENUM type name. |
DOUBLE literal |
NUMERIC FLOAT |
Coercion may not be exact, and returns a close value. |
STRING literal | DATE DATETIME TIME TIMESTAMP ENUM PROTO |
String literals will implicitly coerce to PROTO or ENUM type when necessary, or can be explicitly CAST to a specific PROTO or ENUM type name. |
BYTES literal | PROTO |
Literal coercion is needed when the actual literal type is different from the
type expected by the function in question. For
example, if function func()
takes a DATE argument, then the expression
func("2014-09-27")
is valid because the STRING literal "2014-09-27"
is coerced to DATE.
Literal conversion is evaluated at analysis time, and gives an error if the input literal cannot be converted successfully to the target type.
Note: String literals do not coerce to numeric types.
ZetaSQL supports the following parameter coercions:
Input Data Type | Result Data Type |
---|---|
INT32 parameter | ENUM |
INT64 parameter | ENUM |
STRING parameter | PROTO |
BYTES parameter | PROTO |
If the parameter value cannot be coerced successfully to the target type, an error is provided.