hive科学计数法引发的问题

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 floatstring出现科学计数法
    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.

参考

参考2-Hive数据类型

原文地址:https://www.cnblogs.com/damahuhu/p/11675553.html