Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: add more information about Decimal type

...

With the present implementation, the Decimal datatype has unlimited precision. While this has absolutely no 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.

Union types

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:

...

Floating point literals are assumed to be DOUBLE. Scientific notation is not yet supported.

Decimal types

Decimal literals provide greater precision and range for floating point numbers than the DOUBLE data type. (See Decimal datatype above.) They are needed for use cases in which the precision of a DOUBLE is insufficient, such as financial applications, and for cases that deal with numbers outside the DOUBLE range (approximately -10308 to 10308) or very close to zero (-10-308 to 10-308). The precision of a Decimal type is practically unlimited.

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:

Code Block

create table decimal_1 (t decimal);

The table 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:

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;

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:

  • Plus
  • Negative
  • Addition
  • Subtraction
  • Multiplication
  • Division
  • Modulus

These rounding functions can also take decimal types:

  • Floor
  • Ceiling
  • Round

But these mathematical UDFs are not currently supported:

  • Exp
  • Log (Ln)
  • Log2
  • Log10
  • Sqrt

Power(decimal, n) only supports positive integer values for the exponent n.

Casting is supported between decimal values and integer, double, 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.

For general information about running Hive tests, see How to Contribute to Apache Hive and Hive Developer FAQ.

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.)