Hive Data Types
These are all the supported column types in Hive:
BINARY(Note: Only available starting with Hive 0.8.0)
TIMESTAMP(Note: Only available starting with Hive 0.8.0)
DECIMAL(Note: Only available starting with Hive 0.11.0)
DATE(Note: Only available starting with Hive 0.12.0)
STRUCT<col_name : data_type [COMMENT col_comment], ...>
UNIONTYPE<data_type, data_type, ...>
Supports traditional UNIX timestamp with optional nanosecond precision.
- Integer numeric types: Interpreted as UNIX timestamp in seconds
- Floating point numeric types: Intepreted as UNIX timestamp in seconds with decimal precision
- Strings: JDBC compliant java.sql.Timestamp format "YYYY-MM-DD HH:MM:SS.fffffffff" (9 decimal place precision)
Timestamps are interpreted to be timezoneless and stored as an offset from the UNIX epoch. Convenience UDFs for conversion to and from timezones are provided (to_utc_timestamp, from_utc_timestamp).
All existing datetime UDFs (month, day, year, hour, etc.) work with the TIMESTAMP data type.
Timestamps were introduced in Hive 0.8.0 (HIVE-2272).
The Decimal datatype in Hive is based on Java's BigDecimal which is used for representing immutable arbitrary precision decimal numbers in Java. All regular number operations (e.g. +, -, *, /) and relevant UDFs (e.g. Floor, Ceil, Round, and many more) have been updated to handle decimal types. You can cast to/from decimal types like you would do with other numeric types. The persistence format of the decimal type supports both scientific and non-scientific notation. Therefore, regardless of whether your dataset contains data like 1E+44 (scientific notation) or 4004 (non-scientific notation) or a combination of both, decimal datatype can be used for it.
With the present Hive implementation, the Decimal datatype has precision limited to 38 digits. While this has minimal impact on usability, it's worth noting for performance reasons. HIVE-3796 is tracking the progress of allowing users to specify scale and precision when creating tables with decimal datatype.
The Decimal datatype is discussed further in Floating Point Types below.
Decimal datatype was introduced in Hive 0.11.0 (HIVE-2693).
Union types can at any one point hold exactly one of their specified data types. You can create an instance of this type using the
The first part in the deserialized union is the tag which lets us know which part of the union is being used. In this example
0 means the first data_type from the definition which is an
int and so on.
To create a union you have to provide this tag to the
Integral literals are assumed to be INT by default, unless the number exceeds the range of INT in which case it is interpreted as a BIGINT, or if one of the following postfixes is present on the number.
String literals can be expressed with either single quotes (') or double quotes ("). Hive uses C-style escaping within the strings.
Floating Point Types
Floating point literals are assumed to be DOUBLE. Scientific notation is not yet supported.
Decimal literals provide precise values and greater range for floating point numbers than the DOUBLE type. Decimal data types store exact representations of numeric values, while DOUBLE data types store very close approximations of numeric values.
Decimal types are needed for use cases in which the (very close) approximation of a DOUBLE is insufficient, such as financial applications, equality and inequality checks, and rounding operations. They are also needed for use cases that deal with numbers outside the DOUBLE range (approximately -10308 to 10308) or very close to zero (-10-308 to 10-308). For a general discussion of the limits of the DOUBLE type, see the Wikipedia article Double-precision floating-point format.
Using Decimal Types
You can create a table in Hive that uses the Decimal type with the following syntax:
decimal_1 is a table having one field of type decimal which is basically a Decimal value.
You can read and write values in such a table using either the LazySimpleSerDe or the LazyBinarySerDe. For example:
You can use a cast to convert a Decimal value to any other primitive type such as a BOOLEAN. For example:
Decimal also supports many mathematical UDFs with the same syntax as used in the case of DOUBLE.
Basic mathematical operations that can use decimal types include:
- Average (avg)
- Modulus (pmod)
These rounding functions can also take decimal types:
But these mathematical UDFs are not currently supported:
- Log (ln)
Power(decimal, n) only supports positive integer values for the exponent n.
Casting Decimal Values
Casting is supported between decimal values and any other primitive type such as integer, double, boolean, and so on.
Testing Decimal Types
Two new tests have been added as part of the TestCliDriver framework within Hive. They are decimal_1.q and decimal_2.q. Other tests such as udf7.q cover the gamut of UDFs mentioned above.
More tests need to be added that demonstrate failure or when certain types of casts are prevented (for example, casting to date). There is some ambiguity in the round function because the rounding of Decimal does not work exactly as the SQL standard, and therefore it has been omitted in the current work.
Handling of NULL Values
Missing values are represented by the special value NULL. To import data with NULL fields, check documentation of the SerDe used by the table. (The default Text Format uses LazySimpleSerDe which interprets the string \N as NULL when importing.)