You can use type conversion functions in a query statement to convert data to the required types.
The following table describes the type conversion functions that are supported by Simple Log Service.
Function | Syntax | Description | Supported in SQL | Supported in SPL |
cast(x as type) | Converts the values of the x field to a specified data type. If the cast function fails to convert a value, the query statement that uses this function fails. | √ | √ | |
try_cast(x as type) | Converts the values of the x field to a specified data type. If the try_cast function fails to convert a value, the function returns NULL. The query statement that uses this function skips the value and continues to run. Note Logs may contain dirty data. When you query logs, we recommend that you use the try_cast function. This way, conversion failures will not cause your query statement to fail. | √ | × | |
typeof(x) | Returns the data type of the x field. | √ | × |
cast function
The cast function converts the values of the x field to a specified data type. If the cast function fails to convert a value, the query statement that uses this function fails.
Syntax
cast(x as type)
Parameters
Parameter | Description |
x | The value of this parameter is of an arbitrary data type. |
type | The value of this parameter is of an SQL data type. Valid values: bigint, varchar, double, boolean, timestamp, decimal, array, and map. Example: Each SQL data type maps to a data type that you can use in a Simple Log Service index. For more information, see Appendix: Data type mappings. |
Return value type
The data type that is specified by the type parameter.
Examples
SQL
Convert the number 1 to the Boolean type.
Query statement
* | select cast(1 as boolean)
Query and analysis results
SPL
Convert the number 1 to the Boolean type.
Query statement
* | extend a = cast(1 as boolean)
Query and analysis results
try_cast function
The try_cast function converts the values of the x field to a specified data type. If the try_cast function fails to convert a value, the function returns NULL. The query statement that uses this function skips the value and continues to run.
Syntax
try_cast(x as type)
Parameters
Parameter | Description |
x | The value of this parameter is of an arbitrary data type. |
type | The value of this parameter is of an SQL data type. Valid values: bigint, varchar, double, boolean, timestamp, decimal, array, and map. Example: Each SQL data type maps to a data type that you can use in a Simple Log Service index. For more information, see Appendix: Data type mappings. |
Return value type
The data type that is specified by the type parameter.
Examples
Convert the values of the uid field to the varchar type.
Query statement
* | select try_cast(uid as varchar)
Query and analysis results
typeof function
The typeof function returns the data type of the x field.
Syntax
typeof(x)
Parameters
Parameter | Description |
x | The value of this parameter is of an arbitrary data type. |
Return value type
The varchar type.
Examples
Obtain the data type of the request_time field.
Query statement
* |SELECT typeof(request_time)
Query and analysis results
Appendix: Data type mappings
The following table describes the mappings between the SQL data types and the data types that are supported by Simple Log Service indexes.
Data type supported by Simple Log Service indexes | SQL data type |
long | bigint |
text | varchar |
double | double |
json | varchar |