0

我有一个包含几个字段的表。第一个字段是userId. 我正在使用哈希函数按 userId 对数据进行分片。

我正在运行以下查询:

SELECT userId, HASH(userId) as hashedId, HASH(userId) % 3 as hashedIdMod3
FROM mydataset.mytable LIMIT 1000

例如:

-5655326518438853587 % 3 ==> -1 when it should be 2
HASH(27315207816077732041734307321022553299) is -3139846784539570547 and the remainder is -2 when divided by 3 when it should be 1

那么,除以正整数时,余数怎么可能是负数呢?

4

1 回答 1

1

负数的 mod 在 SQL(和 c++、java 等)中为负数。所以你会想要使用 ABS()-- 如下:

SELECT userId, 
  HASH(userId) as hashedId, 
  ABS(HASH(userId) % 3) as hashedIdMod3
FROM mydataset.mytable LIMIT 1000
于 2013-05-23T17:02:31.420 回答