hive科學計數法引發的問題
20191011
- (1)
20191010
hive 中數值類型
和字符串類型string
運算
hive中數值類型
可以和和字符串類型string
運算,其中字符串為純數字類型
,都轉為了浮點類型double
.若字符串不為純數字類型
,計算結果則為NULL
.
select 3 * '2'
6.0
select 3 * '2.2'
6.6
select '3' * '2'
6.0
- (2)
20191010
hive中使用聯結union all
union all
中的子查詢要求相同的列數
,對應字段類型相同
或可以隱式轉化為同一種類型
-
(3)
20191010
hive中int
double
float
轉string
出現科學計數法
hive中int , float , double
這些數值類型在存儲大額度數字
時,在前端展現上總是使用科學計數法
來表示,其實無論是普通的表示方式還是科學計數法表示,只是一個習慣問題,結果都是一樣的。
可是不能理解的是當把數值類型
轉化成字符串類型
以后hive竟然把數值轉換成了科學計數法表示的字符串
而非數值本身的字符串
-
處理方法參考下面鏈接,
整數
直接先轉成bigint
,若是小數
需要特殊處理
參考1-hive中科學計數法 -
【坑】若是(1)-(3)正好同時出現,一步小心就會有有問題
有一張表create table test_table(bignumstr string)
-
(1)單獨執行下面
sql
語句
select 123456789101112;
123456789101112
select 123456789101112.0;
1.23456789101112E14
--或
123456789101112.0
select 123456789101111
union all
select 123456789101112.0;
1.23456789101111E14
1.23456789101112E14
-- 或
123456789101111.0
123456789101112.0
- (2)單獨
insert
insert into table test_table
select 123456789101111;
select * table test_table;
123456789101112
insert into table test_table
select 123456789101112.0;
select * table test_table;
1.23456789101112E14
- (3)
union all
insert overwrite table test_hjy
select 123456789101111
union all
select 123456789101112.0;
select * table test_table;
1.23456789101111E14
1.23456789101112E14
- (4)即使第一個為string
select cast(123456789101111 as string)
union all
select 123456789101112.0;
1.23456789101111E14
1.23456789101112E14
-- 或
123456789101111.0
123456789101112.0
insert overwrite table test_hjy
select cast(123456789101111 as string)
union all
select 123456789101112.0;
select * table test_table;
1.23456789101111E14
1.23456789101112E14
- (1、2、3)巧合,且不會報錯(其實還是寫的有問題0-0,但是找錯太難受了。。。)
insert overwrite table test_hjy
select cast(123456789101111 as string)
union all
select 1 * '123456789101112';
select * table test_table;
1.23456789101111E14
1.23456789101112E14
原因在於hive在聯結union all時,可以進行隱式轉換,先都轉換為同一種類型,
string
可以轉換為double
,見參考2.