Data type version 2.0
This topic describes MaxCompute data type version 2.0, including its definition, use cases, supported data types, and its differences from other data type versions.
Background
Data type version 2.0 is one of the three data type versions available in MaxCompute.
It is suitable for MaxCompute projects that had no existing data before April 2020, provided that all dependent product components support data type version 2.0.
Enabling data type version 2.0
To use data type version 2.0 in a project, set the following properties.
setproject odps.sql.type.system.odps2=true; -- Enable MaxCompute data type version 2.0.
setproject odps.sql.decimal.odps2=true; -- Enable the DECIMAL 2.0 data type.
setproject odps.sql.hive.compatible=false; -- Disable Hive-compatible mode.Basic data types
Type | Literal | Description |
TINYINT | 1Y, -127Y |
|
SMALLINT | 32767S, -100S |
|
INT | 1000, -15645787 |
|
BIGINT | 100000000000L, -1L |
|
BINARY |
|
|
FLOAT | 3.14F, CAST(3.14159261E+7 AS FLOAT) |
|
DOUBLE | 3.14D, 3.14159261E+7 |
|
DECIMAL(precision,scale) | 3.5BD, 99999999999.9999999BD |
|
VARCHAR(n) | None |
|
CHAR(n) | None | A fixed-length character type of length n, with a maximum value of 255. Shorter strings are padded with spaces to the defined length. These trailing spaces are ignored during comparisons. |
STRING | "abc", 'bcd', "alibaba", 'inc' |
|
DATE | DATE'2017-11-11' |
|
DATETIME | DATETIME'2017-11-11 00:00:00' |
|
TIMESTAMP | TIMESTAMP'2017-11-11 00:00:00.123456789' |
|
TIMESTAMP_NTZ | TIMESTAMP_NTZ '2017-11-11 00:00:00.123456789' |
|
BOOLEAN | True, False |
|
INTERVAL |
| A data type that represents a time interval between two dates or times. It includes two types: INTERVAL_YEAR_MONTH and INTERVAL_DAY_TIME. For more information, see INTERVAL data type. |
Blob | None | For more information, see Blob data type and multimodal storage. |
VECTOR | [1.1, 2.2, 3.3] | A vector type that consists of a fixed number of FLOAT elements. It represents high-dimensional embedding vectors, such as the semantic features of text or images. The number of dimensions can range from 1 to 4,096. For more information, see VECTOR data type. |
GEOGRAPHY |
| Geospatial data type. Used to represent geometric objects on the Earth's surface, such as points (Point), lines (LineString), and polygons (Polygon).
|
Notes on data types:
All data types listed above can be NULL.
The INT keyword in SQL is a 32-bit integer.
-- Convert a to a 32-bit integer. CAST(a AS INT)Literals
Integer literals are treated as the INT type by default. For example, in
SELECT 1 + a;, the integer literal 1 is processed as an INT. If a literal exceeds the value range of INT but falls within the range of BIGINT, it is treated as a BIGINT. If it exceeds the range of BIGINT, it is treated as a DOUBLE.When you insert a literal into a DECIMAL column, the literal must conform to the format specified in its definition. For example, see the
3.5BDliteral in the following code.INSERT INTO test_tb(a) VALUES (3.5BD)STRING literals support concatenation. When two or more string literals are placed next to each other, they are automatically combined into a single string. For example,
SELECT 'abc' 'efg' 'ddt';returnsabcefgddt.
Implicit conversion
Some implicit conversions are disabled. For example, conversions from STRING to BIGINT, STRING to DATETIME, DOUBLE to BIGINT, DECIMAL to DOUBLE, and DECIMAL to BIGINT are disabled because they can cause precision loss or errors. Use the CAST function to perform these conversions explicitly.
VARCHAR literals can be implicitly converted to STRING literals.
Tables, functions, and UDFs
Built-in functions that accept data type version 2.0 parameters work as expected.
The data types in a User-Defined Function (UDF) are resolved and overloaded based on data type version 2.0.
Partition columns support the STRING, VARCHAR, CHAR, TINYINT, SMALLINT, INT, and BIGINT data types.
Complex data types
Complex data types in MaxCompute can be nested up to 20 levels deep. For more information about related built-in functions, see ARRAY, MAP, STRUCT, or JSON.
The size of a complex data type should not exceed 1 MB. Complex data types larger than 1 MB can cause out-of-memory (OOM) errors during computation.
Type | Definition | Constructor |
ARRAY |
|
|
MAP |
|
|
STRUCT |
|
|
JSON |
|
|
Differences from other data type versions
Differences in DML syntax behavior
Behavior of the LIMIT clause in set operations
For example, consider the query
SELECT * FROM t1 UNION ALL SELECT * FROM t2 LIMIT 10;:In data type version 1.0, this is equivalent to
SELECT * FROM t1 UNION ALL SELECT * FROM ( SELECT * FROM t2 LIMIT 10) t2;.In data type version 2.0, this is equivalent to
SELECT * FROM (SELECT * FROM t1 UNION ALL SELECT * FROM t2 ) t LIMIT 10;.
The ORDER BY, DISTRIBUTE BY, SORT BY, and CLUSTER BY operations exhibit the same behavior.
Type resolution for IN expressions
For example, in the expression
a in (1, 2, 3):In data type version 1.0, all values in the list following IN must have the same data type.
In data type version 2.0, the values only need to be implicitly convertible to a common data type.
INSERT type conversion rules differ.
In Hive-compatible mode, if a source data type can be explicitly converted to the target column type, MaxCompute automatically inserts a conversion function and runs it.
In V1.0 and V2.0 modes, the source type must be implicitly convertible to the target column type; otherwise, an error is returned.
-- Succeeds in Hive-compatible mode but fails in other modes. create table t (a bigint); insert into table select 1.5;
Function behavior differences
+,-,*,/, and POW functionHive-compatible data type edition: On data overflow, the value wraps around (returns to the initial value after exceeding the range).
V1.0 and V2.0 data type editions: An error is returned on data overflow; in other modes, NULL is returned.
>,>=,=,<,<=Hive-compatible data type edition: DOUBLE values are compared directly.
V1.0 and V2.0 data type editions: DOUBLE values are considered equal if the first 15 digits to the right of the decimal point are the same. Digits after the 15th decimal place are not compared.
Bitwise operators
&,|,^Hive-compatible data type edition: The return type is the same as the input type.
V1.0 and V2.0 data type editions: The return type is BIGINT.
LENGTH, LENGTHB, FIND_IN_SET, INSTR, SIZE, HASH, and SIGN functions
Hive-compatible data type edition: The return type is INT.
V1.0 and V2.0 data type editions: The return type is BIGINT.
FLOOR, CEIL
Hive-compatible data type edition: If the input parameter is of the DECIMAL type, a DECIMAL value is returned.
V1.0 and V2.0 data type editions: If the input parameter is of the DECIMAL type, a BIGINT value is returned.
FROM_UNIXTIME
Hive-compatible data type edition: A STRING value is returned.
V1.0 and V2.0 data type editions: A DATETIME value is returned.
CONCAT_WS
Hive-compatible data type edition: If an input string to be concatenated is NULL, it is ignored.
V1.0 and V2.0 data type editions: If any input string to be concatenated is NULL, NULL is returned.
FIND_IN_SET
Hive-compatible data type edition: An empty string matches the tail of the search string.
-- In Hive-compatible mode: find_in_set("", "") -- Returns 1. find_in_set("", "a,") -- Returns 2.V1.0 and V2.0 data type editions: An empty string is treated as unmatched and 0 is returned.
REGEXP_(EXTRACT/REPLACE)
Hive-compatible data type edition: REGEXP patterns follow Java regular expression specifications.
V1.0 and V2.0 data type editions: REGEXP patterns follow MaxCompute specifications.
SUBSTR
string substr(string <str>, bigint <start_position>[, bigint <length>])start_position: Required. BIGINT type. The default start position is 1.
Hive-compatible data type version: If start_position=0, it behaves the same as position 1.
1.0 and 2.0 data type versions: If start_position=0, it returns NULL.