Realtime Compute for Apache Flink supports both implicit and explicit type conversions in Flink SQL, extending the standard Apache Flink data types. This topic describes the Legacy-Cast behavior settings and the full type conversion support matrix.
Usage notes
Legacy-Cast behavior
Realtime Compute for Apache Flink enables Legacy-Cast behavior by default. This setting controls how CAST operations behave when a conversion fails.
| Parameter | Default (Realtime Compute for Apache Flink) | Default (Apache Flink) | Introduced in |
|---|---|---|---|
table.exec.legacy-cast-behaviour | enabled | disabled | Flink 1.15 |
When table.exec.legacy-cast-behaviour is enabled, a cast from a source data type to a target type may cause precision loss, value overflow, or return null.
CAST and TRY_CAST behavior
CAST
By default (Legacy-Cast enabled), CAST returns null when a conversion exception occurs instead of throwing an error. To make jobs fail explicitly on conversion exceptions, set table.exec.legacy-cast-behaviour=disabled.
CAST('42' AS INT) -- returns 42 of type INT
CAST(NULL AS VARCHAR) -- returns NULL of type VARCHAR
-- With table.exec.legacy-cast-behaviour=disabled:
CAST('non-number' AS INT) -- throws an explicit errorTRY_CAST
TRY_CAST always returns null on conversion exceptions without failing the job. This behavior is equivalent to CAST when table.exec.legacy-cast-behaviour=enabled.
TRY_CAST('42' AS INT) -- returns 42 of type INT
TRY_CAST(NULL AS VARCHAR) -- returns NULL of type VARCHAR
TRY_CAST('non-number' AS INT) -- returns NULL of type INTNull value handling
To handle null values explicitly, use the cast(null as target_type) syntax. For example:
cast(null as varchar)This avoids unexpected results such as the "NULL" string or conversion exceptions.
Type conversion support matrix
The following table shows which type conversions are supported. I means implicit conversion is supported. E means only explicit conversion is supported. - means conversion is not supported.
| Input type \ Target type | null | boolean | tinyint | smallint | int | bigint | decimal | float | double | interval | date | time | timestamp | [var]char | [var]binary | variant | bitmap |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| null | I | I | I | I | I | I | I | I | I | I | I | I | I | I | I | I | I |
| boolean | - | I | E | E | E | E | E | E | E | - | - | - | - | I | - | E | - |
| tinyint | - | E | I | I | I | I | I | I | I | E | - | - | E | I | - | E | - |
| smallint | - | E | I | I | I | I | I | I | I | E | - | - | E | I | - | E | - |
| int | - | E | I | I | I | I | I | I | I | E | - | - | E | I | - | E | - |
| bigint | - | E | I | I | I | I | I | I | I | E | - | - | E | I | - | E | - |
| decimal | - | E | I | I | I | I | I | I | I | E | - | - | E | I | - | E | - |
| float | - | E | I | I | I | I | I | I | I | - | - | - | E | I | - | E | - |
| double | - | E | I | I | I | I | I | I | I | - | - | - | E | I | - | E | - |
| interval | - | - | E | E | E | E | E | - | - | I | - | - | - | E | - | E | - |
| date | - | - | - | - | - | - | - | - | - | - | I | - | I | I | - | E | - |
| time | - | - | - | - | - | - | - | - | - | - | - | I | E | I | - | E | - |
| timestamp | - | - | E | E | E | E | E | E | E | - | I | E | I | I | - | E | - |
| [var]char | - | E | I | I | I | I | I | I | I | I | I | I | I | I | I | E | - |
| [var]binary | - | - | - | - | - | - | - | - | - | - | E | E | E | I | I | E | - |
| variant | - | E | E | E | E | E | E | E | E | E | E | E | E | E | E | I | - |
| bitmap | - | - | - | - | - | - | - | - | - | - | - | - | - | E | E | - | I |
References
For the SQL data types supported by the Apache Flink community, see Community Flink SQL data types.