Data type conversion
MaxCompute SQL allows type conversions between different data types. These conversions can be either explicit or implicit.
Explicit conversion
An explicit conversion uses the CAST function to convert a value from one data type to another. The following table shows the explicit conversions supported by MaxCompute SQL. For more information about this function, see CAST.
From/to | BIGINT | DOUBLE | STRING | DATETIME | BOOLEAN | DECIMAL | FLOAT |
BIGINT | N/A | Y | Y | N | Y | Y | Y |
DOUBLE | Y | N/A | Y | N | Y | Y | Y |
STRING | Y | Y | N/A | Y | Y | Y | Y |
DATETIME | N | N | Y | N/A | N | N | N |
BOOLEAN | Y | Y | Y | N | N/A | Y | Y |
DECIMAL | Y | Y | Y | N | Y | N/A | Y |
FLOAT | Y | Y | Y | N | Y | Y | N/A |
In this table, Y indicates that the conversion is supported, N indicates that it is not supported, and N/A means that no conversion is necessary. Performing an unsupported explicit conversion returns an error.
Sample data
NoteThe following examples use a
usertable with this sample data.create table if not exists user ( user_name string, user_id bigint, age bigint ); insert into user values ('zhangsan',111,20);Examples
SELECT CAST(user_id AS DOUBLE) AS new_id from user; SELECT CAST('2015-10-01 00:00:00' AS DATETIME) AS new_date; SELECT CAST(ARRAY(1,2,3) AS ARRAY<STRING>); SELECT CONCAT_WS(',', CAST(ARRAY(1, 2) AS ARRAY<STRING>));
Usage notes and limitations
When you convert a DOUBLE type to a BIGINT type, the fractional part is truncated. For example,
CAST(1.6 AS BIGINT) = 1.When you convert a STRING in DOUBLE format to a BIGINT, it is first converted to DOUBLE and then to BIGINT, truncating the fractional part. For example,
CAST("1.6" AS BIGINT) = 1.You can convert a STRING in BIGINT format to a DOUBLE. The result has one decimal place. For example,
CAST("1" AS DOUBLE) = 1.0.Conversions to the DATETIME type use the default format
yyyy-mm-dd hh:mi:ss.You can use built-in functions to convert some data types that do not support direct explicit conversion. For example, you can use the
TO_CHARfunction to convert a BOOLEAN value to a STRING. For more information, see TO_CHAR. TheTO_DATEfunction also supports converting a STRING value to a DATETIME. For more information, see TO_DATE.If a DECIMAL's value range is exceeded,
CAST STRING TO DECIMALmay cause issues such as an overflow error or data truncation.Explicitly converting a DECIMAL to a DOUBLE or FLOAT may result in a loss of precision. For high-precision scenarios, such as currency calculations, we recommend using the DECIMAL type.
MaxCompute supports type conversions for complex data types. For both implicit and explicit conversions of a complex type, its subtypes must also be convertible. When you convert a STRUCT, the field names are not required to match, but the number of fields must be the same and the corresponding fields must be implicitly or explicitly convertible. For example:
ARRAY<BIGINT>can be implicitly or explicitly converted toARRAY<STRING>.ARRAY<BIGINT>can be explicitly converted toARRAY<INT>but cannot be implicitly converted.ARRAY<BIGINT>cannot be implicitly or explicitly converted toARRAY<DATETIME>.STRUCT<a:BIGINT,b:INT>can be implicitly converted toSTRUCT<col1:STRING,col2:BIGINT>but cannot be implicitly or explicitly converted toSTRUCT<a:STRING>.
Implicit conversion and its scope
An implicit conversion is a type conversion that MaxCompute performs automatically at runtime based on the context and conversion rules. The following tables show the implicit conversions that MaxCompute supports.
From/To | BOOLEAN | TINYINT | SMALLINT | INT | BIGINT | FLOAT |
BOOLEAN | N/A | N | N | N | N | N |
TINYINT | N | N/A | Y | Y | Y | Y |
SMALLINT | N | N | N/A | Y | Y | Y |
INT | N | N | Y | N/A | Y | Y |
BIGINT | N | N | N | N | N/A | Y |
FLOAT | N | N | N | N | Y | N/A |
From/to | DOUBLE | DECIMAL | STRING | VARCHAR | TIMESTAMP | BINARY |
DOUBLE | N/A | Y | Y | Y | N | N |
DECIMAL | N | N/A | Y | Y | N | N |
STRING | Y | Y | N/A | Y | N | N |
VARCHAR | Y | Y | Y | N/A | N | N |
TIMESTAMP | N | N | Y | Y | N/A | N |
BINARY | N | N | N | N | N | N/A |
In these tables, Y indicates that the conversion is supported, N indicates that it is not supported, and N/A means that no conversion is necessary. MaxCompute returns an error for an unsupported implicit conversion or for a conversion that fails at runtime.
MaxCompute 2.0 introduces constant definitions for DECIMAL and DATETIME types. For example, 100BD is a DECIMAL constant with a value of 100, and
2017-11-11 00:00:00is a DATETIME constant. You can use these constants directly in VALUES clauses and tables.Because MaxCompute performs implicit conversions automatically based on context, we recommend using CAST for explicit conversions when data types do not match.
Implicit conversion rules are effective only within a specific scope. In some scopes, only a subset of these rules can apply.
Example
SELECT user_id+age+'12345', CONCAT(user_name,user_id,age) FROM user;The rules for implicit conversion vary depending on the operator:
Implicit conversion with relational operators
Relational operators include
=, <>, <, <=, >, >=, IS NULL, IS NOT NULL, LIKE, RLIKE, and IN. The implicit conversion rules forLIKE, RLIKE, and INdiffer from those of other relational operators and are described separately. The rules in this section do not apply to these three operators.When different data types are involved in a relational operation, they are implicitly converted according to the following rules.
From/to
BIGINT
DOUBLE
STRING
DATETIME
BOOLEAN
DECIMAL
BIGINT
N/A
DOUBLE
DOUBLE
N
N
DECIMAL
DOUBLE
DOUBLE
N/A
DOUBLE
N
N
DECIMAL
STRING
DOUBLE
DOUBLE
N/A
DATETIME
N
DECIMAL
DATETIME
N
N
DATETIME
N/A
N
N
BOOLEAN
N
N
N
N
N/A
N
DECIMAL
DECIMAL
DECIMAL
DECIMAL
N
N
N/A
NoteIf the two data types being compared cannot be implicitly converted, the relational operation fails and returns an error.
For more information about relational operators, see Operators.
Implicit conversion with special relational operators
Special relational operators include
LIKE, RLIKE, and IN.The syntax for LIKE and RLIKE is as follows:
source LIKE pattern; source RLIKE pattern;NoteThe source and pattern parameters for LIKE and RLIKE must be of the STRING type.
Other data types are not allowed and are not implicitly converted to STRING for this operation.
The syntax for IN is as follows:
key IN (value1, value2, …)NoteIn an IN expression, all values in the value list must have the same data type.
When the key is compared with the values, if the types include BIGINT, DOUBLE, and STRING, we recommend converting them all to DOUBLE. If the types include DATETIME and STRING, we recommend converting them all to DATETIME. No other type conversions are allowed.
Implicit conversion with arithmetic operators
Arithmetic operators include
+, -, *, /, and %. Their implicit conversion rules are as follows:Only STRING, BIGINT, DOUBLE, and DECIMAL types can be used in arithmetic operations.
STRING values are implicitly converted to DOUBLE before the operation.
BIGINT is implicitly converted to DOUBLE when used in calculations with DOUBLE.
DATETIME and BOOLEAN types cannot be used in arithmetic operations.
Implicit conversion with logical operators
Logical operators include
and, or, and not. Their implicit conversion rules are as follows:Only BOOLEAN values can be used in logical operations.
Other data types cannot be used in logical operations and cannot be implicitly converted for this purpose.
Implicit conversion in built-in functions
MaxCompute SQL provides a wide range of built-in functions to perform calculations on one or more columns from any row and return various data types. Their implicit conversion rules are as follows:
When you call a function, if an input parameter's data type does not match the defined type for that parameter, the parameter is implicitly converted to the required type.
The implicit conversion rules vary for each built-in function in MaxCompute SQL (see Built-in functions overview for more information).
Implicit conversion in CASE WHEN expressions
For more information about CASE WHEN expressions, see CASE WHEN expression. The implicit conversion rules are as follows:
If the return types include only BIGINT and DOUBLE, all are converted to DOUBLE.
If any potential return value is a STRING, all return values are converted to STRING. If a type, such as BOOLEAN, cannot be converted, an error is returned.
Other type conversions are not allowed.
STRING and DATETIME conversion
MaxCompute supports conversions between STRING and DATETIME types. The format yyyy-mm-dd hh:mi:ss is used for these conversions.
Unit | Format string | Value range |
Year | yyyy | 0001-9999 |
Month | mm | 01-12 |
Day | dd | 01-28|29|30|31 |
Hour | hh | 00-23 |
Minute | mi | 00-59 |
Second | ss | 00-59 |
If a unit value is a single digit, the leading zero cannot be omitted. For example,
2014-1-9 12:12:12is an invalid DATETIME format and cannot be converted from STRING to DATETIME. You must write it as2014-01-09 12:12:12.A STRING can be converted to DATETIME only if the string matches the format described above. For example,
CAST("2013-12-31 02:34:34" AS DATETIME)converts the STRING2013-12-31 02:34:34to the DATETIME type. Similarly, when a DATETIME value is converted to a STRING, it defaults to theyyyy-mm-dd hh:mi:ssformat.
MaxCompute provides the TO_DATE function, which allows you to convert STRING values that do not match the default date format to the DATETIME type. For more information, see TO_DATE.