【问题标题】:hive/impala - inserted decimal showed as nullhive/impala - 插入显示为 null 的小数
【发布时间】:2019-05-07 12:21:46
【问题描述】:

我创建了表格

create table test ( x decimal(5,2)) 

然后我尝试插入一个值

insert into test values ( cast( 1000.2  as decimal(5,2) ) );
insert into test values ( cast('2000.3' as decimal(5,2) ) );
insert into test values ( cast('3000,4' as decimal(5,2) ) );

但最终 select * from test; 返回 3x NULL 值。

我做错了什么?我不敢相信上述任何陈述都行不通。

我在最近的 Cloudera 快速入门 VM 中使用 impala

【问题讨论】:

  • 精度是数字的位数。比例是数字中小数点右侧的位数。例如,数字 123.45 的精度为 5,小数位数为 2
  • @Bala 当我只插入数字而不转换其AnalysisException: Possible loss of precision for target table 'default.test'. Expression '1000.2' (type: DECIMAL(5,1)) would need to be cast to DECIMAL(5,2) for column 'x' 时,我尝试将数字舍入到insert into test values ( cast( round(3000.5) as decimal(5,2))); 少于5 位,即使这最终为NULL。
  • 100.2 会发生什么?
  • 哦,你是对的! 1000.2 为 NULL 但 100.2 没问题。所以它也被认为是“点”!谢谢!

标签: impala cloudera-cdh


【解决方案1】:

精度是数字中的位数。比例是数字中小数点右侧的位数。例如,数字 123.45 的精度为 5,小数位数为 2

select cast(1000.2 as decimal(5,1)) as a, 
       cast(1000.2 as decimal(5,2)) as b,
       cast(1000.2 as decimal(6,2)) as c

会给你

a   1000.2
b   NULL
c   1000.2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多