Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Detailed explanation of hive Mathematical Operation

Shulou Source: shulou.com Published: 2022-06-03 07:59:33 10月01日 Update

1. Addition operation: +

Grammar: a + B

Operation types: all numeric types

Description: returns the result of the addition of An and B. The numeric type of the result is equal to the type of An and the smallest parent of the type of B. (see the inheritance relationship of the data type for details). For example, the general result of int + int is int, while the general result of int + double is double.

For example:

Hive > select1 + 9 from lxw_dual

ten

Hive > createtable lxw_dual as select 1 + 1.2 from lxw_dual

Hive > describe lxw_dual

_ c0 double

two。 Subtraction operation:-

Grammar: a-B

Operation types: all numeric types

Description: returns the result of subtracting An and B. The numeric type of the result is equal to the type of An and the smallest parent of the type of B. (see the inheritance relationship of the data type for details). For example, the general result of int-int is int, while the general result of int-double is double.

For example:

Hive > select10-5 from lxw_dual

five

Hive > createtable lxw_dual as select 5.6-4 from lxw_dual

Hive > describe lxw_dual

_ c0 double

3. Multiplication: *

Grammar: a * B

Operation types: all numeric types

Description: returns the result of multiplying An and B. The numeric type of the result is equal to the type of An and the smallest parent of the type of B. (see the inheritance relationship of the data type for details). Note that if the result of multiplying A by B exceeds the numerical range of the default result type, you need to convert the result to a wider range of numeric types through cast

For example:

Hive > select40 * 5 from lxw_dual

two hundred

4. Division operation: /

Grammar: a / B

Operation types: all numeric types

Description: returns the result of A divided by B. The numerical type of the result is double

For example:

Hive > select40 / 5 from lxw_dual

8.0

Note: the highest precision data type in hive is double, which is only accurate to 16 places after the decimal point. You should pay special attention when doing division operations.

Hive > select ceil (28.0 amp 6.999999999999999999999) from lxw_duallimit 1

The result is 4.

Hive > select ceil (28.0 amp 6.999999999999) from lxw_dual limit1

The result is 5.

5. Residual operation:%

Syntax: a% B

Operation types: all numeric types

Description: returns the remainder of A divided by B. The numeric type of the result is equal to the type of An and the smallest parent of the type of B. (see the inheritance relationship of the data type for details).

For example:

Hive > select 41 5 from lxw_dual

one

Hive > select 8.4 4 from lxw_dual

0.40000000000000036

Note: precision is a big problem in hive, and operations like this are best specified through round.

Hive > select round (8.4% 4,2) from lxw_dual

0.4

6. Bit and operation: &

Grammar: a & B

Operation types: all numeric types

Description: returns the results of bit-by-bit operations of An and B. The numeric type of the result is equal to the type of An and the smallest parent of the type of B. (see the inheritance relationship of the data type for details).

For example:

Hive > select 4 & 8 from lxw_dual

0

Hive > select 6 & 4 from lxw_dual

four

7. Bit or operation: |

Syntax: a | B

Operation types: all numeric types

Description: returns the result of bit-by-bit or operation of An and B. The numeric type of the result is equal to the type of An and the smallest parent of the type of B. (see the inheritance relationship of the data type for details).

For example:

Hive > select 4 | 8 from lxw_dual

twelve

Hive > select 6 | 8 from lxw_dual

fourteen

8. Bit XOR operation: ^

Grammar: a ^ B

Operation types: all numeric types

Description: returns the result of bit-by-bit XOR operations of An and B. The numeric type of the result is equal to the type of An and the smallest parent of the type of B. (see the inheritance relationship of the data type for details).

For example:

Hive > select 4 ^ 8 from lxw_dual

twelve

Hive > select 6 ^ 4 from lxw_dual

two

9. Bit inversion operation: ~

Syntax: ~ A

Operation types: all numeric types

Description: returns the result of A bitwise reverse operation. The numeric type of the result is equal to the type of A.

For example:

Hive > select ~ 6 from lxw_dual

-7

Hive > select ~ 4 from lxw_dual

-5

For more exciting content, please follow: http://bbs.superwu.cn

Follow the Superman College Wechat QR code:

Tags: Type result numerical value grammar data minimum precision range division operation large maximum wonderful accurate multiplication remainder content subtraction addition academy Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Apple Huawei MySQL vpn