You can use type conversion functions to convert data to the correct type in a query statement.
Function | Syntax | Description |
---|---|---|
cast function | cast(x as type) | Converts the values of the x field as a data type.
If the cast function cannot convert a value, the query that calls this function fails. |
try_cast function | try_cast(x as type) | Converts the values of the x as a data type.
If the try_cast function cannot convert a value, the function returns NULL. The query that calls this function can process the NULL value and continue to run. Note A log may contain data of the data types that you do not expect. When you query logs,
we recommend that you use the try_cast function. This way, conversion failures will
not cause your queries fail.
|
typeof function | typeof(x) | Returns the data type of the x field. |
cast function
The cast function is used to convert the values of the x field to a specified data type. If the cast function cannot convert a value, the query that calls this function fails.
Syntax
cast(x as type)
Parameters
Parameter | Description |
---|---|
x | The value of this parameter can be of any data type. |
type | A 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 Log Service index. For information about the mapping, see Data type mappings. |
Return value type
The data type that is specified by the type parameter.
Examples
Converts number 1 to the boolean data type.
- Query statement
* | select cast(1 as boolean)
- Query result
try_cast function
Converts the values of the x field to a specified data type. If the try_cast function cannot convert a value, the function returns NULL. The query that calls this function can process the NULL value and continue to run.
Syntax
try_cast(x as type)
Parameters
Parameter | Description |
---|---|
x | The value of this parameter can be of any data type. |
type | A 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 Log Service index. For information about the mapping, see Data type mappings. |
Return value type
The data type that the type parameter specifies.
Examples
Converts the values of the uid field to the varchar data type.
- Query statement
* | select try_cast(uid as varchar)
- Query result
typeof function
Returns the data type of the x field.
Syntax
typeof(x)
Parameters
Parameter | Description |
---|---|
x | The value of this parameter can be of any data type. |
Return value type
Data of the varchar data type.
Examples
Returns the data type of the request_time field.
- Query statement
* |SELECT typeof(request_time)
- Query result
Data type mappings
The following table describes the mappings between the SQL data types and the data types that are supported by a Log Service index.
Data types that are supported by Log Service index | SQL data type |
---|---|
long | bigint |
text | varchar |
double | double |
json | varchar |