表中的某个字段同udfs函数进行相乘,然后再使用cast函数进行数据类型转换,发现结果比真实的结果小0.01,如下图所示:
这个结果不正确的原因,有谁知道吗?
表中的某个字段同udfs函数进行相乘,然后再使用cast函数进行数据类型转换,发现结果比真实的结果小0.01,如下图所示:
这个结果不正确的原因,有谁知道吗?
查找到原因了。
因为udfs函数获取汇率的返回值为double类型,double类型的字段乘以decmial类型的字段,结果数据也为double类型,使用cast函数对double类型进行转换为decimal类型,结果就有可能出现错误的情况。
如下所示:
[master:21000] > select cast(cast(291328.29 as double) as decimal(16,2));
Query: select cast(cast(291328.29 as double) as decimal(16,2))
Query submitted at: 2018-02-27 14:59:22 (Coordinator: http://master:25000)
Query progress can be monitored at: http://master:25000/query_plan?query_id=ee4756fa52bdbeea:dee570ad00000000
+--------------------------------------------------+
| cast(cast(291328.29 as double) as decimal(16,2)) |
+--------------------------------------------------+
| 291328.28 |
+--------------------------------------------------+
Fetched 1 row(s) in 0.02s
[master:21000] > select cast(cast(291328.28 as double) as decimal(16,2));
Query: select cast(cast(291328.28 as double) as decimal(16,2))
Query submitted at: 2018-02-27 15:02:23 (Coordinator: http://master:25000)
Query progress can be monitored at: http://master:25000/query_plan?query_id=2e462958268f7e04:802b9d9f00000000
+--------------------------------------------------+
| cast(cast(291328.28 as double) as decimal(16,2)) |
+--------------------------------------------------+
| 291328.28 |
+--------------------------------------------------+
Fetched 1 row(s) in 0.01s
可能是java浮点数计算的问题