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
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; otherwise an error is returned.
The following succeeds in Hive-compatible mode but fails in V1.0 and V2.0 modes:
CREATE TABLE t (a BIGINT); INSERT INTO TABLE SELECT 1.5;
Operator and function behavioral differences across data type editions
For the
+,-,*,/, andPOWoperators:Hive-compatible:Returns the initial value
V1.0 and V2.0:Returns an error; in other modes, NULL is returned
For the
>,>=,=,<, and<=operators applied to DOUBLE values:Hive-compatible:Compares all digits directly
V1.0 and V2.0:Compares only the first 15 digits to the right of the decimal point; remaining digits are ignored
For the
&,|, and^operators:Hive-compatible:Same type as the input
V1.0 and V2.0:Always BIGINT
For function like:LENGTH,LENGTHB,FIND_IN_SET,INSTR,SIZE,HASH,SIGN
Hive-compatible:Returns a INT value.
V1.0 and V2.0:Returns a BIGINT value.
FLOOR,CEIL
Hive-compatible: If the input parameter is of the DECIMAL type, a DECIMAL value is returned.
V1.0 and V2.0: If the input parameter is of the DECIMAL type, a BIGINT value is returned.
FROM_UNIXTIME
Hive-compatible:Returns a STRING value.
V1.0 and V2.0:Returns a DATETIME value.
CONCAT_WS
Hive-compatible:NULL input strings are ignored; remaining strings are concatenated.
V1.0 and V2.0:If any input string is NULL, NULL is returned.
FIND_IN_SET
Hive-compatible:an empty string matches the tail of the search string:
find_in_set("","") -- Returns 1 find_in_set("", "a,") -- Returns 2V1.0 and V2.0:An empty string is treated as unmatched and 0 is returned.
REGEXP_(EXTRACT/REPLACE)
Hive-compatible:Java regular expression specifications
V1.0 and V2.0:MaxCompute specifications
SUBSTR
STRING SUBSTR(STRING <string>, BIGINT <start_position>[, BIGINT <length>])start_position: Required. BIGINT type. The default start position is 1.
Hive-compatible data type edition: When start_position is 0, the behavior is the same as when the start position is 1.
1.0 and 2.0 data type editions: When start_position is 0, an empty string is returned.
BigQuery-compatible data type edition: When start_position is 0, the behavior is the same as when the start position is 1.
BigQuery-compatible data type edition: When start_position is less than the negative length of the string, the behavior is the same as when the start position is 1.