Type Conversion Functions
Overview
Type conversion functions are used to convert between different data types in Flink SQL. These functions can convert a value of one data type to another, or obtain the data type information of an expression at runtime. Type conversion is a core operation in data processing, cleansing, and format adaptation, and is widely used in scenarios such as ETL pipelines, heterogeneous data source integration, and dynamic type determination.
For more information, see Type Conversion Functions.
Application Scenarios
- ETL data cleansing: After reading data from heterogeneous data sources, uses CAST or TRY_CAST to convert string fields to target types such as numeric and date types.
- Data quality fault tolerance: Uses TRY_CAST for fields that may contain dirty data to prevent the processing of the entire record from being interrupted due to individual conversion failures.
- Dynamic type determination: Uses TYPEOF to obtain the data type of a field or expression at runtime for conditional branching or debugging and troubleshooting.
- Cross-source data alignment: When field types from different data sources are inconsistent, uses type conversion functions to unify the data types before performing JOIN or aggregation operations.
Notes and Constraints
- The target type of CAST and TRY_CAST must be a data type keyword supported by Flink SQL, such as INT, BIGINT, STRING, DOUBLE, DECIMAL, and TIMESTAMP.
- Not all types can be converted to each other. For example, direct conversion between ARRAY and MAP, or between ROW and INT, is not supported. Converting an unsupported type pair throws an exception (CAST) or returns NULL (TRY_CAST).
- When converting from STRING to a numeric type, the string must conform to the format requirements of the target type. For example, CAST('abc' AS INT) throws an exception, while TRY_CAST('abc' AS INT) returns NULL.
- When converting from a numeric type to STRING, the result format is determined by the system by default, and the original precision information may not be retained.
- The type string returned by TYPEOF is the internal type representation of Flink. Its format may vary with the version, and you should not hardcode dependencies on the specific content of this string in your business logic.
- In TYPEOF(input, forceSerializable), forceSerializable is of the BOOLEAN type. When set to TRUE, the returned type string is guaranteed to be serializable, but some details of non-serializable types may be omitted.
Syntax Structure of Type Conversion
| Syntax | Return Type | Description | Example |
|---|---|---|---|
| CAST(value AS type) | Determined by type | Converts the value to the data type specified by type. The value is the expression to be converted, and type is the keyword of the target data type (such as INT, BIGINT, STRING, DOUBLE, DECIMAL(p, s), BOOLEAN, DATE, and TIMESTAMP). If the conversion fails, a runtime exception is thrown. Valid conversions between all Flink SQL data types are supported. An error is reported for unsupported type pairs. | SELECT CAST('123' AS INT); -- 123 |
| TRY_CAST(value AS type) | Determined by type. If the conversion fails, NULL is returned. | Converts the value to the data type specified by type. The parameter description is the same as that of CAST. The difference between this function and CAST is that when the conversion fails (for example, the type is incompatible or the value format does not match), no exception is thrown, but NULL is returned. This function is applicable to scenarios where the data quality is uncertain and fault tolerance is required. | SELECT TRY_CAST('abc' AS INT); -- NULL |
Syntax Structure of Type Query
| Syntax | Return Type | Description | Example |
|---|---|---|---|
| TYPEOF(input) | STRING | Returns the string representation of the data type of the input expression. The input is an expression of any type. The return value reflects the information about the expression type in Flink, in the format of INTEGER, VARCHAR(2147483647), or ROW<f1 INT, f2 STRING>. | SELECT TYPEOF(123); -- INTEGER |
| TYPEOF(input, forceSerializable) | STRING | Returns the string representation of the data type of the input expression. The input is an expression of any type, and forceSerializable is of the BOOLEAN type. When forceSerializable is set to TRUE, the returned type string is guaranteed to be serializable, which is applicable to scenarios where type information needs to be transmitted over the network or persisted. When forceSerializable is set to FALSE, the behavior is the same as that of the single-parameter version. | SELECT TYPEOF(123, TRUE); -- INTEGER |
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot