Hive-compatible data type edition

Updated at:

The Hive-compatible data type edition is one of the three data type editions in MaxCompute and supports only Hive-compatible data types. This topic describes its definition, supported data types, and differences from other data type editions.

Definition

When a project uses the Hive-compatible data type edition, its data type properties are defined as follows.

setproject odps.sql.type.system.odps2=true; -- Enable MaxCompute V2.0 data types.
setproject odps.sql.decimal.odps2=true; -- Enable the DECIMAL data type in MaxCompute V2.0.
setproject odps.sql.hive.compatible=true; -- Enable Hive-compatible mode.

Applicable scenarios

This edition is designed for MaxCompute projects migrated from Hadoop whose dependent components support the MaxCompute V2.0 data type edition.

Basic data types

The basic data types supported by the Hive-compatible data type edition are generally the same as those in the V2.0 data type edition. Only the DECIMAL data type differs between the two editions.

Data type

Constant examples

Description

TINYINT

1Y, -127Y

8-bit signed integer.

Value range: -128 to 127.

SMALLINT

32767S, -100S

16-bit signed integer.

Value range: -32,768 to 32,767.

INT

1000, -15645787

32-bit signed integer.

Value range: -2 31 to 2 31-1.

BIGINT

100000000000L, -1L

64-bit signed integer.

Value range: -2 63+1 to 2 63-1.

BINARY

unhex('FA34E10293CB42848573A4E39937F479')

Binary data. Max length: 8 MB.

FLOAT

cast(3.14159261E+7 as float)

32-bit binary floating-point.

DOUBLE

3.14159261E+7

64-bit binary floating-point.

DECIMAL(precision,scale)

3.5BD, 99999999999.9999999BD

Base-10 exact numeric type. The default is decimal(10,0). The precision and scale can be configured.

  • precision: The maximum number of digits. Value range: 1 <= precision <= 38.

  • scale: The number of decimal places. Value range: 0 <= scale <= 18.

    To use a larger scale, use the flag set odps.sql.decimal2.extended.scale.enable=true; , which extends the scale range to 0 <= scale <= 38.

VARCHAR(n)

None

Variable-length character type. n specifies the length.

Value range: 1 to 65,535.

CHAR(n)

None

Fixed-length character type. n specifies the length, up to 255. Values shorter than n are padded with spaces, which are ignored in comparisons.

STRING

"abc", 'bcd', "alibaba", 'inc'

String type. Max length: 8 MB.

DATE

DATE'2017-11-11'

Date type. Format: yyyy-mm-dd.

Value range: 0000-01-01 to 9999-12-31.

DATETIME

DATETIME'2017-11-11 00:00:00'

Date and time type.

Value range: 0000-01-01 00:00:00.000 to 9999-12-31 23:59:59.999, with millisecond precision.

TIMESTAMP

TIMESTAMP'2017-11-11 00:00:00.123456789'

Time zone-independent timestamp type.

Value range: 0000-01-01 00:00:00.000000000 to 9999-12-31 23:59:59.999999999, with nanosecond precision.

Note

For time zone-related functions such as cast(<a timestamp> as string), TIMESTAMP values must be displayed according to the current time zone.

BOOLEAN

True, False

Boolean type.

Value range: True or False.

The data types have the following characteristics:

  • All the preceding data types support NULL values.

  • The INT keyword in SQL represents a 32-bit integer.

    -- Convert a to a 32-bit integer.
    cast(a as INT)
  • Unsuffixed integer literals default to INT. For example, the integer literal 1 in SELECT 1 + a; is treated as INT. If the value exceeds the INT range but fits in BIGINT, it is treated as BIGINT. If it exceeds the BIGINT range, it is treated as DOUBLE.

  • Implicit conversions

    • Some implicit type conversions are disabled. For example, STRING->BIGINT, STRING->DATETIME, DOUBLE->BIGINT, DECIMAL->DOUBLE, and DECIMAL->BIGINT can cause precision loss or errors. Use the CAST function to explicitly perform conversions that are disabled implicitly.

    • VARCHAR constants can be implicitly converted to STRING constants.

  • Tables, functions, and UDFs

    • Built-in functions whose parameters use V2.0 data types can be used normally.

    • Data types in UDFs are parsed and overloaded according to the Hive-compatible data types.

    • The data type of a partition key column can be STRING, VARCHAR, CHAR, TINYINT, SMALLINT, INT, or BIGINT.

    • In Hive-compatible mode, some functions do not support partition pruning. For more information, see Built-in function comparison with Hive, MySQL, and Oracle.

  • String constants can be concatenated directly. For example, abc and xyz combine to abcxyz.

  • When inserting a constant into a DECIMAL column, use the format specified in the constant definition, such as 3.5BD in the following example.

    insert into test_tb(a) values (3.5BD)
  • DATETIME values displayed in query results do not include milliseconds. Tunnel commands use -dfp to specify the time format, which can include milliseconds. For example, tunnel upload -dfp 'yyyy-MM-dd HH:mm:ss.SSS'. For more information about Tunnel commands, see Tunnel commands.

Complex data types

Data type

Definition examples

Constructor examples

ARRAY

  • array<int>

  • array<struct<a:int, b:string>>

  • array(1, 2, 3)

  • array(array(1, 2), array(3, 4))

MAP

  • map<string, string>

  • map<smallint, array<string>>

  • map(“k1”, “v1”, “k2”, “v2”)

  • map(1S, array(‘a’, ‘b’), 2S, array(‘x’, ‘y’))

STRUCT

  • struct<x:int, y:int>

  • struct<field1:bigint, field2:array<int>, field3:map<int, int>>

  • named_struct(‘x’, 1, ‘y’, 2)

  • named_struct(‘field1’, 100L, ‘field2’, array(1, 2), ‘field3’, map(1, 100, 2, 200))

Note

Complex data types in MaxCompute can be nested. For built-in functions that work with complex types, see ARRAY, MAPor STRUCT.

Differences from other editions

  • 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 function

      • Hive-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.