Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Hive Data Types

Table of Contents

Overview

This lists all supported data types in Hive. See Type System in the Tutorial for additional information.

...

Char types are similar to Varchar but they are fixed-length meaning that values shorter than the specified length value are padded with spaces but trailing spaces are not important during comparisons. The maximum length is fixed at 255.

Code Block
sql
sql

CREATE TABLE foo (bar CHAR(10))

...

Hive 0.11 and 0.12 have the precision of the DECIMAL type fixed and limited to 38 digits. As of Hive 0.13 users can specify scale and precision when creating tables with the DECIMAL datatype using a DECIMAL(precision, scale) syntax:

Code Block
sql
sql

CREATE TABLE foo (
  a DECIMAL,
  b DECIMAL(9, 7)
)

...

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 create_union UDF:

No Format

CREATE TABLE union_test(foo UNIONTYPE<int, double, array<string>, struct<a:int,b:string>>);
SELECT foo FROM union_test;

{0:1}
{1:2.0}
{2:["three","four"]}
{3:{"a":5,"b":"five"}}
{2:["six","seven"]}
{3:{"a":8,"b":"eight"}}
{0:9}
{1:10.0}

...

To create a union you have to provide this tag to the create_union UDF:

No Format

SELECT create_union(0, key), create_union(if(key<100, 0, 1), 2.0, value), create_union(1, "a", struct(2, "b")) FROM src LIMIT 2;

{0:"238"}	{1:"val_238"}	{1:{"col1":2,"col2":"b"}}
{0:"86"}	{0:2.0}	{1:{"col1":2,"col2":"b"}}

...

You can create a table in Hive that uses the Decimal type with the following syntax:

Code Block

create table decimal_1 (t decimal);

...

You can read and write values in such a table using either the LazySimpleSerDe or the LazyBinarySerDe. For example:

Code Block

alter table decimal_1 set serde 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe';

or:

Code Block

alter table decimal_1 set serde 'org.apache.hadoop.hive.serde2.lazy.LazyBinarySerDe';

You can use a cast to convert a Decimal value to any other primitive type such as a BOOLEAN. For example:

Code Block

select cast(t as boolean) from decimal_2;

...

  • Positive
  • Negative
  • Addition
  • Subtraction
  • Multiplication
  • Division
  • Average (avg)
  • Sum
  • Count
  • Modulus (pmod)
  • Sign

These rounding functions can also take decimal types:

...