sql—配置单元数据类型问题

ff29svar  于 2021-06-25  发布在  Hive
关注(0)|答案(1)|浏览(526)

我们有3列源代码,cola是3位,colb是5位,colc是5位。我们需要根据以上3列创建13位唯一id

Query used - select colA*1000000000000 + colC*100000 + colC
Example - 

hive> select 123*1000000000000 + 12345*100000 + 12345;
OK
123001234512345 -- Not Expected
Time taken: 0.091 seconds, Fetched: 1 row(s)

进一步检查后,下面的配置单元查询不会给出正确的结果。

hive> !hive --version;
Hive 2.3.3-mapr-1904-r9
Git git://738a1fde0d37/root/opensource/mapr-hive-2.3/dl/mapr-hive-2.3 -r 265b539b942d0b9f4811b15880204dec5c0c7e1b
Compiled by root on Tue Aug 6 05:36:17 PDT 2019
From source with checksum 88f44b7532ffd7141c15cb5742e9cb51
hive> select cast(12345*1000000 as bigint);
OK
-539901888
Time taken: 0.126 seconds, Fetched: 1 row(s)
hive> select cast(12345*10000000 as bigint);
OK
-1104051584
Time taken: 0.02 seconds, Fetched: 1 row(s)
hive> select cast(12345*100000000 as bigint);
OK
1844386048
Time taken: 0.018 seconds, Fetched: 1 row(s)
hive> select cast(12345*1000000000 as bigint);
OK
1263991296
Time taken: 0.032 seconds, Fetched: 1 row(s)

而下面的查询是有效的-

hive> select cast(12345*10000000000 as bigint);
OK
123450000000000
Time taken: 0.017 seconds, Fetched: 1 row(s)
hive> select cast(12345*1000 as bigint);
OK
12345000
Time taken: 0.025 seconds, Fetched: 1 row(s)
hive> select cast(12345*10000 as bigint);
OK
123450000
Time taken: 0.035 seconds, Fetched: 1 row(s)
hive> select cast(12345*100000 as bigint);
OK
1234500000
Time taken: 0.247 seconds, Fetched: 1 row(s)
33qvvth1

33qvvth11#

如文件所述:
默认情况下,整型文字被假定为int,除非数字超过int的范围,在这种情况下,它被解释为bigint,或者如果数字上存在以下后缀之一。
在这个表达式中:

cast(12345*1000000 as bigint)

结果 12345*1000000 被选为演员 bigint . 这并不意味着乘法是用这种类型来完成的。为此,你需要在乘法之前施放:

12345 * cast(1000000 as bigint)

或者,您可以使用以下后缀:

12345L * 1000000L

请注意,没有明确的 cast() 是必需的,因为值已存在 bigint .

相关问题